import java.io.IOException;
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.MasterNotRunningException;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.util.Bytes;
public class HbaseConnetion {
Configuration cfg;
HBaseAdmin admin;
//初始化建立连接
public HbaseConnetion() throws MasterNotRunningException, ZooKeeperConnectionException{
cfg=HBaseConfiguration.create();
admin=new HBaseAdmin(cfg);
}
//关闭数据库连接
public void close() throws IOException{
if (admin != null) admin.close();
}
//判断表是否存在
public boolean isTableExist(String tablename) throws IOException{
if(admin.tableExists(tablename)){
return true;
}
else{
System.out.println("Table not exists");
return false;
}
}
//新建一个表
public void creat(String tablename,String[] culumnFamlilies) throws IOException{
if(admin==null) {
System.out.println("Database not Connected");
return;
}
if(isTableExist(tablename)) return;
HTableDescriptor desc =new HTableDescriptor(tablename);
for(int i=0;i<culumnFamlilies.length;i++){
desc.addFamily(new HColumnDescriptor(culumnFamlilies[i]));
}
admin.createTable(desc);
}
//添加一条记录
public void put(String tablename,String row_key,String family,String value)throws Exception{
if (!isTableExist(tablename)) return;
HTable table=new HTable(cfg, Bytes.toBytes(tablename));
Put put=new Put(Bytes.toBytes(row_key));
put.add(Bytes.toBytes(family),null,Bytes.toBytes(value));
table.put(put);
table.close();
}
//添加一条记录
public void put(String tablename,String row_key,String family,String column,String value)throws Exception{
if (!isTableExist(tablename)) return;
HTable table=new HTable(cfg, Bytes.toBytes(tablename));
Put put=new Put(Bytes.toBytes(row_key));
put.add(Bytes.toBytes(family),Bytes.toBytes(column),Bytes.toBytes(value));
table.put(put);
table.close();
}
//按照行键查询记录
public Result get(String tablename ,String row_key) throws IOException{
if(!isTableExist(tablename)) return null;
Get get=new Get(Bytes.toBytes(row_key));
HTable table=new HTable(cfg, tablename);
Result result=table.get(get);
table.close();
System.out.println(result);
return result;
}
//按行键删除记录
public boolean deleteRow(String tablename,String row_key) throws IOException{
if(!isTableExist(tablename)) return false;
HTable table=new HTable(cfg,Bytes.toBytes(tablename));
Delete delete=new Delete(Bytes.toBytes(row_key));
table.delete(delete);
table.close();
return true;
}
//删除table
public boolean deleteTable(String tablename) throws IOException{
if(!isTableExist(tablename))
return false;
admin.enableTable(tablename);
admin.deleteTable(tablename);
return true;
}
//显示所有数据
public void scan(String tablename) throws IOException{
if(!isTableExist(tablename)) return;
Scan scan=new Scan();
HTable table=new HTable(cfg, tablename);
ResultScanner rs=table.getScanner(scan);
if(rs==null){System.out.println("No result");}
for(Result r:rs){
System.out.println("Scan: "+r);
}
table.close();
}
//测试
public static void main(String[] args) throws Exception{
HbaseConnetion hbaseConnetion=new HbaseConnetion();
hbaseConnetion.put("teachers", "no1", "name", "famlilyName", "bai");
hbaseConnetion.put("teachers", "no1", "age", "24");
hbaseConnetion.get("teachers", "no1");
hbaseConnetion.close();
}
}