Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import javax.security.auth.Subject;
import javax.security.auth.login.LoginContext;
import javax.security.sasl.Sasl;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.thrift2.generated.TColumnValue;
import org.apache.hadoop.hbase.thrift2.generated.TGet;
Expand Down Expand Up @@ -63,7 +64,7 @@ public static void main(String[] args) throws Exception {
if (args.length >= 2) {
port = Integer.parseInt(args[1]);
}
org.apache.hadoop.conf.Configuration conf = HBaseConfiguration.create();
Configuration conf = HBaseConfiguration.create();
String principal = conf.get("hbase.thrift.kerberos.principal");
if (principal != null) {
secure = true;
Expand Down Expand Up @@ -140,12 +141,12 @@ public void run() throws Exception {

TResult result = client.get(table, get);

System.out.print("row = " + ClientUtils.utf8(result.getRow()));
System.out.println("row = " + ClientUtils.utf8(result.getRow()));
for (TColumnValue resultColumnValue : result.getColumnValues()) {
System.out.print("family = " + ClientUtils.utf8(resultColumnValue.getFamily()));
System.out.print("qualifier = " + ClientUtils.utf8(resultColumnValue.getFamily()));
System.out.print("value = " + ClientUtils.utf8(resultColumnValue.getValue()));
System.out.print("timestamp = " + resultColumnValue.getTimestamp());
System.out.println("family = " + ClientUtils.utf8(resultColumnValue.getFamily()));
System.out.println("qualifier = " + ClientUtils.utf8(resultColumnValue.getQualifier()));
System.out.println("value = " + ClientUtils.utf8(resultColumnValue.getValue()));
System.out.println("timestamp = " + resultColumnValue.getTimestamp());
}

transport.close();
Expand Down
20 changes: 20 additions & 0 deletions hbase-examples/src/main/python/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#
# 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.
#

six==1.17.0
thrift==0.14.1
117 changes: 73 additions & 44 deletions hbase-examples/src/main/python/thrift2/DemoClient.py
Original file line number Diff line number Diff line change
@@ -1,74 +1,103 @@
#!/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.
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.9.3
# 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

print("Thrift2 Demo")
print('This demo assumes you have a table called '
'"example" with a column family called "family1"')

host = "localhost"
port = 9090
framed = False
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)

socket = TSocket.TSocket(host, port)
if framed:
transport = TTransport.TFramedTransport(socket)
else:
transport = TTransport.TBufferedTransport(socket)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = THBaseService.Client(protocol)
get = ttypes.TGet(row="row1".encode())
print("\nGetting:", get)
result = client.get(table, get)

transport.open()
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}")

# Check Thrift Server Type
serverType = client.getThriftServerType()
if serverType != ttypes.TThriftServerType.TWO:
raise RuntimeError(f"Mismatch between client and server, server type is {serverType}")
transport.close()

table = "example"

t_column_values = ttypes.TColumnValue(family="family1",qualifier="qualifier1",value="value1")
put = ttypes.TPut(row="row1", columnValues=[t_column_values])
print("Putting:", put)
client.put(table, put)
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"'
)

get = ttypes.TGet(row="row1")
print("Getting:", get)
result = client.get(table, get)
default_host = "localhost"
default_port = 9090
is_framed_transport = False

print("Result:", result)
if len(sys.argv) >= 2:
default_host = sys.argv[1]
if len(sys.argv) >= 3:
default_port = int(sys.argv[2])

transport.close()
run(default_host, default_port, is_framed_transport)
28 changes: 28 additions & 0 deletions hbase-examples/src/main/resources/hbase-site.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<!--
/**
*
* 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.
*/
-->
<configuration>
<property>
<name>hbase.defaults.for.version.skip</name>
<value>true</value>
</property>
</configuration>