在虚拟机里,用HBase 使用 Java API 创建学生信息表 student,并向表中添加如下数据: student 表 name score english math computer student1 55 66 70 student2 80 92 100
时间: 2024-10-21 15:15:23 浏览: 33
在HBase中使用Java API创建学生信息表`student`并添加数据,首先需要确保已经设置了HBase的环境变量,然后按照以下步骤操作:
1. **初始化连接**:
导入必要的HBase和HBase客户端API:
```java
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.Table;
```
2. **配置连接**:
根据实际情况配置HBase连接,例如从配置文件读取:
```java
Configuration config = HBaseConfiguration.create();
config.set("hbase.zookeeper.quorum", "your_zookeeper_hosts");
config.set("hbase.zookeeper.property.clientPort", "your_zookeeper_port");
```
3. **创建表**:
如果表不存在,使用`Admin`对象创建它:
```java
Admin admin = ConnectionFactory.createConnection(config).getAdmin();
TableName tableName = TableName.valueOf("student");
if (!admin.tableExists(tableName)) {
byte[] schema = Bytes.toBytes("name\tscore\tenglish\temath\tcomputer\nfam1:qf1,fam2:qf2");
admin.createTable(tableName, schema);
}
```
4. **添加数据**:
定义一个`Put`对象来存储数据,然后使用`Table`对象将数据写入表中:
```java
Table table = ConnectionFactory.createConnection(config).getTable(tableName);
Put put = new Put(Bytes.toBytes("student1"));
put.addColumn(Bytes.toBytes("fam1"), Bytes.toBytes("qf1"), Bytes.toBytes("55"));
put.addColumn(Bytes.toBytes("fam1"), Bytes.toBytes("qf2"), Bytes.toBytes("66"));
put.addColumn(Bytes.toBytes("fam2"), Bytes.toBytes("qf3"), Bytes.toBytes("70"));
table.put(put);
// 添加另一个学生
Put put2 = new Put(Bytes.toBytes("student2"));
put2.addColumn(Bytes.toBytes("fam1"), Bytes.toBytes("qf1"), Bytes.toBytes("80"));
put2.addColumn(Bytes.toBytes("fam1"), Bytes.toBytes("qf2"), Bytes.toBytes("92"));
put2.addColumn(Bytes.toBytes("fam2"), Bytes.toBytes("qf3"), Bytes.toBytes("100"));
table.put(put2);
table.close(); // 关闭表连接
admin.close(); // 关闭管理员连接
```
完成以上代码后,"student"表就会包含指定的学生信息。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.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)
![7z](https://img-home.csdnimg.cn/images/20241231044736.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.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_ask_c1.png)