惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

量子位
C
CXSECURITY Database RSS Feed - CXSecurity.com
S
Schneier on Security
博客园 - 叶小钗
博客园 - 三生石上(FineUI控件)
C
Cybersecurity and Infrastructure Security Agency CISA
Engineering at Meta
Engineering at Meta
Google DeepMind News
Google DeepMind News
酷 壳 – CoolShell
酷 壳 – CoolShell
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园_首页
T
Threat Research - Cisco Blogs
C
Cisco Blogs
Recent Announcements
Recent Announcements
S
Securelist
N
Netflix TechBlog - Medium
The Register - Security
The Register - Security
P
Privacy & Cybersecurity Law Blog
宝玉的分享
宝玉的分享
D
Darknet – Hacking Tools, Hacker News & Cyber Security
L
LINUX DO - 热门话题
T
Tor Project blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
月光博客
月光博客
AWS News Blog
AWS News Blog
P
Proofpoint News Feed
博客园 - 司徒正美
L
LINUX DO - 最新话题
Stack Overflow Blog
Stack Overflow Blog
博客园 - 聂微东
H
Help Net Security
Spread Privacy
Spread Privacy
PCI Perspectives
PCI Perspectives
Project Zero
Project Zero
I
Intezer
T
The Blog of Author Tim Ferriss
有赞技术团队
有赞技术团队
The Last Watchdog
The Last Watchdog
C
Check Point Blog
Blog — PlanetScale
Blog — PlanetScale
B
Blog RSS Feed
MyScale Blog
MyScale Blog
V
Vulnerabilities – Threatpost
Recorded Future
Recorded Future
T
Tenable Blog
Jina AI
Jina AI
D
DataBreaches.Net
阮一峰的网络日志
阮一峰的网络日志

博客园 - pcwanli

Python批量将Word文档(.doc)转换为.docx格式的完整实现步骤 Linux下版本控制器(SVN) -命令行客户端 linux svn 命令 Qwen3-VL视频科技:内容审核系统搭建 svn update 出现Skipped 'feifazuzhi' -- Node remains in conflict处理方法 linux redis.service如何编写 深入php redis pconnect PHP中使用Redis长连接笔记 python happybase 批量读取 如何用phpredis持久化连接pconnect方法提升应用响应速度 python处理常见格式压缩包文件的全指南 Python编程:happybase读写HBase数据库 python redis zset 按分值获取记录 hbase日志如何清理 hbase日志清理 Python的time.strftime()方法 python re.sub第二参数,前值如何引用 【Python】基于python实现Windows Service程序 python解析url参数 提取网页源码头信息的正则表达式 PHP字符串分割:explode()函数详解与应用 python requests get请求禁用自动解压 python importlib动态模块加载遇到is not a package错误,解决方法。 Python正则表达式替换(re.sub)的6种典型应用场景 用Python处理HTML转义字符的5种方式 pymongo批量更新bulk_write php mongodb操作 PHP 通过 Thrift 操作 Hbase PHP通过Thrift操作Hbase
使用thrift的php版本操作hbase数据库
pcwanli · 2026-03-04 · via 博客园 - pcwanli

来源:https://www.codeleading.com/article/19661999901/

目录

hbase环境配置

thrift环境配置

api接口生成

操作案例


最近要进行用户行为分析,数据存储决定采用hbase。并且为了便于和当前系统契合,采用php进行存取。

hbase环境配置

1. 进入hbase官网,进入下载页面,下载对应版本的可执行文件和源文件。

2. 直接将可执行文件进行解压缩,并且进行配置。

conf/hbase-env.sh中的 export JAVA_HOME=/***/jdk 设定为自己系统的jdk路径

conf/hbase-site.xml 中配置数据存储路径和端口

  1. <name>hbase.rootdir</name>

  2. <value>file:///opt/bigdata/hbase/data</value>

  3. <name>hbase.master.info.port</name>

另外,要把机器名字配置到hosts当。 执行hostname,并且将机器名称结果在hosts写入 127.0.0.1  devname

3.进入bin目录,启动hbase

start-hbase.sh

至此,hbase环境就搭建好了

thrift环境配置

1.进入thrift官网下载页面,并且进行下载

2.解压缩,并且进行安装

./configure -prefix=/安装目录  -with-php=/usr/bin

make

make install

3.启动hbase的thrift服务

./hbase-daemon.sh start thrift

api接口生成

1. 记得第一步中下载的hbase源码,在这里可以用的上了,解压缩hbase的源码,并且找到文件Hbase.thrift

hbase-2.1.0/hbase-thrift/src/main/resources/org/apache/hadoop/hbase/thrift/Hbase.thrift

进入thrift可执行文件目录bin中,生成php版本的接口。

./thrift --gen php hbase-2.1.0/hbase-thrift/src/main/resources/org/apache/hadoop/hbase/thrift/Hbase.thrift

2.拷贝thrift中php的库文件 hrift-0.11.0/lib/php , 

  1. cp -r thrift-0.11.0/lib/php ./thrift_lib

  2. cp -r gen-php/* ./thrift_lib

操作案例

1.在hbase中插入表,并且插入数据,具体详细命令可以参考这里

  1. create 'test',{NAME=>'cf1',VERSIONS=>1000},{NAME=>'cf2',VERSIONS=>1000}

  2. put 'test','20180814','cf1:column1','test_cf1_column1'

  3. put 'test','20180814','cf2:column1','test_cf2_column1'

2.依赖于上一步中的thrift_lib,我们开始创建php代码

  1. $GLOBALS['THRIFT_ROOT'] = '/opt/bigdata/test/test_thrift/thrift_lib';

  2. require_once($GLOBALS['THRIFT_ROOT'].'/Hbase/Hbase.php');

  3. require_once($GLOBALS['THRIFT_ROOT'].'/Hbase/Types.php');

  4. require_once( $GLOBALS['THRIFT_ROOT'].'/lib/Thrift/ClassLoader/ThriftClassLoader.php' );

  5. use Thrift\ClassLoader\ThriftClassLoader;

  6. $loader = new ThriftClassLoader();

  7. $loader->registerNamespace('Thrift',$GLOBALS['THRIFT_ROOT'].'/lib/');

  8. $loader->registerDefinition('shared',$GLOBALS['THRIFT_ROOT']);

  9. $loader->registerDefinition('tutorial',$GLOBALS['THRIFT_ROOT']);

  10. use Thrift\Protocol\TBinaryProtocol;

  11. use Thrift\Transport\TSocket;

  12. use Thrift\Transport\THttpClient;

  13. use Thrift\Transport\TBufferedTransport;

  14. use Thrift\Exception\TException;

  15. use Hbase\ColumnDescriptor;

  16. $socket = new TSocket( 'localhost', 9090 );

  17. $socket->setSendTimeout( 10000 );

  18. $socket->setRecvTimeout( 20000 );

  19. $transport = new TBufferedTransport( $socket );

  20. $protocol = new TBinaryProtocol( $transport );

  21. $client = new HBase\HbaseClient( $protocol );

  22. $tables = $client->getTableNames();

  23. $rows = $client->getRows($table,['20180814'],['cf1']);

  24. $rows = $client->getRow($table,'20180814',['cf1']);

  25. $rows = $client->getRowsWithColumns($table,['20180814',],['cf1:column1'],[]);

  26. $rows = $client->getRowTs($table,'20180814',1544214539999,[]);

  27. $rows = $client->getVerTs($table,'20180814','cf1:column1',1544214539933,2,[]);

  28. $rows = $client->getVer($table,'20180814','cf1:column1',20,[]);

  29. new Mutation(['column'=>'cf1:column1','value'=>'101505033']),

  30. new Mutation(['column'=>'cf1:column2','value'=>'3']),

  31. new Mutation(['column'=>'cf1:column3','value'=>'12']),

  32. new Mutation(['column'=>'cf2:column1','value'=>'101505033']),

  33. new Mutation(['column'=>'cf2:column2','value'=>'3']),

  34. new Mutation(['column'=>'cf2:column3','value'=>'12']),

  35. $client->mutateRow($table,'20180814',$dataArr,[]);