温馨提示×

linux hbase怎样建表

小樊
40
2026-10-28 03:53:47
栏目: 智能运维

在 Linux 环境下使用 HBase 建表,通常有两种方式:HBase Shell 和 Java API(或 Python/Thrift)。下面以最常用、最直观的 HBase Shell 方式为主说明,并补充 Java 示例。


一、前提条件

  1. Linux 已安装 HBase
  2. HBase 服务已启动
start-hbase.sh
  1. 能进入 HBase Shell
hbase shell

二、HBase Shell 建表示例

1️⃣ 最简单的建表(1 个列族)

create 'student', 'info'
  • 表名:student
  • 列族:info

2️⃣ 建表并指定多个列族

create 'user', 'base', 'ext'

3️⃣ 建表并指定列族参数(版本数、TTL)

create 'user', {NAME => 'base', VERSIONS => 3, TTL => 86400}
  • VERSIONS:保留 3 个版本
  • TTL:数据存活时间(秒)

4️⃣ 预分区建表(生产推荐)

create 'order', 'cf', SPLITS => ['10', '20', '30']

避免数据热点,提高写入性能。


5️⃣ 查看表是否创建成功

list
describe 'student'

三、插入和查询示例

put 'student', 'row1', 'info:name', 'zhangsan'
get 'student', 'row1'
scan 'student'

四、Java API 建表(简要)

Configuration conf = HBaseConfiguration.create();
Connection conn = ConnectionFactory.createConnection(conf);
Admin admin = conn.getAdmin();

TableName tableName = TableName.valueOf("student");
HTableDescriptor table = new HTableDescriptor(tableName);
HColumnDescriptor cf = new HColumnDescriptor("info");
table.addFamily(cf);

admin.createTable(table);

五、常见问题

✅ 表已存在

Table already exists

解决:

disable 'student'
drop 'student'

✅ 无法连接 HBase

  • 检查 hbase-site.xml
  • 检查 ZooKeeper 是否启动

如果你有 具体场景(如时间序列、日志、用户画像、亿级数据),我可以帮你设计更合理的表结构和预分区方案。

0 踩