【问题标题】:HBase shell - Retrieve (only) column values (and not column name)HBase shell - 检索(仅)列值(而不是列名)
【发布时间】:2014-01-07 13:05:25
【问题描述】:

我对 Hadoop 和 HBase 还很陌生,正在尝试学习和评估它是否可以用于我的用例。作为 Java 新手(我基本上是 Perl/Unix 和 DB 开发人员),如果可能的话,我会尝试在 Hbase shell 中获得解决方案。

我有一个 HBase 表(下面的架构),我试图在其中实现历史数据(可用于审计和分析)。

假设基本结构如下,

    rowkey 'cf1:id', 'cf1:price', 'cf1:user', 'cf1:timestamp'

现在, rowkey - 仪器或任何对象 id - 使用它来识别哪个 col 具有最新数据。第一个条目的值为 1,然后继续 user - 更新数据的用户

例如

最初的数据看起来像,

    hbase(main):009:0> scan 'price_history'
    ROW  COLUMN+CELL                                                                                                                 
    row1        column=cf1:id, timestamp=1389020633920,value=1
    row1        column=cf1:pr, timestamp=1389020654614, value=109.45
    row1        column=cf1:us, timestamp=1389020668338, value=feed
    row2        column=cf1:id, timestamp=1389020687334, value=1
    row2        column=cf1:pr, timestamp=1389020697880, value=1345.65
    row2        column=cf1:us, timestamp=1389020708403, value=feed

现在假设 row2 或工具 2 在同一天以新价格更新,

    hbase(main):003:0> scan 'price_history'
    ROW                   COLUMN+CELL                        
    row1                 column=cf1:id, timestamp=1389020633920, value=1
    row1                 column=cf1:pr, timestamp=1389020654614, value=109.45
    row1                 column=cf1:us, timestamp=1389020668338, value=feed
    row2                   column=cf1:id, timestamp=1389020859674, value=2
    row2                 column=cf1:pr, timestamp=1389020697880, value=1345.65
    row2                 column=cf1:pr1, timestamp=1389020869856, value=200
    row2                 column=cf1:us, timestamp=1389020708403, value=feed
    row2                 column=cf1:us1, timestamp=1389020881601, value=user1`

如果您看到 id 更改为 2 表示第二组数据是最新的。并添加了新值或列。

我想要的是,

    1) Can I fetch the value of columns id? i.e. the output should be 1 or 2 and not  all other attribs
    2) Based on the above o/p i will fetch the further data, but can I also have a search and o/p as value of rowkey? i.e. something like give me o/p of row having VALUE as row1 (I can have list of row1, row2, rown..)

如果可能,请尽可能在 HBase shell 中提供帮助(也欢迎其他解决方案)

此外,如果任何架构师可以提出更好的解决方案来为表格建模以跟踪价格的变化/版本,我们也欢迎。

谢谢。

【问题讨论】:

    标签: hadoop hbase


    【解决方案1】:

    如果不进行大量管道输出和对结果进行 grep,这将很难在 shell 中完成。 shell 输出格式也使这变得困难,因为它如何分解行。比编写 Java 更轻量级的解决方案是用 ruby​​ 编写您的扫描仪。 HBase 带有 jruby jar,让您可以执行 ruby​​ 脚本。

    include Java
    import "org.apache.hadoop.hbase.client.Scan"
    import "org.apache.hadoop.hbase.util.Bytes"
    import "org.apache.hadoop.hbase.client.HTable"
    
    config = HBaseConfiguration.create()
    family = Bytes.toBytes("family-name")
    qual = Bytes.toBytes("qualifier"
    scan = Scan.new()
    scan.addColumn(family, qualifier)
    
    table = HTable.new(config, "table-name")
    scanner = table.getScanner(scan)
    scanner.each do |result|
       keyval = result.getColumnLatest(family, qualifier) 
       puts "#{Bytes.toDouble(keyval.getValue())}"
    end
    

    这应该让您非常接近,您可以将其他数据添加到输出中,例如行键。要运行它,只需使用hbase org.jruby.Main your_ruby_file.rb

    【讨论】:

    • 感谢您的回复。
    • 是的,我也同意使用 Shell 会很困难,但是由于我现在正在为 POC 工作,因此我希望快速完成 hte 设计,并且我对 Java 的了解非常有限,我正在考虑是否可以实现。这种方法看起来很适合开始工作。感谢您的想法,我会努力解决这个问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-26
    • 1970-01-01
    • 1970-01-01
    • 2015-09-23
    • 1970-01-01
    相关资源
    最近更新 更多