我有一个表,可以说它的名字是“SampleTab”,有一个名为“ColumnFam1”的列家族,名为“1”的列,值为“col1value”
我编写了这段代码并试图通过传递我输出的列值
来获取输出/ 04/04 06:50:08 INFO zookeeper.ZooKeeper:客户端环境:java.io.tmpdir = / tmp 15/04/04 06:50:08 INFO zookeeper.ZooKeeper:客户端环境:java.compiler = 15/04/04 06:50:08 INFO zookeeper.ZooKeeper:客户端环境:os.name = Linux 15/04/04 06:50:08 INFO zookeeper.ZooKeeper:客户端环境:os.arch = i386 15/04/04 06:50:08 INFO zookeeper.ZooKeeper:客户环境:os.version = 2.6.18-238.9.1.el5 15/04/04 06:50:08 INFO zookeeper.ZooKeeper:客户端环境:user.name = training 15/04/04 06:50:08 INFO zookeeper.ZooKeeper:客户环境:user.home = / home / training 15/04/04 06:50:08 INFO zookeeper.ZooKeeper:客户环境:user.dir = / home / training 15/04/04 06:50:08 INFO zookeeper.ZooKeeper:启动客户端连接,connectString = localhost:2181 sessionTimeout = 180000 watcher = hconnection 15/04/04 06:50:09 INFO zookeeper.ClientCnxn:打开到服务器localhost / 127.0.0.1的套接字连接:2181 15/04/04 06:50:09 INFO zookeeper.ClientCnxn:建立到localhost / 127.0.0.1的套接字连接:2181,启动会话 15/04/04 06:50:09 INFO zookeeper.ClientCnxn:在服务器localhost / 127.0.0.1上完成会话建立:2181,sessionid = 0x14c84ae2fb30005,协商超时= 40000 15/04/04 06:50:11 INFO zookeeper.ZooKeeper:启动客户端连接,connectString = localhost:2181 sessionTimeout = 180000 watcher = hconnection 15/04/04 06:50:11 INFO zookeeper.ClientCnxn:打开到服务器localhost / 127.0.0.1的套接字连接:2181 15/04/04 06:50:11 INFO zookeeper.ClientCnxn:建立到localhost / 127.0.0.1的套接字连接:2181,启动会话 15/04/04 06:50:12 INFO zookeeper.ClientCnxn:在服务器localhost / 127.0.0.1上完成会话建立:2181,sessionid = 0x14c84ae2fb30006,协商超时= 40000 获取:OUTPUT键值=无
我要获得的部分代码如下:
HTable table = new HTable(config, tablename);
byte [] row1 = Bytes.toBytes("KeyIn");
Put p1 = new Put(row1);
byte [] databytes = Bytes.toBytes("ColumnFam1");
p1.add(databytes, Bytes.toBytes("1"), Bytes.toBytes("col1value"));
table.put(p1);
Get g = new Get(Bytes.toBytes("col1value"));
g.addColumn(Bytes.toBytes("ColumnFam1"), Bytes.toBytes("1"));
Result result = table.get(g);
System.out.println("Get: OUTPUT " + result);
我有办法通过传递Get中的值来获取columnfamily名称和列名 我也尝试过不使用g.addColumn行..它仍然没有给出
答案 0 :(得分:1)
Get g = new Get(Bytes.toBytes("col1value"));
不起作用,因为该行不存在。您必须提供刚刚插入到get:
HTable table = new HTable(config, tablename);
byte [] row1 = Bytes.toBytes("KeyIn");
Put p1 = new Put(row1);
byte [] databytes = Bytes.toBytes("ColumnFam1");
p1.add(databytes, Bytes.toBytes("1"), Bytes.toBytes("col1value"));
table.put(p1);
Get g = new Get(row1);
//g.addColumn(Bytes.toBytes("ColumnFam1"), Bytes.toBytes("1")); // Not needed if you want to retrieve all families/columns
Result result = table.get(g);
System.out.println("Get: OUTPUT " + result);
如果您知道列的名称(如图所示),您可以直接访问该值:
if (result.containsColumn(databytes, Bytes.toBytes("1"))) {
System.out.println("Value: " + Bytes.toStringBinary(result.getColumnLatest(databytes, Bytes.toBytes("1"))));
}
或者,如果您想要遍历所有检索到的列:
Result result = table.get(get);
if (result!=null) {
System.out.println(result);
Set<Entry<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>>> entries = result.getMap().entrySet();
for(Entry<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> familyEntry: entries) {
byte[] family = familyEntry.getKey();
for(Entry<byte[], NavigableMap<Long, byte[]>> columnEntry: familyEntry.getValue().entrySet()) {
byte[] column = columnEntry.getKey();
System.out.println("Found column "+Bytes.toStringBinary(family)+":"+Bytes.toStringBinary(column));
if (columnEntry.getValue().size()>0) {
Entry<Long, byte[]> valueEntry = columnEntry.getValue().firstEntry();
System.out.println(" Found value "+Bytes.toStringBinary(valueEntry.getValue())+" with timestamp "+valueEntry.getKey());
}
}
}
}
columnEntry.getValue().entrySet()
答案 1 :(得分:0)
我是Rubén的回答。如果您不知道RowKey,可以使用ValueFilter
并通过以下方式创建Scan
-
Scan scan = new Scan();
byte[] value = Bytes.toBytes("col1value")
Filter filter = new ValueFilter(CompareOp.EQUAL,new BinaryComparator(value));
scan.setFilter(filter);
ResultScanner scanner = table.getScanner(scan);
然后使用scanner
扫描结果。 但是,这可能过于昂贵,因为您正在执行完整扫描。您还可以使用Get
过滤器。