本文对以下内容进行介绍
1、封装连接
2、封装操作对象
3、封装命名空间的创建
4、封装表的创建
5、封装插入数据的操作
6、封装获取数据的操作
7、封装删除1数据的操作
8、封装删除2数据的操作(具体到某一列)
9、对以上6个函数进行操作检验(hbase创建命名空间、创建表、插入数据、查询数据)
1、封装连接
//封装连接
public static Connection GetConnection() throws IOException {
Configuration conf = HBaseConfiguration.create();
Connection connection = ConnectionFactory.createConnection(conf);
return connection;
}
2、封装操作对象
// 封装对操作对象
public static Admin Getadmin() throws IOException {
Admin admin = GetConnection().getAdmin();
return admin;
}
3、封装命名空间的创建
//封装命名空间的创建
public static boolean Exec_NameSpace(String namespace) throws IOException {
boolean result = false;
try {
Getadmin().getNamespaceDescriptor(namespace);
result=true;
} catch (NamespaceNotFoundException exception) {
NamespaceDescriptor build = NamespaceDescriptor.create(namespace).build();
Getadmin().createNamespace(build);
result=true;
}
return result;
}
4、封装表的创建
// 封装创建表的操作
public static boolean Exec_CreateTable(String tablename,String family)throws IOException{
boolean result = false;
//先判断该表不存在
TableName tableName = TableName.valueOf(tablename);
if(Getadmin().tableExists(tableName)==false){
// 创建
HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(family);
hTableDescriptor.addFamily(hColumnDescriptor);
Getadmin().createTable(hTableDescriptor);
result=true;
}
else
{
result=true;
}
return result;
}
5、封装插入数据的操作
// 封装插入数据的操作
public static boolean Insert_Data(String tablename,String family,String rowkey,String column,String value) throws Exception {
boolean result=false;
try{
Put put = new Put(Bytes.toBytes(rowkey));
put.addColumn(Bytes.toBytes(family),Bytes.toBytes(column),Bytes.toBytes(value));
TableName table = TableName.valueOf(tablename);
GetConnection().getTable(table).put(put);
result=true;
}catch(Exception ex){
result=false;
throw new Exception("执行插入到Hbase过程中,出现了问题!");
}
return result;
}
6、封装获取数据的操作
// 封装获取数据的操作
public static Result GetDateByRowKey(String tablename,String Rowkey) throws IOException{
Get get=new Get(Bytes.toBytes(Rowkey));
TableName table=TableName.valueOf(tablename);
Result result = GetConnection().getTable(table).get(get);
return result;
}
}
7、封装删除1数据的操作
//封装删除(1)
public static boolean Exec_DelDate(String tablename,String Rowkey) throws IOException{
TableName tableName = TableName.valueOf(tablename);
//删除之前,先查看数据是否存在,调用GetDateByRowKey方法
if(GetDateByRowKey(tablename,Rowkey)!=null){
Delete delete = new Delete(Bytes.toBytes(Rowkey));
GetConnection().getTable(tableName).delete(delete);
return true;
}
else{
return false;
}
}
8、封装删除2数据的操作(具体到某一列)
//删除(方法重载)
public static boolean Exec_DelDate(String tablename,String Rowkey,String family,String column) throws IOException{
TableName tableName = TableName.valueOf(tablename);
if(GetDateByRowKey(tablename,Rowkey)!=null){
Delete delete = new Delete(Bytes.toBytes(Rowkey));
delete.addColumn(Bytes.toBytes(family),Bytes.toBytes(column));
GetConnection().getTable(tableName).delete(delete);
return true;
}
else{
return false;
}
}
9、对以上6个函数进行操作检验(hbase创建命名空间、创建表、插入数据、查询数据)
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
public class ddemo1 {
public static void main(String[] args) throws Exception {
boolean result1=HbaseHelper.Exec_NameSpace("myschool1");
if(result1){
System.out.println("创建命名空间成功");
}
else{
System.out.println("创建失败");
}
boolean result2=HbaseHelper.Exec_CreateTable("myschool1:stuinfo","info");
if(result2){
System.out.println("创建表成功");
}
else{
System.out.println("创建失败");
}
boolean result3=HbaseHelper.Insert_Data("myschool1:stuinfo","info","101","name","小镭") ;
HbaseHelper.Insert_Data("myschool1:stuinfo","info","101","tall","180") ;
HbaseHelper.Insert_Data("myschool1:stuinfo","info","102","name","小黑") ;
//HbaseHelper.Insert_Data("myschool1:stuinfo","info","102","tall","80") ;
HbaseHelper.Insert_Data("myschool1:stuinfo","info","103","name","blue") ;
HbaseHelper.Insert_Data("myschool1:stuinfo","info","103","tall","3") ;
if(result3){
System.out.println("插入成功");
}
else{
System.out.println("插入失败");
}
Result result4=HbaseHelper.GetDateByRowKey("myschool1:stuinfo","101");
if(result4.isEmpty()){
System.out.println("没有查询到数据!");
}
else{
System.out.println("查询数据!");
}
System.out.println("查询结果为:");
System.out.println(result4);
// 删除102 的‘tall’这个列
if(HbaseHelper.Exec_DelDate("myschool1:stuinfo","103","info","name")){
System.out.println("删除成功!");
}
else{
System.out.println("删除失败!");
}
// 删除101整个列族
if(HbaseHelper.Exec_DelDate("myschool1:stuinfo","101")){
System.out.println("删除成功!");
}
else{
System.out.println("删除失败!");
}
}
public static void ShowResult(Result r){
for (Cell cell:r.rawCells()){
System.out.println("rowkey"+ Bytes.toString(CellUtil.cloneRow(cell)));
System.out.println("family"+ Bytes.toString(CellUtil.cloneFamily(cell)));
System.out.println("column"+ Bytes.toString(CellUtil.cloneQualifier(cell)));
System.out.println("value"+ Bytes.toString(CellUtil.cloneValue(cell)));
}
}
}
结果展示: