设置多个filter
代码如下:
package com.pzoom.xiaochen.javaHbase;
import java.io.IOException;
import java.util.Arrays;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.KeyValue;
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.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.FilterList.Operator;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.junit.Test;
public class TestFilter {
private static Configuration conf = null;
private static HTable tableKeyword = null ;
/**
* 初始化配置
*/
static {
Configuration HBASE_CONFIG = new Configuration();
HBASE_CONFIG.set("hbase.zookeeper.quorum", "192.168.1.171,192.168.1.172,192.168.1.173");
HBASE_CONFIG.set("hbase.zookeeper.property.clientPort", "2181");
conf = HBaseConfiguration.create(HBASE_CONFIG);
try {
tableKeyword = new HTable(conf, "tab_keyword");
tableKeyword.setAutoFlush(false);
} catch (IOException e) {
e.printStackTrace();
};
}
@Test
public void testFilter1() throws IOException {
// HTable hTable = new HTable("tab_keyword");
Scan scan = new Scan();
SingleColumnValueFilter aFilter = new SingleColumnValueFilter(
"content".getBytes(), "id".getBytes(), CompareOp.EQUAL, "f8d0c4489f4ddf5e".getBytes());
aFilter.setFilterIfMissing(true);
SingleColumnValueFilter bFilter = new SingleColumnValueFilter(
"content".getBytes(), "netword".getBytes(), CompareOp.EQUAL, "BAIDU".getBytes());
bFilter.setFilterIfMissing(true);
FilterList filter = new FilterList(Operator.MUST_PASS_ONE, Arrays.asList((Filter) aFilter, bFilter)) ;
scan.setFilter(filter);
ResultScanner rs = tableKeyword.getScanner(scan);
for(Result r : rs) {
for(KeyValue kv : r.raw()) {
System.out.println("qu = " + new String(kv.getQualifier()) + ",va = " + new String(kv.getValue()));
}
}
}
}
上面的代码容易出错的地方在于,需要导入HBase的类所在的包。导入时需要选择包,由于类可能出现在HBase的各个子包中,所以要选择好,下面列出常用的包。尽量用HBase的包
下面列出HBase常用的操作
(1)时间戳到时间的转换.单一的时间戳无法给出直观的解释。
public String GetTimeByStamp(String timestamp)
{
long datatime= Long.parseLong(timestamp);
Date date=new Date(datatime);
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:MM:ss");
String timeresult=format.format(date);
System.out.println("Time : "+timeresult);
return timeresult;
}
(2)时间到时间戳的转换。注意时间是字符串格式。字符串与时间的相互转换,此不赘述。
public String GetStampByTime(String time)
{
String Stamp="";
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date;
try
{
date=sdf.parse(time);
Stamp=date.getTime()+"000";
System.out.println(Stamp);
}catch(Exception e){e.printStackTrace();}
return Stamp;
}