大约10个月没有更新博客了,一直在学习新的东西,忙的不可开胶,现在一边整理做过的事情,以便复习一下。白天要工作也只有后半夜才有时间看书了。------青春无悔
下面的代码是关于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_access