通过HBase Java APi 读取数据到 Result中,再对Result进行操作
import java.util.Map;
import java.util.Map.Entry;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
public class ResultTest {
private static Configuration cfg = HBaseConfiguration.create();
private static Connection connection = null;
private static Table table = null;
private static Get get = null;
private static Result result = null;
/**
* 测试Result的getFamilyMap方法
* <p>获取列族下的所有列修饰符与值的键值对
*/
public static void getFamilyMapTest(String tableName, String rowKey, String family) throws Exception {
initTableResult(tableName, rowKey);
get.addFamily(Bytes.toBytes(family));
connection.close();
// 获取列族下的所有列修饰符与值的键值对,并保存于Map集合中
Map<byte[], byte[]> kvPair = result.getFamilyMap(Bytes.toBytes(family));
// 通过for循环遍历kvPair
for (Entry<byte[], byte[]> entry : kvPair.entrySet()) {
String key = new String(entry.getKey(), "utf8");
String value = new String(entry.getValue(), "utf8");
System.out.println(family + ":" + key + " = " + value);
}
// // 通过迭代器遍历kvPair
// Iterator<Entry<byte[], byte[]>> itr = kvPair.entrySet().iterator();
// while (itr.hasNext()) {
// Entry<byte[], byte[]> entry = itr.next();
// String key = new String(entry.getKey());
// String value = new String(entry.getValue());
// System.out.println(family + ":" + key + " = " + value);
// }
}
/**
* 测试Result的getValue方法
* <p>获取指定列键的值
*/
public static void getValueTest(String tableName, String rowKey, String family, String qualifier) throws Exception {
initTableResult(tableName, rowKey);
get.addFamily(Bytes.toBytes(family));
connection.close();
String value = new String(result.getValue(Bytes.toBytes(family), Bytes.toBytes(qualifier)));
System.out.println(family + ":" + qualifier + " = " + value);
}
/**
* 测试Result的containsColumn方法
* <p>检测列键是否存在
*/
public static void containsColumnTest(String tableName, String rowKey, String family, String qualifier)
throws Exception {
initTableResult(tableName, rowKey);
get.addFamily(Bytes.toBytes(family));
connection.close();
boolean b = result.containsColumn(family.getBytes(), qualifier.getBytes());
System.out.println(family + ":" + qualifier + " is exist: " + b);
}
/**
* 测试Result的listCells方法
* <p>列出指定行键的所有单元格Cell
*/
public static void listCellsTest(String tableName, String rowKey) throws Exception {
initTableResult(tableName, rowKey);
connection.close();
System.out.println(Bytes.toString(result.getRow()) + "'s Cells : " + result.listCells());
// 与上一条代码结果相同
// System.out.println(new String(result.getRow()) + "'s Cells : " + result.listCells());
}
/**
* 初始化表及其结果集
*/
private static void initTableResult(String tableName, String rowKey) throws Exception {
connection = ConnectionFactory.createConnection(cfg);
table = connection.getTable(TableName.valueOf(tableName));
get = new Get(Bytes.toBytes(rowKey));
result = table.get(get);
}
public static void main(String[] args) throws Exception {
// PutGet.put("ns1:scores", "tom", "grade", "", "1");
// PutGet.put("ns1:scores", "tom", "course", "art", "80");
// PutGet.put("ns1:scores", "tom", "course", "math", "89");
// PutGet.put("ns1:scores", "jason", "grade", "", "2");
// PutGet.put("ns1:scores", "jason", "course", "art", "87");
// PutGet.put("ns1:scores", "jason", "course", "math", "57");
// PutGet.get("ns1:scores", "jason");
// PutGet.get("ns1:scores", "jason", "course");
// PutGet.get("ns1:scores", "jason", "course", "math");
}
}