thirft连接hbase的例子

hbase支持的thrift有两种方式
thrift和
thrift2
官方似乎推荐thrift2
一下是两种thrift的java客户端的连接方式
然后是一个cpp以thrift2的方式连接hbase的操作
如果是使用thrift1
hbase-daemon.sh start thrift
如果是使用thrift2
hbase-daemon.sh start thrift2
官方例子在
/data/hadoop/hbase/hbase-0.94.17/src/examples/thrift
thrift文件在
./src/main/resources/org/apache/hadoop/hbase/thrift/Hbase.thrift
thrift2 的例子在
/data/hadoop/hbase/hbase-0.94.17/src/examples/thrift2
thrift2文件在
./src/main/resources/org/apache/hadoop/hbase/thrift2/hbase.thrift
(只有java和python的)


先生成文件
thrift -r --gen java Hbase.thrift
客户端代码
HaoClient.java

package org.apache.hadoop.hbase.thrift.generated;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;

public class HaoClient {
public static String byteBufferToString(ByteBuffer buffer) {
CharBuffer charBuffer = null;
try {
Charset charset = Charset.forName("UTF-8");
CharsetDecoder decoder = charset.newDecoder();
charBuffer = decoder.decode(buffer);
buffer.flip();
return charBuffer.toString();
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}

public static ByteBuffer getByteBuffer(String str) {
return ByteBuffer.wrap(str.getBytes());
}
private void start() {
try {
TTransport socket = new TSocket("10.230.13.100", 9090);// 我的虚拟机,线上用的thrift2
TProtocol protocol = new TBinaryProtocol(socket, true, true);// 注意这里
Hbase.Client client = new Hbase.Client(protocol);
socket.open();
System.out.println("open");
try {
System.out.println("scanning tables...");
for (ByteBuffer name : client.getTableNames()) {
System.out.println("find:" + byteBufferToString(name));
}
} catch (Exception e) {
e.printStackTrace();
}
socket.close();
System.out.println("close");
} catch (TTransportException e) {
e.printStackTrace();
} catch (TException e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
HaoClient c = new HaoClient();
c.start();

}
}


如果是thrift2的话
[color=red]hbase-daemon.sh start thrift2[/color]
thrift -r --gen java hbase.thrift
HaoClient2.java

package org.apache.hadoop.hbase.thrift2.generated;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;
public class HaoClient2 {
public static String byteBufferToString(ByteBuffer buffer) {
CharBuffer charBuffer = null;
try {
Charset charset = Charset.forName("UTF-8");
CharsetDecoder decoder = charset.newDecoder();
charBuffer = decoder.decode(buffer);
buffer.flip();
return charBuffer.toString();
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
public static ByteBuffer getByteBuffer(String str) {
return ByteBuffer.wrap(str.getBytes());
}
private void start() {
try {
TTransport socket = new TSocket("10.230.13.100", 9090);
// TTransport socket = new TSocket("10.77.112.191",9090);
//TTransport transport = new TFramedTransport(socket);
// TProtocol protocol = new TCompactProtocol(socket);
TProtocol protocol = new TBinaryProtocol(socket, true, true);//注意这里
// THBaseService.Client client = new THBaseService.Client(protocol);
THBaseService.Iface client = new THBaseService.Client(protocol);
socket.open();
System.out.println("open");
ByteBuffer table = ByteBuffer.wrap("mytable".getBytes());

/* TPut put = new TPut();
put.setRow("first".getBytes());

TColumnValue columnValue = new TColumnValue();
columnValue.setFamily("cf".getBytes());
columnValue.setQualifier("fromjava".getBytes());
columnValue.setValue("java is ok".getBytes());
List<TColumnValue> columnValues = new ArrayList<TColumnValue>();
columnValues.add(columnValue);
put.setColumnValues(columnValues);

client.put(table, put);*/
// byte[] aa=Bytes.toBytesBinary("\\x00\\x00\\x15o");
TGet get = new TGet();
get.setRow("first".getBytes());
TResult result = client.get(table, get);
System.out.println("row = " + new String(result.getRow()));
for (TColumnValue resultColumnValue : result.getColumnValues()) {
System.out.println("family = " + new String(resultColumnValue.getFamily()));
System.out.println("qualifier = " + new String(resultColumnValue.getQualifier()));
System.out.println("value = " + new String((resultColumnValue.getValue())));
System.out.println("timestamp = " + resultColumnValue.getTimestamp());
System.out.println("");
}
socket.close();
System.out.println("close");
} catch (TTransportException e) {
e.printStackTrace();
} catch (TException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
HaoClient2 c = new HaoClient2();
c.start();
}
}



注意需要,thrift1的Hbase.thirft
thrift2的hbase.thirft
还需要生成的代码,和hbase所有的jar

hbase的thrift1 和thrift2生成的类不一样
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
如果是c++连接的话
官方只给了个java和py的例子,网上有很多thrift1的接口的,但是没找到thirft2 的
自己写了个客户端如下
c++代码如下
thrift -r --gen cpp hbase.thrift
[code="java"]
# cat HbaseClient.cpp
#include "THBaseService.h"
#include <config.h>
#include <vector>
#include <transport/TSocket.h>
#include <transport/TBufferTransports.h>
#include <protocol/TBinaryProtocol.h>

using namespace std;
using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;
using namespace apache::hadoop::hbase::thrift2;

using boost::shared_ptr;

int main(int argc, char **argv) {
boost::shared_ptr<TSocket> socket(new TSocket("10.230.13.100", 9090));
boost::shared_ptr<TTransport> transport(new TBufferedTransport(socket));
boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));

transport->open();
printf("open\n");
THBaseServiceClient client(protocol);
TResult tresult;
TGet get;
//const std::string table="mytable";
const std::string table("mytable");
const std::string thisrow="first";
get.__set_row(thisrow);
//get.__set_row("first");
client.get(tresult,table,get);
vector<TColumnValue> list=tresult.columnValues;
std::vector<TColumnValue>::const_iterator iter;
for(iter=list.begin();iter!=list.end();iter++) {
printf("list size: %d\n",list.size());
printf("get : %s, %s,%s\n",(*iter).family.c_str(),(*iter).qualifier.c_str(),(*iter).value.c_str());//,(*iter).timestamp
}

transport->close();
printf("close\n");
return 0;
}
#
[/code]
Makefile如下
[code="java"]
# cat Makefile
BOOST_DIR = /usr/include/boost
THRIFT_DIR = /usr/local/thrift/include/thrift
#LIB_DIR = /usr/local/lib
LIB_DIR = /usr/local/thrift/lib/
GEN_SRC = ./gen-cpp/hbase_types.cpp ./gen-cpp/hbase_constants.cpp ./gen-cpp/THBaseService.cpp
default: client
client: HbaseClient.cpp
g++ -g -o HbaseClient -lm -pthread -lz -lrt -lssl -I${THRIFT_DIR} -I${BOOST_DIR} -I./gen-cpp -L${LIB_DIR} -lthrift HbaseClient.cpp ${GEN_SRC}
clean:
$(RM) -r HbaseClient


#
[/code]
代码目录
# tree
.
├── gen-cpp
│   ├── hbase_constants.cpp
│   ├── hbase_constants.h
│   ├── hbase_types.cpp
│   ├── hbase_types.h
│   ├── THBaseService.cpp
│   ├── THBaseService.h
│   └── THBaseService_server.skeleton.cpp
├── HbaseClient.cpp
├── hbase.thrift
└── Makefile

1 directory, 10 files
#
# ls
gen-cpp HbaseClient HbaseClient.cpp hbase.thrift Makefile
hbase需要先建立:
hbase shell
create 'mytable','cf'

# ./HbaseClient
open
list size: 4
get : cf, cf,value1
list size: 4
get : cf, foo,7
list size: 4
get : cf, fromjava,java is ok
list size: 4
get : cf, message,hello haonings hbase
close
#
注意设置
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib:/usr/local/thrift/lib
找thrift的so

插入的例子
[code="java"]
# cat HbaseClient.cpp
#include "THBaseService.h"
#include <config.h>
#include <vector>
#include <transport/TSocket.h>
#include <transport/TBufferTransports.h>
#include <protocol/TBinaryProtocol.h>

using namespace std;
using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;
using namespace apache::hadoop::hbase::thrift2;

using boost::shared_ptr;

int main(int argc, char **argv) {
// boost::shared_ptr<TSocket> socket(new TSocket("10.217.12.179", 9090));
boost::shared_ptr<TSocket> socket(new TSocket("10.77.112.191", 9090));
boost::shared_ptr<TTransport> transport(new TBufferedTransport(socket));
boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));

transport->open();
TPut put;
THBaseServiceClient client(protocol);
const std::string table="mytable";
const std::string thisrow="second";
put.__set_row(thisrow);
TColumnValue columnValue;
const std::string thisfamily="cf";
columnValue.__set_family(thisfamily);
const std::string thisqualifier="fromcpp";
columnValue.__set_qualifier(thisqualifier);
const std::string thisvalue="vppisok";
columnValue.__set_value(thisvalue);
columnValue.__set_timestamp(1395036661654);
vector<TColumnValue> columnValues;
columnValues.push_back(columnValue);
put.__set_columnValues(columnValues);
// std::cout<<"Texception"<<std::endl;
/* try{

}catch(TException &e){

}*/
client.put(table,put);
/*printf("open\n");
THBaseServiceClient client(protocol);
TResult tresult;
TGet get;
//const std::string table="mytable";
const std::string table("mytable");
const std::string thisrow="first";
get.__set_row(thisrow);
//get.__set_row("first");
client.get(tresult,table,get);
vector<TColumnValue> list=tresult.columnValues;
std::vector<TColumnValue>::const_iterator iter;
for(iter=list.begin();iter!=list.end();iter++) {
printf("list size: %d\n",list.size());
printf("get : %s, %s,%s\n",(*iter).family.c_str(),(*iter).qualifier.c_str(),(*iter).value.c_str());//,(*iter).timestamp
} */

transport->close();
printf("close\n");
return 0;
}
#
[/code]
java代码使用thrift2操作hbase示例 java代码使用thrift2操作hbase示例,thrift2连接hbase添加数据,单条查找,删除数据,根据扫描器查找,修改数据等测试实例 立即下载

相关推荐

thrift1 查询hbase

博客配套文件,详细演示了python通过thrift1接口查询hbase的方法。

thrift2访问hbase

使用thrift2访问hbase需求与环境项目中需要使用c++访问hbase批量导入数据,所以这两天配置了一个hadoop集群,安装了hadoop,zookeeper,hbase。 使用三台虚拟机,一个namenode节点和2个datanode节点。hbase thrift2简介hbase使用java语言编写,原生的hbase客户端是不支持C++的,所以选择使用thrift完成项目的开发。 th

指甲的专栏 7552

使用Java对Hbase操作总结及示例代码

主要介绍了使用Java对Hbase进行操作总结,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

通过thrift访问hbase

最近工作跟hadoop相关,中间用了一个thrift的东西,大概知道thrift提供rpc接口访问和操作hbase,更深入的东西,需要进一步研究。 https://yq.aliyun.com/articles/88299 ...

qq_15156403的博客 1247

Docker 安装 CentOS7

本文主要介绍了docker 下安装centos7。访问 CentOS 镜像库地址:https://hub.docker.com/_/centos?tab=tags&page=1。可以通过 Sort by 查看其他版本的 CentOS 。默认是最新版本 centos:latest 。 使用以下命令来查看是否已安装了 centos7: 启动centos容器,并把docker上centos的22端口映射到本机60001端口(端口号可以自己指定) 3、进入到Centos容器 4.安装ssh服务和网络必须软件

折腾java的博客 1万+

Abaqus梁模型仿真中的常见陷阱与优化策略

本文深入探讨了Abaqus梁模型仿真中的常见陷阱与优化策略,涵盖几何建模、材料属性设置、边界条件处理等关键环节。针对梁截面方向定义、网格划分、复合截面建模等典型问题,提供了实用的Python脚本示例和优化方案,帮助工程师避免计算错误并提升仿真精度。文章特别强调了动力分析中的网格密度要求和材料非线性考虑,为复杂工程应用提供专业指导。

weixin_29099217的博客 461

hbase集群 java连接_HBase分布式数据库开发——异常填坑1

在HBase分布式数据库开发中,为了操作方便,通过在本地执行HBase APP程序,来远程操作HBase分布式集群,如下图:基于Client和Server互通,即可在本地完成对HBase分布式集群的测试操作。问题来了,在执行过程中,很多开发者会遇到如下问题:2020-05-11 15:44:36,064 ERROR util.Shell (Shell.java:getWinUtilsPath(30...

weixin_39548541的博客 263

无人机基本组成部件

无人机基本组成部件

qq_37410522的博客 1万+

java hbase thrift_HBase Thrift协议编程入门教程

Thrift 是一个软件框架,用来进行可扩展且跨语言的服务的开发。Thrift 定义了一种描述对象和服务的接口定义语言(Interface Definition Language, IDL),它先通过 IDL 来定义 RPC 的接口和数据类型,再通过编译器生成不同语言的代码,并由生成的代码负责 RPC 协议层和传输层的实现。Thrift 支持的语言有 C++、Java、Python、PHP 等。下...

weixin_30456153的博客 628

2024 年高教社杯全国大学生数学建模竞赛题目【A/B/C/D/E题】完整论文+代码+结果

2024国赛A题参考论文。2024国赛D题参考论文。2024国赛E题参考论文。2024国赛B题参考论文。2024国赛C题参考论文。

qq_52590045的博客 5356

撸毛笔记-电信营业厅 v5.03

电信营业厅 v5.03签到和完成部分金豆任务, 喂宠物7天,15天,28天连签抽奖, 宠物等级权益兑换自己设置电信的服务密码, 把 手机号#服务密码 填到变量里:每天运行一两次const $ = new Env("电信营业厅");

xiaoshun007的专栏 4496

HBase实践篇 _ 为HBase的ThriftAPI设计连接池

文章目录1. 摘要2. commons-pool22.1 GenericObjectPool2.2 PooledObjectFactory接口2.3 BasePooledObjectFactory类2.4 GenericKeyedObjectPool带Key的对象池2.5 GenericObjectPoolConfig参数配置类3. HBase Thrift 客户端连接池的实现3.1 连接池核心实现类的UML图3.2 快速使用4. 把连接池的功能再包装成一个单例服务5. 总结6. 参考链接 1. 摘要 客户

wolf_333的博客 1348

127. 答网友疑问:ABAP Function Module 如何支持内表结构不确定的动态输入参数

ABAP RTTI 的全称是 Runtime Type Identification. 使用 RTTI,ABAP 开发人员可以在运行时获取关于数据对象的各种信息,包括其数据类型、字段列表(如果数据是Structure 或者内表的话)以及这些字段的技术属性,比如字段名称,字段数据类型,长度等等。对于这些字段信息,ABAP 提供了一组特殊的类和接口,包括 CL_ABAP_TYPEDESCR、CL_ABAP_STRUCTDESCR、CL_ABAP_TABLEDESCR 等等。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 1762

hbase和R之thirft探究

MR去调用R的时候导致R卡住,最终超时退出。分布式的测试比较痛苦。 free检查内容,top检查cpu,iostat检查硬盘,发现都未达到瓶颈,那么问题出在哪里?细细检查代码,一行一行定位,发现根本问题在于thirft。 下面摘录一段hbase配置thirft相关的参数 hbase.thrift.minWorkerThreads The "core size" of the thread

yibei8811的专栏 1004

保姆级教程!Python爬虫爬取知乎回答,保存为Markdown文件

本教程实现了知乎公开回答的完整爬取与Markdown保存,核心流程为「请求页面→解析作者/时间/正文/图片→下载图片→保存MD文件」,步骤清晰,代码可直接复用关键特性:支持合并/单文件两种保存模式、自动下载图片并引用、中文乱码防护、非法文件名过滤,满足日常整理知乎回答的需求新手友好:无需复杂配置,仅需修改知乎问题URL即可运行,全程无坑,5分钟内可看到运行结果灵活扩展:支持进阶优化(登录、分页、格式完善),可根据个人需求调整功能。

专注于Python爬虫开发,分享爬虫技巧、项目实战与反爬经验,使用Scrapy、BeautifulSoup等工具,解决数据抓取难题。 1232

点云处理指南介绍

指南目录,Open3D与PCL内容介绍

NNNNNathan的博客 1万+

thirft连接hbase : thrift1(c++) hbase 0.92 之前版本 ; thift2 (java/python) hbase 0.94 版本

thirft连接hbase的例子 hbase支持的thrift有两种方式 thrift和 thrift2 官方似乎推荐thrift2 一下是两种thrift的java客户端的连接方式 然后是一个cpp以thrift2的方式连接hbase的操作 如果是使用thrift1 hbase-daemon.sh start thrift 如果是使用thrift2 hbase-daemon.

425

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

Python3读取Hbase包hbase-thrift异常处理

python连接hbase需要用到hbase-thirft类库,但是hbase-thirft只在python2下能正常使用,如果在python3下,会报错, 主要有一下几个错误 : except IOError, io: invalid syntax ModuleNotFoundError: No module named 'ttypes' NameError:name ...

ai_1046067944的专栏 1278

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

java通过thrift连接hbase查询数据

文章目录1、依赖(thrift,非zk)2、连接3、查询4、数据筛选4.1 根据时间戳筛选4.2 根据行键筛选5、字段绑定 1、依赖(thrift,非zk) 参考:https://blog.youkuaiyun.com/chinabestchina/article/details/105720328 <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</a

微不足道的小博 2416

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

Python3使用Happybase操作Hbase

Python3使用Happybase操作Hbase GuidePython3使用Happybase操作Hbase前言准备1.开启thrift2.安装happybase库hbase-thrift报错开始使用happybase连接hbase连接池创建表查看表禁用/启用表删除表插入数据查询数据获取单元格数据获取一行数据获取多行数据扫描器获取数据filter过滤器删除数据批量处理数据 前言 Happybase官网文档: https://happybase.readthedocs.io/en/latest/api.h

Simon的博客 3042

pyspark通过thirft接口读写hbase

数据类型:hbase只支持字符串,而dataframe有许多的数据类型,如何进行类型转换?

sunseaxhj的博客 278

go通过thrift连接hbase_Hbase架构与原理

Hbase架构与原理HBase是一个分布式的、面向列的开源数据库,该技术来源于 Fay Chang所撰写的Google论文“Bigtable:一个结构化数据的分布式存储系统”。就像Bigtable利用了Google文件系统(File System)所提供的分布式数据存储一样,HBase在Hadoop之上提供了类似于Bigtable的能力。HBase是Apache的Hadoop项目的子项目。HBas...

weixin_39956350的博客 241

Hbase Thrift/Happybase 错误:TTransportException

原因是HBase判断超时,强制中断连接 需要在hbase-site.xml中手动加入如下两项(如果指定超时时间为一天) <property> <name>hbase.thrift.server.socket.read.timeout</name> <value>86400000</value&gt...

weixin_30466421的博客 548

用Python3.6操作HBase之HBase-Thrift

ubuntu上hbase的安装和简单使用参考:https://blog.youkuaiyun.com/luanpeng825485697/article/details/81027601 参考链接https://blog.youkuaiyun.com/luanpeng825485697/article/details/81048468 本机Linux下安装Thrift 0.11.0版本下载地址:http://thrift...

weixin_39965184的博客 1049

PHP通过Thrift操作Hbase

PHP通过Thrift操作Hbase

skiwnc的博客 1235

Thirft框架介绍

Thirft框架介绍 1.thrift 概述  Thrift是Apache下的一个子项目,最早是Facebook的项目,后来Facebook提供给Apache作为开源项目,在官网上,Thrift被描述为“Scalable Cross-Language Services Implementation”,说的通俗一些,Thrift具有以下特征:     1)它有自己的跨机器的通信框架,并提供...

freespace 299
上一篇: hbase的安装和测试
下一篇: nginx的远程调用模块
linux内核控
博客等级 码龄19年 41粉丝 638原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值