java hbase 条件查询_java实现Hbase中的查询(一)Filter方式

1、需要的jar包:

commons-codec-1.4.jar

commons-logging-1.0.4.jar

hadoop-0.20.2-core.jar

hbase-0.20.6.jar

log4j-1.2.15.jar

zookeeper-3.2.2.jar

2、已有表结构:

1、表名:scores

2、列族:

course:art

course:math

grade:

3、scan 'scores'的内容:

ROW                          COLUMN+CELL

Jerry                       column=course:art, timestamp=1301294630194, value=80

Jerry                       column=course:math, timestamp=1301294630132, value=100

Jerry                       column=grade:, timestamp=1301294630073, value=2

Jim                         column=course:art, timestamp=1301294630363, value=97

Jim                         column=course:math, timestamp=1301294630305, value=100

Jim                         column=grade:, timestamp=1301294630247, value=3

Tom                         column=course:art, timestamp=1301294630015, value=97

Tom                         column=course:math, timestamp=1301294629987, value=87

Tom                         column=grade:, timestamp=1301294629931, value=1

4、代码:

[java:nogutter] view plaincopypackage org.myhbase;

import java.io.IOException;

import java.util.ArrayList;

import java.util.List;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.hbase.HBaseConfiguration;

import org.apache.hadoop.hbase.KeyValue;

import org.apache.hadoop.hbase.client.Get;

import org.apache.hadoop.hbase.client.HTable;

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.FilterList;

import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;

import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;

import org.apache.hadoop.hbase.io.Cell;

import org.apache.hadoop.hbase.util.Bytes;

public class HBaseBasic03 {

private static HBaseConfiguration hbaseConfig=null;

static{

Configuration config=new Configuration();

config.set("hbase.zookeeper.quorum","192.168.10.149,192.168.10.44,192.168.10.49");

config.set("hbase.zookeeper.property.clientPort", "2181");

hbaseConfig=new HBaseConfiguration(config);

}

/**

* get方式,通过rowKey查询

* @param tablename

* @param rowKey

* @throws IOException

*/

public static void selectByRowKey(String tablename,String rowKey) throws IOException{

HTable table=new HTable(hbaseConfig,tablename);

Get g = new Get(Bytes.toBytes(rowKey));

Result r=table.get(g);

for(KeyValue kv:r.raw()){

System.out.println("column: "+new String(kv.getColumn()));

System.out.println("value: "+new String(kv.getValue()));

}

}

/**

* get方式,通过rowKey、column查询

* @param tablename

* @param rowKey

* @param column

* @throws IOException

*/

public static void selectByRowKeyColumn(String tablename,String rowKey,String column) throws IOException{

HTable table=new HTable(hbaseConfig,tablename);

Get g = new Get(Bytes.toBytes(rowKey));

g.addColumn(Bytes.toBytes(column));

Result r=table.get(g);

for(KeyValue kv:r.raw()){

System.out.println("column: "+new String(kv.getColumn()));

System.out.println("value: "+new String(kv.getValue()));

}

}

public static void selectByFilter(String tablename,List arr) throws IOException{

HTable table=new HTable(hbaseConfig,tablename);

FilterList filterList = new FilterList();

Scan s1 = new Scan();

for(String v:arr){ // 各个条件之间是“与”的关系

String [] s=v.split(",");

filterList.addFilter(new SingleColumnValueFilter(Bytes.toBytes(s[0]),

Bytes.toBytes(s[1]),

CompareOp.EQUAL,Bytes.toBytes(s[2])

)

);

// 添加下面这一行后,则只返回指定的cell,同一行中的其他cell不返回

//          s1.addColumn(Bytes.toBytes(s[0]), Bytes.toBytes(s[1]));

}

s1.setFilter(filterList);

ResultScanner ResultScannerFilterList = table.getScanner(s1);

for(Result rr=ResultScannerFilterList.next();rr!=null;rr=ResultScannerFilterList.next()){

for(KeyValue kv:rr.list()){

System.out.println("row : "+new String(kv.getRow()));

System.out.println("column : "+new String(kv.getColumn()));

System.out.println("value : "+new String(kv.getValue()));

}

}

}

public static void main(String [] args) throws IOException{

// 按rowkey查询,查询Tom行的所有cell

HBaseBasic03.selectByRowKey("scores","Tom");

// 按rokey 和 column 来查询,查询Tom行course列族的所有列值

HBaseBasic03.selectByRowKeyColumn("scores","Tom","course");

// Filter多条件查询,条件:查询 course列族中art列值为97 ,且 course列族中math列值为100的行

List arr=new ArrayList();

arr.add("course,art,97");

arr.add("course,math,100");

HBaseBasic03.selectByFilter("scores",arr);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java,可以使用HBase API进行表根据时间戳范围进行查询。下面是一个简单的示例代码: ```java import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.CellUtil; 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.ResultScanner; import org.apache.hadoop.hbase.client.Scan; import org.apache.hadoop.hbase.filter.FilterList; import org.apache.hadoop.hbase.filter.SingleColumnValueFilter; import org.apache.hadoop.hbase.filter.TimestampsFilter; import org.apache.hadoop.hbase.util.Bytes; public class HBaseQueryExample { public static void main(String[] args) throws IOException { Configuration conf = HBaseConfiguration.create(); Connection conn = ConnectionFactory.createConnection(conf); TableName tableName = TableName.valueOf("table_name"); Scan scan = new Scan(); long startTime = System.currentTimeMillis() - 24 * 60 * 60 * 1000; // start time is 24 hours ago long endTime = System.currentTimeMillis(); // end time is current time List<Long> timestamps = new ArrayList<>(); timestamps.add(startTime); timestamps.add(endTime); TimestampsFilter filter = new TimestampsFilter(timestamps); SingleColumnValueFilter columnFilter = new SingleColumnValueFilter(Bytes.toBytes("column_family"), Bytes.toBytes("column_qualifier"), CompareOperator.EQUAL, Bytes.toBytes("column_value")); FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL); filterList.addFilter(filter); filterList.addFilter(columnFilter); scan.setFilter(filterList); ResultScanner scanner = conn.getTable(tableName).getScanner(scan); for (Result result : scanner) { for (Cell cell : result.listCells()) { byte[] valueBytes = CellUtil.cloneValue(cell); String value = Bytes.toString(valueBytes); System.out.println("Value: " + value); } } scanner.close(); conn.close(); } } ``` 在上面的代码,我们首先创建了一个HBase连接和一个表名。然后,我们使用Scan对象创建一个查询,并设置起始时间和结束时间。接下来,我们使用TimestampsFilter和SingleColumnValueFilter创建一个过滤器列表FilterList,以仅返回指定时间范围内的包含指定列的行。最后,我们使用ResultScanner遍历结果并打印每个包含指定列的单元格的值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值