HBASE,分别使用控制台命令和Java编程创建一个HBase表,表名分别为students1和students2,列簇为grade和scores 作业提交要求: 1、代码或输出结果,需要打印出自己的姓名和学号(姓名:wzq)
时间: 2024-05-06 17:18:27 浏览: 146
姓名:wzq
Java编程创建HBase表的示例代码如下:
```java
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.TableDescriptor;
import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
import org.apache.hadoop.hbase.io.compress.Compression;
import org.apache.hadoop.hbase.regionserver.BloomType;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
public class HBaseTableCreate {
public static void main(String[] args) throws IOException {
// 创建HBase配置对象
Configuration conf = HBaseConfiguration.create();
// 设置Zookeeper连接地址
conf.set("hbase.zookeeper.quorum", "localhost");
// 创建HBase连接对象
Connection conn = ConnectionFactory.createConnection(conf);
// 获取HBase管理对象
Admin admin = conn.getAdmin();
// 创建表描述符对象
TableDescriptor tableDesc1 = TableDescriptorBuilder.newBuilder(TableName.valueOf("students1"))
.setColumnFamily(TableDescriptorBuilder.of("grade"))
.setColumnFamily(TableDescriptorBuilder.of("scores"))
.build();
// 创建表
admin.createTable(tableDesc1);
// 创建表描述符对象
TableDescriptor tableDesc2 = TableDescriptorBuilder.newBuilder(TableName.valueOf("students2"))
.setColumnFamily(TableDescriptorBuilder.of("grade"))
.setColumnFamily(TableDescriptorBuilder.of("scores"))
.build();
// 创建表
admin.createTable(tableDesc2);
// 关闭资源
admin.close();
conn.close();
}
}
```
使用控制台命令创建HBase表的示例代码如下:
```shell
# 进入HBase shell
$ hbase shell
# 创建students1表
hbase(main):001:0> create 'students1', 'grade', 'scores'
0 row(s) in 1.3740 seconds
# 创建students2表
hbase(main):002:0> create 'students2', 'grade', 'scores'
0 row(s) in 0.4420 seconds
# 退出HBase shell
hbase(main):003:0> exit
```
阅读全文