HBase建表、删除、清空数据

在hbase根目录下执行../hbase shell   进入hbase客户端操作窗口

HBase表的创建

# 语法:create <table>, {NAME => <family>, VERSIONS => <VERSIONS>}
# 例如:创建表t1,有两个family name:f1,f2,且版本数前者为1,后者为2

hbase(main)> create 't1',{NAME => 'f1', VERSIONS => 1},{NAME => 'f2', VERSIONS => 2}

HBase表的删除

  1. disable表
  2. drop表

hbase(main)> disable 't1'
hbase(main)> drop 't1'

HBase表的清空

hbase(main)> truncate 't1'

HBase是一个开源的非关系型分布式数据库(NoSQL),它基于Google的Bigtable模型,适用于存储大量稀疏的数据集。HBase数据模型由行、列和间戳组成,数据是按照列族(Column Family)存储的。下面是使用HBase进行建表和插入数据的基本步骤: 1. 建表HBase建表主要涉及确定表名和列族。使用HBase Shell或者编程API可以创建一个新的表。例如,在HBase Shell中创建一个名为`my_table`的表,可以按照以下命令操作: ```shell create 'my_table', 'cf1', 'cf2' ``` 上述命令创建了一个新表`my_table`,并指定了两个列族`cf1`和`cf2`。 2. 插入数据 插入数据HBase的表中,你需要指定行键(Row Key)、列族、列限定符(Column Qualifier)和对应的数据值。在HBase Shell中,使用`put`命令插入数据。例如: ```shell put 'my_table', 'row1', 'cf1:column1', 'value1' put 'my_table', 'row1', 'cf2:column2', 'value2' ``` 上述命令为`my_table`表中的`row1`行插入了两个列值,第一个来自`cf1`列族的`column1`,第二个来自`cf2`列族的`column2`。 除了使用Shell命令行,还可以通过Java API来执行建表和插入数据的操作。以下是使用Java API创建表和插入数据的示例代码: ```java // 首先,需要配置HBase的连接 Configuration config = HBaseConfiguration.create(); Connection connection = ConnectionFactory.createConnection(config); // 使用HTablePool获取表实例 HTable table = connection.getTable(TableName.valueOf("my_table")); // 创建建表的描述信息 TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(TableName.valueOf("my_table")); ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("cf1")); // 添加列族到表描述信息中 tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptorBuilder.build()); // 执行建表操作 Admin admin = connection.getAdmin(); admin.createTable(tableDescriptorBuilder.build()); // 插入数据操作 Put put = new Put(Bytes.toBytes("row1")); put.addColumn(Bytes.toBytes("cf1"), Bytes.toBytes("column1"), Bytes.toBytes("value1")); table.put(put); ``` 在上述Java代码中,我们首先配置了连接到HBase的环境,然后创建了表描述,并指定了列族。接着,我们创建了表,并插入了数据
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值