- HBase 教程
- HBase - 首页
- HBase - 概述
- HBase - 架构
- HBase - 安装
- HBase - Shell
- HBase - 常用命令
- HBase - 管理员 API
- HBase - 创建表
- HBase - 列出表
- HBase - 禁用表
- HBase - 启用表
- HBase - 描述 & 修改
- HBase - 检查表是否存在
- HBase - 删除表
- HBase - 关闭
- HBase - 客户端 API
- HBase - 创建数据
- HBase - 更新数据
- HBase - 读取数据
- HBase - 删除数据
- HBase - 扫描
- HBase - 计数 & 截断
- HBase - 安全
- HBase 资源
- HBase - 问答
- HBase - 快速指南
- HBase - 有用资源
HBase - 创建表
使用 HBase Shell 创建表
您可以使用 **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
使用 Java API 创建表
您可以使用 **HBaseAdmin** 类的 **createTable()** 方法在 HBase 中创建表。此类属于 **org.apache.hadoop.hbase.client** 包。下面是在 HBase 中使用 Java API 创建表的步骤。
步骤 1:实例化 HBaseAdmin
此类需要 Configuration 对象作为参数,因此首先实例化 Configuration 类并将此实例传递给 HBaseAdmin。
Configuration conf = HBaseConfiguration.create(); HBaseAdmin admin = new HBaseAdmin(conf);
步骤 2:创建 TableDescriptor
**HTableDescriptor** 是一个属于 **org.apache.hadoop.hbase** 类的类。此类就像一个表名和列族的容器。
//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);
步骤 3:通过 Admin 执行
使用 **HBaseAdmin** 类的 **createTable()** 方法,您可以在 Admin 模式下执行创建的表。
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
广告