错误:类htable中的构造函数htable不能应用于给定类型

htzpubme  于 2021-05-27  发布在  Hadoop
关注(0)|答案(1)|浏览(823)

我正在使用hadoop hbase。我只是写了一个简单的程序来插入数据库。运行程序时出现以下错误:
插入数据。java:31:错误:类htable中的构造函数htable不能应用于给定类型;htable htable=新htable(“emp”,conn);
我的代码:

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;

public class InsertData {

   public static void main(String[] args) throws IOException {

      // Instantiating Configuration class
      Configuration config = HBaseConfiguration.create();

      // Instantiating HTable class
      HTable hTable = new HTable(config, "emp");

      // Instantiating Put class
      // accepts a row name.
      Put p = new Put(Bytes.toBytes("row1")); 

      // adding values using add() method
      // accepts column family name, qualifier/row name ,value
      p.add(Bytes.toBytes("personal"),
      Bytes.toBytes("name"),Bytes.toBytes("raju"));

      p.add(Bytes.toBytes("personal"),
      Bytes.toBytes("city"),Bytes.toBytes("hyderabad"));

      p.add(Bytes.toBytes("professional"),Bytes.toBytes("designation"),
      Bytes.toBytes("manager"));

      p.add(Bytes.toBytes("professional"),Bytes.toBytes("salary"),
      Bytes.toBytes("50000"));

      // Saving the put Instance to the HTable.
      hTable.put(p);
      System.out.println("data inserted");

      // closing HTable
      hTable.close();
   }
}

我的帮助?
谢谢

qij5mzcb

qij5mzcb1#

我不确定您使用的是哪个版本的hbase,但是 HTable 已被弃用一段时间(请参阅 HTable api文档),现在纯粹是一个内部方法。相反,使用 Table (确保版本与您的hbase部署保持一致):
你应该依靠 org.apache.hbase:hbase-client:<VERSION> (不需要任何其他hbase依赖项),并使用以下方法:

try {
  Configuration conf = HBaseConfiguration.create();
  Connection connection = ConnectionFactory.createConnection(conf);
  Table table = connection.getTable(TableName.valueOf("emp"));

  Put p = new Put(Bytes.toBytes("row1"));
  p.add(Bytes.toBytes("personal"), Bytes.toBytes("name"),Bytes.toBytes("raju"));

  table.put(p);
} finally {
  table.close();
  connection.close();
}

相关问题