在HBase中如何创建表结构?创建表结构并添加删除和查询数据
时间: 2024-05-30 22:14:03 浏览: 14
在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();
```
hbase描述表的结构语句
HBase是一个基于Hadoop的分布式列存储系统,可以存储海量数据,支持高并发读写操作。下面是HBase描述表结构的语句:
1. 创建表
```
create 'table_name', 'column_family'
```
其中,'table_name'为表名,'column_family'为列族名,可以创建多个列族。
2. 删除表
```
disable 'table_name'
drop 'table_name'
```
先禁用表,再删除表。
3. 添加列族
```
alter 'table_name', {NAME => 'column_family'}
```
4. 删除列族
```
alter 'table_name', 'delete' => 'column_family'
```
5. 插入数据
```
put 'table_name', 'row_key', 'column_family:column_qualifier', 'value'
```
其中,'row_key'为行键,'column_family'为列族名,'column_qualifier'为列名,'value'为值。
6. 查询数据
```
get 'table_name', 'row_key'
```
查询指定行键的数据。
7. 扫描数据
```
scan 'table_name'
```
扫描整个表的数据。
8. 删除数据
```
delete 'table_name', 'row_key', 'column_family:column_qualifier'
```
删除指定行键、列族、列名的数据。
以上就是HBase描述表结构的语句。
相关推荐
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)