| #!/usr/bin/env python |
| """ |
| Licensed to the Apache Software Foundation (ASF) under one |
| or more contributor license agreements. See the NOTICE file |
| distributed with this work for additional information |
| regarding copyright ownership. The ASF 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. |
| """ |
| |
| # Instructions: |
| # 1. Run Thrift to generate the python module hbase |
| # thrift --gen py ../../../../../hbase-thrift/src/main/resources/org/apache/hadoop \ |
| # /hbase/thrift2/hbase.thrift |
| # 2. Create a directory of your choosing that contains: |
| # a. This file (DemoClient.py). |
| # b. The directory gen_py/hbase (generated by instruction step 1). |
| # 3. pip install thrift==0.14.1 |
| # 4. Create a table call "example", with a family called "family1" using the hbase shell. |
| # 5. Start the hbase thrift2 server |
| # bin/hbase thrift2 start |
| # 6. Execute {python DemoClient.py}. |
| |
| import sys |
| from thrift.transport import TTransport |
| from thrift.transport import TSocket |
| from thrift.protocol import TBinaryProtocol |
| from gen_py.hbase import THBaseService |
| from gen_py.hbase import ttypes |
| |
| |
| def run(host, port, framed=False): |
| |
| socket = TSocket.TSocket(host, port) |
| if framed: |
| transport = TTransport.TFramedTransport(socket) |
| else: |
| transport = TTransport.TBufferedTransport(socket) |
| protocol = TBinaryProtocol.TBinaryProtocol(transport) |
| client = THBaseService.Client(protocol) |
| |
| transport.open() |
| |
| # Check Thrift Server Type |
| serverType = client.getThriftServerType() |
| if serverType != ttypes.TThriftServerType.TWO: |
| raise RuntimeError( |
| f"Mismatch between client and server, server type is {serverType}" |
| ) |
| |
| table = "example".encode() |
| |
| t_column_values = ttypes.TColumnValue( |
| family="family1".encode(), |
| qualifier="qualifier1".encode(), |
| value="value1".encode(), |
| ) |
| put = ttypes.TPut(row="row1".encode(), columnValues=[t_column_values]) |
| print("\nPutting:", put) |
| client.put(table, put) |
| |
| get = ttypes.TGet(row="row1".encode()) |
| print("\nGetting:", get) |
| result = client.get(table, get) |
| |
| print("\nResult:", result) |
| print(f"row = {result.row.decode()}") |
| for column_value in result.columnValues: |
| print(f"family = {column_value.family.decode()}") |
| print(f"qualifier = {column_value.qualifier.decode()}") |
| print(f"value = {column_value.value.decode()}") |
| print(f"timestamp = {column_value.timestamp}") |
| |
| transport.close() |
| |
| |
| if __name__ == "__main__": |
| print("Thrift2 Demo") |
| print("Usage: DemoClient [host=localhost] [port=9090]") |
| print( |
| "This demo assumes you have a table called " |
| '"example" with a column family called "family1"' |
| ) |
| |
| default_host = "localhost" |
| default_port = 9090 |
| is_framed_transport = False |
| |
| if len(sys.argv) >= 2: |
| default_host = sys.argv[1] |
| if len(sys.argv) >= 3: |
| default_port = int(sys.argv[2]) |
| |
| run(default_host, default_port, is_framed_transport) |