Error: the constructor htable (configuration string) is deprecated

柔情痞子 提交于 2019-12-21 21:24:10

问题


I am using CDH 5.4.2 and trying to create Hbase Table have the following code snippet:

     Configuration conf = HBaseConfiguration.create(new Configuration());
        HBaseAdmin hba = new <strike>HBaseAdmin</strike>(conf);
        if(!hba.tableExists(args[0])){
            HTableDescriptor ht = new      <strike>HTableDescriptor</strike>    (args[0]);
            ht.addFamily(new HColumnDescriptor("sample"));

There is a Deprecated error.

  • How to avoid these warnings?
  • Do I need to add any specific jars for CDH 5.4.2?

回答1:


It's just a warning. But you should not use deprecated methods in your code.

In place of :

HBaseAdmin admin = new HBaseAdmin(conf);

You should use:

Connection conn =ConnectionFactory.createConnection(conf);
Admin admin  = conn.getAdmin();



回答2:


Sample code for your reference. Assuming that you have Hbase running in standalone mode.

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;

public class HBaseSample {

    public static void main(String[] args) {
        Configuration conf = HBaseConfiguration.create();
        try {           
            Connection conn = ConnectionFactory.createConnection(conf);
            Admin hAdmin = conn.getAdmin();

            HTableDescriptor hTableDesc = new HTableDescriptor(
                    TableName.valueOf("Customer"));
            hTableDesc.addFamily(new HColumnDescriptor("name"));
            hTableDesc.addFamily(new HColumnDescriptor("contactinfo"));
            hTableDesc.addFamily(new HColumnDescriptor("address"));

            hAdmin.createTable(hTableDesc);

            System.out.println("Table created Successfully...");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}



回答3:


If you need to retrieve a table for usage, you can use Connection.getTable(TableName)

But If you need to create a table instead, use TableDescriptorBuilder and Admin.createTable(TableDescriptor)

For instance:

val tableDescriptor: TableDescriptor = TableDescriptorBuilder
                          .newBuilder(TableName.valueOf("mytable"))
                          .setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder("myId".getBytes).build())
                          .setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder("data".getBytes).build())
                          .build()

admin.createTable(tableDescriptor)


来源:https://stackoverflow.com/questions/32188635/error-the-constructor-htable-configuration-string-is-deprecated

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!