小工具知识网

当前位置: 首页 > 行业知识 > 使用Python访问Phoenix

行业知识

使用Python访问Phoenix

2026-11-06 12:39:26 来源:互联网转载

需求分析

���解Phoenix的原理及应用场景,理解Thrift的原理及操作,掌握Python如何访问操作HBase。

系统实现

实验流程分析:

开启HBase集群

在Phoenix上创建表

启动Thrift服务

实现Python访问HBase

关闭启动的服务

开启HBase集群:

开启ZooKeeper集群

 在master,slave1和slave2上分别打开Zookeeper集群

 查看Zookeeper的运行状态

开启Hadoop集群

 在master上开启Hadoop集群

启动HBase集群

 在master上启动HBase集群

在Phoenix上创建表:

启动Phoenix服务

执行!table指令,出现如下的表

创建Phoenix表

创建一个Phoenix表,表名为population,具体指令如下:

create table if not exists population(state char(2) not null,city varchar not null,population bigint constraint my_pk primary key(state,city));                               

向表中插入数据

向population表插入数据,具体指令如下:

查询表中插入数据

使用seletc命令查询population表中数据

数据如下

启动Thrift服务:

启动HBase Thrift服务

在master服务器启动Thrift服务,具体指令如下:

进入HBase Shell客户端

在任意一台服务器上进入HBase Shell客户端,具体指令如下:

查看HBase表

表中数据如下

实现Python访问HBase:

编写Python程序

在master服务器输入以下指令,打开文件

输入以下代码

from thrift import Thriftfrom thrift.transport import TSocketfrom thrift.transport import TTransportfrom thrift.protocol import TBinaryProtocolfrom hbase import Hbasefrom hbase.ttypes import *transport = TSocket.TSocket('master', 9090)transport = TTransport.TBufferedTransport(transport)protocol = TBinaryProtocol.TBinaryProtocol(transport)client = Hbase.Client(protocol)transport.open()result = client.getRow('POPULATION','CALos Angeles')print(result)

运行Python程序

输入以下指令运行程序

结果如下

关闭启动的服务

关闭Thrift服务

在master上输入下图指令

退出Phoenix服务

在master的第一个终端退出Poenix服务

关闭HBase集群

在master上关闭HBase集群

关闭Hadoop集群

在master上关闭Hadoop集群

关闭ZooKeeper集群

在三个服务器上都关闭ZooKeeper集群

上一篇:京医通下载攻略,轻松掌握下载步骤,一键开启健康之旅!

下一篇:https怎么做服务器