gpt4 book ai didi

java - HBase内部是如何解析 "hbase shell command"的?

转载 作者:可可西里 更新时间:2023-11-01 15:11:07 24 4
gpt4 key购买 nike

假设,我在 hbase shell 中运行 get 't1','r1' 命令,HBase 内部如何分析并执行这个命令?

最佳答案

这是一个 jruby 脚本。这是在一组 shell 命令下定义的。

I am quoting here java HashMap as an example for better understanding..

  • 在插入时,您的 rowkey 就像 java HashMap 中的键,它将存储在其中一个区域服务器中(在 HashMap 情况下,这些是均匀分布的桶..)
  • 在取回该行时,它使用 rowkey,它将定位特定的区域服务器并从您提到的表中获取该服务器的值。 example of hashmap like...

That's the reason while dealing with hbase rowkey design should be perfect (with salting technique , using hashing algorithm for ex: mumur hash) and it should be uniformly distributed across region servers to prevent hot spotting... enter image description here

有关更多详细信息,请查看 get.rb

module Shell
module Commands
class Get < Command
def help
return <<-EOF
Get row or cell contents; pass table name, row, and optionally
a dictionary of column(s), timestamp, timerange and versions. Examples:
hbase> get 'ns1:t1', 'r1'
hbase> get 't1', 'r1'
hbase> get 't1', 'r1', {TIMERANGE => [ts1, ts2]}
hbase> get 't1', 'r1', {COLUMN => 'c1'}
hbase> get 't1', 'r1', {COLUMN => ['c1', 'c2', 'c3']}
hbase> get 't1', 'r1', {COLUMN => 'c1', TIMESTAMP => ts1}
hbase> get 't1', 'r1', {COLUMN => 'c1', TIMERANGE => [ts1, ts2], VERSIONS => 4}
hbase> get 't1', 'r1', {COLUMN => 'c1', TIMESTAMP => ts1, VERSIONS => 4}
hbase> get 't1', 'r1', {FILTER => "ValueFilter(=, 'binary:abc')"}
hbase> get 't1', 'r1', 'c1'
hbase> get 't1', 'r1', 'c1', 'c2'
hbase> get 't1', 'r1', ['c1', 'c2']
hbase> get 't1', 'r1', {COLUMN => 'c1', ATTRIBUTES => {'mykey'=>'myvalue'}}
hbase> get 't1', 'r1', {COLUMN => 'c1', AUTHORIZATIONS => ['PRIVATE','SECRET']}
hbase> get 't1', 'r1', {CONSISTENCY => 'TIMELINE'}
hbase> get 't1', 'r1', {CONSISTENCY => 'TIMELINE', REGION_REPLICA_ID => 1}
Besides the default 'toStringBinary' format, 'get' also supports custom formatting by
column. A user can define a FORMATTER by adding it to the column name in the get
specification. The FORMATTER can be stipulated:
1. either as a org.apache.hadoop.hbase.util.Bytes method name (e.g, toInt, toString)
2. or as a custom class followed by method name: e.g. 'c(MyFormatterClass).format'.
Example formatting cf:qualifier1 and cf:qualifier2 both as Integers:
hbase> get 't1', 'r1' {COLUMN => ['cf:qualifier1:toInt',
'cf:qualifier2:c(org.apache.hadoop.hbase.util.Bytes).toInt'] }
Note that you can specify a FORMATTER by column only (cf:qualifier). You cannot specify
a FORMATTER for all columns of a column family.

The same commands also can be run on a reference to a table (obtained via get_table or
create_table). Suppose you had a reference t to table 't1', the corresponding commands
would be:
hbase> t.get 'r1'
hbase> t.get 'r1', {TIMERANGE => [ts1, ts2]}
hbase> t.get 'r1', {COLUMN => 'c1'}
hbase> t.get 'r1', {COLUMN => ['c1', 'c2', 'c3']}
hbase> t.get 'r1', {COLUMN => 'c1', TIMESTAMP => ts1}
hbase> t.get 'r1', {COLUMN => 'c1', TIMERANGE => [ts1, ts2], VERSIONS => 4}
hbase> t.get 'r1', {COLUMN => 'c1', TIMESTAMP => ts1, VERSIONS => 4}
hbase> t.get 'r1', {FILTER => "ValueFilter(=, 'binary:abc')"}
hbase> t.get 'r1', 'c1'
hbase> t.get 'r1', 'c1', 'c2'
hbase> t.get 'r1', ['c1', 'c2']
hbase> t.get 'r1', {CONSISTENCY => 'TIMELINE'}
hbase> t.get 'r1', {CONSISTENCY => 'TIMELINE', REGION_REPLICA_ID => 1}
EOF
end

def command(table, row, *args)
get(table(table), row, *args)
end

def get(table, row, *args)
@start_time = Time.now
formatter.header(["COLUMN", "CELL"])

count, is_stale = table._get_internal(row, *args) do |column, value|
formatter.row([ column, value ])
end

formatter.footer(count, is_stale)
end
end
end
end

#add get command to table
::Hbase::Table.add_shell_command('get')

如果你想像hbase shell命令一样获取一条记录,你可以按照下面的代码片段。

根据您的评论进行更新:如果您想在 java 中拥有相同的功能

 /**
* Get a row
*/
@Override
public void getOneRecord(final String tableName, final String rowKey) throws IOException {
final HTable table = new HTable(HBaseConn.getHBaseConfig(), getTable(tableName));
final Get get = new Get(rowKey.getBytes());
final Result rs = table.get(get);
for (final KeyValue kv : rs.raw()) {
LOG.info(kv.getRow() + " " + kv.getFamily() + ":" + kv.getQualifier() + " " + +kv.getTimestamp());
LOG.info(new String(kv.getValue()));
}
}

注意:java 方法和 shell 方法是两种不同的东西。请。不要混合使用作为I have seen your other questions as well ,我觉得你对他们有点困惑。如果你想像我解释的那样编写 jruby,你也可以这样做。但这不是常见的方法。

希望对您有所帮助。

关于java - HBase内部是如何解析 "hbase shell command"的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39585574/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com