HBase Basic API Demo Code

又这么久... 太懒了... 放个很久以前写的HBase基本API的示例程序代码吧,涉及crud操作和几个简单的filter :evil:


import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
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.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.HTablePool;
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.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.filter.SubstringComparator;
import org.apache.hadoop.hbase.filter.ValueFilter;
import org.apache.hadoop.hbase.util.Bytes;

public class HBaseAPIDemoCode {

private static final Configuration config;
private static final HTablePool tablePool;
private static HBaseAdmin admin;

static {
config = HBaseConfiguration.create();
tablePool = new HTablePool(config, 10);
config.set("hbase.zookeeper.quorum", "master");
}

public static HBaseAdmin getHBaseAdmin() {
if (config == null) {
throw new RuntimeException("config null");
}
if (admin == null) {
try {
admin = new HBaseAdmin(config);
} catch (MasterNotRunningException e) {
e.printStackTrace();
} catch (ZooKeeperConnectionException e) {
e.printStackTrace();
}
}
return admin;
}

public static HTable getHTable(final String tableName) {
return (HTable) tablePool.getTable(tableName);
}

public static void printResult(final Result result) {
byte[] family = null;
System.out.println("RowKey: " + Bytes.toString(result.getRow()));
for (KeyValue kv : result.raw()) {
family = kv.getFamily();
System.out.println("Column: " + Bytes.toString(family) + ":"
+ Bytes.toString(kv.getQualifier()));
System.out.println("Value: " + Bytes.toString(kv.getValue()));
}
}

public static void testCreateTable(final String tableName,
final String[] columnFamily) {
HBaseAdmin admin = getHBaseAdmin();
// if exists,delete first.
try {
if (admin.tableExists(tableName)) {
admin.disableTable(tableName);
admin.deleteTable(tableName);
}

HTableDescriptor htd = new HTableDescriptor(tableName);
for (String column : columnFamily) {
htd.addFamily(new HColumnDescriptor(column));
}
admin.createTable(htd);
System.out.println(tableName + " created!");
} catch (IOException e) {
e.printStackTrace();
}

}

public static void testDelTable(final String tableName) {
HBaseAdmin admin = getHBaseAdmin();
try {
admin.disableTable(tableName);
admin.deleteTable(tableName);
System.out.println(tableName + " deleted!");
} catch (IOException e) {
e.printStackTrace();
}
}

public static void testInsertData(final String tableName) {
HTable table = getHTable(tableName);
int rowIdStart = 10001;
int rowIdEnd = 10010;
int index = 1;
// put 10 cookies
List<Put> putList = new ArrayList<Put>();
for (; rowIdStart <= rowIdEnd; rowIdStart++) {
Put put = new Put(Bytes.toBytes(String.valueOf(rowIdStart)));
put.add(Bytes.toBytes("product"), Bytes.toBytes("proID"),
Bytes.toBytes(String.valueOf(index)));
put.add(Bytes.toBytes("product"), Bytes.toBytes("proTime"),
Bytes.toBytes(String.valueOf(new Date())));
if (index % 2 == 0) {
put.add(Bytes.toBytes("product"), Bytes.toBytes("proType"),
Bytes.toBytes("coco"));
} else {
put.add(Bytes.toBytes("product"), Bytes.toBytes("proType"),
Bytes.toBytes("milk"));
}
putList.add(put);
index++;
}
try {
table.put(putList);
table.close();
System.out.println("cookie data inserted!");
} catch (IOException e) {
e.printStackTrace();
}
}

public static void testQueryAll(final String tableName) {
HTable table = getHTable(tableName);
try {
ResultScanner scanner = table.getScanner(new Scan());
for (Result result : scanner) {
printResult(result);
}
table.close();
} catch (IOException e) {
e.printStackTrace();
}
}

// 删除rowKey不存在的记录,HBase会吞掉该错误。
public static void testDelARow(final String tableName, final String rowKey) {
HTable table = getHTable(tableName);
Delete del = new Delete(rowKey.getBytes());
try {
table.delete(del);
table.close();
System.out.println("RowKey: " + rowKey + " deleted!");
testQueryAll(tableName);
} catch (IOException e) {
e.printStackTrace();
}
}

public static void testGetARow(final String tableName, final String rowKey) {
HTable table = getHTable(tableName);
Get get = new Get(rowKey.getBytes());
try {
Result result = table.get(get);
List<KeyValue> resultList = result.list();
if (resultList == null) {
System.out.println(rowKey + " null!");
return;
}
printResult(result);
table.close();
} catch (IOException e) {
e.printStackTrace();
}
}

public static void testGetRowsByCookieType(final String tableName,
final String cookieType) {
HTable table = getHTable(tableName);
Filter vfilter = new ValueFilter(CompareOp.EQUAL,
new SubstringComparator(cookieType));
Scan scan = new Scan();
scan.setFilter(vfilter);
try {
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
printResult(result);
}
table.close();
} catch (IOException e) {
e.printStackTrace();
}
}

public static void testGetRowsBySingleColumn(final String tableName,
final String family, final String column, final String columnValue) {
HTable table = getHTable(tableName);
Filter vfilter = new SingleColumnValueFilter(Bytes.toBytes(family),
Bytes.toBytes(column), CompareOp.EQUAL,
Bytes.toBytes(columnValue));
Scan scan = new Scan();
scan.setFilter(vfilter);
try {
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
printResult(result);
}
table.close();
} catch (IOException e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
final String tableName = "cookie";
// testCreateTable(tableName, new String[] { "product", "sale" });
// testInsertData(tableName);
// testDelTable(tableName);
// testQueryAll(tableName);
// testDelARow(tableName, "10010");
// testGetARow(tableName, "10011");
// testGetRowsByCookieType(tableName, "milk");
testGetRowsBySingleColumn(tableName, "product", "proType", "coco");
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值