Error: the constructor htable (configuration strin

2019-04-12 14:47发布

问题:

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)


标签: hadoop hbase