代码如下》》
package Flink_HBase_APL
import org.apache.hadoop.hbase.{HBaseConfiguration, TableName}
import org.apache.hadoop.hbase.client.{ConnectionFactory, Put}
import org.apache.hadoop.hbase.util.Bytes
object this_one {
def main(args: Array[String]): Unit = {
//hbase连接配置
val conf = HBaseConfiguration.create()
conf.set("hbase.zookeeper.quorum", "master,slave1,slave2")
conf.set("hbase.zookeeper.property.clientPort", "2181")
//创建hbase连接
val connection = ConnectionFactory.createConnection(conf)
try{
//获取hbase表
val table = connection.getTable(TableName.valueOf("NB:t1")) //表名
//创建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库中查看数据,确保数据成功存入》》
参考阅读
发表评论