2008-06-17 19:58:05 -04:00
|
|
|
# HBase ruby classes.
|
|
|
|
# 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.
|
|
|
|
include Java
|
2008-09-22 17:53:38 -04:00
|
|
|
include_class('java.lang.Integer') {|package,name| "J#{name}" }
|
2009-01-29 11:51:26 -05:00
|
|
|
include_class('java.lang.Long') {|package,name| "J#{name}" }
|
2008-09-22 17:53:38 -04:00
|
|
|
include_class('java.lang.Boolean') {|package,name| "J#{name}" }
|
|
|
|
|
2009-09-11 20:11:39 -04:00
|
|
|
import org.apache.hadoop.hbase.KeyValue
|
2008-06-17 19:58:05 -04:00
|
|
|
import org.apache.hadoop.hbase.client.HBaseAdmin
|
|
|
|
import org.apache.hadoop.hbase.client.HTable
|
2009-07-25 19:14:16 -04:00
|
|
|
import org.apache.hadoop.hbase.client.Get
|
|
|
|
import org.apache.hadoop.hbase.client.Put
|
2009-09-11 20:11:39 -04:00
|
|
|
import org.apache.hadoop.hbase.client.Scan
|
2009-07-09 23:25:20 -04:00
|
|
|
import org.apache.hadoop.hbase.client.Delete
|
2009-10-05 18:34:16 -04:00
|
|
|
import org.apache.hadoop.hbase.filter.FirstKeyOnlyFilter
|
2008-06-17 19:58:05 -04:00
|
|
|
import org.apache.hadoop.hbase.HConstants
|
2009-03-03 16:02:31 -05:00
|
|
|
import org.apache.hadoop.hbase.io.hfile.Compression
|
2008-06-17 19:58:05 -04:00
|
|
|
import org.apache.hadoop.hbase.HBaseConfiguration
|
|
|
|
import org.apache.hadoop.hbase.HColumnDescriptor
|
|
|
|
import org.apache.hadoop.hbase.HTableDescriptor
|
2008-06-18 18:24:34 -04:00
|
|
|
import org.apache.hadoop.hbase.util.Bytes
|
|
|
|
import org.apache.hadoop.hbase.util.Writables
|
2008-12-23 18:10:25 -05:00
|
|
|
import org.apache.hadoop.hbase.HRegionInfo
|
2009-06-16 23:46:12 -04:00
|
|
|
import org.apache.zookeeper.ZooKeeper
|
|
|
|
import org.apache.zookeeper.ZooKeeperMain
|
2008-06-17 19:58:05 -04:00
|
|
|
|
2008-06-12 02:00:35 -04:00
|
|
|
module HBase
|
2008-06-17 19:58:05 -04:00
|
|
|
COLUMN = "COLUMN"
|
|
|
|
COLUMNS = "COLUMNS"
|
|
|
|
TIMESTAMP = "TIMESTAMP"
|
|
|
|
NAME = HConstants::NAME
|
|
|
|
VERSIONS = HConstants::VERSIONS
|
2008-07-22 15:22:21 -04:00
|
|
|
IN_MEMORY = HConstants::IN_MEMORY
|
2008-06-17 19:58:05 -04:00
|
|
|
STOPROW = "STOPROW"
|
|
|
|
STARTROW = "STARTROW"
|
2008-06-18 18:24:34 -04:00
|
|
|
ENDROW = STOPROW
|
2008-06-17 19:58:05 -04:00
|
|
|
LIMIT = "LIMIT"
|
2008-09-22 17:16:21 -04:00
|
|
|
METHOD = "METHOD"
|
2009-02-26 12:55:27 -05:00
|
|
|
MAXLENGTH = "MAXLENGTH"
|
2009-09-11 20:11:39 -04:00
|
|
|
CACHE_BLOCKS = "CACHE_BLOCKS"
|
2008-06-17 19:58:05 -04:00
|
|
|
|
|
|
|
# Wrapper for org.apache.hadoop.hbase.client.HBaseAdmin
|
2008-06-12 02:00:35 -04:00
|
|
|
class Admin
|
|
|
|
def initialize(configuration, formatter)
|
|
|
|
@admin = HBaseAdmin.new(configuration)
|
2009-06-16 23:46:12 -04:00
|
|
|
connection = @admin.getConnection()
|
|
|
|
@zkWrapper = connection.getZooKeeperWrapper()
|
|
|
|
zk = @zkWrapper.getZooKeeper()
|
|
|
|
@zkMain = ZooKeeperMain.new(zk)
|
2008-06-12 02:00:35 -04:00
|
|
|
@formatter = formatter
|
|
|
|
end
|
|
|
|
|
|
|
|
def list
|
|
|
|
now = Time.now
|
|
|
|
@formatter.header()
|
|
|
|
for t in @admin.listTables()
|
|
|
|
@formatter.row([t.getNameAsString()])
|
|
|
|
end
|
|
|
|
@formatter.footer(now)
|
|
|
|
end
|
|
|
|
|
2008-06-13 01:50:00 -04:00
|
|
|
def describe(tableName)
|
|
|
|
now = Time.now
|
2009-04-22 15:14:15 -04:00
|
|
|
@formatter.header(["DESCRIPTION", "ENABLED"], [64])
|
2008-06-13 01:50:00 -04:00
|
|
|
found = false
|
2008-10-08 19:41:17 -04:00
|
|
|
tables = @admin.listTables().to_a
|
|
|
|
tables.push(HTableDescriptor::META_TABLEDESC, HTableDescriptor::ROOT_TABLEDESC)
|
|
|
|
for t in tables
|
2008-06-13 01:50:00 -04:00
|
|
|
if t.getNameAsString() == tableName
|
2009-04-22 15:14:15 -04:00
|
|
|
@formatter.row([t.to_s, "%s" % [@admin.isTableEnabled(tableName)]], true, [64])
|
2008-06-13 01:50:00 -04:00
|
|
|
found = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if not found
|
2008-06-17 19:58:05 -04:00
|
|
|
raise ArgumentError.new("Failed to find table named " + tableName)
|
2008-06-13 01:50:00 -04:00
|
|
|
end
|
|
|
|
@formatter.footer(now)
|
|
|
|
end
|
|
|
|
|
2008-06-12 02:00:35 -04:00
|
|
|
def exists(tableName)
|
|
|
|
now = Time.now
|
|
|
|
@formatter.header()
|
2008-06-17 19:58:05 -04:00
|
|
|
e = @admin.tableExists(tableName)
|
|
|
|
@formatter.row([e.to_s])
|
2008-06-12 02:00:35 -04:00
|
|
|
@formatter.footer(now)
|
|
|
|
end
|
|
|
|
|
2009-01-05 20:12:36 -05:00
|
|
|
def flush(tableNameOrRegionName)
|
|
|
|
now = Time.now
|
|
|
|
@formatter.header()
|
|
|
|
@admin.flush(tableNameOrRegionName)
|
|
|
|
@formatter.footer(now)
|
|
|
|
end
|
|
|
|
|
|
|
|
def compact(tableNameOrRegionName)
|
|
|
|
now = Time.now
|
|
|
|
@formatter.header()
|
|
|
|
@admin.compact(tableNameOrRegionName)
|
|
|
|
@formatter.footer(now)
|
|
|
|
end
|
|
|
|
|
|
|
|
def major_compact(tableNameOrRegionName)
|
|
|
|
now = Time.now
|
|
|
|
@formatter.header()
|
|
|
|
@admin.majorCompact(tableNameOrRegionName)
|
|
|
|
@formatter.footer(now)
|
|
|
|
end
|
|
|
|
|
|
|
|
def split(tableNameOrRegionName)
|
|
|
|
now = Time.now
|
|
|
|
@formatter.header()
|
|
|
|
@admin.split(tableNameOrRegionName)
|
|
|
|
@formatter.footer(now)
|
|
|
|
end
|
|
|
|
|
2008-06-12 02:00:35 -04:00
|
|
|
def enable(tableName)
|
|
|
|
# TODO: Need an isEnabled method
|
|
|
|
now = Time.now
|
|
|
|
@admin.enableTable(tableName)
|
|
|
|
@formatter.header()
|
|
|
|
@formatter.footer(now)
|
|
|
|
end
|
|
|
|
|
|
|
|
def disable(tableName)
|
|
|
|
# TODO: Need an isDisabled method
|
|
|
|
now = Time.now
|
|
|
|
@admin.disableTable(tableName)
|
|
|
|
@formatter.header()
|
|
|
|
@formatter.footer(now)
|
|
|
|
end
|
|
|
|
|
2008-12-23 18:10:25 -05:00
|
|
|
def enable_region(regionName)
|
|
|
|
online(regionName, false)
|
|
|
|
end
|
|
|
|
|
|
|
|
def disable_region(regionName)
|
|
|
|
online(regionName, true)
|
|
|
|
end
|
|
|
|
|
|
|
|
def online(regionName, onOrOff)
|
|
|
|
now = Time.now
|
|
|
|
meta = HTable.new(HConstants::META_TABLE_NAME)
|
|
|
|
bytes = Bytes.toBytes(regionName)
|
2009-07-25 19:14:16 -04:00
|
|
|
g = Get.new(bytes)
|
|
|
|
g.addColumn(HConstants::CATALOG_FAMILY,
|
|
|
|
HConstants::REGIONINFO_QUALIFIER)
|
|
|
|
hriBytes = meta.get(g).value()
|
2008-12-23 18:10:25 -05:00
|
|
|
hri = Writables.getWritable(hriBytes, HRegionInfo.new());
|
|
|
|
hri.setOffline(onOrOff)
|
2009-07-25 19:14:16 -04:00
|
|
|
put = Put.new(bytes)
|
|
|
|
put.add(HConstants::CATALOG_FAMILY,
|
|
|
|
HConstants::REGIONINFO_QUALIFIER, Writables.getBytes(hri))
|
|
|
|
meta.put(put);
|
2008-12-23 18:10:25 -05:00
|
|
|
@formatter.header()
|
|
|
|
@formatter.footer(now)
|
|
|
|
end
|
|
|
|
|
2008-06-12 02:00:35 -04:00
|
|
|
def drop(tableName)
|
|
|
|
now = Time.now
|
|
|
|
@formatter.header()
|
2008-08-08 14:53:30 -04:00
|
|
|
if @admin.isTableEnabled(tableName)
|
|
|
|
raise IOError.new("Table " + tableName + " is enabled. Disable it first")
|
|
|
|
else
|
|
|
|
@admin.deleteTable(tableName)
|
2009-08-17 13:34:19 -04:00
|
|
|
flush(HConstants::META_TABLE_NAME);
|
2009-06-25 19:14:29 -04:00
|
|
|
major_compact(HConstants::META_TABLE_NAME);
|
2008-08-08 14:53:30 -04:00
|
|
|
end
|
2008-06-12 02:00:35 -04:00
|
|
|
@formatter.footer(now)
|
|
|
|
end
|
|
|
|
|
2008-10-29 14:01:35 -04:00
|
|
|
def truncate(tableName)
|
|
|
|
now = Time.now
|
|
|
|
@formatter.header()
|
|
|
|
hTable = HTable.new(tableName)
|
|
|
|
tableDescription = hTable.getTableDescriptor()
|
|
|
|
puts 'Truncating ' + tableName + '; it may take a while'
|
|
|
|
puts 'Disabling table...'
|
|
|
|
disable(tableName)
|
|
|
|
puts 'Dropping table...'
|
|
|
|
drop(tableName)
|
|
|
|
puts 'Creating table...'
|
|
|
|
@admin.createTable(tableDescription)
|
|
|
|
@formatter.footer(now)
|
|
|
|
end
|
|
|
|
|
2008-06-17 19:58:05 -04:00
|
|
|
# Pass tablename and an array of Hashes
|
2008-06-12 02:00:35 -04:00
|
|
|
def create(tableName, args)
|
|
|
|
now = Time.now
|
|
|
|
# Pass table name and an array of Hashes. Later, test the last
|
|
|
|
# array to see if its table options rather than column family spec.
|
|
|
|
raise TypeError.new("Table name must be of type String") \
|
|
|
|
unless tableName.instance_of? String
|
|
|
|
# For now presume all the rest of the args are column family
|
|
|
|
# hash specifications. TODO: Add table options handling.
|
|
|
|
htd = HTableDescriptor.new(tableName)
|
|
|
|
for arg in args
|
2008-06-17 19:58:05 -04:00
|
|
|
if arg.instance_of? String
|
2009-09-17 12:17:59 -04:00
|
|
|
htd.addFamily(HColumnDescriptor.new(arg))
|
2008-06-17 19:58:05 -04:00
|
|
|
else
|
|
|
|
raise TypeError.new(arg.class.to_s + " of " + arg.to_s + " is not of Hash type") \
|
|
|
|
unless arg.instance_of? Hash
|
|
|
|
htd.addFamily(hcd(arg))
|
|
|
|
end
|
2008-06-12 02:00:35 -04:00
|
|
|
end
|
|
|
|
@admin.createTable(htd)
|
|
|
|
@formatter.header()
|
|
|
|
@formatter.footer(now)
|
|
|
|
end
|
2008-06-13 18:42:11 -04:00
|
|
|
|
|
|
|
def alter(tableName, args)
|
2008-09-22 17:16:21 -04:00
|
|
|
now = Time.now
|
2008-06-13 18:42:11 -04:00
|
|
|
raise TypeError.new("Table name must be of type String") \
|
|
|
|
unless tableName.instance_of? String
|
2008-09-22 17:16:21 -04:00
|
|
|
htd = @admin.getTableDescriptor(tableName.to_java_bytes)
|
|
|
|
method = args.delete(METHOD)
|
|
|
|
if method == "delete"
|
2009-09-17 12:17:59 -04:00
|
|
|
@admin.deleteColumn(tableName, args[NAME])
|
2009-01-29 11:51:26 -05:00
|
|
|
elsif method == "table_att"
|
|
|
|
args[MAX_FILESIZE]? htd.setMaxFileSize(JLong.valueOf(args[MAX_FILESIZE])) :
|
|
|
|
htd.setMaxFileSize(HTableDescriptor::DEFAULT_MAX_FILESIZE);
|
|
|
|
args[READONLY]? htd.setReadOnly(JBoolean.valueOf(args[READONLY])) :
|
|
|
|
htd.setReadOnly(HTableDescriptor::DEFAULT_READONLY);
|
2009-06-24 15:56:51 -04:00
|
|
|
args[MEMSTORE_FLUSHSIZE]?
|
|
|
|
htd.setMemStoreFlushSize(JLong.valueOf(args[MEMSTORE_FLUSHSIZE])) :
|
|
|
|
htd.setMemStoreFlushSize(HTableDescriptor::DEFAULT_MEMSTORE_FLUSH_SIZE);
|
2009-03-03 16:02:31 -05:00
|
|
|
@admin.modifyTable(tableName.to_java_bytes, htd)
|
2008-09-22 17:16:21 -04:00
|
|
|
else
|
|
|
|
descriptor = hcd(args)
|
|
|
|
if (htd.hasFamily(descriptor.getNameAsString().to_java_bytes))
|
|
|
|
@admin.modifyColumn(tableName, descriptor.getNameAsString(),
|
|
|
|
descriptor);
|
|
|
|
else
|
|
|
|
@admin.addColumn(tableName, descriptor);
|
|
|
|
end
|
|
|
|
end
|
2008-06-13 18:42:11 -04:00
|
|
|
@formatter.header()
|
|
|
|
@formatter.footer(now)
|
|
|
|
end
|
|
|
|
|
2008-12-23 18:10:25 -05:00
|
|
|
def close_region(regionName, server)
|
|
|
|
now = Time.now
|
|
|
|
s = nil
|
|
|
|
s = [server].to_java if server
|
|
|
|
@admin.closeRegion(regionName, s)
|
|
|
|
@formatter.header()
|
|
|
|
@formatter.footer(now)
|
|
|
|
end
|
|
|
|
|
2009-02-03 05:22:06 -05:00
|
|
|
def shutdown()
|
|
|
|
@admin.shutdown()
|
|
|
|
end
|
|
|
|
|
2009-05-28 01:48:52 -04:00
|
|
|
def status(format)
|
|
|
|
status = @admin.getClusterStatus()
|
|
|
|
if format != nil and format == "detailed"
|
2009-06-19 17:09:22 -04:00
|
|
|
puts("version %s" % [ status.getHBaseVersion() ])
|
2009-08-04 01:09:27 -04:00
|
|
|
# Put regions in transition first because usually empty
|
|
|
|
puts("%d regionsInTransition" % status.getRegionsInTransition().size())
|
|
|
|
for k, v in status.getRegionsInTransition()
|
|
|
|
puts(" %s" % [v])
|
|
|
|
end
|
2009-05-28 01:48:52 -04:00
|
|
|
puts("%d live servers" % [ status.getServers() ])
|
|
|
|
for server in status.getServerInfo()
|
|
|
|
puts(" %s:%d %d" % \
|
|
|
|
[ server.getServerAddress().getHostname(), \
|
|
|
|
server.getServerAddress().getPort(), server.getStartCode() ])
|
|
|
|
puts(" %s" % [ server.getLoad().toString() ])
|
|
|
|
for region in server.getLoad().getRegionsLoad()
|
|
|
|
puts(" %s" % [ region.getNameAsString() ])
|
|
|
|
puts(" %s" % [ region.toString() ])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
puts("%d dead servers" % [ status.getDeadServers() ])
|
|
|
|
for server in status.getDeadServerNames()
|
|
|
|
puts(" %s" % [ server ])
|
|
|
|
end
|
2009-05-28 13:34:51 -04:00
|
|
|
elsif format != nil and format == "simple"
|
|
|
|
puts("%d live servers" % [ status.getServers() ])
|
|
|
|
for server in status.getServerInfo()
|
|
|
|
puts(" %s:%d %d" % \
|
|
|
|
[ server.getServerAddress().getHostname(), \
|
|
|
|
server.getServerAddress().getPort(), server.getStartCode() ])
|
|
|
|
puts(" %s" % [ server.getLoad().toString() ])
|
|
|
|
end
|
|
|
|
puts("%d dead servers" % [ status.getDeadServers() ])
|
|
|
|
for server in status.getDeadServerNames()
|
|
|
|
puts(" %s" % [ server ])
|
|
|
|
end
|
2009-05-28 01:48:52 -04:00
|
|
|
else
|
|
|
|
puts("%d servers, %d dead, %.4f average load" % \
|
|
|
|
[ status.getServers(), status.getDeadServers(), \
|
|
|
|
status.getAverageLoad()])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-06-13 18:42:11 -04:00
|
|
|
def hcd(arg)
|
|
|
|
# Return a new HColumnDescriptor made of passed args
|
|
|
|
# TODO: This is brittle code.
|
|
|
|
# Here is current HCD constructor:
|
2009-03-03 16:02:31 -05:00
|
|
|
# public HColumnDescriptor(final byte [] familyName, final int maxVersions,
|
|
|
|
# final String compression, final boolean inMemory,
|
|
|
|
# final boolean blockCacheEnabled, final int blocksize,
|
|
|
|
# final int maxValueLength,
|
|
|
|
# final int timeToLive, final boolean bloomFilter) {
|
2008-06-13 18:42:11 -04:00
|
|
|
name = arg[NAME]
|
|
|
|
raise ArgumentError.new("Column family " + arg + " must have a name") \
|
|
|
|
unless name
|
|
|
|
# TODO: What encoding are Strings in jruby?
|
|
|
|
return HColumnDescriptor.new(name.to_java_bytes,
|
|
|
|
# JRuby uses longs for ints. Need to convert. Also constants are String
|
2008-09-22 17:53:38 -04:00
|
|
|
arg[VERSIONS]? JInteger.new(arg[VERSIONS]): HColumnDescriptor::DEFAULT_VERSIONS,
|
2009-03-03 16:02:31 -05:00
|
|
|
arg[HColumnDescriptor::COMPRESSION]? arg[HColumnDescriptor::COMPRESSION]: HColumnDescriptor::DEFAULT_COMPRESSION,
|
2008-09-22 17:53:38 -04:00
|
|
|
arg[IN_MEMORY]? JBoolean.valueOf(arg[IN_MEMORY]): HColumnDescriptor::DEFAULT_IN_MEMORY,
|
|
|
|
arg[HColumnDescriptor::BLOCKCACHE]? JBoolean.valueOf(arg[HColumnDescriptor::BLOCKCACHE]): HColumnDescriptor::DEFAULT_BLOCKCACHE,
|
2009-03-03 16:02:31 -05:00
|
|
|
arg[HColumnDescriptor::BLOCKSIZE]? JInteger.valueOf(arg[HColumnDescriptor::BLOCKSIZE]): HColumnDescriptor::DEFAULT_BLOCKSIZE,
|
2008-09-22 17:53:38 -04:00
|
|
|
arg[HColumnDescriptor::TTL]? JInteger.new(arg[HColumnDescriptor::TTL]): HColumnDescriptor::DEFAULT_TTL,
|
|
|
|
arg[HColumnDescriptor::BLOOMFILTER]? JBoolean.valueOf(arg[HColumnDescriptor::BLOOMFILTER]): HColumnDescriptor::DEFAULT_BLOOMFILTER)
|
2008-06-13 18:42:11 -04:00
|
|
|
end
|
2009-06-16 23:46:12 -04:00
|
|
|
|
|
|
|
def zk(args)
|
|
|
|
line = args.join(' ')
|
|
|
|
line = 'help' if line.empty?
|
|
|
|
@zkMain.executeLine(line)
|
|
|
|
end
|
|
|
|
|
|
|
|
def zk_dump
|
|
|
|
puts @zkWrapper.dump
|
|
|
|
end
|
2008-06-12 02:00:35 -04:00
|
|
|
end
|
|
|
|
|
2008-06-17 19:58:05 -04:00
|
|
|
# Wrapper for org.apache.hadoop.hbase.client.HTable
|
2008-06-12 02:00:35 -04:00
|
|
|
class Table
|
2008-06-17 19:58:05 -04:00
|
|
|
def initialize(configuration, tableName, formatter)
|
|
|
|
@table = HTable.new(configuration, tableName)
|
|
|
|
@formatter = formatter
|
|
|
|
end
|
|
|
|
|
|
|
|
# Delete a cell
|
2008-07-09 17:39:45 -04:00
|
|
|
def delete(row, column, timestamp = HConstants::LATEST_TIMESTAMP)
|
2008-06-17 19:58:05 -04:00
|
|
|
now = Time.now
|
2009-07-09 23:25:20 -04:00
|
|
|
d = Delete.new(row.to_java_bytes, timestamp, nil)
|
2009-09-17 12:17:59 -04:00
|
|
|
split = KeyValue.parseColumn(column.to_java_bytes)
|
2009-11-22 23:25:24 -05:00
|
|
|
d.deleteColumn(split[0], split.length > 1 ? split[1] : nil, timestamp)
|
2009-07-09 23:25:20 -04:00
|
|
|
@table.delete(d)
|
2008-06-17 19:58:05 -04:00
|
|
|
@formatter.header()
|
|
|
|
@formatter.footer(now)
|
|
|
|
end
|
|
|
|
|
|
|
|
def deleteall(row, column = nil, timestamp = HConstants::LATEST_TIMESTAMP)
|
|
|
|
now = Time.now
|
2009-07-09 23:25:20 -04:00
|
|
|
d = Delete.new(row.to_java_bytes, timestamp, nil)
|
2009-11-22 23:25:24 -05:00
|
|
|
if column != nil
|
|
|
|
split = KeyValue.parseColumn(column.to_java_bytes)
|
|
|
|
d.deleteColumns(split[0], split.length > 1 ? split[1] : nil, timestamp)
|
|
|
|
end
|
2009-07-09 23:25:20 -04:00
|
|
|
@table.delete(d)
|
2008-06-17 19:58:05 -04:00
|
|
|
@formatter.header()
|
|
|
|
@formatter.footer(now)
|
|
|
|
end
|
|
|
|
|
2008-06-18 18:24:34 -04:00
|
|
|
def getAllColumns
|
2008-08-29 19:36:02 -04:00
|
|
|
htd = @table.getTableDescriptor()
|
2008-06-18 18:24:34 -04:00
|
|
|
result = []
|
|
|
|
for f in htd.getFamilies()
|
|
|
|
n = f.getNameAsString()
|
|
|
|
n << ':'
|
|
|
|
result << n
|
|
|
|
end
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2008-10-08 19:45:30 -04:00
|
|
|
def scan(args = {})
|
2008-06-17 19:58:05 -04:00
|
|
|
now = Time.now
|
|
|
|
limit = -1
|
2009-02-26 12:55:27 -05:00
|
|
|
maxlength = -1
|
2008-10-08 19:45:30 -04:00
|
|
|
if args != nil and args.length > 0
|
2008-06-17 19:58:05 -04:00
|
|
|
limit = args["LIMIT"] || -1
|
2009-02-26 12:55:27 -05:00
|
|
|
maxlength = args["MAXLENGTH"] || -1
|
2008-06-17 19:58:05 -04:00
|
|
|
filter = args["FILTER"] || nil
|
|
|
|
startrow = args["STARTROW"] || ""
|
|
|
|
stoprow = args["STOPROW"] || nil
|
2009-09-11 20:11:39 -04:00
|
|
|
timestamp = args["TIMESTAMP"] || nil
|
2008-10-08 19:45:30 -04:00
|
|
|
columns = args["COLUMNS"] || getAllColumns()
|
2009-09-11 20:11:39 -04:00
|
|
|
cache = args["CACHE_BLOCKS"] || true
|
2009-11-13 02:04:11 -05:00
|
|
|
versions = args["VERSIONS"] || 1
|
2008-10-08 19:45:30 -04:00
|
|
|
|
|
|
|
if columns.class == String
|
|
|
|
columns = [columns]
|
|
|
|
elsif columns.class != Array
|
|
|
|
raise ArgumentError.new("COLUMNS must be specified as a String or an Array")
|
|
|
|
end
|
2008-06-17 19:58:05 -04:00
|
|
|
if stoprow
|
2009-09-11 20:11:39 -04:00
|
|
|
scan = Scan.new(startrow.to_java_bytes, stoprow.to_java_bytes)
|
2008-06-17 19:58:05 -04:00
|
|
|
else
|
2009-09-11 20:11:39 -04:00
|
|
|
scan = Scan.new(startrow.to_java_bytes)
|
|
|
|
end
|
|
|
|
for c in columns
|
2009-12-14 01:20:21 -05:00
|
|
|
scan.addColumns(c)
|
2009-09-11 20:11:39 -04:00
|
|
|
end
|
|
|
|
if filter != nil
|
|
|
|
scan.setFilter(filter)
|
2008-06-17 19:58:05 -04:00
|
|
|
end
|
2009-09-11 20:11:39 -04:00
|
|
|
if timestamp != nil
|
|
|
|
scan.setTimeStamp(timestamp)
|
|
|
|
end
|
|
|
|
scan.setCacheBlocks(cache)
|
2009-11-13 02:04:11 -05:00
|
|
|
scan.setMaxVersions(versions) if versions > 1
|
2008-10-08 19:45:30 -04:00
|
|
|
else
|
2009-09-11 20:11:39 -04:00
|
|
|
scan = Scan.new()
|
2008-10-08 19:45:30 -04:00
|
|
|
end
|
2009-09-11 20:11:39 -04:00
|
|
|
s = @table.getScanner(scan)
|
2008-06-17 19:58:05 -04:00
|
|
|
count = 0
|
2008-06-18 18:24:34 -04:00
|
|
|
@formatter.header(["ROW", "COLUMN+CELL"])
|
2008-06-17 19:58:05 -04:00
|
|
|
i = s.iterator()
|
|
|
|
while i.hasNext()
|
2009-09-17 12:17:59 -04:00
|
|
|
r = i.next()
|
2008-06-17 19:58:05 -04:00
|
|
|
row = String.from_java_bytes r.getRow()
|
2009-09-11 20:11:39 -04:00
|
|
|
if limit != -1 and count >= limit
|
|
|
|
break
|
|
|
|
end
|
2009-09-17 12:17:59 -04:00
|
|
|
for kv in r.list
|
|
|
|
family = String.from_java_bytes kv.getFamily()
|
|
|
|
qualifier = String.from_java_bytes kv.getQualifier()
|
|
|
|
column = family + ':' + qualifier
|
|
|
|
cell = toString(column, kv, maxlength)
|
2009-11-14 17:25:11 -05:00
|
|
|
@formatter.row([row, "column=%s, %s" % [column, cell]])
|
2008-06-17 19:58:05 -04:00
|
|
|
end
|
2009-12-08 18:55:36 -05:00
|
|
|
count += 1
|
2008-06-17 19:58:05 -04:00
|
|
|
end
|
2009-09-11 20:11:39 -04:00
|
|
|
@formatter.footer(now, count)
|
2008-06-17 19:58:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def put(row, column, value, timestamp = nil)
|
|
|
|
now = Time.now
|
2009-09-17 12:17:59 -04:00
|
|
|
p = nil
|
2008-06-17 19:58:05 -04:00
|
|
|
if timestamp
|
2009-09-17 12:17:59 -04:00
|
|
|
p = Put.new(row.to_java_bytes, timestamp)
|
|
|
|
else
|
|
|
|
p = Put.new(row.to_java_bytes)
|
|
|
|
end
|
|
|
|
split = KeyValue.parseColumn(column.to_java_bytes)
|
|
|
|
if split.length > 1
|
|
|
|
p.add(split[0], split[1], value.to_java_bytes)
|
2008-06-17 19:58:05 -04:00
|
|
|
else
|
2009-09-17 12:17:59 -04:00
|
|
|
p.add(split[0], nil, value.to_java_bytes)
|
2008-06-17 19:58:05 -04:00
|
|
|
end
|
2009-09-17 12:17:59 -04:00
|
|
|
@table.put(p)
|
2008-06-17 19:58:05 -04:00
|
|
|
@formatter.header()
|
|
|
|
@formatter.footer(now)
|
|
|
|
end
|
2008-06-18 18:24:34 -04:00
|
|
|
|
|
|
|
def isMetaTable()
|
|
|
|
tn = @table.getTableName()
|
2008-08-17 18:13:13 -04:00
|
|
|
return Bytes.equals(tn, HConstants::META_TABLE_NAME) ||
|
|
|
|
Bytes.equals(tn, HConstants::ROOT_TABLE_NAME)
|
2008-06-18 18:24:34 -04:00
|
|
|
end
|
|
|
|
|
2009-09-17 12:17:59 -04:00
|
|
|
# Make a String of the passed kv
|
2008-06-18 18:24:34 -04:00
|
|
|
# Intercept cells whose format we know such as the info:regioninfo in .META.
|
2009-09-17 12:17:59 -04:00
|
|
|
def toString(column, kv, maxlength)
|
2008-06-18 18:24:34 -04:00
|
|
|
if isMetaTable()
|
|
|
|
if column == 'info:regioninfo'
|
2009-09-17 12:17:59 -04:00
|
|
|
hri = Writables.getHRegionInfoOrNull(kv.getValue())
|
|
|
|
return "timestamp=%d, value=%s" % [kv.getTimestamp(), hri.toString()]
|
2008-06-18 18:24:34 -04:00
|
|
|
elsif column == 'info:serverstartcode'
|
2009-09-17 12:17:59 -04:00
|
|
|
return "timestamp=%d, value=%s" % [kv.getTimestamp(), \
|
|
|
|
Bytes.toLong(kv.getValue())]
|
2008-06-18 18:24:34 -04:00
|
|
|
end
|
|
|
|
end
|
2009-11-14 17:25:11 -05:00
|
|
|
val = "timestamp=" + kv.getTimestamp().to_s + ", value=" + Bytes.toStringBinary(kv.getValue())
|
2009-02-26 12:55:27 -05:00
|
|
|
maxlength != -1 ? val[0, maxlength] : val
|
2008-06-18 18:24:34 -04:00
|
|
|
end
|
2008-06-17 19:58:05 -04:00
|
|
|
|
|
|
|
# Get from table
|
|
|
|
def get(row, args = {})
|
|
|
|
now = Time.now
|
|
|
|
result = nil
|
2009-02-26 12:55:27 -05:00
|
|
|
if args == nil or args.length == 0 or (args.length == 1 and args[MAXLENGTH] != nil)
|
2009-09-17 12:17:59 -04:00
|
|
|
get = Get.new(row.to_java_bytes)
|
2008-06-17 19:58:05 -04:00
|
|
|
else
|
|
|
|
# Its a hash.
|
|
|
|
columns = args[COLUMN]
|
|
|
|
if columns == nil
|
|
|
|
# Maybe they used the COLUMNS key
|
|
|
|
columns = args[COLUMNS]
|
|
|
|
end
|
|
|
|
if columns == nil
|
|
|
|
# May have passed TIMESTAMP and row only; wants all columns from ts.
|
|
|
|
ts = args[TIMESTAMP]
|
|
|
|
if not ts
|
|
|
|
raise ArgumentError.new("Failed parse of " + args + ", " + args.class)
|
|
|
|
end
|
2009-09-17 12:17:59 -04:00
|
|
|
get = Get.new(row.to_java_bytes, ts)
|
2008-06-17 19:58:05 -04:00
|
|
|
else
|
2009-09-17 12:17:59 -04:00
|
|
|
get = Get.new(row.to_java_bytes)
|
2008-06-17 19:58:05 -04:00
|
|
|
# Columns are non-nil
|
|
|
|
if columns.class == String
|
|
|
|
# Single column
|
2009-09-17 12:17:59 -04:00
|
|
|
split = KeyValue.parseColumn(columns.to_java_bytes)
|
|
|
|
if (split.length > 1)
|
|
|
|
get.addColumn(split[0], split[1])
|
|
|
|
else
|
|
|
|
get.addFamily(split[0])
|
|
|
|
end
|
2008-06-17 19:58:05 -04:00
|
|
|
elsif columns.class == Array
|
2009-09-17 12:17:59 -04:00
|
|
|
for column in columns
|
|
|
|
split = KeyValue.parseColumn(columns.to_java_bytes)
|
|
|
|
if (split.length > 1)
|
|
|
|
get.addColumn(split[0], split[1])
|
|
|
|
else
|
|
|
|
get.addFamily(split[0])
|
|
|
|
end
|
|
|
|
end
|
2008-06-17 19:58:05 -04:00
|
|
|
else
|
|
|
|
raise ArgumentError.new("Failed parse column argument type " +
|
|
|
|
args + ", " + args.class)
|
|
|
|
end
|
2009-09-17 12:17:59 -04:00
|
|
|
get.setMaxVersions(args[VERSIONS] ? args[VERSIONS] : 1)
|
|
|
|
if args[TIMESTAMP]
|
|
|
|
get.setTimeStamp(args[TIMESTAMP])
|
|
|
|
end
|
2008-06-17 19:58:05 -04:00
|
|
|
end
|
|
|
|
end
|
2009-09-17 12:17:59 -04:00
|
|
|
result = @table.get(get)
|
2008-06-17 19:58:05 -04:00
|
|
|
# Print out results. Result can be Cell or RowResult.
|
2009-02-26 12:55:27 -05:00
|
|
|
maxlength = args[MAXLENGTH] || -1
|
2009-09-17 12:17:59 -04:00
|
|
|
@formatter.header(["COLUMN", "CELL"])
|
|
|
|
if !result.isEmpty()
|
|
|
|
for kv in result.list()
|
|
|
|
family = String.from_java_bytes kv.getFamily()
|
|
|
|
qualifier = String.from_java_bytes kv.getQualifier()
|
|
|
|
column = family + ':' + qualifier
|
|
|
|
@formatter.row([column, toString(column, kv, maxlength)])
|
2008-06-17 19:58:05 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
@formatter.footer(now)
|
|
|
|
end
|
2008-08-12 14:30:33 -04:00
|
|
|
|
|
|
|
def count(interval = 1000)
|
|
|
|
now = Time.now
|
2009-09-11 20:11:39 -04:00
|
|
|
scan = Scan.new()
|
|
|
|
scan.setCacheBlocks(false)
|
2009-10-09 17:55:44 -04:00
|
|
|
# We can safely set scanner caching with the first key only filter
|
|
|
|
scan.setCaching(10)
|
2009-10-05 18:34:16 -04:00
|
|
|
scan.setFilter(FirstKeyOnlyFilter.new())
|
2009-09-11 20:11:39 -04:00
|
|
|
s = @table.getScanner(scan)
|
2008-08-12 14:30:33 -04:00
|
|
|
count = 0
|
|
|
|
i = s.iterator()
|
2008-12-31 19:36:27 -05:00
|
|
|
@formatter.header()
|
2008-08-12 14:30:33 -04:00
|
|
|
while i.hasNext()
|
|
|
|
r = i.next()
|
|
|
|
count += 1
|
|
|
|
if count % interval == 0
|
|
|
|
@formatter.row(["Current count: " + count.to_s + ", row: " + \
|
|
|
|
(String.from_java_bytes r.getRow())])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
@formatter.footer(now, count)
|
|
|
|
end
|
|
|
|
|
2008-06-17 19:58:05 -04:00
|
|
|
end
|
|
|
|
|
2008-06-18 18:24:34 -04:00
|
|
|
# Testing. To run this test, there needs to be an hbase cluster up and
|
|
|
|
# running. Then do: ${HBASE_HOME}/bin/hbase org.jruby.Main bin/HBase.rb
|
2008-06-17 19:58:05 -04:00
|
|
|
if $0 == __FILE__
|
|
|
|
# Add this directory to LOAD_PATH; presumption is that Formatter module
|
|
|
|
# sits beside this one. Then load it up.
|
|
|
|
$LOAD_PATH.unshift File.dirname($PROGRAM_NAME)
|
|
|
|
require 'Formatter'
|
|
|
|
# Make a console formatter
|
|
|
|
formatter = Formatter::Console.new(STDOUT)
|
|
|
|
# Now add in java and hbase classes
|
|
|
|
configuration = HBaseConfiguration.new()
|
|
|
|
admin = Admin.new(configuration, formatter)
|
2008-06-18 18:24:34 -04:00
|
|
|
# Drop old table. If it does not exist, get an exception. Catch and
|
|
|
|
# continue
|
2008-06-17 19:58:05 -04:00
|
|
|
TESTTABLE = "HBase_rb_testtable"
|
|
|
|
begin
|
|
|
|
admin.disable(TESTTABLE)
|
|
|
|
admin.drop(TESTTABLE)
|
|
|
|
rescue org.apache.hadoop.hbase.TableNotFoundException
|
|
|
|
# Just suppress not found exception
|
|
|
|
end
|
|
|
|
admin.create(TESTTABLE, [{NAME => 'x', VERSIONS => 5}])
|
|
|
|
# Presume it exists. If it doesn't, next items will fail.
|
|
|
|
table = Table.new(configuration, TESTTABLE, formatter)
|
|
|
|
for i in 1..10
|
2008-06-18 18:24:34 -04:00
|
|
|
table.put('x%d' % i, 'x:%d' % i, 'x%d' % i)
|
2008-06-17 19:58:05 -04:00
|
|
|
end
|
2009-09-17 12:17:59 -04:00
|
|
|
table.get('x1', {COLUMNS => 'x:1'})
|
2008-06-17 19:58:05 -04:00
|
|
|
if formatter.rowCount() != 1
|
|
|
|
raise IOError.new("Failed first put")
|
|
|
|
end
|
2009-09-17 12:17:59 -04:00
|
|
|
table.scan({COLUMNS => ['x:']})
|
2008-06-18 18:24:34 -04:00
|
|
|
if formatter.rowCount() != 10
|
|
|
|
raise IOError.new("Failed scan of expected 10 rows")
|
|
|
|
end
|
|
|
|
# Verify that limit works.
|
2009-09-17 12:17:59 -04:00
|
|
|
table.scan({COLUMNS => ['x:'], LIMIT => 4})
|
2008-06-18 18:24:34 -04:00
|
|
|
if formatter.rowCount() != 3
|
|
|
|
raise IOError.new("Failed scan of expected 3 rows")
|
|
|
|
end
|
|
|
|
# Should only be two rows if we start at 8 (Row x10 sorts beside x1).
|
2009-09-17 12:17:59 -04:00
|
|
|
table.scan({COLUMNS => ['x:'], STARTROW => 'x8', LIMIT => 3})
|
2008-06-18 18:24:34 -04:00
|
|
|
if formatter.rowCount() != 2
|
|
|
|
raise IOError.new("Failed scan of expected 2 rows")
|
|
|
|
end
|
|
|
|
# Scan between two rows
|
2009-09-17 12:17:59 -04:00
|
|
|
table.scan({COLUMNS => ['x:'], STARTROW => 'x5', ENDROW => 'x8'})
|
2008-06-18 18:24:34 -04:00
|
|
|
if formatter.rowCount() != 3
|
|
|
|
raise IOError.new("Failed endrow test")
|
|
|
|
end
|
2008-07-09 17:39:45 -04:00
|
|
|
# Verify that delete works
|
|
|
|
table.delete('x1', 'x:1');
|
2009-09-17 12:17:59 -04:00
|
|
|
table.scan({COLUMNS => ['x:1']})
|
2008-07-09 17:39:45 -04:00
|
|
|
scan1 = formatter.rowCount()
|
2009-09-17 12:17:59 -04:00
|
|
|
table.scan({COLUMNS => ['x:']})
|
2008-07-09 17:39:45 -04:00
|
|
|
scan2 = formatter.rowCount()
|
|
|
|
if scan1 != 0 or scan2 != 9
|
|
|
|
raise IOError.new("Failed delete test")
|
|
|
|
end
|
|
|
|
# Verify that deletall works
|
|
|
|
table.put('x2', 'x:1', 'x:1')
|
|
|
|
table.deleteall('x2')
|
2009-09-17 12:17:59 -04:00
|
|
|
table.scan({COLUMNS => ['x:2']})
|
2008-07-09 17:39:45 -04:00
|
|
|
scan1 = formatter.rowCount()
|
2009-09-17 12:17:59 -04:00
|
|
|
table.scan({COLUMNS => ['x:']})
|
2008-07-09 17:39:45 -04:00
|
|
|
scan2 = formatter.rowCount()
|
|
|
|
if scan1 != 0 or scan2 != 8
|
|
|
|
raise IOError.new("Failed deleteall test")
|
|
|
|
end
|
2008-06-17 19:58:05 -04:00
|
|
|
admin.disable(TESTTABLE)
|
|
|
|
admin.drop(TESTTABLE)
|
2008-06-12 02:00:35 -04:00
|
|
|
end
|
|
|
|
end
|