{"id":5232,"date":"2021-08-31T17:29:59","date_gmt":"2021-08-31T09:29:59","guid":{"rendered":"https:\/\/nj.transwarp.cn:8180\/?p=5232"},"modified":"2022-04-13T15:16:43","modified_gmt":"2022-04-13T07:16:43","slug":"%e7%94%a8python3%e8%bf%9e%e6%8e%a5%e5%bc%80%e5%90%af%e5%ae%89%e5%85%a8%e7%9a%84hyperbase","status":"publish","type":"post","link":"https:\/\/kbwp.transwarp.cn\/?p=5232","title":{"rendered":"\u7528python3\u8fde\u63a5\u5f00\u542f\u5b89\u5168\u7684Hyperbase"},"content":{"rendered":"<h3>\u6982\u8981\u63cf\u8ff0<\/h3>\n<hr \/>\n<p>\u672c\u6587\u610f\u5728\u63cf\u8ff0\u5982\u4f55\u7528Python3\u8fde\u63a5\u5f00\u4e86\u5b89\u5168\u7684 Hyperbase\uff1b<br \/>\n\u7531\u4e8e\u7248\u672c\u539f\u56e0\uff0c\u9700\u8981\u4fee\u6539\u90e8\u5206\u5e93\u7684\u6e90\u7801\u6765\u8fdb\u884c\u9002\u914d\u3002<br \/>\nPython2\u8fde\u63a5\u5f00\u542f\u5b89\u5168\u7684hyperbase\u53c2\u8003\uff1a<a href=\"https:\/\/nj.transwarp.cn:8180\/?p=585\">https:\/\/nj.transwarp.cn:8180\/?p=585<\/a><\/p>\n<h3>\u8be6\u7ec6\u8bf4\u660e<\/h3>\n<h4>\u64cd\u4f5c\u6b65\u9aa4<\/h4>\n<ol>\n<li>\u73af\u5883\u9ed8\u8ba4\u5df2\u5b89\u88c5python3\u73af\u5883<\/li>\n<li>\u5b89\u88c5kinit\u5ba2\u6237\u7aef\n<pre><code class=\"language-shell\">#yum install krb5*<\/code><\/pre>\n<p>\u4ece\u5f00\u4e86 Hyperbase \u5b89\u5168\u7684\u96c6\u7fa4\u670d\u52a1\u5668\u4e0a\uff0c\u5c06 <strong>\/etc\/hyperbase1\/conf<\/strong> \u76ee\u5f55\u4e0b\u7684 <strong>krb5.conf<\/strong> \u6587\u4ef6\u6587\u4ef6\u62f7\u8d1d\u5230\u96c6\u7fa4\u5916\u90e8\u7f72python\u7684\u670d\u52a1\u5668\u7684 <strong>\/etc<\/strong> \u76ee\u5f55\u4e0b<br \/>\n<img decoding=\"async\" src=\"https:\/\/nj.transwarp.cn:8180\/wp-content\/uploads\/2021\/08\/image-1630399965823.png\" alt=\"file\" \/><\/p>\n<\/li>\n<li>\u914d\u7f6e <strong>\/etc\/hosts<\/strong> \u6587\u4ef6\uff0c\u4f7f\u5176\u5305\u542b\u96c6\u7fa4\u8282\u70b9\u4fe1\u606f\uff0c\u4f8b\u5982\uff1a\n<pre><code class=\"language-shell\"># cat \/etc\/hosts<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/nj.transwarp.cn:8180\/wp-content\/uploads\/2021\/08\/image-1630399861669.png\" alt=\"file\" \/><\/p>\n<\/li>\n<li>\u5b89\u88c5\u6240\u9700 Python \u5305\n<pre><code class=\"language-shell\"># pip install pure-sasl\n# pip install thrift\n# pip install hbase-thrift<\/code><\/pre>\n<\/li>\n<li>\u5b89\u88c5Kerberos\n<ol>\n<li><a href=\"\/wp-content\/uploads\/2019\/08\/kerberos.tar.gz\" title=\"\u4e0b\u8f7d\">\u4e0b\u8f7d<\/a>Kerberos\u6587\u4ef6\uff1b<\/li>\n<li>\u89e3\u538b\u5e76\u5c06\u89e3\u538b\u540e\u7684\u6587\u4ef6\u590d\u5236\u5230python3\u5b89\u88c5\u76ee\u5f55\u4e0bsite-packages\u76ee\u5f55\u4e2d\uff0c<strong>\u9700\u8981\u6839\u636e\u5b9e\u9645\u73af\u5883\u8fdb\u884c\u4fee\u6539<\/strong>\n<pre><code class=\"language-shell\"># tar zxvf kerberos.gar.gz\n# tar zxvf kerberos-1.3.0.dist-info.tar.gz\n# cp -p * \/usr\/lib\/python3.6.8\/site-packages\/<\/code><\/pre>\n<\/li>\n<li>\u9a8c\u8bc1kerberos\u662f\u5426\u5b89\u88c5\u6210\u529f\n<pre><code class=\"language-shell\"># pip list | grep kerberos<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/nj.transwarp.cn:8180\/wp-content\/uploads\/2021\/08\/image-1630400539251.png\" alt=\"file\" \/><\/p>\n<\/li>\n<\/ol>\n<\/li>\n<li>\u66ff\u6362site-packages\/hbase\u76ee\u5f55\u4e0b\u7684Hbase.py\u548cttypes.py<br \/>\n<a href=\"https:\/\/nj.transwarp.club:666\/main.html?download&weblink=f73e09fc9d35e8ba0d3d5a47b66ba5d8&realfilename=Hbase.py\" title=\"Hbase.py\">Hbase.py&#8211;\u70b9\u6b64\u4e0b\u8f7d<\/a><br \/>\n<a href=\"https:\/\/nj.transwarp.club:666\/main.html?download&weblink=d0042794c4ab8e09a50e6c13646bcae9&realfilename=ttypes.py\" title=\"ttypes.py--\u70b9\u6b64\u4e0b\u8f7d\">ttypes.py&#8211;\u70b9\u6b64\u4e0b\u8f7d<\/a><\/li>\n<li>\u4fee\u6539site-packages\/puresasl\/mechanisms.py,\u5c06\u6587\u4ef6\u4e2d\u7684565\u884c\u4fee\u6539\u4e3a\uff1a\n<pre><code class=\"language-python\">kerberos.authGSSClientWrap(self.context, outgoing.decode('utf-8'), None, protect)<\/code><\/pre>\n<p>\u4fee\u6539\u5b8c\u6210\u5982\u4e0b\u56fe\uff1a<br \/>\n<img decoding=\"async\" src=\"https:\/\/nj.transwarp.cn:8180\/wp-content\/uploads\/2021\/08\/image-1630401916160.png\" alt=\"file\" \/><\/p>\n<\/li>\n<\/ol>\n<h4>Python3\u8fde\u63a5Hyperbase\u811a\u672c\u53c2\u8003\u5982\u4e0b\uff1a<\/h4>\n<pre><code class=\"language-python\">from thrift.transport import TSocket\nfrom thrift.transport import TTransport\nfrom thrift.protocol import TBinaryProtocol\nfrom hbase import Hbase\nfrom hbase.ttypes import ColumnDescriptor, Mutation, AlreadyExists, IOError\n\nclass HbaseClient:\n    def __init__(self, host='tdh700-mc001', port=19090, saslServiceName=\"hbase\"):\n        sock = TSocket.TSocket(host, port)\n        self.transport = TTransport.TSaslClientTransport(sock, host, saslServiceName)\n        protocol = TBinaryProtocol.TBinaryProtocol(self.transport)\n        self.client = Hbase.Client(protocol)\n        self.transport.open()\n\n    def get_tables(self):\n        \"\"\"\n        \u83b7\u53d6\u6240\u6709\u8868\n        \"\"\"\n        return self.client.getTableNames()\n\n    def create_table(self, table_name, column_list, max_versions=3):\n        \"\"\"\n        \u521b\u5efa\u8868\n        \"\"\"\n        column_families = list(map(lambda column: ColumnDescriptor(column, maxVersions=max_versions), column_list))\n        try:\n            self.client.createTable(table_name, column_families)\n        except AlreadyExists as e:\n            print('{}-->{}'.format(table_name, e))\n\n    def delete_table(self, table_name):\n        try:\n            self.client.deleteTable(table_name)\n        except IOError as e:\n            print(e)\n\n    # \u63d2\u5165\u6570\u636e\n    def put_data(self, table_name, column_data):\n        print('start put data to-->'.format(table_name))\n        for rowkey, value in column_data.items():\n            rowkey = rowkey\n            mutation = list(map(lambda value: Mutation(column=value['key'], value=value['value']), value))\n            self.client.mutateRow(table_name, rowkey, mutation)\n\n    def close_connection(self):\n        self.transport.close()\n        print('has closed connection')\n\nif __name__ == \"__main__\":\n    client = HbaseClient()\n\n    # \u5efa\u8868\n    table_name = 'test0006'\n    column_list = ['cf1:', 'cf2:', 'cf3:']\n    client.create_table(table_name, column_list, max_versions=5)\n\n    # \u83b7\u53d6\u6240\u6709\u5df2\u521b\u5efa\u8868\n    all_tables = client.get_tables()\n    print('all_tables:{}'.format(all_tables))\n\n    # \u63d2\u5165\u6570\u636e\n    column_data = {\n        'rowkey01': [\n            {'key': 'cf1:a', 'value': 'value-1-a'},\n            {'key': 'cf1:b', 'value': 'value-1-b'},\n        ],\n        'rowkey02': [\n            {'key': 'cf2:a', 'value': 'value-2-a'},\n            {'key': 'cf2:b', 'value': 'value-2-b'},\n        ]\n    }\n    client.put_data(table_name, column_data)\n\n    # \u5220\u9664\u8868\n    client.delete_table(table_name)\n\n    # \u5173\u95ed\u8fde\u63a5\n    client.close_connection()\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6982\u8981\u63cf\u8ff0 \u672c\u6587\u610f\u5728\u63cf\u8ff0\u5982\u4f55\u7528Python3\u8fde\u63a5\u5f00\u4e86\u5b89\u5168\u7684 Hyperbase\uff1b \u7531\u4e8e\u7248\u672c\u539f\u56e0\uff0c\u9700\u8981\u4fee\u6539\u90e8\u5206\u5e93\u7684 ..<\/p>\n<div class=\"clear-fix\"><\/div>\n<p><a href=\"https:\/\/kbwp.transwarp.cn\/?p=5232\" title=\"read more...\">Read more<\/a><\/p>\n","protected":false},"author":80,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[],"class_list":["post-5232","post","type-post","status-publish","format-standard","hentry","category-third_part"],"acf":[],"_links":{"self":[{"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=\/wp\/v2\/posts\/5232","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=\/wp\/v2\/users\/80"}],"replies":[{"embeddable":true,"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5232"}],"version-history":[{"count":1,"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=\/wp\/v2\/posts\/5232\/revisions"}],"predecessor-version":[{"id":6636,"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=\/wp\/v2\/posts\/5232\/revisions\/6636"}],"wp:attachment":[{"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5232"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5232"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5232"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}