HBASE-487 Replace hql w/ a hbase-friendly jirb or jython shell

M  bin/HBase.rb
  Remove Module-level constants.  Move them instead back into
  HColumnDescriptor, etc. and read them from there.
  (describe): Added.
M  bin/hirb.rb
  Fixed up comments.  Added TODOs.
  Add here the constants from HColumnDescriptor and HTableDescriptor
  Use HERE doc. for main help.


git-svn-id: https://svn.apache.org/repos/asf/hadoop/hbase/trunk@667364 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael Stack 2008-06-13 05:50:00 +00:00
parent 390233511b
commit 82639a32fd
2 changed files with 102 additions and 70 deletions

View File

@ -1,15 +1,5 @@
# HBase ruby classes # HBase ruby classes
module HBase module HBase
# Constants needed as keys creating tables, etc.
NAME = "NAME"
MAX_VERSIONS = "MAX_VERSIONS"
MAX_LENGTH = "MAX_LENGTH"
TTL = "TTL"
BLOOMFILTER = "BLOOMFILTER"
COMPRESSION_TYPE = "COMPRESSION_TYPE"
# TODO: Add table options here.
class Admin class Admin
def initialize(configuration, formatter) def initialize(configuration, formatter)
@admin = HBaseAdmin.new(configuration) @admin = HBaseAdmin.new(configuration)
@ -25,10 +15,26 @@ module HBase
@formatter.footer(now) @formatter.footer(now)
end end
def describe(tableName)
now = Time.now
@formatter.header()
found = false
for t in @admin.listTables()
if t.getNameAsString() == tableName
@formatter.row([t.to_s])
found = true
end
end
if not found
raise new ArgumentError.new("Failed to find table named " + tableName)
end
@formatter.footer(now)
end
def exists(tableName) def exists(tableName)
now = Time.now now = Time.now
@formatter.header() @formatter.header()
@formatter.row([@admin.tableExists(tableName)]) @formatter.row([@admin.tableExists(tableName).to_s])
@formatter.footer(now) @formatter.footer(now)
end end
@ -52,7 +58,6 @@ module HBase
now = Time.now now = Time.now
@admin.deleteTable(tableName) @admin.deleteTable(tableName)
@formatter.header() @formatter.header()
@formatter.row(["Deleted %s" % tableName])
@formatter.footer(now) @formatter.footer(now)
end end
@ -83,7 +88,6 @@ module HBase
end end
@admin.createTable(htd) @admin.createTable(htd)
@formatter.header() @formatter.header()
@formatter.row(["Created %s" % tableName])
@formatter.footer(now) @formatter.footer(now)
end end
end end

View File

@ -1,11 +1,13 @@
# Command passed to org.jruby.Main. Pollutes jirb with hbase imports and hbase # File passed to org.jruby.Main by bin/hbase. Pollutes jirb with hbase imports
# commands and then loads jirb. Outputs a banner that tells user where to find # and hbase commands and then loads jirb. Outputs a banner that tells user
# help, shell version, etc. # where to find help, shell version, and loads up a custom hirb.
# TODO: Process command-line arguments: e.g. --master= or -Dhbase.etc and --formatter # TODO: Add 'debug' support (client-side logs show in shell). Add it as
# or read hbase shell configurations from irbrc # command-line option and as command.
# TODO: Interrupt a table creation or a connection to a bad master. Currently
# TODO: Write a base class for formatters with ascii, xhtml, and json subclasses. # has to time out.
# TODO: Add support for listing and manipulating catalog tables, etc.
# TODO: Fix 'irb: warn: can't alias help from irb_help.' in banner message
# Run the java magic include and import basic HBase types that will help ease # Run the java magic include and import basic HBase types that will help ease
# hbase hacking. # hbase hacking.
@ -21,21 +23,20 @@ import org.apache.hadoop.hbase.io.BatchUpdate
# Some goodies for hirb. Should these be left up to the user's discretion? # Some goodies for hirb. Should these be left up to the user's discretion?
require 'irb/completion' require 'irb/completion'
# Add the $HBASE_HOME/bin directory, the presumed location of this script, # Add the $HBASE_HOME/bin directory, the location of this script, to the ruby
# to the ruby load path so I can load up my HBase ruby modules # load path so I can load up my HBase ruby modules
$LOAD_PATH.unshift File.dirname($PROGRAM_NAME) $LOAD_PATH.unshift File.dirname($PROGRAM_NAME)
# Require formatter and hbase
require 'Formatter' require 'Formatter'
require 'HBase' require 'HBase'
# A HERE document used outputting shell command-line options. # See if there are args for this shell. If any, read and then strip from ARGV
@cmdline_help = <<HERE # so they don't go through to irb. Output shell 'usage' if user types '--help'
cmdline_help = <<HERE # HERE document output as shell usage
HBase Shell command-line options: HBase Shell command-line options:
format Formatter outputting results: console | html. Default: console. format Formatter for outputting results: console | html. Default: console
master HBase master shell should connect to: e.g --master=example:60000. master HBase master shell should connect to: e.g --master=example:60000
HERE HERE
# See if there are args for us. If any, read and then strip from ARGV
# so they don't go through to irb.
master = nil master = nil
@formatter = Formatter::Console.new(STDOUT) @formatter = Formatter::Console.new(STDOUT)
found = [] found = []
@ -53,7 +54,7 @@ for arg in ARGV
raise ArgumentError.new("Unsupported format " + arg) raise ArgumentError.new("Unsupported format " + arg)
end end
elsif arg == '-h' || arg == '--help' elsif arg == '-h' || arg == '--help'
puts @cmdline_help puts cmdline_help
exit exit
end end
end end
@ -64,54 +65,78 @@ end
# Setup the HBase module. Create a configuration. If a master, set it. # Setup the HBase module. Create a configuration. If a master, set it.
@configuration = HBaseConfiguration.new() @configuration = HBaseConfiguration.new()
@configuration.set("hbase.master", master) if master @configuration.set("hbase.master", master) if master
# Do lazy create of admin. If we are pointed at bad master, will hang # Do lazy create of admin because if we are pointed at bad master, it will hang
# shell on startup trying to connect. # shell on startup trying to connect.
@admin = nil @admin = nil
# Promote all HBase constants to be constants of this module. # Promote hbase constants to be constants of this module so can
for c in HBase.constants # be used bare as keys in 'create', 'alter', etc. To see constants
if c == c.upcase # in IRB, type 'Object.constants'.
eval("%s = \"%s\"" % [c, c]) def promoteConstants(constants)
# The constants to import are all in uppercase
for c in constants
if c == c.upcase
eval("%s = \"%s\"" % [c, c])
end
end end
end end
promoteConstants(HColumnDescriptor.constants)
promoteConstants(HTableDescriptor.constants)
# TODO: Add table options here. # Start of the hbase shell commands.
# General shell methods
# General Shell Commands: help and version
def help def help
# Format is command name and then short description # Output help. Help used to be a dictionary of name to short and long
# TODO: Can't do 'help COMMAND'. Interpreter runs help and then the command # descriptions emitted using Formatters but awkward getting it to show
commands = {'version' => 'Output HBase version', # nicely on console; instead use a HERE document. Means we can't
'list' => 'List all tables', # output help other than on console but not an issue at the moment.
# The help string in the below is carefully formatted to wrap nicely in # TODO: Add help to the commands themselves rather than keep it distinct
# our dumb Console formatter h = <<HERE
'create' => "Create table; pass a table name, a dictionary of \ HBASE SHELL COMMANDS:
specifications per column family, and optionally, named parameters of table \ create Create table; pass a table name, a dictionary of specifications per
options. Dictionaries are specified with curly-brackets, uppercase keys, a '=>'\ column family, and optionally, named parameters of table options.
key/value delimiter and then a value. Named parameters are like dict- \ Dictionaries are described below in the GENERAL NOTES section. Named
ionary elements with uppercase names and a '=>' delimiter. E.g. To \ parameters are like dictionary elements with uppercase names
create a table named 'table1' with an alternate maximum region size \ (constants) as keys and a '=>' key/value delimiter. Parameters are
and a single family named 'family1' with an alternate maximum cells: \ comma-delimited. For example, to create a table named 't1' with an
create 'table1' {NAME =>'family1', MAX_NUM_VERSIONS => 5}, REGION_SIZE => 12345", alternate maximum region size and a single family named 'f1' with an
'enable' => "Enable named table", alternate maximum number of cells, type:
'disable' => "Disable named table",
'exists' => "Does named table exist", create 't1' {NAME => 'f1', MAX_VERSIONS => 5}, REGION_SIZE => 123
}
@formatter.header(["HBase Shell Commands:"]) describe Describe the named table. Outputs the table and family descriptors
# TODO: Add general note that all names must be quoted and a general drop Drop the named table. Table must first be disabled
# description of dictionary so create doesn't have to be so long. disable Disable the named table: e.g. "disable 't1'<RETURN>"
for k, v in commands.sort enable Enable the named table
@formatter.row([k, v]) exists Does the named table exist? e.g. "exists 't1'<RETURN>"
end exit Exit the shell
@formatter.footer() list List all tables
version Output this HBase version
GENERAL NOTES:
Quote all names in the hbase shell such as table and column names. Don't
forget commas delimiting command parameters. Dictionaries of configuration used
in the creation and alteration of tables are ruby-style Hashes. They look like
this:
{ 'key1' => 'value1', 'key2' => 'value2', ...}
They are opened and closed with curley-braces. Key/values are delimited by
the '=>' character combination. Usually keys are predefined constants that
do not need to be quoted such as NAME, MAX_VERSIONS, MAX_LENGTH, TTL, etc.
Type 'Object.constants' to see a (messy) list of all constants in the
environment.
HERE
puts h
end end
def version def version
@formatter.header() # Output version.
@formatter.row(["Version: #{org.apache.hadoop.hbase.util.VersionInfo.getVersion()},\ puts "Version: #{org.apache.hadoop.hbase.util.VersionInfo.getVersion()},\
r#{org.apache.hadoop.hbase.util.VersionInfo.getRevision()},\ r#{org.apache.hadoop.hbase.util.VersionInfo.getRevision()},\
#{org.apache.hadoop.hbase.util.VersionInfo.getDate()}"]) #{org.apache.hadoop.hbase.util.VersionInfo.getDate()}"
@formatter.footer()
end end
# DDL # DDL
@ -136,6 +161,11 @@ def list
@admin = HBase::Admin.new(@configuration, @formatter) unless @admin @admin = HBase::Admin.new(@configuration, @formatter) unless @admin
@admin.list() @admin.list()
end end
def describe(table_name)
@admin = HBase::Admin.new(@configuration, @formatter) unless @admin
@admin.describe(table_name)
end
def enable(table_name) def enable(table_name)
@admin = HBase::Admin.new(@configuration, @formatter) unless @admin @admin = HBase::Admin.new(@configuration, @formatter) unless @admin
@ -171,9 +201,7 @@ def delete(table_name, row_key, *args)
end end
# Output a banner message that tells users where to go for help # Output a banner message that tells users where to go for help
# TODO: Test that we're in irb context. For now presume it. puts "HBase Shell; type 'help<RETURN>' for the list of supported HBase commands"
# TODO: Test that we are in shell context.
puts "HBase Shell; type 'hbase<RETURN>' for the list of supported HBase commands"
version version
require "irb" require "irb"