使用Java获取Hbase中所有行的所有值
我有以下代码。我正在尝试检索给定列族的表中的所有行。我能够得到所有的行,但是输出不是我期望的。我得到一个显示键和时间戳但不显示值的输出。为什么不显示行的值?请帮忙。输出如下所示:使用Java获取Hbase中所有行的所有值,java,eclipse,get,hbase,Java,Eclipse,Get,Hbase,我有以下代码。我正在尝试检索给定列族的表中的所有行。我能够得到所有的行,但是输出不是我期望的。我得到一个显示键和时间戳但不显示值的输出。为什么不显示行的值?请帮忙。输出如下所示: keyvalues={Justin/marks:total/1375104216267/Put/vlen=7/ts=0, Justin/marks:markPercentage/ 1375104186783/Put/vlen=4/ts=0} //从hbase获取所有行的代码 public class GetHb
keyvalues={Justin/marks:total/1375104216267/Put/vlen=7/ts=0, Justin/marks:markPercentage/ 1375104186783/Put/vlen=4/ts=0}
//从hbase获取所有行的代码
public class GetHbaseData {
public static void getdata() throws IOException{
@SuppressWarnings("resource")
HTable table = new HTable(HBaseConfiguration.create(), "Student");
Scan scan = new Scan();
scan.setCaching(20);
scan.addFamily(Bytes.toBytes("marks"));
ResultScanner scanner = table.getScanner(scan);
for (Result result = scanner.next(); (result != null); result = scanner.next()) {
Get get = new Get(result.getRow());
Result entireRow = table.get(get);
System.out.println(entireRow);
}
}
下面是扫描表中“marks”列族的代码 使用它可以获得行、列、时间戳和值
Scan scan = new Scan();
scan.setCaching(hBaseScanCacheSize);
scan.setBatch(hbaseScanBatchSize);
scan.addFamily(Bytes.toBytes("marks"));
ResultScanner resultScanner = table.getScanner(scan);
Iterator<Result> iterator = resultScanner.iterator();
while (iterator.hasNext())
{
Result next = iterator.next();
for(Entry<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> columnFamilyMap : next.getMap().entrySet())
{
for (Entry<byte[], NavigableMap<Long, byte[]>> entryVersion : columnFamilyMap.getValue().entrySet())
{
for (Entry<Long, byte[]> entry : entryVersion.getValue().entrySet())
{
String row = Bytes.toString(next.getRow());
String column = Bytes.toString(entryVersion.getKey());
byte[] value = entry.getValue();
long timesstamp = entry.getKey();
}
}
}
}
Scan扫描=新扫描();
scan.setCaching(hbascancachesize);
scan.setBatch(hbascanbatchsize);
scan.addFamily(Bytes.toBytes(“标记”));
ResultScanner ResultScanner=table.getScanner(扫描);
迭代器迭代器=resultScanner.Iterator();
while(iterator.hasNext())
{
Result next=iterator.next();
对于(条目columnFamilyMap:next.getMap().entrySet())
{
对于(条目entryVersion:columnFamilyMap.getValue().entrySet())
{
对于(条目:entryVersion.getValue().entrySet())
{
字符串行=Bytes.toString(next.getRow());
String column=Bytes.toString(entryVersion.getKey());
字节[]值=entry.getValue();
long timestamp=entry.getKey();
}
}
}
}
用于获取包含所有列的所有行,您不需要在For循环中再次进行Get调用。试试这样的
for (Result result = scanner.next(); (result != null); result = scanner.next()) {
for(KeyValue keyValue : result.list()) {
System.out.println("Qualifier : " + keyValue.getKeyString() + " : Value : " + Bytes.toString(keyValue.getValue()));
}
}
我想提供没有弃用方法的解决方案
//Get
Get theGet = new Get(Bytes.toBytes("rowkey1"));
Result result = table.get(theGet);
//get value first column
String inValue1 = Bytes.toString(result.value());
//get value by ColumnFamily and ColumnName
byte[] inValueByte = result.getValue(Bytes.toBytes(columnFamily), Bytes.toBytes(columnQualifier1));
String inValue2 = Bytes.toString(inValueByte);
//loop for result
for (Cell cell : result.listCells()) {
String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell));
String value = Bytes.toString(CellUtil.cloneValue(cell));
System.out.printf("Qualifier : %s : Value : %s", qualifier, value);
}
//create Map by result and print it
Map<String, String> getResult = result.listCells().stream().collect(Collectors.toMap(e -> Bytes.toString(CellUtil.cloneQualifier(e)), e -> Bytes.toString(CellUtil.cloneValue(e))));
getResult.entrySet().stream().forEach(e-> System.out.printf("Qualifier : %s : Value : %s", e.getKey(), e.getValue()));
//获取
Get-theGet=new-Get(Bytes.toBytes(“rowkey1”);
结果=table.get(theGet);
//在第一列中获取值
字符串inValue1=Bytes.toString(result.value());
//按ColumnFamily和ColumnName获取值
byte[]inValueByte=result.getValue(Bytes.toBytes(columnFamily),Bytes.toBytes(columnQualifier1));
字符串inValue2=Bytes.toString(inValueByte);
//循环搜索结果
对于(单元格:result.listCells()){
字符串限定符=Bytes.toString(CellUtil.cloneQualifier(cell));
字符串值=Bytes.toString(CellUtil.cloneValue(cell));
System.out.printf(“限定符:%s:值:%s”,限定符,值);
}
//根据结果创建地图并打印
Map getResult=result.listCells().stream().collect(Collectors.toMap(e->Bytes.toString(CellUtil.cloneQualifier(e)),e->Bytes.toString(CellUtil.cloneValue(e)));
getResult.entrySet().stream().forEach(e->System.out.printf(“限定符:%s:Value:%s”,e.getKey(),e.getValue());
对于新的HBase API,代码如下所示:
for (Result result = scanner.next(); (result != null); result = scanner.next()) {
for(Cell cell : result.listCells()) {
String qualifier = Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength());
String value = Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
System.out.println("Qualifier : " + qualifier
+ " : Value : " + value);
}
}
这看起来好多了。现在不推荐使用result.list()。你知道在新的API中使用什么吗?你可以在那里找到更多的例子