2.请编程实现以下功能:
(1) createTable(String tableName, String[] fields)
创建表,参数 tableName 为表的名称,字符串数组 fields 为存储记录各个字段名称的数组。
要求当 HBase 已经存在名为 tableName 的表的时候,先删除原有的表,然后再创建新的表。
package cn.wl.edu.hbase;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
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 java.io.IOException;public classCreateTable {public staticConfiguration configuration;public staticConnection connection;public staticAdmin admin;public static voidcreateTable(String tableName, String[] fields)
throws IOException {
init();
TableName tablename=TableName.valueOf(tableName);if(admin.tableExists(tablename)) {
System.out.println("table is exists!");
}
HTableDescriptor hTableDescriptor= newHTableDescriptor(tablename);for(String str : fields) {
HColumnDescriptor hColumnDescriptor= newHColumnDescriptor(str);
hTableDescriptor.addFamily(hColumnDescriptor);
}
admin.createTable(hTableDescriptor);
close();
}public static voidinit() {
configuration=HBaseConfiguration.create();
configuration.set("hbase.rootdir", "hdfs://localhost:9000/hbase");try{
connection=ConnectionFactory.createConnection(configuration);
admin=connection.getAdmin();
}catch(IOException e) {
e.printStackTrace();
}
}public static voidclose() {try{if (admin != null) {
admin.close();
}if (null !=connection) {
connection.close();
}
}catch(IOException e) {
e.printStackTrace();
}
}public static voidmain(String[] args) {
String[] fields= { "Score"};try{
createTable("person", fields);
}catch(IOException e) {
e.printStackTrace();
}
}
}
(2)addRecord(String tableName, String r