$ Hbase shell 命令中添加自定义sql语句实现

        最近一直在搞hbase sql工具的本地化,有时候受限于网络连接,不是很理想。

        于是开始琢磨能不能将sql的查询放在hbase的shell命令中,类似list的命令,可以将所有表都显示出来。

        步骤如下:

1.找到$hbasepath\lib\ruby目录,结构及作用如下:

hbase/ :定义要加载的java类的ruby文件;

        比如admin.rb:org.apache.hadoop.hbase.client.HBaseAdmin

        hbase.rb:org.apache.hadoop.hbase.HBaseConfiguration

        replication_admin.rb:org.apache.hadoop.hbase.client.replication.ReplicationAdmin

        security.rb:org.apache.hadoop.hbase.security.access.TablePermission

        table.rb:org.apache.hadoop.hbase.client包下类

irb/ :可执行ruby的环境;

shell/:存放可执行命令的ruby文件夹;

hbase.rb:# Has wrapper classes for org.apache.hadoop.hbase.client.HBaseAdmin
          # and for org.apache.hadoop.hbase.client.HTable.  Classes take
          # Formatters on construction and outputs any results using
          # Formatter methods.  These classes are only really for use by
          # the hirb.rb HBase Shell script; they don't make much sense elsewhere.
          # For example, the exists method on Admin class prints to the formatter
          # whether the table exists and returns nil regardless.

shell.rb:shell装载ruby命令


2.在shell.rb中添加命令代码,比如select

Shell.load_command_group(
  'ddl',
  :full_name => 'TABLES MANAGEMENT COMMANDS',
  :commands => %w[
    alter
    create
    describe
    disable
    disable_all
    is_disabled
    drop
    drop_all
    enable
    enable_all
    select
    is_enabled
    exists
    list
    show_filters
    alter_status
    alter_async
  ]
)

3.在hbase/下添加加载自定义的类文件的ruby文件,例如

select.rb

include Java

# Wrapper for org.apache.hadoop.hbase.client.Query

module Hbase
  class Select
    include HBaseConstants

    def initialize(configuration, formatter)
      @query= org.apache.hadoop.hbase.client.Select.new(configuration)
    end

    #----------------------------------------------------------------------------------------------
    # Returns a list of tables in hbase
    def select
      @select.querySql { sql }
    end
  end
end

4.在shell\commands下创建文件

Select.rb

module Shell
  module Commands
    class Select < Command
      def help
        return <<-EOF
Create table; pass table name, a dictionary of specifications per
column family, and optionally a dictionary of table configuration.
Dictionaries are described below in the GENERAL NOTES section.
Examples:

  hbase> select * from tables;
  hbase> select [columns] from [tableName] [where rowkey like '%'] [limit nums]|[group by ...]
EOF
      end

      def command(*args)
        formatter.header
        sql = select.querySql(args[0])  
        formatter.row([ "your input the sql is :#{sql}" ])
        formatter.footer(sql)
      end
    end
  end
end


5.效果

[root@hdfs004 hbase]# hbase  shell
13/01/31 17:24:51 WARN conf.Configuration: hadoop.native.lib is deprecated. Instead, use io.native.lib.available
HBase Shell; enter 'help<RETURN>' for list of supported commands.
Type "exit<RETURN>" to leave the HBase Shell
Version 0.92.1-cdh4.1.2, rUnknown, Thu Nov  1 18:01:09 PDT 2012

hbase(main):001:0> select

ERROR: wrong number of arguments (0 for 1)

Here is some help for this command:
Examples:

  hbase> select * from [tables]
[root@hdfs004 hbase]# 

上述过程就是自定义hbase shell命令的实现步骤。


未完整,待续

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值