HBase应用程序开发03查询

来源:互联网 发布:日本神户制钢造假知乎 编辑:程序博客网 时间:2024/06/05 07:47

一、获取一行的数据get 'table', 'rowkey', 'cf:qualiter'

获取数据是通操作table来获取, 根据制定的rowkey获取对应的行数据result

通过Table.get(Get)获取Result

rs = table.get(get);

代码实现:

/**     * 通过rowkey查询数据     * @param tableName     * @param row   rowkey     * @return     */    public Result queryByRow(String tableName, byte[] row) {        //返回结果        Result rs = null;        //获取hbase库的连接        conn = getConn();        Table table = null;        try {            table = conn.getTable(TableName.valueOf(tableName));            Get get  = new Get(row);            rs = table.get(get);        } catch (IOException e) {            e.printStackTrace();        }finally {            closeTable(table);            closeConn();        }        return rs;    }测试:    /**     * 获取一个row的数据     */    public static void get() {        HBaseDAO hBaseDAO = HBaseDAO.getInstance();        Result rs = hBaseDAO.queryByRow(tableName, rowkey1.getBytes());        Cell cell = rs.getColumnLatestCell(HBaseConfConstant.family, HBaseConfConstant.qualifier);        System.out.println(new String(cell.getValue()));    }

二、通过批量rowkey查询数据

通过Table.get(List<Get> gets)这个方法获取批量的数据。

    /**     * 通过批量rowkey查询数据     * @param tableName     * @param rows   批量rowkey     * @return     */    public Result[] queryByRows(String tableName, List<byte[]> rows) {        Result[] rs = null;        conn = getConn();        Table table = null;        try {            table = conn.getTable(TableName.valueOf(tableName));            List<Get> gets = new ArrayList<Get>();            for (int i = 0; i < rows.size(); i++) {                Get get  = new Get(rows.get(i));                gets.add(get);            }            rs = table.get(gets);        } catch (IOException e) {            e.printStackTrace();        }finally {            closeTable(table);            closeConn();        }        return rs;    }测试:    /**     * 批量获取     */    public static void getByRows() {        HBaseDAO hBaseDAO = HBaseDAO.getInstance();        List<byte[]> list = new ArrayList<byte[]>();        list.add(rowkey1.getBytes());        list.add(rowkey2.getBytes());        Result[] rs = hBaseDAO.queryByRows(tableName, list);        for (Result result : rs) {            Cell cell = result.getColumnLatestCell(HBaseConfConstant.family, HBaseConfConstant.qualifier);            System.out.println(new String(cell.getValue()));        }    }

三、通过Scan(byte[] startRow, byte[] stopRow)

首先要了解几个对象

3.1、Scan: 用于执行scan操作的对象。

设置一个开始rowkey和一个endrowkey, hbase 中的数据是按照rowkey的字典ASCII排序的 ,这个必须重视, 我在测试的时候,
设置两个rowkey

static String rowkey1 = "5708A2.B1.B2-20100001_2101_221.130.189.6_735622241";static String rowkey2 = "5608A2.B1.B2-20100001_2101_221.181.72.246_736059657";

但是在设置Scan的时候rowkey1为startRow, rowkey2为endkey, 发现总是无法查询到数据,
最后发现按照自然排序, rowkey1在后, 调换之后就可以了。

Scan scan = new Scan();scan.setStartRow(startRow);scan.setStopRow(stopRow);

通过table操作scan对象获取一个ResultScanner,
该对象就是一个Result的集合
public interface ResultScanner extends Closeable, Iterable

ResultScanner rScanner = table.getScanner(scan)#通过遍历获取内容Iterator<Result> irs = rScanner.iterator();while (irs.hasNext()) {    Result rs = irs.next();    byte[] value = rs.getValue(HBaseConfConstant.family, HBaseConfConstant.qualifier);    System.out.println(new String(value));}
0 0
原创粉丝点击