HBASE-836 Update thrift examples to work with changed IDL (HBASE-697)

git-svn-id: https://svn.apache.org/repos/asf/hadoop/hbase/trunk@690429 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael Stack 2008-08-29 23:58:10 +00:00
parent 19c87a375d
commit 1371c33e8d
2 changed files with 75 additions and 49 deletions

View File

@ -45,6 +45,8 @@ Release 0.18.0 - Unreleased
HBASE-826 Delete table followed by recreation results in honked table
HBASE-834 'Major' compactions and upper bound on files we compact at any
one time (Billy Pearson via Stack)
HBASE-836 Update thrift examples to work with changed IDL (HBASE-697)
(Toby White via Stack)
IMPROVEMENTS
HBASE-801 When a table haven't disable, shell could response in a "user

View File

@ -19,23 +19,30 @@
'''
# Instructions:
# 1. Run Thrift to generate python module HBase
# thrift -py ../../../src/java/org/apache/hadoop/hbase/thrift/Hbase.thrift
# 2. Rename gen-py folder to gen_py or just copy gen-py/HBase module into your project tree and change import string
# thrift --gen py ../../../src/java/org/apache/hadoop/hbase/thrift/Hbase.thrift
# 2. Copy gen-py/HBase module into your project tree and change import string
# Contributed by: Ivan Begtin (ibegtin@gmail.com, ibegtin@enotpoiskun.ru)
import sys
import time
from thrift import Thrift
from thrift.transport import TSocket, TTransport
from thrift.protocol import TBinaryProtocol
from gen_py.Hbase import *
from Hbase import ttypes
from Hbase.Hbase import Client, ColumnDescriptor, Mutation
def printRow(row, values):
print "row: %s, cols: " %(row)
for key in values.keys():
print '\t%s => %s' %(key, values[key])
def printVersions(row, versions):
print "row: " + row + ", values: ",
for cell in versions:
print cell.value + "; ",
print
def printEntry(entry):
printRow(entry.row, entry.columns)
def printRow(entry):
print "row: " + entry.row + ", cols",
for k in sorted(entry.columns):
print k + " => " + entry.columns[k].value,
print
# Make socket
@ -48,7 +55,7 @@ transport = TTransport.TBufferedTransport(transport)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
# Create a client to use the protocol encoder
client = Hbase.Client(protocol)
client = Client(protocol)
# Connect!
transport.open()
@ -62,19 +69,25 @@ print "scanning tables..."
for table in client.getTableNames():
print " found: %s" %(table)
if table == t:
print " disabling table: %s" %(t)
if client.isTableEnabled(table):
client.disableTable(table)
print " deleting table: %s" %(t)
client.deleteTable(table)
columns = []
col = Hbase.ColumnDescriptor()
col = ColumnDescriptor()
col.name = 'entry:'
col.maxVersions = 10
columns.append(col)
col = Hbase.ColumnDescriptor()
col = ColumnDescriptor()
col.name = 'unused:'
columns.append(col)
try:
client.createTable(t, columns)
except AlreadyExists, ae:
print "WARN: " + ae.message
cols = client.getColumnDescriptors(t)
for col_name in cols.keys():
@ -87,30 +100,33 @@ invalid = "foo-\xfc\xa1\xa1\xa1\xa1\xa1"
valid = "foo-\xE7\x94\x9F\xE3\x83\x93\xE3\x83\xBC\xE3\x83\xAB";
# non-utf8 is fine for data
client.put(t, "foo", "entry:foo", invalid)
mutations = [Mutation({"column":"entry:foo", "value":invalid})]
client.mutateRow(t, "foo", mutations)
# try empty strings
client.put(t, "", "entry:", "");
mutations = [Mutation({"column":"entry:", "value":""})]
client.mutateRow(t, "foo", mutations)
# this row name is valid utf8
client.put(t, valid, "entry:foo", valid)
mutations = [Mutation({"column":"entry:foo", "value":valid})]
client.mutateRow(t, "foo", mutations)
# non-utf8 is not allowed in row names
try:
client.put(t, invalid, "entry:foo", invalid)
mutations = [Mutation({"column":"entry:foo", "value":invalid})]
client.mutateRow(t, invalid, mutations)
except ttypes.IOError, e:
print 'expected exception: %s' %(e.message)
# Run a scanner on the rows we just created
print "Starting scanner..."
scanner = client.scannerOpen(t, "", ["entry:"])
scanner = client.scannerOpen(t, "", ["entry::"])
try:
while 1:
printEntry(client.scannerGet(scanner))
printRow(client.scannerGet(scanner))
except ttypes.NotFound, e:
print "Scanner finished"
#
# Run some operations on a bunch of rows.
#
@ -118,66 +134,74 @@ for e in range(100, 0, -1):
# format row keys as "00000" to "00100"
row = "%0.5d" % (e)
client.put(t, row, "unused:", "DELETE_ME");
printRow(row, client.getRow(t, row));
mutations = [Mutation({"column":"unused:", "value":"DELETE_ME"})]
client.mutateRow(t, row, mutations)
printRow(client.getRow(t, row))
client.deleteAllRow(t, row)
client.put(t, row, "entry:num", "0")
client.put(t, row, "entry:foo", "FOO")
printRow(row, client.getRow(t, row));
mutations = [Mutation({"column":"entry:num", "value":"0"}),
Mutation({"column":"entry:foo", "value":"FOO"})]
client.mutateRow(t, row, mutations)
printRow(client.getRow(t, row));
mutations = []
m = Hbase.Mutation()
m = Mutation()
m.column = "entry:foo"
m.isDelete = 1
mutations.append(m)
m = Hbase.Mutation()
m = Mutation()
m.column = "entry:num"
m.value = "-1"
mutations.append(m)
client.mutateRow(t, row, mutations)
printRow(row, client.getRow(t, row));
printRow(client.getRow(t, row));
client.put(t, row, "entry:num", str(e))
client.put(t, row, "entry:sqr", str((e*e)))
printRow(row, client.getRow(t, row));
mutations = [Mutation({"column":"entry:num", "value":str(e)}),
Mutation({"column":"entry:sqr", "value":str(e*e)})]
client.mutateRow(t, row, mutations)
printRow(client.getRow(t, row));
time.sleep(0.05)
mutations = []
m = Hbase.Mutation()
m = Mutation()
m.column = "entry:num"
m.value = "-999"
mutations.append(m)
m = Hbase.Mutation()
m = Mutation()
m.column = "entry:sqr"
m.isDelete = 1
mutations.append(m)
client.mutateRowTs(t, row, mutations, 1) # shouldn't override latest
printRow(row, client.getRow(t, row))
printRow(client.getRow(t, row))
versions = client.getVer(t, row, "entry:num", 10)
print "row: %s, values: " %(row)
for v in versions:
print "\t%s;" %(v)
print ""
printVersions(row, versions)
if len(versions) != 4:
print("FATAL: wrong # of versions")
sys.exit(-1)
try:
client.get(t, row, "entry:foo")
raise "shouldn't get here!"
except ttypes.NotFound, e:
print("FATAL: shouldn't get here")
sys.exit(-1)
except ttypes.NotFound:
pass
print ""
print
columns = client.getColumnDescriptors(t)
columnNames = []
for col2 in client.getColumnDescriptors(t):
print "column name is "+col2.name
print col2
columnNames.append(col2.name+":")
print "Starting scanner..."
scanner = client.scannerOpenWithStop(t, "00020", "00040", columns)
scanner = client.scannerOpenWithStop(t, "00020", "00040", columnNames)
try:
while 1:
printEntry(client.scannerGet(scanner))
except ttypes.NotFound, e:
printRow(client.scannerGet(scanner))
except ttypes.NotFound:
client.scannerClose(scanner)
print "Scanner finished"