hbase代码报错(以及解决方法)

》》》直接上代码 

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()

  }

}

》》》报错 

》》》 解决方法(映射有问题)

 》》》重新运行

》》》结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值