无法根据查询参数从Hbase获取数据

时间:2012-08-28 18:55:53

标签: hadoop hbase

如何从HBase获取数据?我有一个包含empId,name,startDate,endDate和其他列的表。现在我想基于empId,startDate和endDate从HBase表中获取数据。在正常的SQL中我可以使用:

select * from tableName where empId=val and date>=startDate and date<=endDate

我如何在HBase中执行此操作,因为它将数据存储为键值对?关键是empId。

1 个答案:

答案 0 :(得分:2)

HBase shell中获取过滤的行很棘手。由于shell为JRuby-based,因此您也可以使用Ruby命令:

import org.apache.hadoop.hbase.filter.CompareFilter
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter
import org.apache.hadoop.hbase.filter.BinaryComparator
import org.apache.hadoop.hbase.filter.FilterList
import java.text.SimpleDateFormat
import java.lang.Long

def dateToBytes(val)
  Long.toString(
    SimpleDateFormat.new("yyyy/MM/dd").parse(val).getTime()).to_java_bytes
end

# table properties
colfam='c'.to_java_bytes;
col_name='name';
col_start='startDate';
col_end='endDate';

# query params
q_name='name2';
q_start='2012/08/14';
q_end='2012/08/24';

# filters
f_name=SingleColumnValueFilter.new(
         colfam, col_name.to_java_bytes, 
         CompareFilter::CompareOp::EQUAL, 
         BinaryComparator.new(q_name.to_java_bytes));

f_start=SingleColumnValueFilter.new(
          colfam, col_start.to_java_bytes, 
          CompareFilter::CompareOp::GREATER_OR_EQUAL,
          BinaryComparator.new(dateToBytes(q_start)));

f_end=SingleColumnValueFilter.new(
          colfam, col_end.to_java_bytes, 
          CompareFilter::CompareOp::LESS_OR_EQUAL,
          BinaryComparator.new(dateToBytes(q_end)));

filterlist= FilterList.new([f_name, f_start, f_end]);

# get the result
scan 'mytable', {"FILTER"=>filterlist}

同样在Java构建FilterList

// Query params
String nameParam = "name2";
String startDateParam = "2012/08/14";
String endDateParam = "2012/08/24";

Filter nameFilter = 
  new SingleColumnValueFilter(colFam, nameQual, CompareOp.EQUAL,
        Bytes.toBytes(nameParam));

//getBytesFromDate(): parses startDateParam and create a byte array out of it
Filter startDateFilter = 
  new SingleColumnValueFilter(colFam, startDateQual,
        CompareOp.GREATER_OR_EQUAL, getBytesFromDate(startDateParam));

Filter endDateFilter = 
  new SingleColumnValueFilter(colFam, endDateQual,
        CompareOp.LESS_OR_EQUAL, getBytesFromDate(endDateParam));

FilterList filters = new FilterList();
filters.addFilter(nameFilter);
filters.addFilter(startDateFilter);
filters.addFilter(endDateFilter);

HTable htable = new HTable(conf, tableName);

Scan scan = new Scan();
scan.setFilter(filters);
ResultScanner rs = htable.getScanner(scan);
//process your result...