Hbase API,
java类
HbaseAdmin
org.apache.hadoop.hbase.client.HBaseAdmin
作用:提供了一个接口来管理HBase数据库的表信息。
HbaseConfiguration
org.apache.hadoop.hbase.HBaseConfiguration
用法示例:
HBaseConfiguration hconfig = new HBaseConfiguration();
hconfig.set(“hbase.zookeeper.property.clientPort”,”2181”);
HTabel
org.apache.hadoop.hbase.client.HTabel
作用:可以用来和HBase表直接通信
HTablePool
作用:解决HTable存在的线程不安全的问题,同时通过维护固定数量的HTable数量,在程序运行期间复用这些HTabel资源对象
说明:
自动创建Htable对象,对客户端来说完全透明,避免多线程并发修改问题
htable对象之间公用configuration连接,减少网络开销
使用简单,通过htablepool的gettable方法取得一个htable对象,然后进行put、get、scan、delete等操作,最后通过htablepool的puttable方法将htable对象放回到htbalepool中
HTableDescriptor
org.apache.hadoop.hbase.HTableDescriptor
HColumnDescriptor
org.apache.hadoop.hbase.HColumnDescriptor
Put
作用:用来对单个行执行添加操作
Get
Scanner
public class HBaseConnection{
private String rootDir;
private String zkServer;
private String port;
private Configution conf;
private HConnection hConn =null;
private HbaseConnection (String rootDir,String zkServer,String port){
this.rootDir=rootDir;
this.zkServer=zkServer;
this.port=port;
conf=HbaseConfigution.create();
conf.set("hbase.rootDir",rootDir);
conf.set("hbase.zookeeper.quorum",zkServer);
conf.set("hbase.zoopkeeper.property.clientPort",port);
try{
hConn=HConnectionManager.createConnection(conf);
}catch(IOException e){
e.printStacktrace();
}
}
public void createTable(String tbaleName,List<String> cols){
try{
HBaseAdmin admin=new HBaseAdmin(conf);
if(admin.tableExists(tableName)){
throw new Exception("table exists");
}else{
HBaseDescriptor tableDesc=new HBaseDescriptor(tableName);
for(String col:cols){
HColumnDescriptor colDesc=new HColumnDescriptor(col);
colDesc.setCompressionType(Algorithm.GZ);
tableDesc.addFamily(colDesc);
}
}
admin.createTable(tableDesc);
}catch(Exception e){
……
}
}
public void saveData(String tableName,List<Put> puts){
try{}catch{}……
HTableInterface table=hConn.getTabel(tableName);
table.put(puts);
table.setAutoFlush(false);
table.flushCommits();
}
public Result getData(String tableName,String rowKey){
try{}catch{}……
HTabelInterface table = hConn.getTabel(tableName);
Get get = new Get(Bytes.toBytes(rowKey));
return table.get(get);
}
public void format(Result result){
String rowKey = Bytes.toString(result.getRow());
KeyValue[] kvs=result.row();
for(KeyValue kv:lvs){
String family=Bytes.toString(kv.getFamily());
String qualifier=Bytes.toString(kv.getQualifier());
System.out.println("rowkey->"+rowKey+"family->"+family+"qualifier->"+qualifier);
}
}
public void static main(String[] args){
String rootDir="hdfs://ubuntu/hbase";
String zkServer="ip";
String port="2181";
HbaseConnection conn =new HbaseConnection(rootDir,zkServer,port);
List<String> cols=new LinkedList<String>();
cols.add("basicInfo");
cols.add("moreInfo");
conn.createTable("student",cols);
//保存数据
List<Put> puts=new LinkedList<Put>();
Put put1=new Put(Bytes.toBytes("Tom"));
put1.add(Bytes.toBytes("basicInfo"),Bytes.toBytes("age"),Bytes.toBytes("27"));
put1.add(Bytes.toBytes("moreInfo"),Bytes.toBytes("tel"),Bytes.toBytes("12456y"));
conn.saveData("student",puts);
Put put2 = new Put(Bytes.toBytes("Jim"));
put2.add(Bytes.toBytes("basicInfo"),Bytes.toBytes("age"),Bytes.toBytes("24"));
put2.add(Bytes.toBytes("moreInfo"),Bytes.toBytes("tel"),Bytes.toBytes("345678"));
puts.add(put1);
puts.add(put2);
conn.saveData("student",puts);
Result reslut =conn.getData("student","Tom");
conn.format(result);
}
}