您可以使用 create 命令创建表,此处必须指定表名和列族名称.在HBase shell中创建表的语法如下所示.
create'< table name>', '< column family>'
下面给出了一个名为emp的表的示例模式.它有两个列族:"个人数据"和"专业数据".
行密钥 | 个人数据 | 专业数据 |
---|---|---|
你可以在HBase shell中创建这个表,如下所示.
hbase(main):002:0> create 'emp', 'personal data', 'professional data'
它会给你以下输出.
0 row(s) in 1.1300 seconds => Hbase::Table - emp
您可以使用 list 命令验证表是否已创建,如下所示.在这里你可以观察创建的emp表.
hbase(main):002:0> list TABLE emp 2 row(s) in 0.0340 seconds
您可以使用 HBaseAdmin 类的 createTable()方法在HBase中创建表.该类属于 org.apache.hadoop.hbase.client 包.下面给出了使用java API在HBase中创建表的步骤.
此类需要将Configuration对象作为参数因此,最初实例化Configuration类并将此实例传递给HBaseAdmin.
Configuration conf = HBaseConfiguration.create(); HBaseAdmin admin = new HBaseAdmin(conf);
HTableDescriptor 是一个属于
//creating table descriptor HTableDescriptor table = new HTableDescriptor(toBytes("Table name")); //creating column family descriptor HColumnDescriptor family = new HColumnDescriptor(toBytes("column family")); //adding coloumn family to HTable table.addFamily(family);
使用 createTable()方法 HBaseAdmin 类,您可以在管理模式下执行创建的表.
admin.createTable(table);
以下是通过管理员创建表的完整程序.
import java.io.IOException; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.HColumnDescriptor; import org.apache.hadoop.hbase.HTableDescriptor; import org.apache.hadoop.hbase.client.HBaseAdmin; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.conf.Configuration; public class CreateTable { public static void main(String[] args) throws IOException { // Instantiating configuration class Configuration con = HBaseConfiguration.create(); // Instantiating HbaseAdmin class HBaseAdmin admin = new HBaseAdmin(con); // Instantiating table descriptor class HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf("emp")); // Adding column families to table descriptor tableDescriptor.addFamily(new HColumnDescriptor("personal")); tableDescriptor.addFamily(new HColumnDescriptor("professional")); // Execute the table through admin admin.createTable(tableDescriptor); System.out.println(" Table created "); } }
编译并执行上述程序,如下所示.
$javac CreateTable.java $java CreateTable
以下应该是输出:
Table created