HBase table Get Data 获取数据
2016-06-27 14:07
218 查看
HBase 版本: 1.2.1
package com.feng.hbase; import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.Connection; import org.apache.hadoop.hbase.client.ConnectionFactory; import org.apache.hadoop.hbase.client.Get; import org.apache.hadoop.hbase.client.Result; import org.apache.hadoop.hbase.client.Table; import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp; import org.apache.hadoop.hbase.filter.Filter; import org.apache.hadoop.hbase.filter.FilterList; import org.apache.hadoop.hbase.filter.SingleColumnValueFilter; import org.apache.hadoop.hbase.util.Bytes; public class GetData { public static void main(String[] args) { String talbeName = "test1"; String columnFamily = "cf"; String columnFamily2 = "cf2"; String attribute = "a"; String rowkey = "row1"; Connection connection = null; Table table = null; try { Configuration config = HBaseConfiguration.create(); config.set("hbase.zookeeper.quorum", "localhost"); connection = ConnectionFactory.createConnection(config); table = connection.getTable(TableName.valueOf(talbeName)); if (table != null) { System.out.println("========第一种========"); // 第一种利用 Get 获取值 Get get = new Get(Bytes.toBytes(rowkey)); get.setMaxVersions(1);// will return last 4 versions of row Result result = table.get(get); if (!result.isEmpty()) { // 根据 columnFamily 和 attribute 取value值 byte[] value = result.getValue(Bytes.toBytes(columnFamily), Bytes.toBytes(attribute)); String valueStr = Bytes.toString(value); System.out.println(rowkey + " GET value : " + valueStr); // 用cell取的值 不太好用 Cell cell = result.getColumnLatestCell( Bytes.toBytes(columnFamily), Bytes.toBytes(attribute)); System.out.println("cell.getFamilyLength(): " + cell.getFamilyLength()); System.out.println("cell.getValueArray(): " + Bytes.toString(cell.getValueArray())); System.out.println("cell.getQualifierArray(): " + Bytes.toString(cell.getQualifierArray())); } else { System.out.println(rowkey + " is null rowkey!"); } System.out.println("========第二种========"); // 第二种利用 Get 获取值加过滤器获取值 好像是filter 没什么用 get = new Get(Bytes.toBytes(rowkey)); get.setMaxVersions(2); FilterList listFilter = new FilterList( FilterList.Operator.MUST_PASS_ONE); Filter filter = new SingleColumnValueFilter( Bytes.toBytes(columnFamily), Bytes.toBytes(attribute), CompareOp.EQUAL, Bytes.toBytes("")); listFilter.addFilter(filter); get.setFilter(listFilter); result = table.get(get); if (!result.isEmpty()) { // 根据 columnFamily 和 attribute 取value值 byte[] value = result.getValue(Bytes.toBytes(columnFamily), Bytes.toBytes(attribute)); String valueStr = Bytes.toString(value); System.out.println(rowkey + " GET value : " + valueStr); } else { System.out.println(rowkey + " is null rowkey!"); } } else { System.out.println("There is not " + talbeName); } } catch (Exception e) { e.printStackTrace(); } finally { if (table != null) { try { table.close(); } catch (IOException e) { e.printStackTrace(); } } if (connection != null) { try { connection.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
相关文章推荐
- mysql乐观锁总结和实践
- Git搭建
- spring mvc 条件查询导出excel
- Shell字符串
- 打破摩尔定律:谷歌与腾讯的下一代计算平台选择是?
- WebForm页面生命周期及asp.net运行机制
- dede后台登陆后一片空白的解决办法汇总
- Java核心API列表
- 单片机驱动DM9000网卡芯片(详细调试过程)【下】
- sqlserver中修改自增列
- java list去重
- ACM_置换群 burnside引理 Polya定理
- Python爬上不得姐 并将段子写入数据库
- aes 加密,解密(2)
- list for循环中删除元素
- oracle表空间的扩展
- iOS9的几个新关键字(nonnull、nullable、null_resettable、__null_unspecified)
- 单片机驱动DM9000网卡芯片(详细调试过程)【上】
- php发送短信(调用网易云信实现)
- "建造者模式"-之Java,Android面试必问设计模式(4/9)