20,809
社区成员
发帖
与我相关
我的任务
分享
Get.setMaxVersions()//可以返回多个版本的数据。
Get.setTimeRange()//可以返回其他时间范围的版本。
hbase(main):005:0> create 'test0001',{NAME=>'_0',VERSIONS=>3}
//建表
hbase(main):010:0> put 'test0001','111','_0:t1','111',111
hbase(main):013:0> put 'test0001','111','_0:t1','222',222
//对ROWKEY=111 插入两条数据 TS分别为 111 222
hbase(main):017:0> scan 'test0001',{VERSIONS=>1}
ROW COLUMN+CELL
111 column=_0:t1, timestamp=222, value=222
//这时只有timestamp=111的数据
hbase(main):016:0> scan 'test0001',{VERSIONS=>2}
ROW COLUMN+CELL
111 column=_0:t1, timestamp=222, value=222
111 column=_0:t1, timestamp=111, value=111
//这时有timestamp=111和222两个version数据
再看java API 调用测试
import org.apache.hadoop.conf.Configuration;
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;
public class hbasetest {
public static void main(String[] agrs) throws Exception {
Configuration myConf;
myConf = new Configuration();
myConf.set("hbase.zookeeper.quorum", "10.128.24.200");
myConf.set("hbase.zookeeper.property.clientPort", "2181");
HTable tb = new HTable(myConf, "test0001");
Get get = new Get("111".getBytes());
get.setMaxVersions(2);
Result rs = tb.get(get);
for (KeyValue kv : rs.list())
{
System.out.println(new String(kv.getQualifier()) + ":" +
new String(kv.getValue()) + "\t");
System.out.println();
}
}
}
//get.setMaxVersions(1); 结果看下方
t1:222
//get.setMaxVersions(2); 结果看下方
t1:222
t1:111
怎么过滤最后一个VERSION数据,不用我再写了吧[/quote]
你把get.setMaxVersions(2) 结合ValueFilter 使用的时候 就达不到这个效果了,
代码如下:
ValueFilter vf = new ValueFilter(CompareFilter.CompareOp.EQUAL,
new SubstringComparator("D_"));
String key = "6745701";
Get get = new Get(key.getBytes());
get.setMaxVersions(1);
get.setFilter(vf);
Result re = table.get(get);
List<KeyValue> list = re.list();
for (int i = 0; i < list.size(); i++) {
System.out.println("列名:" + Bytes.toString(list.get(i).getQualifier()));
System.out.println("列值:" + Bytes.toString(list.get(i).getValue()));
System.out.println("时间戳:" + list.get(i).getTimestamp());
System.out.println();
}
结果如下:
列名:h_8679935873_111031001706
列值:D_43610
时间戳:1388994473015
列名:h_8679935873_111031001708
列值:D_6001
时间戳:1388996755580
列名:h_8888dsahjdsa
列值:D_200
时间戳:1389166949242
控制台信息如下:
最新版本的数据中值以D_开头的只有一个列,其他的列如:h_8888dsahjdsa 这个过滤到的是D_200但是现在的数据是 H_200 ,如何一次查询取得两个的时间戳呢?[/quote]
不管怎么设置Version数量
如果用filter 限制 “D_”开头,那永远都拿不到“H_”的数据
除非有两个“D_”开头,不同版本的数据,才肯被搜索到
要解决这个问题,LZ你只能放弃filter ,全部取下来让Client筛选
要不就重新设计TS,用有规则的TS插入数据
然后用TimestampsFilter判断筛选
hbase(main):005:0> create 'test0001',{NAME=>'_0',VERSIONS=>3}
//建表
hbase(main):010:0> put 'test0001','111','_0:t1','111',111
hbase(main):013:0> put 'test0001','111','_0:t1','222',222
//对ROWKEY=111 插入两条数据 TS分别为 111 222
hbase(main):017:0> scan 'test0001',{VERSIONS=>1}
ROW COLUMN+CELL
111 column=_0:t1, timestamp=222, value=222
//这时只有timestamp=111的数据
hbase(main):016:0> scan 'test0001',{VERSIONS=>2}
ROW COLUMN+CELL
111 column=_0:t1, timestamp=222, value=222
111 column=_0:t1, timestamp=111, value=111
//这时有timestamp=111和222两个version数据
import org.apache.hadoop.conf.Configuration;
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;
public class hbasetest {
public static void main(String[] agrs) throws Exception {
Configuration myConf;
myConf = new Configuration();
myConf.set("hbase.zookeeper.quorum", "10.128.24.200");
myConf.set("hbase.zookeeper.property.clientPort", "2181");
HTable tb = new HTable(myConf, "test0001");
Get get = new Get("111".getBytes());
get.setMaxVersions(2);
Result rs = tb.get(get);
for (KeyValue kv : rs.list())
{
System.out.println(new String(kv.getQualifier()) + ":" +
new String(kv.getValue()) + "\t");
System.out.println();
}
}
}
//get.setMaxVersions(1); 结果看下方
t1:222
//get.setMaxVersions(2); 结果看下方
t1:222
t1:111
hbase(main):005:0> create 'test0001',{NAME=>'_0',VERSIONS=>3}
//建表
hbase(main):010:0> put 'test0001','111','_0:t1','111',111
hbase(main):013:0> put 'test0001','111','_0:t1','222',222
//对ROWKEY=111 插入两条数据 TS分别为 111 222
hbase(main):017:0> scan 'test0001',{VERSIONS=>1}
ROW COLUMN+CELL
111 column=_0:t1, timestamp=222, value=222
//这时只有timestamp=111的数据
hbase(main):016:0> scan 'test0001',{VERSIONS=>2}
ROW COLUMN+CELL
111 column=_0:t1, timestamp=222, value=222
111 column=_0:t1, timestamp=111, value=111
//这时有timestamp=111和222两个version数据
import org.apache.hadoop.conf.Configuration;
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;
public class hbasetest {
public static void main(String[] agrs) throws Exception {
Configuration myConf;
myConf = new Configuration();
myConf.set("hbase.zookeeper.quorum", "10.128.24.200");
myConf.set("hbase.zookeeper.property.clientPort", "2181");
HTable tb = new HTable(myConf, "test0001");
Get get = new Get("111".getBytes());
get.setMaxVersions(2);
Result rs = tb.get(get);
for (KeyValue kv : rs.list())
{
System.out.println(new String(kv.getQualifier()) + ":" +
new String(kv.getValue()) + "\t");
System.out.println();
}
}
}
//get.setMaxVersions(1); 结果看下方
t1:222
//get.setMaxVersions(2); 结果看下方
t1:222
t1:111