从动态获取所有列得到HBase

时间:2017-03-01 03:55:17

标签: java hadoop hbase

我正在处理从Habse中的表中检索到的Get对象。我想动态检索与该get相关的所有列值,因为我不知道列族的确切名称

      val result1 = hTable.get(g)  
    if (!result1.isEmpty) {
            //binaryEpisodes = result1.getValue(Bytes.toBytes("episodes"),Bytes.toBytes("episodes"))
        //instead of above retrieve all values dynamically
}

1 个答案:

答案 0 :(得分:5)

  • 简单方法:

获取rawcells并了解CF,列信息。 你必须做类似下面的事情

public static void printResult(Result result, Logger logger) {
        logger.info("Row: ");
        for (Cell cell : result.rawCells()) {
            byte[] family = CellUtil.cloneFamily(cell);
            byte[] column = CellUtil.cloneQualifier(cell);
            byte[] value = CellUtil.cloneValue(cell);
            logger.info("\t" + Bytes.toString(family) + ":" + Bytes.toString(column) + " = " + Bytes.toString(value));
    }
}
  • Hbase管理方式:Hbase客户端API由HbaseAdmin类公开,如下所示......

客户端就像

package mytest;
import com.usertest.*;

import java.io.IOException;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;


public class ListHbaseTablesAndColumns {
    public static void main(String[] args) {
        try {
            HbaseMetaData hbaseMetaData  =new HbaseMetaData();
            for(String hbaseTable:hbaseMetaData  .getTableNames(".*yourtables.*")){
                    for (String column : hbaseMetaData  .getColumns(hbaseTable, 10000)) {
                        System.out.println(hbaseTable + "," + column);
                    }

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

使用以下类来获取HbaseMetaData ..

package com.usertest;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.PageFilter;

import java.io.IOException;
import java.util.*;
import java.util.regex.Pattern;

public class HbaseMetaData {
    private HBaseAdmin hBaseAdmin;
    private Configuration hBaseConfiguration;

    public HbaseMetaData () throws IOException {
        this.hBaseConfiguration = HBaseConfiguration.create();
        this.hBaseAdmin = new HBaseAdmin(hBaseConfiguration);
    }
/** get all Table names **/
    public List<String> getTableNames(String regex) throws IOException {
        Pattern pattern=Pattern.compile(regex);
        List<String> tableList = new ArrayList<String>();
        TableName[] tableNames=hBaseAdmin.listTableNames();
        for (TableName tableName:tableNames){
            if(pattern.matcher(tableName.toString()).find()){
                tableList.add(tableName.toString());
            }
        }
        return tableList;
    }
/** Get all columns **/
    public Set<String> getColumns(String hbaseTable) throws IOException {
        return getColumns(hbaseTable, 10000);
    }
/** get all columns from the table **/
    public Set<String> getColumns(String hbaseTable, int limitScan) throws IOException {
        Set<String> columnList = new TreeSet<String>();
        HTable hTable=new HTable(hBaseConfiguration, hbaseTable);
        Scan scan=new Scan();
        scan.setFilter(new PageFilter(limitScan));
        ResultScanner results = hTable.getScanner(scan);
        for(Result result:results){
            for(KeyValue keyValue:result.list()){
                columnList.add(
                        new String(keyValue.getFamily()) + ":" +
                                new String(keyValue.getQualifier())
                );
            }
        }
        return columnList;
    }
}