我正在使用CDH 5.4.2并尝试创建Hbase表具有以下代码片段:
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"));
发生Deprecated
错误。
答案 0 :(得分:4)
示例代码供您参考。假设您在独立模式下运行Hbase。
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();
}
}
}
答案 1 :(得分:0)
如果您需要检索使用表,则可以使用Connection.getTable(TableName)
但是,如果您需要创建一个表,请使用TableDescriptorBuilder
和Admin.createTable(TableDescriptor)
例如:
val tableDescriptor: TableDescriptor = TableDescriptorBuilder
.newBuilder(TableName.valueOf("mytable"))
.setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder("myId".getBytes).build())
.setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder("data".getBytes).build())
.build()
admin.createTable(tableDescriptor)