Is it possible to get hbase data records by list of row ids via hbase java API?
For example, I have a known list of hbase row ids:
mykey1:myhash1,
mykey1:myhash2,
mykey1:myhash3,
mykey2:myhash5,
...
and I want to get with single call to hbase all relevant column cell informations. I'm pretty new to hbase and i don't know is this even supported by the API.
API pseudo code:
GetById(String tableName, List<byte[]> rowIds);
Something like that?
I can retrieve information from single row with Get(byte[] rowName)
, but when I have list of rowIds, I need to execute the get action several times, which cause establishing connection and closing it when completed each time.
Thanks
Pass a list of Get
operations to a batch call:
...
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
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.util.Bytes;
...
HTable htable = null;
try {
htable = new HTable(conf, "mytable");
List<Get> queryRowList = new ArrayList<Get>();
queryRowList.add(new Get(Bytes.toBytes("mykey1:myhash1")));
queryRowList.add(new Get(Bytes.toBytes("mykey1:myhash2")));
queryRowList.add(new Get(Bytes.toBytes("mykey1:myhash3")));
queryRowList.add(new Get(Bytes.toBytes("mykey2:myhash5")));
Result[] results = htable.get(queryRowList);
for (Result r : results) {
//do something
}
}
finally {
if (htable != null) {
htable.close();
}
}
...
You can use MultiAction as a container for multiple get (or put, delete and combinations of them) that you can execute in batch.
That said, note that you can perform multiple get operations without closing/reopening the connection every time.