Hbase之使用多Get實例返回數據


import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 給定一系列的Get實例返回數據集
 *
 */
public class GetDataWithListGet {
    public static void main(String args[]) throws IOException {
        Configuration configuration = HBaseConfiguration.create();
        Connection connection = ConnectionFactory.createConnection(configuration);
        //建立表的連接
        Table table = connection.getTable(TableName.valueOf("testtable"));
        //列族
        byte[] cf1 = Bytes.toBytes("colfam1");
        //列限定符
        byte[] qf1 = Bytes.toBytes("qual1");
        //列限定符
        byte[] qf2 = Bytes.toBytes("qual2");
        //行鍵
        byte[] row1 = Bytes.toBytes("10010");
        //行鍵
        byte[] row2 = Bytes.toBytes("10086");
        List<Get> gets = new ArrayList<Get>();
        Get get1 = new Get(row1);
        get1.addColumn(cf1, qf1);
        gets.add(get1);
        Get get2 = new Get(row2);
        get2.addColumn(cf1, qf1);
        gets.add(get2);
        Get get3 = new Get(row2);
        get3.addColumn(cf1, qf2);
        gets.add(get3);

        Result[] results = table.get(gets);

        System.out.println("First iteration...");
        for (Result result : results) {
            String row = Bytes.toString(result.getRow());
            System.out.print("Row: " + row + " ");
            byte[] val = null;
            //包含列族colfam1和列分隔符qual1的
            if (result.containsColumn(cf1, qf1)) {
                val = result.getValue(cf1, qf1);
                System.out.println("Value: " + Bytes.toString(val));
            }
            //包含列族colfam1和列分隔符qual2的
            if (result.containsColumn(cf1, qf2)) {
                val = result.getValue(cf1, qf2);
                System.out.println("Value: " + Bytes.toString(val));
            }
        }
        System.out.println("\nSecond iteration...");
        for (Result result : results) {
            if (!result.isEmpty()) {
                for (Cell cell : result.listCells()) {
                    System.out.println("Row: " + Bytes.toString(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength()) + " Value: " + Bytes.toString(CellUtil.cloneValue(cell)));
                }
            }
        }
        System.out.println("Third iteration...");
        for (Result result : results) {
            System.out.println(result);
        }
    }
}

/**
result:
 First iteration...
 Row: 10010 Value: 中國聯通
 Row: 10086 Value: 中國移動
 Row: null
 Second iteration...
 Row: 10010 Value: 中國聯通
 Row: 10086 Value: 中國移動
 Third iteration...
 keyvalues={10010/colfam1:qual1/1471836722159/Put/vlen=12/seqid=0}
 keyvalues={10086/colfam1:qual1/1471836722159/Put/vlen=12/seqid=0}
 keyvalues=NONE
**/

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM