HBase – Hadoop Database,是一个高可靠性、高性能、面向列、可伸缩的分布式存储系统,利用HBase技术可在廉价PC Server上搭建起大规模结构化存储集群。
HBase是Google Bigtable的开源实现,类似Google Bigtable利用GFS作为其文件存储系统,HBase利用Hadoop HDFS作为其文件存储系统;Google运行MapReduce来处理Bigtable中的海量数据,HBase同样利用Hadoop MapReduce来处理HBase中的海量数据;Google Bigtable利用 Chubby作为协同服务,HBase利用Zookeeper作为对应。
Hbase提供了Java的Api接口,通过JavaApi操作Hbase中数据的CURD等等的相关操作,下面是部分测试的例子,读者可以进行相应的参考。
public static Configuration config = null;
static {
config = HBaseConfiguration.create();
config.set("hbase.zookeeper.property.clientPort", "2181");
config.set("hbase.zookeeper.quorum", "192.168.154.100");
config.set("hbase.master", "192.168.1.102:600000");
}
public static void main(String[] args) throws Exception {
testScanChacheMethod();
}
public static void testScanChacheMethod() {
testScanCache(1, 1);
testScanCache(100, 10);
testScanCache(20, 10);
}
/** * Scan */
public static void testScanCache(int caching, int batch) {
final int[] counters = {0, 0};
AppenderSkeleton appender = new AppenderSkeleton() {
@Override
public boolean requiresLayout() {
return false;
}
@Override
public void close() {}
@Override
protected void append(LoggingEvent event) {
String ev = event.getMessage().toString();
if(ev != null && ev.contains("Call:next")) {
counters[0] ++;
}
}
};
log.removeAllAppenders();
log.setAdditivity(false);
log.addAppender(appender);
log.setLevel(Level.INFO);
try {
HTable table = new HTable(config, "testtable");
Scan scan = new Scan();
scan.setCaching(caching);
scan.setBatch(batch);
ResultScanner result = table.getScanner(scan);
for (Result res : result) {
log.info(res);
}
System.out.println(">>>>>>Caching :" + caching + ", Batch :" + batch + " , Results :" + result + ", RPCs:" + counters[0]);
log.info(">>>>>>Caching :" + caching + ", Batch :" + batch + " , Results :" + result + ", RPCs:" + counters[0]);
table.close();
} catch (IOException e) {
log.error(e);
}
}
/** * 扫描 */
public static void testScan() {
try {
HTable table = new HTable(config, "testtable");
Scan scan = new Scan();
ResultScanner result = table.getScanner(scan);
for (Result res : result) {
log.info(res);
}
Scan scan2 = new Scan();
scan2.addColumn("col1".getBytes(), "name".getBytes()).setStartRow("row2".getBytes());
scan2.addFamily("col1".getBytes());
ResultScanner scanner = table.getScanner(scan2);
for (Result res : scanner) {
log.info(res);
}
table.close();
} catch (IOException e) {
log.error(e);
}
}
/** * 批处理 */
public static void testBatch() {
try {
HTable table = new HTable(config, "testtable");
List<Row> batch = new ArrayList<Row>();
Put put = new Put("row1".getBytes());
put.add("col1".getBytes(), "name".getBytes(), "wy".getBytes());
batch.add(put);
Get get = new Get("row1".getBytes());
get.addColumn("col1".getBytes(), "name".getBytes());
batch.add(get);
Delete delete = new Delete("row1".getBytes());
delete.deleteColumn("col1".getBytes(), "name".getBytes());
batch.add(delete);
try {
Object[] obj = table.batch(batch);
for (int i = 0; i < obj.length; i++) {
log.info(obj[i]);
}
} catch (InterruptedException e) {
log.error(e);
}
table.close();
} catch (IOException e) {
log.error(e);
}
}
/** * 删除操作 */
public static void testCompareAndDelete() {
try {
HTable table = new HTable(config, "testtable");
Delete delete = new Delete("row1".getBytes());
boolean result = table.checkAndDelete("row1".getBytes(), "col1".getBytes(), "name".getBytes(), "x".getBytes(), delete);
log.info(result);
table.close();
} catch (IOException e) {
log.error(e);
}
}
/** * 删除数据 */
public static void testDelete() {
try {
HTable table = new HTable(config, "testtable");
Delete delete = new Delete("row1".getBytes());
delete.deleteColumn("col1".getBytes(), "name".getBytes());
delete.deleteFamily("col1".getBytes());
table.delete(delete);
table.close();
} catch (IOException e) {
log.error(e);
}
}
/** * 获取特定的行或者前一行 */
public static void testRowBefore() {
try {
HTable table = new HTable(config, "testtable");
log.info(table.getRegionLocation("row1"));
Result result = table.getRowOrBefore("row1".getBytes(), "col1".getBytes());
byte[] value = result.getValue("col1".getBytes(), "name".getBytes());
log.info(Bytes.toString(value));
table.close();
} catch (IOException e) {
log.error(e);
}
}
/** * 批量的get */
public static void testListGet() {
try {
HTable table = new HTable(config, "testtable");
List<Get> getList = new ArrayList<Get>();
Get get = new Get("row1".getBytes());
get.addColumn("col1".getBytes(), "name".getBytes());
getList.add(get);
Get get2 = new Get("row2".getBytes());
get2.addColumn("col1".getBytes(), "name".getBytes());
getList.add(get2);
Result[] results = table.get(getList);
for (Result result : results) {
for (Cell cell : result.rawCells()) {
log.info(Bytes.toString(cell.getFamilyArray())+ "<>" +
Bytes.toString(cell.getQualifierArray()) + "<>" +
Bytes.toString(cell.getRowArray()) + "<>" +
cell.getTimestamp() + "<>" +
Bytes.toString(cell.getValueArray())
);
}
}
table.close();
} catch (Exception e) {
log.error(e);
}
}
/** * 批量的put */
public static void testListPut() {
try {
HTable table = new HTable(config, "testtable");
List<Put> putList = new ArrayList<Put>();
Put put = new Put("row1".getBytes());
put.add("col1".getBytes(), "name".getBytes(), "y".getBytes());
putList.add(put);
Put put2 = new Put("row1".getBytes());
put2.add("col1".getBytes(), "name".getBytes(), "x".getBytes());
putList.add(put2);
table.put(putList);
table.close();
} catch (Exception e) {
log.error(e);
}
}
/** * 获取数据 */
public static void testGet() {
try {
HTable table = new HTable(config, "testtable");
Get get = new Get(Bytes.toBytes("row1"));
get.addColumn("col1".getBytes(), "name".getBytes());
Result result = table.get(get);
log.info(result);
// 输出值
byte[] values = result.getValue("col1".getBytes(), "name".getBytes());
log.info(Bytes.toString(values));
table.close();
} catch (IOException e) {
log.error(e);
}
}
/** * 向列中添加数据 */
public static void testPut() {
Configuration config = HBaseConfiguration.create();
try {
HTable table = new HTable(config, "testtable");
Put put = new Put(Bytes.toBytes("row1"));
put.add("col1".getBytes(), "name".getBytes(), "x".getBytes());
table.put(put);
table.close();
} catch (Exception e) {
log.error(e);
}
}
转载注明出处:Hbase葱岭探秘–JavaApi实践