HBase中没有库的概念
HBase lib目录下所有JAR包复制到项目中,Hbase 版本0.98.5
package com.zxing.imgQRCode;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
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.KeyValue;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HConnection;
import org.apache.hadoop.hbase.client.HConnectionManager;
import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.io.compress.Compression.Algorithm;
import org.apache.hadoop.hbase.io.encoding.DataBlockEncoding;
public class HbaseConnection {
private String rootDir;
private String zkServer;
private String port;
private Configuration conf;
private HConnection hConn=null;
public HbaseConnection(String rootDir, String zkServer, String port) {
super();
this.rootDir = rootDir;
this.zkServer = zkServer;
this.port = port;
conf=HBaseConfiguration.create();
conf.set("hbase.rootdir", rootDir);
conf.set("hbase.zookeeper.quorum ", zkServer);
conf.set("hbase.zookeeper.property.clientPort", port);
try {
hConn=HConnectionManager.createConnection(conf);
} catch (IOException e) {
e.printStackTrace();
}
}
//创建表
public void crateTable(String tableName,List<String> cols){
try {
HBaseAdmin admin=new HBaseAdmin(conf);
if(admin.tableExists(tableName))
throw new IOException("table exists");
else{
HTableDescriptor tableDesc=new HTableDescriptor(tableName);
for(String col:cols){
HColumnDescriptor colDesc=new HColumnDescriptor(col);
colDesc.setCompressionType(Algorithm.GZ);
colDesc.setDataBlockEncoding(DataBlockEncoding.DIFF);
tableDesc.addFamily(colDesc);
}
admin.createTable(tableDesc);
}
} catch (MasterNotRunningException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ZooKeeperConnectionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//插入数据
public void saveData(String tableName,List<Put> puts){
try {
HTableInterface table =hConn.getTable(tableName);
table.put(puts);
table.setAutoFlush(false);
table.flushCommits();
} catch (IOException e) {
e.printStackTrace();
}
}
//得到数据
public Result getData(String tableName,String rowkey){
try {
HTableInterface table =hConn.getTable(tableName);
Get get=new Get(rowkey.getBytes());
return table.get(get);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
//输出result结果
public void format(Result result){
String rowkey=Bytes.toString(result.getRow());
KeyValue[] kvs=result.raw();
for (KeyValue kv:kvs){
String family= Bytes.toString(kv.getFamily());
String qualifier= Bytes.toString(kv.getQualifier());
System.out.println("rowkey->"+rowkey+"family->"+family+"qualifier->"+qualifier);
}
}
//全表扫描
public void hbaseScan(String tableName){
Scan scan=new Scan();//扫描器
scan.setCaching(1000);//缓存1000条数据,一次读取1000条
try {
HTableInterface table =hConn.getTable(tableName);
ResultScanner scanner=table.getScanner(scan);//返回迭代器
for(Result res:scanner){
format(res);
}
} catch (IOException e) {
e.printStackTrace();
}
}
//比较过滤器
public void filterTest(String tableName){
Scan scan=new Scan();//扫描器
scan.setCaching(1000);//缓