Skip to content

Instantly share code, notes, and snippets.

@busbey
Created November 19, 2015 14:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save busbey/e850b098479517e43a2f to your computer and use it in GitHub Desktop.
Save busbey/e850b098479517e43a2f to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# Copyright 2015 Cloudera. all rights reserved
#
# Cloudera, Inc. licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# test thrift server over HTTP
#
# presumes thrift python lib is installed
# presumes you have access to hbase thrift binding (e.g. you are in the gen-py dir)
#
# usage:
# ./test_thrift1_over_http.py http host-running-thrift1.example.com
# to check ssl for the http transport:
# ./test_thrift1_over_http.py https host-running-thrift1.example.com
import sys
from thrift import Thrift
from thrift.transport import THttpClient
from thrift.protocol import TBinaryProtocol
from hbase import Hbase
from hbase.ttypes import ColumnDescriptor
from hbase.ttypes import Mutation
from hbase.ttypes import IOError as HBaseIOError
print "[INFO] setup connection"
transport = THttpClient.THttpClient("{0}://{1}:{2}".format("https" if sys.arv[1] == "https" else "http", sys.argv[2], 9090))
client = Hbase.Client(TBinaryProtocol.TBinaryProtocol(transport))
table='some_table'
print "[INFO] start client"
transport.open()
print "[INFO] list the current tables"
print client.getTableNames()
print "[INFO] create a table, place some data"
client.createTable(table, [ColumnDescriptor(name ='family1:')])
client.mutateRow(table, 'row1', [Mutation(column = 'family1:cq1', value = 'foo'), Mutation(column = 'family1:cq2', value = 'foo')], {})
client.mutateRow(table, 'row2', [Mutation(column = 'family1:cq1', value = 'bar'), Mutation(column = 'family1:cq2', value = 'bar')], {})
client.mutateRow(table, 'row3', [Mutation(column = 'family1:cq1', value = 'foo'), Mutation(column = 'family1:cq2', value = 'foo')], {})
client.mutateRow(table, 'row4', [Mutation(column = 'family1:cq1', value = 'bar'), Mutation(column = 'family1:cq2', value = 'bar')], {})
print "[INFO] scan"
scan_id = client.scannerOpen(table, 'row1', [], {})
for row in client.scannerGetList(scan_id, 25):
print row
client.scannerClose(scan_id)
print "[INFO] get"
for row in client.getRow(table, 'row3', {}):
print row
print "[INFO] clean up"
client.disableTable(table)
client.deleteTable(table)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment