》》》直接上代码
package HBase_Api
import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.hbase.{HBaseConfiguration, TableName}
import org.apache.hadoop.hbase.client.{Connection, ConnectionFactory, Get, Put}
import org.apache.hadoop.hbase.util.Bytes
object HBase_judgment {
def main(args: Array[String]): Unit = {
// HBase配置
val conf = HBaseConfiguration.create()
conf.set("hbase.zookeeper.quorum", "master,slave1,slave2") // 设置ZooKeeper的主机名或IP地址
conf.set("hbase.zookeeper.property.clientPort", "2181") // 设置ZooKeeper客户端连接端口
val connection = ConnectionFactory.createConnection(conf)
println(connection)
val damin = connection.getAdmin
val tableExists = damin.tableExists(TableName.valueOf("nike:n1"))//表名
if (tableExists) {
println("you")
}else{
println("wu")
}
val table = connection.getTable(TableName.valueOf("nike:n1"))
val rowkey = "101" //行键
val get = new Get(rowkey.getBytes)
val result = table.get(get)
val nameValue = result.getValue("mag".getBytes,"age".getBytes)
val str = new String(nameValue) //讲读取的数据转换成string
println(str)
try {
val table = connection.getTable(TableName.valueOf("nike:n1"))
val put = new Put(Bytes.toBytes("103")) //行键
put.addColumn(
Bytes.toBytes("mag"), //列族
Bytes.toBytes("name"), //列名
Bytes.toBytes("wanhaihang") //数值
)
put.addColumn(
Bytes.toBytes("mag"), //列族
Bytes.toBytes("age"), //列名
Bytes.toBytes("19") //数值
)
//将数据写入表中
table.put(put)
//关闭table
table.close()
}
//测试输出代码(可有可无 )
println("成功插入数值")
damin.close()
table.close()
connection.close()
}
}
》》》报错
》》》 解决方法(映射有问题)
》》》重新运行
》》》结果