在HBase中如何创建表结构?创建表结构并添加删除和查询数据
时间: 2024-05-30 14:14:03 浏览: 223
在HBase中创建表可以使用命令"create",具体的命令格式如下:
create '表名', '列族1', '列族2', ...
例如,要创建一个名为"student"的表,并为其指定两个列族"info"和"score",可以使用以下命令:
create 'student', 'info', 'score'
然后,可以使用"put"命令向表中添加数据,使用"get"命令查询数据,使用"delete"命令删除数据。例如,向表中添加一条数据可以使用以下命令:
put 'student', '001', 'info:name', '张三'
put 'student', '001', 'info:age', '18'
put 'student', '001', 'score:math', '90'
put 'student', '001', 'score:english', '80'
查询某个单元格中的数据可以使用以下命令:
get 'student', '001', 'score:math'
删除某条数据可以使用以下命令:
delete 'student', '001', 'info:name'
相关问题
在HBase 中如何创建表结构?创建如下的表结构,并添加、删除和查询数据。RowKey是"com.google" ColumnFamily:CF1 Column:C11 "C11 good" Column:C12 "C12 good" ColumnFamily:CF2 Column:C21 "C12 bad" Column:C22 "C12 bad" TimeStamp T1
在 HBase 中创建表结构,需要使用 HBase Shell 或 HBase Java API。
1. 使用 HBase Shell 创建表结构:
打开 HBase Shell,输入以下命令:
create 'table_name', 'column_family1', 'column_family2', ...
其中,table_name 为表名,column_family1、column_family2 为列族名。
创建表结构:
create 'my_table', 'CF1', 'CF2'
2. 使用 HBase Java API 创建表结构:
在 Java 代码中,使用 HBase Admin 类创建表结构:
```
Configuration config = HBaseConfiguration.create();
Connection connection = ConnectionFactory.createConnection(config);
Admin admin = connection.getAdmin();
HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf("my_table"));
HColumnDescriptor cf1 = new HColumnDescriptor("CF1");
HColumnDescriptor cf2 = new HColumnDescriptor("CF2");
tableDescriptor.addFamily(cf1);
tableDescriptor.addFamily(cf2);
admin.createTable(tableDescriptor);
admin.close();
connection.close();
```
其中,HTableDescriptor 代表表的描述信息,HColumnDescriptor 代表列族的描述信息。
创建表结构后,就可以添加、删除和查询数据了。使用 HBase Shell 或 HBase Java API 均可。
在 HBase Shell 中添加数据:
```
put 'my_table', 'com.google', 'CF1:C11', 'C11 good'
put 'my_table', 'com.google', 'CF1:C12', 'C12 good'
put 'my_table', 'com.google', 'CF2:C21', 'C12 bad'
put 'my_table', 'com.google', 'CF2:C22', 'C12 bad'
```
在 Java 代码中添加数据:
```
Configuration config = HBaseConfiguration.create();
Connection connection = ConnectionFactory.createConnection(config);
Table table = connection.getTable(TableName.valueOf("my_table"));
Put put = new Put(Bytes.toBytes("com.google"));
put.addColumn(Bytes.toBytes("CF1"), Bytes.toBytes("C11"), Bytes.toBytes("C11 good"));
put.addColumn(Bytes.toBytes("CF1"), Bytes.toBytes("C12"), Bytes.toBytes("C12 good"));
put.addColumn(Bytes.toBytes("CF2"), Bytes.toBytes("C21"), Bytes.toBytes("C12 bad"));
put.addColumn(Bytes.toBytes("CF2"), Bytes.toBytes("C22"), Bytes.toBytes("C12 bad"));
table.put(put);
table.close();
connection.close();
```
在 HBase Shell 中查询数据:
```
get 'my_table', 'com.google'
```
在 Java 代码中查询数据:
```
Configuration config = HBaseConfiguration.create();
Connection connection = ConnectionFactory.createConnection(config);
Table table = connection.getTable(TableName.valueOf("my_table"));
Get get = new Get(Bytes.toBytes("com.google"));
Result result = table.get(get);
for (Cell cell : result.listCells()) {
System.out.println(Bytes.toString(CellUtil.cloneQualifier(cell)) + " : " + Bytes.toString(CellUtil.cloneValue(cell)));
}
table.close();
connection.close();
```
在 HBase Shell 中删除数据:
```
delete 'my_table', 'com.google', 'CF1:C11'
```
在 Java 代码中删除数据:
```
Configuration config = HBaseConfiguration.create();
Connection connection = ConnectionFactory.createConnection(config);
Table table = connection.getTable(TableName.valueOf("my_table"));
Delete delete = new Delete(Bytes.toBytes("com.google"));
delete.addColumn(Bytes.toBytes("CF1"), Bytes.toBytes("C11"));
table.delete(delete);
table.close();
connection.close();
```
1,Hbase创建数据库表自己名字缩写,表中包含列族info1,info2,存储版本为5。2,查看数据表的表结构。3,向数据库表中增加两行数据包含name.sex,在info,在info2中添加class, age。4,删除3中添加数据行键的整行记录。5,删除数据库。命令代码
在HBase中,你可以按照以下步骤操作:
1. 创建数据库表(使用表名的简写,例如TB_Example):
```shell
hbase> create 'TB_Example', {'info1': {NAME => 'info1'}, 'info2': {NAME => 'info2'}}
```
这里指定两个列族(info1 和 info2),并设置默认版本数为5(HBase的默认版本保存策略是无限制,除非显式设置)。
2. 查看表结构:
```shell
hbase> describe 'TB_Example'
```
这将显示表TB_Example的所有列族及其属性。
3. 向表中插入数据:
```shell
hbase> put 'TB_Example', 'row1', 'name:sex', 'John:Male'
hbase> put 'TB_Example', 'row2', 'name:sex', 'Jane:Female'
hbase> put 'TB_Example', 'row1', 'info:class', 'Student'
hbase> put 'TB_Example', 'row1', 'info:age', '18'
```
这里分别向行键为`row1`和`row2`的单元格添加了name、sex、class和age的信息。
4. 删除特定行(键为row1)的数据:
```shell
hbase> delete 'TB_Example', 'row1'
```
这条命令会删除所有属于`row1`的行。
5. 最后,如果需要完全删除表TB_Example:
```shell
hbase> drop 'TB_Example'
```
注意,这将永久删除表及其中所有数据,所以在删除前一定要确认。
阅读全文
相关推荐















