Error: the constructor htable (configuration strin

2019-04-12 14:07发布

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?

标签: hadoop hbase
3条回答
我命由我不由天
2楼-- · 2019-04-12 14:33

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();
查看更多
做自己的国王
3楼-- · 2019-04-12 14:37

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();
        }
    }
}
查看更多
不美不萌又怎样
4楼-- · 2019-04-12 14:37

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)
查看更多
登录 后发表回答