Hbase_shell底层源码乱砍乱伐--59
2016-07-12 01:06
429 查看
大约10个月没有更新博客了,一直在学习新的东西,忙的不可开胶,现在一边整理做过的事情,以便复习一下。白天要工作也只有后半夜才有时间看书了。------青春无悔
下面的代码是关于Hbase shell脚本的源码的改写,屏蔽了一些功能,便于理解和安全操作。
现在没时间逐一解释关键点的含义,以后有时间再改,替换掉集群中:hbase_home:/lib/ruby/shell.rb
下面的代码是关于Hbase shell脚本的源码的改写,屏蔽了一些功能,便于理解和安全操作。
现在没时间逐一解释关键点的含义,以后有时间再改,替换掉集群中:hbase_home:/lib/ruby/shell.rb
# Shell commands module module Shell @@commands = {} def self.commands @@commands end @@command_groups = {} def self.command_groups @@command_groups end def self.load_command(name, group, aliases=[]) return if commands[name] # Register command in the group raise ArgumentError, "Unknown group: #{group}" unless command_groups[group] command_groups[group][:commands] << name # Load command begin require "shell/commands/#{name}" klass_name = name.to_s.gsub(/(?:^|_)(.)/) { $1.upcase } # camelize commands[name] = eval("Commands::#{klass_name}") aliases.each do |an_alias| commands[an_alias] = commands[name] end rescue => e raise "Can't load hbase shell command: #{name}. Error: #{e}\n#{e.backtrace.join("\n")}" end end def self.load_command_group(group, opts) raise ArgumentError, "No :commands for group #{group}" unless opts[:commands] command_groups[group] = { :commands => [], :command_names => opts[:commands], :full_name => opts[:full_name] || group, :comment => opts[:comment] } all_aliases = opts[:aliases] || {} opts[:commands].each do |command| aliases = all_aliases[command] || [] load_command(command, group, aliases) end end #---------------------------------------------------------------------- class Shell attr_accessor :hbase attr_accessor :formatter attr_accessor :interactive alias interactive? interactive @debug = false attr_accessor :debug def initialize(hbase, formatter, interactive=true) self.hbase = hbase self.formatter = formatter self.interactive = interactive end def hbase_admin @hbase_admin ||= hbase.admin(formatter) end def hbase_table(name) hbase.table(name, self) end def hbase_replication_admin @hbase_replication_admin ||= hbase.replication_admin(formatter) end def hbase_security_admin @hbase_security_admin ||= hbase.security_admin(formatter) end def hbase_visibility_labels_admin @hbase_visibility_labels_admin ||= hbase.visibility_labels_admin(formatter) end def hbase_quotas_admin @hbase_quotas_admin ||= hbase.quotas_admin(formatter) end def export_commands(where) ::Shell.commands.keys.each do |cmd| # here where is the IRB namespace # this method just adds the call to the specified command # which just references back to 'this' shell object # a decently extensible way to add commands where.send :instance_eval, <<-EOF def #{cmd}(*args) ret = @shell.command('#{cmd}', *args) puts return ret end EOF end end def command_instance(command) ::Shell.commands[command.to_s].new(self) end #call the method 'command' on the specified command def command(command, *args) internal_command(command, :command, *args) end #call a specific internal method in the command instance # command - name of the command to call # method_name - name of the method on the command to call. Defaults to just 'command' # args - to be passed to the named method def internal_command(command, method_name= :command, *args) command_instance(command).command_safe(self.debug,method_name, *args) end def print_banner puts "HBase Shell; enter 'help<RETURN>' for list of supported commands." puts 'Type "exit<RETURN>" to leave the HBase Shell' print 'Version ' command('version') puts end def help_multi_command(command) puts "Command: #{command}" puts command_instance(command).help puts return nil end def help_command(command) puts command_instance(command).help return nil end def help_group(group_name) group = ::Shell.command_groups[group_name.to_s] group[:commands].sort.each { |cmd| help_multi_command(cmd) } if group[:comment] puts '-' * 80 puts puts group[:comment] puts end return nil end def help(command = nil) if command return help_command(command) if ::Shell.commands[command.to_s] return help_group(command) if ::Shell.command_groups[command.to_s] puts "ERROR: Invalid command or command group name: #{command}" puts end puts help_header puts puts 'COMMAND GROUPS:' ::Shell.command_groups.each do |name, group| puts " Group name: " + name puts " Commands: " + group[:command_names].sort.join(', ') puts end unless command puts 'SHELL USAGE:' help_footer end return nil end def help_header return "HBase Shell, version #{org.apache.hadoop.hbase.util.VersionInfo.getVersion()}, " + "r#{org.apache.hadoop.hbase.util.VersionInfo.getRevision()}, " + "#{org.apache.hadoop.hbase.util.VersionInfo.getDate()}" + "\n" + "Type 'help \"COMMAND\"', (e.g. 'help \"get\"' -- the quotes are necessary) for help on a specific command.\n" + "Commands are grouped. Type 'help \"COMMAND_GROUP\"', (e.g. 'help \"general\"') for help on a command group." end def help_footer puts <<-HERE Quote all names in HBase Shell such as table and column names. Commas delimit command parameters. Type <RETURN> after entering a command to run it. Dictionaries of configuration used in the creation and alteration of tables are Ruby Hashes. They look like this: {'key1' => 'value1', 'key2' => 'value2', ...} and are opened and closed with curley-braces. Key/values are delimited by the '=>' character combination. Usually keys are predefined constants such as NAME, VERSIONS, COMPRESSION, etc. Constants do not need to be quoted. Type 'Object.constants' to see a (messy) list of all constants in the environment. If you are using binary keys or values and need to enter them in the shell, use double-quote'd hexadecimal representation. For example: hbase> get 't1', "key\\x03\\x3f\\xcd" hbase> get 't1', "key\\003\\023\\011" hbase> put 't1', "test\\xef\\xff", 'f1:', "\\x01\\x33\\x40" The HBase shell is the (J)Ruby IRB with the above HBase-specific commands added. For more on the HBase Shell, see http://hbase.apache.org/book.html HERE end end end # Load commands base class require 'shell/commands' # Load all commands Shell.load_command_group( 'general', :full_name => 'GENERAL HBASE SHELL COMMANDS', :commands => %w[ version whoami ] ) Shell.load_command_group( 'ddl', :full_name => 'TABLES MANAGEMENT COMMANDS', :commands => %w[ describe exists list ], :aliases => { 'describe' => ['desc'] } ) Shell.load_command_group( 'namespace', :full_name => 'NAMESPACE MANAGEMENT COMMANDS', :commands => %w[ ] ) Shell.load_command_group( 'dml', :full_name => 'DATA MANIPULATION COMMANDS', :commands => %w[ count get scan ] ) Shell.load_command_group( 'tools', :full_name => 'HBASE SURGERY TOOLS', :comment => "WARNING: Above commands are for 'experts'-only as misuse can damage an install", :commands => %w[ ] ) Shell.load_command_group( 'replication', :full_name => 'CLUSTER REPLICATION TOOLS', :comment => "In order to use these tools, hbase.replication must be true.", :commands => %w[ ] ) Shell.load_command_group( 'snapshots', :full_name => 'CLUSTER SNAPSHOT TOOLS', :commands => %w[ ] ) Shell.load_command_group( 'configuration', :full_name => 'ONLINE CONFIGURATION TOOLS', :commands => %w[ ] ) Shell.load_command_group( 'quotas', :full_name => 'CLUSTER QUOTAS TOOLS', :commands => %w[ ] ) Shell.load_command_group( 'security', :full_name => 'SECURITY TOOLS', :comment => "NOTE: Above commands are only applicable if running with the AccessController coprocessor", :commands => %w[ ] ) Shell.load_command_group( 'visibility labels', :full_name => 'VISIBILITY LABEL TOOLS', :comment => "NOTE: Above commands are only applicable if running with the VisibilityController coprocessor", :commands => %w[ ] )
相关文章推荐
- SecureCRT 常用技巧 & Bash快捷键
- shell获取函数的返回值
- shell学习笔记
- 最近需要学习有关shell的知识,搜集的资料
- shell - 查看天气
- Hadoop实战4:MapR分布式集群的安装配置及shell自动化脚本
- [awk]awk 与 shell的参数传递(双向)
- shell 脚本批量安装perl包
- Linus的redHat在root出现错误:-bash: addgroup: command not found
- 分布式监控系统Zabbix-3.0.3-完整安装记录 - 添加shell脚本监控
- 【转】mac终端安装node时候,显示“-bash: brew: command not found”,怎么解决?
- shell学习笔记
- linux shell脚本替换反斜杠
- Shell脚本中计算字符串长度的5种方法
- 查看data目录
- python中执行shell命令的几种方式
- Xshell个性化设置,解决Xshell遇到中文显示乱码的问题
- linux中shell截取字符串方法总结
- Shell脚本中计算字符串长度的5种方法
- 【Linux】shell变量篇(收藏)