HBase实战(1):使用Python连接Hbase数据库

释放双眼,带上耳机,听听看~!

使用Python连接Hbase数据库

1,Hbase下载。

下载地址:http://hbase.apache.org/downloads.html

2,本地Hbase安装


1
2
1root@master:/usr/local/setup_tools# tar -zxvf hbase-2.0.0-bin.tar.gz
2

1
2
3
4
5
6
1root@master:/usr/local/setup_tools# mv hbase-2.0.0 /usr/local/
2root@master:/usr/local/setup_tools# cd /usr/local
3root@master:/usr/local# ls | grep hbase
4hbase-2.0.0
5root@master:/usr/local#
6

1
2
3
4
5
6
1root@master:/usr/local/hbase-2.0.0# vi /etc/profile
2export HBASE_HOME=/usr/local/hbase-2.0.0
3
4export PATH=.:$PATH:$JAVA_HOME/bin:$SCALA_HOME/bin:$HADOOP_HOME/bin:$SPARK_HOME/bin:$HIVE_HOME/bin:$FLUME_HOME/bin:$ZOOKEEPER_HOME/bin:$KAFKA_HOME/bin:$IDEA_HOME/bin:$eclipse_HOME:$MAVEN_HOME/bin:$ALLUXIO_HOME/bin:$HBASE_HOME/bin
5
6

1
2
1root@master:/usr/local/hbase-2.0.0# source /etc/profile
2

3,配置

 修改hbase-site.xml,设置存储数据的根目录。


1
2
1root@master:/usr/local/hbase-2.0.0/conf# vi hbase-site.xml
2

1
2
3
4
5
6
7
8
1<configuration>
2    <property>
3        <name>hbase.rootdir</name>
4        <value>file:///usr/local/hbase-2.0.0/data</value>
5    </property>
6
7</configuration>
8

1
2
14,启动hbase  
2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
1root@master:/usr/local/hbase-2.0.0# cd bin
2root@master:/usr/local/hbase-2.0.0/bin# ls
3considerAsDead.sh     hbase             hbase-config.cmd  hbase-jruby             master-backup.sh  replication               start-hbase.sh  zookeepers.sh
4draining_servers.rb   hbase-cleanup.sh  hbase-config.sh   hirb.rb                 region_mover.rb   rolling-restart.sh        stop-hbase.cmd
5get-active-master.rb  hbase.cmd         hbase-daemon.sh   local-master-backup.sh  regionservers.sh  shutdown_regionserver.rb  stop-hbase.sh
6graceful_stop.sh      hbase-common.sh   hbase-daemons.sh  local-regionservers.sh  region_status.rb  start-hbase.cmd           test
7root@master:/usr/local/hbase-2.0.0/bin# start-hbase.sh
8SLF4J: Class path contains multiple SLF4J bindings.
9SLF4J: Found binding in [jar:file:/usr/local/hbase-2.0.0/lib/slf4j-log4j12-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class]
10SLF4J: Found binding in [jar:file:/usr/local/hadoop-2.6.0/share/hadoop/common/lib/slf4j-log4j12-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class]
11SLF4J: Found binding in [jar:file:/usr/local/alluxio-1.7.0-hadoop-2.6/client/alluxio-1.7.0-client.jar!/org/slf4j/impl/StaticLoggerBinder.class]
12SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
13SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
14running master, logging to /usr/local/hbase-2.0.0/logs/hbase-root-master-master.out
15root@master:/usr/local/hbase-2.0.0/bin# jps
162757 Jps
172685 HMaster
18

5,使用hbase shell


1
2
3
4
5
6
7
8
9
10
11
12
13
14
1root@master:/usr/local/hbase-2.0.0/bin#  hbase shell
2SLF4J: Class path contains multiple SLF4J bindings.
3SLF4J: Found binding in [jar:file:/usr/local/hbase-2.0.0/lib/slf4j-log4j12-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class]
4SLF4J: Found binding in [jar:file:/usr/local/hadoop-2.6.0/share/hadoop/common/lib/slf4j-log4j12-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class]
5SLF4J: Found binding in [jar:file:/usr/local/alluxio-1.7.0-hadoop-2.6/client/alluxio-1.7.0-client.jar!/org/slf4j/impl/StaticLoggerBinder.class]
6SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
7SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
8HBase Shell
9Use "help" to get list of supported commands.
10Use "exit" to quit this interactive shell.
11Version 2.0.0, r7483b111e4da77adbfc8062b3b22cbe7c2cb91c1, Sun Apr 22 20:26:55 PDT 2018
12Took 0.0044 seconds                                                                                                                                                    
13hbase(main):001:0>
14

1
2
3
4
5
1hbase(main):003:0> version
22.0.0, r7483b111e4da77adbfc8062b3b22cbe7c2cb91c1, Sun Apr 22 20:26:55 PDT 2018
3Took 0.0054 seconds                                                                                                                                                    
4hbase(main):004:0>
5

启动hbase thrift服务。


1
2
3
4
5
6
7
8
9
10
11
12
13
1root@master:/usr/local/hbase-2.0.0/bin# hbase-daemon.sh start thrift
2running thrift, logging to /usr/local/hbase-2.0.0/logs/hbase-root-thrift-master.out
3SLF4J: Class path contains multiple SLF4J bindings.
4SLF4J: Found binding in [jar:file:/usr/local/hbase-2.0.0/lib/slf4j-log4j12-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class]
5SLF4J: Found binding in [jar:file:/usr/local/hadoop-2.6.0/share/hadoop/common/lib/slf4j-log4j12-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class]
6SLF4J: Found binding in [jar:file:/usr/local/alluxio-1.7.0-hadoop-2.6/client/alluxio-1.7.0-client.jar!/org/slf4j/impl/StaticLoggerBinder.class]
7SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
8root@master:/usr/local/hbase-2.0.0/bin# jps
93332 Jps
103254 ThriftServer
112685 HMaster
12root@master:/usr/local/hbase-2.0.0/bin#
13

二,使用Python连接Hbase。 

1,建立Python开发环境 —虚拟环境。

  •       先进行检查。


1
2
3
4
5
6
7
8
9
10
1root@master:/usr/local/hbase-2.0.0/bin# pip
2The program 'pip' is currently not installed. You can install it by typing:
3apt-get install python-pip
4root@master:/usr/local/hbase-2.0.0/bin# python
5Python 2.7.6 (default, Mar 22 2014, 22:59:56)
6[GCC 4.8.2] on linux2
7Type "help", "copyright", "credits" or "license" for more information.
8>>> exit()
9root@master:/usr/local/hbase-2.0.0/bin#
10
  • 安装pip。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
1root@master:/usr/local/hbase-2.0.0/bin# apt-get install python-pip
2Reading package lists... Done
3Building dependency tree      
4Reading state information... Done
5The following packages were automatically installed and are no longer required:
6  diffstat hardening-includes libapt-pkg-perl libarchive-zip-perl
7  libautodie-perl libclass-accessor-perl libemail-valid-perl libio-pty-perl
8  libio-string-perl libipc-run-perl libipc-system-simple-perl
9  liblist-moreutils-perl libnet-dns-perl libnet-domain-tld-perl libnet-ip-perl
10  libparse-debianchangelog-perl libperlio-gzip-perl libsub-identify-perl
11  libsub-name-perl libtext-levenshtein-perl patchutils t1utils
12Use 'apt-get autoremove' to remove them.
13The following extra packages will be installed:
14  python-chardet-whl python-colorama python-colorama-whl python-distlib
15  python-distlib-whl python-html5lib python-html5lib-whl python-pip-whl
16  python-pkg-resources python-requests-whl python-setuptools
17  python-setuptools-whl python-six-whl python-urllib3-whl python-wheel
18Suggested packages:
19  python-genshi python-distribute python-distribute-doc
20Recommended packages:
21  python-dev-all
22The following NEW packages will be installed:
23  python-chardet-whl python-colorama python-colorama-whl python-distlib
24  python-distlib-whl python-html5lib python-html5lib-whl python-pip
25  python-pip-whl python-requests-whl python-setuptools python-setuptools-whl
26  python-six-whl python-urllib3-whl python-wheel
27The following packages will be upgraded:
28  python-pkg-resources
291 upgraded, 15 newly installed, 0 to remove and 637 not upgraded.
30Need to get 1,744 kB of archives.
31After this operation, 4,184 kB of additional disk space will be used.
32Do you want to continue? [Y/n] y
33Get:1 http://us.archive.ubuntu.com/ubuntu/ trusty-updates/universe python-chardet-whl all 2.2.1-2~ubuntu1 [170 kB]
34Get:2 http://us.archive.ubuntu.com/ubuntu/ trusty-updates/universe python-chardet-whl all 2.2.1-2~ubuntu1 [170 kB]
351% [2 python-chardet-whl 14.0 kB/170 kB 8%]                                    
36Get:3 http://us.archive.ubuntu.com/ubuntu/ trusty-updates/universe python-chardet-whl all 2.2.1-2~ubuntu1 [170 kB]
37Get:4 http://us.archive.ubuntu.com/ubuntu/ trusty-updates/universe python-chardet-whl all 2.2.1-2~ubuntu1 [170 kB]
382% [4 python-chardet-whl 38.1 kB/170 kB 22%]                                  
396% [4 python-chardet-whl 99.9 kB/170 kB 59%]                                  
40Get:5 http://us.archive.ubuntu.com/ubuntu/ trusty-updates/universe python-colorama all 0.2.5-0.1ubuntu2 [18.4 kB]
41Get:6 http://us.archive.ubuntu.com/ubuntu/ trusty-updates/universe python-colorama-whl all 0.2.5-0.1ubuntu2 [18.2 kB]
42Get:7 http://us.archive.ubuntu.com/ubuntu/ trusty-updates/universe python-distlib all 0.1.8-1ubuntu1 [113 kB]
43Get:8 http://us.archive.ubuntu.com/ubuntu/ trusty-updates/universe python-distlib-whl all 0.1.8-1ubuntu1 [140 kB]
44Get:9 http://us.archive.ubuntu.com/ubuntu/ trusty-updates/main python-html5lib all 0.999-3~ubuntu1 [83.5 kB]
45Get:10 http://us.archive.ubuntu.com/ubuntu/ trusty-updates/main python-html5lib-whl all 0.999-3~ubuntu1 [109 kB]
46Get:11 http://us.archive.ubuntu.com/ubuntu/ trusty-updates/main python-six-whl all 1.5.2-1ubuntu1 [10.5 kB]
47Get:12 http://us.archive.ubuntu.com/ubuntu/ trusty-updates/main python-urllib3-whl all 1.7.1-1ubuntu4 [64.0 kB]
48Get:13 http://us.archive.ubuntu.com/ubuntu/ trusty-updates/main python-requests-whl all 2.2.1-1ubuntu0.3 [227 kB]
49Get:14 http://us.archive.ubuntu.com/ubuntu/ trusty-updates/main python-setuptools-whl all 3.3-1ubuntu2 [244 kB]
50Get:15 http://us.archive.ubuntu.com/ubuntu/ trusty-updates/universe python-pip-whl all 1.5.4-1ubuntu4 [111 kB]
51Get:16 http://us.archive.ubuntu.com/ubuntu/ trusty-updates/main python-pkg-resources all 3.3-1ubuntu2 [61.9 kB]
52Get:17 http://us.archive.ubuntu.com/ubuntu/ trusty-updates/main python-setuptools all 3.3-1ubuntu2 [230 kB]
53Get:18 http://us.archive.ubuntu.com/ubuntu/ trusty-updates/universe python-pip all 1.5.4-1ubuntu4 [97.3 kB]
54Get:19 http://us.archive.ubuntu.com/ubuntu/ trusty-updates/main python-wheel all 0.24.0-1~ubuntu1 [44.7 kB]
55Fetched 1,723 kB in 9min 30s (3,019 B/s)                                      
56Selecting previously unselected package python-chardet-whl.
57(Reading database ... 156494 files and directories currently installed.)
58Preparing to unpack .../python-chardet-whl_2.2.1-2~ubuntu1_all.deb ...
59Unpacking python-chardet-whl (2.2.1-2~ubuntu1) ...
60Selecting previously unselected package python-colorama.
61Preparing to unpack .../python-colorama_0.2.5-0.1ubuntu2_all.deb ...
62Unpacking python-colorama (0.2.5-0.1ubuntu2) ...
63Selecting previously unselected package python-colorama-whl.
64Preparing to unpack .../python-colorama-whl_0.2.5-0.1ubuntu2_all.deb ...
65Unpacking python-colorama-whl (0.2.5-0.1ubuntu2) ...
66Selecting previously unselected package python-distlib.
67Preparing to unpack .../python-distlib_0.1.8-1ubuntu1_all.deb ...
68Unpacking python-distlib (0.1.8-1ubuntu1) ...
69Selecting previously unselected package python-distlib-whl.
70Preparing to unpack .../python-distlib-whl_0.1.8-1ubuntu1_all.deb ...
71Unpacking python-distlib-whl (0.1.8-1ubuntu1) ...
72Selecting previously unselected package python-html5lib.
73Preparing to unpack .../python-html5lib_0.999-3~ubuntu1_all.deb ...
74Unpacking python-html5lib (0.999-3~ubuntu1) ...
75Selecting previously unselected package python-html5lib-whl.
76Preparing to unpack .../python-html5lib-whl_0.999-3~ubuntu1_all.deb ...
77Unpacking python-html5lib-whl (0.999-3~ubuntu1) ...
78Selecting previously unselected package python-six-whl.
79Preparing to unpack .../python-six-whl_1.5.2-1ubuntu1_all.deb ...
80Unpacking python-six-whl (1.5.2-1ubuntu1) ...
81Selecting previously unselected package python-urllib3-whl.
82Preparing to unpack .../python-urllib3-whl_1.7.1-1ubuntu4_all.deb ...
83Unpacking python-urllib3-whl (1.7.1-1ubuntu4) ...
84Selecting previously unselected package python-requests-whl.
85Preparing to unpack .../python-requests-whl_2.2.1-1ubuntu0.3_all.deb ...
86Unpacking python-requests-whl (2.2.1-1ubuntu0.3) ...
87Selecting previously unselected package python-setuptools-whl.
88Preparing to unpack .../python-setuptools-whl_3.3-1ubuntu2_all.deb ...
89Unpacking python-setuptools-whl (3.3-1ubuntu2) ...
90Selecting previously unselected package python-pip-whl.
91Preparing to unpack .../python-pip-whl_1.5.4-1ubuntu4_all.deb ...
92Unpacking python-pip-whl (1.5.4-1ubuntu4) ...
93Preparing to unpack .../python-pkg-resources_3.3-1ubuntu2_all.deb ...
94Unpacking python-pkg-resources (3.3-1ubuntu2) over (3.3-1ubuntu1) ...
95Selecting previously unselected package python-setuptools.
96Preparing to unpack .../python-setuptools_3.3-1ubuntu2_all.deb ...
97Unpacking python-setuptools (3.3-1ubuntu2) ...
98Selecting previously unselected package python-pip.
99Preparing to unpack .../python-pip_1.5.4-1ubuntu4_all.deb ...
100Unpacking python-pip (1.5.4-1ubuntu4) ...
101Selecting previously unselected package python-wheel.
102Preparing to unpack .../python-wheel_0.24.0-1~ubuntu1_all.deb ...
103Unpacking python-wheel (0.24.0-1~ubuntu1) ...
104Processing triggers for man-db (2.6.7.1-1ubuntu1) ...
105Setting up python-chardet-whl (2.2.1-2~ubuntu1) ...
106Setting up python-colorama (0.2.5-0.1ubuntu2) ...
107Setting up python-colorama-whl (0.2.5-0.1ubuntu2) ...
108Setting up python-distlib (0.1.8-1ubuntu1) ...
109Setting up python-distlib-whl (0.1.8-1ubuntu1) ...
110Setting up python-html5lib (0.999-3~ubuntu1) ...
111Setting up python-html5lib-whl (0.999-3~ubuntu1) ...
112Setting up python-six-whl (1.5.2-1ubuntu1) ...
113Setting up python-urllib3-whl (1.7.1-1ubuntu4) ...
114Setting up python-requests-whl (2.2.1-1ubuntu0.3) ...
115Setting up python-setuptools-whl (3.3-1ubuntu2) ...
116Setting up python-pip-whl (1.5.4-1ubuntu4) ...
117Setting up python-pkg-resources (3.3-1ubuntu2) ...
118Setting up python-setuptools (3.3-1ubuntu2) ...
119Setting up python-pip (1.5.4-1ubuntu4) ...
120Setting up python-wheel (0.24.0-1~ubuntu1) ...
121root@master:/usr/local/hbase-2.0.0/bin#
122

 Pip安装好之后,安装virtualenv软件,让操作系统支持虚拟环境,可以得到命令virtualenv或者pyvenv命令。


1
2
3
4
5
6
7
8
1root@master:/usr/local/hbase-2.0.0/bin# pip install virtualenv
2Downloading/unpacking virtualenv
3  Downloading virtualenv-16.0.0-py2.py3-none-any.whl (1.9MB): 1.9MB downloaded
4Installing collected packages: virtualenv
5Successfully installed virtualenv
6Cleaning up...
7root@master:/usr/local/hbase-2.0.0/bin#
8

1
2
1创建虚拟环境  
2

1
2
3
4
5
6
7
8
9
10
11
1root@master:/usr/local# mkdir myPythonEnv
2root@master:/usr/local# cd myPythonEnv
3root@master:/usr/local/myPythonEnv# pwd
4/usr/local/myPythonEnv
5root@master:/usr/local/myPythonEnv# Pyvenv project-env
6Pyvenv: command not found
7root@master:/usr/local/myPythonEnv# virtualenv  project-env
8New python executable in /usr/local/myPythonEnv/project-env/bin/python
9Installing setuptools, pip, wheel...done.
10root@master:/usr/local/myPythonEnv#
11

进入虚拟环境:


1
2
3
1root@master:/usr/local/myPythonEnv/project-env/bin# source /usr/local/myPythonEnv/project-env/bin/activate
2(project-env) root@master:/usr/local/myPythonEnv/project-env/bin#
3

1
2
1安装Thrift依赖包  
2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
1(project-env) root@master:/usr/local/myPythonEnv/project-env/bin#  pip install thrift
2Collecting thrift
3/usr/local/myPythonEnv/project-env/local/lib/python2.7/site-packages/pip/_vendor/urllib3/util/ssl_.py:339: SNIMissingWarning: An HTTPS request has been made, but the SNI (Subject Name Indication) extension to TLS is not available on this platform. This may cause the server to present an incorrect TLS certificate, which can cause validation failures. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
4  SNIMissingWarning
5/usr/local/myPythonEnv/project-env/local/lib/python2.7/site-packages/pip/_vendor/urllib3/util/ssl_.py:137: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
6  InsecurePlatformWarning
7/usr/local/myPythonEnv/project-env/local/lib/python2.7/site-packages/pip/_vendor/urllib3/util/ssl_.py:137: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
8  InsecurePlatformWarning
9  Downloading https://files.pythonhosted.org/packages/c6/b4/510617906f8e0c5660e7d96fbc5585113f83ad547a3989b80297ac72a74c/thrift-0.11.0.tar.gz (52kB)
10    100% |████████████████████████████████| 61kB 22kB/s
11Collecting six>=1.7.2 (from thrift)
12  Downloading https://files.pythonhosted.org/packages/67/4b/141a581104b1f6397bfa78ac9d43d8ad29a7ca43ea90a2d863fe3056e86a/six-1.11.0-py2.py3-none-any.whl
13Building wheels for collected packages: thrift
14  Running setup.py bdist_wheel for thrift ... done
15  Stored in directory: /root/.cache/pip/wheels/be/36/81/0f93ba89a1cb7887c91937948519840a72c0ffdd57cac0ae8f
16Successfully built thrift
17Installing collected packages: six, thrift
18Successfully installed six-1.11.0 thrift-0.11.0
19

1
2
1安装hbase-thrift依赖包  
2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
1(project-env) root@master:/usr/local/myPythonEnv/project-env/bin#  pip install hbase-thrift
2Collecting hbase-thrift
3/usr/local/myPythonEnv/project-env/local/lib/python2.7/site-packages/pip/_vendor/urllib3/util/ssl_.py:339: SNIMissingWarning: An HTTPS request has been made, but the SNI (Subject Name Indication) extension to TLS is not available on this platform. This may cause the server to present an incorrect TLS certificate, which can cause validation failures. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
4  SNIMissingWarning
5/usr/local/myPythonEnv/project-env/local/lib/python2.7/site-packages/pip/_vendor/urllib3/util/ssl_.py:137: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
6  InsecurePlatformWarning
7/usr/local/myPythonEnv/project-env/local/lib/python2.7/site-packages/pip/_vendor/urllib3/util/ssl_.py:137: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
8  InsecurePlatformWarning
9  Downloading https://files.pythonhosted.org/packages/89/f7/dbb6c764bb909ed361c255828701228d8c9867d541cfef84127e6f3704cc/hbase-thrift-0.20.4.tar.gz
10Requirement already satisfied: Thrift in /usr/local/myPythonEnv/project-env/lib/python2.7/site-packages (from hbase-thrift) (0.11.0)
11Requirement already satisfied: six>=1.7.2 in /usr/local/myPythonEnv/project-env/lib/python2.7/site-packages (from Thrift->hbase-thrift) (1.11.0)
12Building wheels for collected packages: hbase-thrift
13  Running setup.py bdist_wheel for hbase-thrift ... done
14  Stored in directory: /root/.cache/pip/wheels/fe/51/f2/afb7b010cd97910aa0b651d492735a38ed69a93a817444904e
15Successfully built hbase-thrift
16Installing collected packages: hbase-thrift
17Successfully installed hbase-thrift-0.20.4
18(project-env) root@master:/usr/local/myPythonEnv/project-env/bin#
19

Python连接HBASE的测试程序


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
1(project-env) root@master:/usr/local/noc_python_201806# vi queryTest.py
2
3from thrift import Thrift
4from thrift.transport import TSocket
5from thrift.transport import TTransport
6from thrift.protocol import TBinaryProtocol
7
8from hbase import Hbase
9from hbase.ttypes import *
10
11transport = TSocket.TSocket('localhost', 9090)
12
13transport = TTransport.TBufferedTransport(transport)
14protocol = TBinaryProtocol.TBinaryProtocol(transport)
15
16client = Hbase.Client(protocol)
17transport.open()
18print client.getTableNames()
19

运行结果为,查询出表:


1
2
3
4
1(project-env) root@master:/usr/local/noc_python_201806# python queryTest.py
2['test', 'testHbase']
3(project-env) root@master:/usr/local/noc_python_201806#
4

Hbase中的表


1
2
3
4
5
6
7
8
9
1hbase(main):025:0> list
2TABLE                                                                                                                                                                  
3test                                                                                                                                                                  
4testHbase                                                                                                                                                              
52 row(s)
6Took 0.0188 seconds                                                                                                                                                    
7=> ["test", "testHbase"]
8hbase(main):026:0>
9

至此,Python连接Hbase已经成功!

给TA打赏
共{{data.count}}人
人已打赏
安全运维

OpenSSH-8.7p1离线升级修复安全漏洞

2021-10-23 10:13:25

安全运维

设计模式的设计原则

2021-12-12 17:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索