HBase封装类

最近需要用到HBase数据库。但是文件数据库不支持SQL脚本,用起来太麻烦了。

而且他的设计模式,还无法使用反射之类的方式实现自动填充。所以为了尽可能的提高易用性,找到一个开源框架吧。

其实知识进行了一个初步的操作封装。叫做hbase.dsl。

记录一下。

原始HBase代码

public class PutAndGet 
{   
	public static void main(String[] args) throws IOException 
	{     
		HTable hTable = new HTable(“test”);     
		byte[] rowId = Bytes.toBytes(“abcd”);     
		byte[] famA = Bytes.toBytes(“famA”);     
		byte[] col1 = Bytes.toBytes(“col1”);     
		Put put = new Put(rowId).add(famA, col1, Bytes.toBytes(“hello world!”));    
		hTable.put(put);     
		Get get = new Get(rowId);     
		Result result = hTable.get(get);     
		byte[] value = result.getValue(famA, col1);     		System.out.println(Bytes.toString(value));   
	} 
}

DSL代码

public class PutAndGetWithDsl 
{   
	public static void main(String[] args) throws IOException 
	{     
		HBase<QueryOps, String> hBase = new HBase<QueryOps, String>(String.class);  
		hBase.save(“test”).row(“abcd”).family(“famA”).col(“col1”,“hello world!”);     
		String value = hBase.fetch(“test”).row(“abcd”).family(“famA”).value(“col1”, String.class);     
		System.out.println(value);   
	} 
}

查询HBase代码

public class Scanner 
{   
	public static void main(String[] args) throws IOException 
	{     
		byte[] famA = Bytes.toBytes(“famA”);     
		byte[] col1 = Bytes.toBytes(“col1”);       
		HTable hTable = new HTable(“test”);       
		Scan scan = new Scan(Bytes.toBytes(“a”), Bytes.toBytes(“z”));     
		scan.addColumn(famA, col1);       
		SingleColumnValueFilter singleColumnValueFilterA = new 		SingleColumnValueFilter(famA, col1, CompareOp.EQUAL, Bytes.toBytes(“hello world!”));     
		singleColumnValueFilterA.setFilterIfMissing(true);       		SingleColumnValueFilter singleColumnValueFilterB = new 		SingleColumnValueFilter(famA, col1, CompareOp.EQUAL, Bytes.toBytes(“hello hbase!”));     
		singleColumnValueFilterB.setFilterIfMissing(true);       
FilterList filter = new FilterList(Operator.MUST_PASS_ONE, Arrays         .asList((Filter) singleColumnValueFilterA,             		singleColumnValueFilterB));       
		scan.setFilter(filter);       
		ResultScanner scanner = hTable.getScanner(scan);       
		for (Result result : scanner) 
		{       
			System.out.println(Bytes.toString(result.getValue(famA, col1)));     
		}   
	} 
}

查询DSL代码

public class ScannerWithDsl 
{   
	public static void main(String[] args) throws IOException 
	{     
		HBase<QueryOps, String> hBase = new HBase<QueryOps, String>(String.class);  
		hBase.scan(“test”,“a”,“z”).select().family(“famA”).col(“col1”).where().family(“famA”).col(“col1”).eq(“hello world!”,“hello hbase!”).foreach(new ForEach<Row>() 
		{         
			@Override         
			public void process(Row row) 
			{           
				System.out.println(row.value(“famA”, “col1”, String.class));         
			}       
		});   
	} 
}

DSL ByteArray代码

public class UsingBytes 
{   
	public static void main(String[] args) 
	{     
		HBase<QueryOps, String> hBase = new HBase<QueryOps, String>(String.class);  
		byte[] rowId = Bytes.toBytes(“1234”);     
		byte[] helloworld = Bytes.toBytes(“hello world!”);     
		hBase.save(“test”).row(rowId).family(“famA”).col(“col1”, helloworld); 
		Iterable<Row<byte[]>> it = hBase.scan(“test”).where().family(“famA”).col(“col1”).eq(helloworld);       
		for (Row<byte[]> row : it) 
		{       
			byte[] value = row.value(“famA”, “col1”, byte[].class);       
			System.out.println(Bytes.toString(value));     
		}   
	} 
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值