I am trying to create 2 HBase tables, but am running into some issues. Here is what I have so far. The tables are not being created correctly. The only tutorials and help I can find online are around creating ONE HBase table and not 2+ HBase tables.
Updates:
The tutorial I'm using is: https://www.tutorialspoint.com/hbase/hbase_create_table.htm
public class TableCreation{
public static void main(String[] args) throws IOException { Configuration config = HBaseConfiguration.create(); HBaseAdmin admin = new HBaseAdmin(config); HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf("holidays")); tableDescriptor.addFamily(new HColumnDescriptor("observed")); tableDescriptor.addFamily(new HColumnDescriptor("date")); admin.createTable(tableDescriptor); HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf("group")); tableDescriptor.addFamily(new HColumnDescriptor("type")); tableDescriptor.addFamily(new HColumnDescriptor("size")); admin.createTable(tableDescriptor); }
}
I managed to write some code that works and properly inserts 2 tables with columns. I am using Cloudera CDH5 Hadoop distribution.
This is a sample class that creates a table with column families:
public class HBaseOps {
public static void createTable(Connection conn, String tableName, String... families) throws IOException {
Admin admin = conn.getAdmin();
HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
for(String family : families) {
tableDescriptor.addFamily(new HColumnDescriptor(family));
}
admin.createTable(tableDescriptor);
}
}
You can call the code above to create the tables:
//Connect to hbase
Configuration config = HBaseConfiguration.create();
Connection connection = ConnectionFactory.createConnection(config);
//Create table
final String TABLE1 = "table1";
final String FAMILY1 = "family1";
final String FAMILY2 = "family2";
HBaseOps.createTable(connection, TABLE1, FAMILY1, FAMILY2);
//Create table
final String TABLE2 = "table2";
final String FAMILY3 = "family3";
final String FAMILY4 = "family4";
HBaseOps.createTable(connection, TABLE2, FAMILY3, FAMILY4);
You can find the full sample at my github: Full Code on Github I tested with hbase shell and the tables are properly created.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.