【问题标题】:Getting all columns from get result in HBase dynamically动态从 HBase 中获取所有列
【发布时间】:2017-03-01 03:55:17
【问题描述】:

我正在处理从 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
}

【问题讨论】:

  • 幸运:您至少可以尝试以下提供的解决方案吗?对此有任何疑问/问题???

标签: java hadoop hbase


【解决方案1】:
  • 简单方法:

获取 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;
    }
}

【讨论】:

  • 您使用的 HTable 构造函数似乎已弃用
  • 是的,我同意很久以前我在 hbase 工作过。请关注this
  • 如果您对答案没问题,请注意以所有者身份接受该解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-23
  • 1970-01-01
  • 1970-01-01
  • 2019-06-08
相关资源
最近更新 更多