任务描述
本关任务:使用 Java API 删除表 exam_tb2,创建表 exam_tb3 并添加如下表中数据。
user_info | address_info | ||||
name | age | address | phone | ||
1 | Avatar | 100 | pandora | avatar@163.com | 123456 |
2 | change | 50 | moon | change@163.com | 234567 |
3 | nezha | 6 | earth | nezha@163.com | 345678 |
编程要求
实现删除表 exam_tb2,创建表 exam_tb3 并添加数据。
代码实现
package com.yy;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.util.ArrayList;
import java.util.List;
import java.io.IOException;
public class HbaseUtil {
private static Admin admin = null;
private static Connection connection = null;
private static Configuration conf = null;
static {
// HBase配置文件
conf = HBaseConfiguration.create();
// 获取连接对象
try {
connection = ConnectionFactory.createConnection(conf);
// 获取HBase管理员对象
admin = connection.getAdmin();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void close(Connection conn, Admin admin) throws IOException {
if (conn != null) {
conn.close();
}
if (admin != null) {
admin.close();
}
}
//删除exam_tb2表
public void deleteTable() throws IOException {
/**********Begin**********/
TableName tableName = TableName.valueOf("exam_tb2");
if (admin.tableExists(tableName)) {
admin.disableTable(tableName);
admin.deleteTable(tableName);
}
/**********End**********/
}
//创建exam_tb3表
public void createTab() throws IOException {
/**********Begin**********/
TableName tableName = TableName.valueOf("exam_tb3");
if (!admin.tableExists(tableName)) {
TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(tableName);
ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder("user_info".getBytes());
tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptorBuilder.build());
columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder("address_info".getBytes());
tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptorBuilder.build());
admin.createTable(tableDescriptorBuilder.build());
}
/**********End**********/
}
//往exam_tb3表中添加数据
public void putBatch() throws IOException {
/**********Begin**********/
TableName tableName = TableName.valueOf("exam_tb3");
Table table = connection.getTable(tableName);
List<Put> puts = new ArrayList<>();
Put put1 = new Put(Bytes.toBytes("1"));
put1.addColumn(Bytes.toBytes("user_info"), Bytes.toBytes("name"), Bytes.toBytes("Avatar"));
put1.addColumn(Bytes.toBytes("user_info"), Bytes.toBytes("age"), Bytes.toBytes("100"));
put1.addColumn(Bytes.toBytes("address_info"), Bytes.toBytes("address"), Bytes.toBytes("pandora"));
put1.addColumn(Bytes.toBytes("address_info"), Bytes.toBytes("email"), Bytes.toBytes("avatar@163.com"));
put1.addColumn(Bytes.toBytes("address_info"), Bytes.toBytes("phone"), Bytes.toBytes("123456"));
puts.add(put1);
Put put2 = new Put(Bytes.toBytes("2"));
put2.addColumn(Bytes.toBytes("user_info"), Bytes.toBytes("name"), Bytes.toBytes("change"));
put2.addColumn(Bytes.toBytes("user_info"), Bytes.toBytes("age"), Bytes.toBytes("50"));
put2.addColumn(Bytes.toBytes("address_info"), Bytes.toBytes("address"), Bytes.toBytes("moon"));
put2.addColumn(Bytes.toBytes("address_info"), Bytes.toBytes("email"), Bytes.toBytes("change@163.com"));
put2.addColumn(Bytes.toBytes("address_info"), Bytes.toBytes("phone"), Bytes.toBytes("234567"));
puts.add(put2);
Put put3 = new Put(Bytes.toBytes("3"));
put3.addColumn(Bytes.toBytes("user_info"), Bytes.toBytes("name"), Bytes.toBytes("nezha"));
put3.addColumn(Bytes.toBytes("user_info"), Bytes.toBytes("age"), Bytes.toBytes("6"));
put3.addColumn(Bytes.toBytes("address_info"), Bytes.toBytes("address"), Bytes.toBytes("earth"));
put3.addColumn(Bytes.toBytes("address_info"), Bytes.toBytes("email"), Bytes.toBytes("nezha@163.com"));
put3.addColumn(Bytes.toBytes("address_info"), Bytes.toBytes("phone"), Bytes.toBytes("345678"));
puts.add(put3);
table.put(puts);
table.close();
/**********End**********/
}
}