?
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.junit.Test;
public class HBaseHelperTest {
@Test
public void getOneRecord() {
try {
Result rs = HBaseHelper.getOneRecord("HBASE_TABLE_NAME", "KEY");
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void getAllRecord() {
try {
// ResultScanner rs = HBaseHelper.getAllRecord("LP_PRICE_CHANNEL");
// ResultScanner rs = HBaseHelper.getAllRecord("LP_LINE");
// ResultScanner rs = HBaseHelper.getAllRecord("LP_FLIGHT");
// String[] family = new String[1];
// family[0] = "planeinfo";
// ResultScanner scan = HBaseHelper.getRecords("LP_LINE", "00002CANDLC2013012500", "00002CANDLC2013012523", family, null);
// List<Filter> filters = new ArrayList<Filter>();
// Filter filter = new PrefixFilter("08".getBytes());
// filters.add(filter);
// filter = new PrefixFilter("09".getBytes());
// filters.add(filter);
// String[] family = new String[1];
// family[0] = "planeinfo";
// Filter fil = new SingleColumnValueFilter(Bytes.toBytes("planeinfo"), Bytes.toBytes("carrierFullName"), CompareOp.EQUAL,Bytes.toBytes("中国南方航空公司"));
// filters.add(filter);
// ResultScanner scan = HBaseHelper.getRecords("LP_LINE", "00002CANDLC2013010000", "00002CANDLC2013019999", family, null);
// ResultScanner rs = HBaseHelper.getAllRecord("LP_FLIGHT");
String[] family = new String[1];
family[0] = "planeinfo";
ResultScanner scan = HBaseHelper.getRecords("LP_FLIGHT", "00002HGHCAN00CZ38302013021200", "00002HGHCAN00CZ38302013021223", family, null);
HBaseHelper.printResult(scan);
} catch (Exception e) {
// TODO: handle exception
}
}
}
?
HbaseDriverManager
import java.io.IOException;
import java.util.ArrayList;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.client.HTablePool;
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 HbaseDriverManager {
private static Configuration conf;
private static HTablePool hTablePool;
private static int POOL_MAX_SIZE = 20;
/**
* 获取Hbase配置信息
*
* @return
*/
public static Configuration getHbaseConf() {
return conf;
}
static {
conf = new Configuration();// FIXME TSS 此处修改为可配置
// 与hbase/conf/hbase-site.xml中hbase.zookeeper.quorum配置的值相同
conf.set("hbase.zookeeper.quorum", "slaver_1");
// 与hbase/conf/hbase-site.xml中hbase.zookeeper.property.clientPort配置的值相同
conf.set("hbase.zookeeper.property.clientPort", "2181");
conf = HBaseConfiguration.create(conf);
}
/**
* 获取HBaseAdmin
*
* @return
*/
public static HBaseAdmin getHBaseAdmin() {
HBaseAdmin habseAdmin = null;
try {
habseAdmin = new HBaseAdmin(conf);
} catch (MasterNotRunningException e) {
e.printStackTrace();
} catch (ZooKeeperConnectionException e) {
e.printStackTrace();
}
return habseAdmin;
}
/**
* 返回htablepool连接池中的一个htable
*
* @param tableName
* @return
*/
public static synchronized HTable getHtable(String tableName) {
if (hTablePool != null)
return (HTable) hTablePool.getTable(tableName);// 如果hTablePool对象已经存在,直接取出一个htable
else {
hTablePool = new HTablePool(conf, POOL_MAX_SIZE);// 注意这个值设置的是每个htable表在pool中的最大值
return (HTable) hTablePool.getTable(tableName);
}
}
@SuppressWarnings("deprecation")
public static 