1、请使用Java API实现以下功能,并将程序代码和运行结果都截图提交。
-
列出所有表名
-
创建表
-
删除表
-
插入一条数据到单元格
-
删除一个单元格的数据
-
显示打印一个单元格的数据信息
正确答案:
我的答案:
package com.test;
import java.io.IOException;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
public class HBaseTset {
public static void main(String[] args) {
list();
//createTable("tb3",new String[] {"lie1","lie2","lie3","lie4"});
//deleteTable("sc");
//insertData("course","c01","Cname","","Lucy");
//insertData("course","c01","Ccredit","Math","85");
//deleteData("course","c02","Cname","");
printData("course","c01","Ccredit","Math");
printData("course","c01","Cname","");
list();
}
//1.Configuration:配置类
private static Configuration conf;
//2.Connection:连接类
private static Connection conn;
//3.Admin:管理员类
private static Admin admin;
//显示打印一个单元格的数据信息
public static void printData(String tableName,String rowKey,String colFamily,String col) {
init();
try {
Table t = conn.getTable(TableName.valueOf(tableName));
Get get = new Get(rowKey.getBytes());
get.addFamily(colFamily.getBytes());
get.addColumn(colFamily.getBytes(), col.getBytes());
Result result = t.get(get);
printResult(result);
t.close();
} catch (IOException e) {
e.printStackTrace();
}close();
}
private static void printResult(Result result) {
// TODO Auto-generated method stub
}
//删除一个单元格的数据
public static void deleteData(String tableName,String rowKey,String colFamily,String col) {
init();
try {
Table t = conn.getTable(TableName.valueOf(tableName));
Delete delete = new Delete(rowKey.getBytes());
delete.addFamily(colFamily.getBytes());
delete.addColumn(colFamily.getBytes(), col.getBytes());
t.delete(delete);
t.close();
System.out.println("数据删除成功!");
} catch (IOException e) {
e.printStackTrace();
}close();
}
//插入一条数据到单元格
public static void insertData(String tableName,String rowKey,String colFamily,String col,String val) {
init();
TableName tn = TableName.valueOf(tableName);
try {
Table t = conn.getTable(tn);
Put put = new Put(rowKey.getBytes());
put.addColumn(colFamily.getBytes(), col.getBytes(), val.getBytes());
t.put(put);
t.close();
System.out.println("添加数据成功!");
} catch (IOException e) {
e.printStackTrace();
}close();
}
//删除表
public static void deleteTable(String tableName) {
init();
//1.String型tableName封装成TableName
TableName tn = TableName.valueOf(tableName);
//2.判断表是否存在
try {
if(admin.tableExists(tn)) {
//1.disable
admin.disableTable(tn);
//2.删除表
admin.deleteTable(tn);
System.out.println(tableName+"删除成功!");
}
}catch (IOException e) {
e.printStackTrace();
}close();
}
//创建表
public static void createTable(String tableName,String[] colFamilies) {
init();
TableName tn = TableName.valueOf(tableName);
try {
if(admin.tableExists(tn)) {
System.out.println(tableName+"已经存在,不能创建!");
}else {
HTableDescriptor htd = new HTableDescriptor(tn);
for(String colFamily:colFamilies) {
HColumnDescriptor hcd = new HColumnDescriptor(colFamily);
htd.addFamily(hcd);
}
admin.createTable(htd);
System.out.println(tableName+"创建成功!");
}
} catch (IOException e) {
e.printStackTrace();
}
close();
}
//list方法
public static void list() {
init(); //初始化
try {
TableName[] tns = admin.listTableNames();
//遍历数组
for(TableName tn:tns) {
System.out.println(tn.toString());
}
} catch (IOException e) {
e.printStackTrace();
}
close();
}
//1.初始化的方法
public static void init() {
conf = HBaseConfiguration.create();
//做配置
conf.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
//得到连接
try {
conn = ConnectionFactory.createConnection(conf);
//得到管理员admin
admin = conn.getAdmin();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//2.关闭的方法
public static void close() {
if(admin !=null){
try {
admin.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(conn!=null) {
try {
conn.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}