数据写入HBase(scala)

Scala 复制代码
package source

import org.apache.hadoop.hbase.{HBaseConfiguration, TableName}
import org.apache.hadoop.hbase.client.{ConnectionFactory, Put}
import org.apache.hadoop.hbase.util.Bytes

object ffff {
  def main(args: Array[String]): Unit = {
    //hbase连接配置
    val conf = HBaseConfiguration.create()
    conf.set("hbase.zookeeper.quorum", "bigdata1,bigdata2,bigdata3")
    conf.set("hbase.zookeeper.property.clientPort", "2181")
    //创建hbase连接
    val connection = ConnectionFactory.createConnection(conf)

    try{
      //获取hbase表
      val table = connection.getTable(TableName.valueOf("test")) //表名
      //创建put对象插入数据
      val put = new Put(Bytes.toBytes("2008"))  //行键

      put.addColumn(
        Bytes.toBytes("info"), //列族
        Bytes.toBytes("name"), //列名
        Bytes.toBytes("beijing")  //数值
      )
      put.addColumn(
        Bytes.toBytes("info"), //列族
        Bytes.toBytes("age"), //列名
        Bytes.toBytes("70")  //数值
      )
      put.addColumn(
        Bytes.toBytes("info"), //列族
        Bytes.toBytes("school"), //列名
        Bytes.toBytes("国家") //数值
      )
      //将数据写入表中
      table.put(put)
      //关闭table
      table.close()
    }
    //测试输出代码(可有可无)
    println("成功插入数值")
    //关闭hbase连接
    connection.close()

  }
}

在HBase里面查看:

相关推荐
斌味代码几秒前
Redis 分库分表实战:从垂直拆分到水平扩容完整记录
数据库·redis·bootstrap
Percep_gan3 分钟前
在芋道自定义数据权限
java·数据库
Trouvaille ~3 分钟前
【MySQL篇】表的约束:保证数据完整性
数据库·mysql·约束·数据完整性·实体完整性·域完整性·参照完整性
rchmin4 分钟前
阿里Tair分布式锁与Redis分布式锁的实现区别
数据库·redis·分布式
等....9 小时前
Minio使用
数据库
win x10 小时前
Redis 使用~如何在Java中连接使用redis
java·数据库·redis
迷枫71211 小时前
DM8 数据库安装实战:从零搭建达梦数据库环境(附全套工具链接)
数据库
Me4神秘11 小时前
国家级互联网骨干直联点及容量、互联网交换中心
大数据·信息与通信
XDHCOM11 小时前
PostgreSQL 25001: active_sql_transaction 报错原因分析,故障修复步骤详解,远程处理解决方案
数据库·sql·postgresql
卤炖阑尾炎12 小时前
PostgreSQL 日常运维全指南:从基础操作到备份恢复
运维·数据库·postgresql