上课跟着老师做的简单学生信息HBase数据库
首先对操作数据库的方法进行封装;创建命名空间、创建表、插入数据、查询数据、删除数据
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
public class HbaseUtil {
// 连接
public static Connection GetConn() throws IOException {
Configuration conf = HBaseConfiguration.create();
Connection connection = ConnectionFactory.createConnection(conf);
return connection;
}
// 操作对象
public static Admin GetAdmin() throws IOException {
return GetConn().getAdmin();
}
// 创建命名空间
public static boolean Exec_CreateNameSpace(String name) throws IOException {
try {
GetAdmin().getNamespaceDescriptor(name);
return true;
} catch (NamespaceNotFoundException e) {
NamespaceDescriptor build = NamespaceDescriptor.create(name).build();
GetAdmin().createNamespace(build);
return true;
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
// 创建表
public static boolean Exec_CreateTable(String name, String info) throws IOException {
TableName tableName = TableName.valueOf(name);
if(GetAdmin().tableExists(tableName) == false){
HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(info);
hTableDescriptor.addFamily(hColumnDescriptor);
GetAdmin().createTable(hTableDescriptor);
return true;
}
return false;
}
// 添加数据
public static boolean Exec_AddData(String tableName, String info, String rowkey, String column, String value) throws IOException {
try{
Put put = new Put(Bytes.toByt