- 浏览: 1489737 次
- 性别:
- 来自: 北京
-
文章分类
- 全部博客 (691)
- linux (207)
- shell (33)
- java (42)
- 其他 (22)
- javascript (33)
- cloud (16)
- python (33)
- c (48)
- sql (12)
- 工具 (6)
- 缓存 (16)
- ubuntu (7)
- perl (3)
- lua (2)
- 超级有用 (2)
- 服务器 (2)
- mac (22)
- nginx (34)
- php (2)
- 内核 (2)
- gdb (13)
- ICTCLAS (2)
- mac android (0)
- unix (1)
- android (1)
- vim (1)
- epoll (1)
- ios (21)
- mysql (3)
- systemtap (1)
- 算法 (2)
- 汇编 (2)
- arm (3)
- 我的数据结构 (8)
- websocket (12)
- hadoop (5)
- thrift (2)
- hbase (1)
- graphviz (1)
- redis (1)
- raspberry (2)
- qemu (31)
- opencv (4)
- socket (1)
- opengl (1)
- ibeacons (1)
- emacs (6)
- openstack (24)
- docker (1)
- webrtc (11)
- angularjs (2)
- neutron (23)
- jslinux (18)
- 网络 (13)
- tap (9)
- tensorflow (8)
- nlu (4)
- asm.js (5)
- sip (3)
- xl2tp (5)
- conda (1)
- emscripten (6)
- ffmpeg (10)
- srt (1)
- wasm (5)
- bert (3)
- kaldi (4)
- 知识图谱 (1)
最新评论
-
wahahachuang8:
我喜欢代码简洁易读,服务稳定的推送服务,前段时间研究了一下go ...
websocket的helloworld -
q114687576:
http://www.blue-zero.com/WebSoc ...
websocket的helloworld -
zhaoyanzimm:
感谢您的分享,给我提供了很大的帮助,在使用过程中发现了一个问题 ...
nginx的helloworld模块的helloworld -
haoningabc:
leebyte 写道太NB了,期待早日用上Killinux!么 ...
qemu+emacs+gdb调试内核 -
leebyte:
太NB了,期待早日用上Killinux!
qemu+emacs+gdb调试内核
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
如果是thrift2的话
hbase-daemon.sh start thrift2
thrift -r --gen java hbase.thrift
HaoClient2.java
注意需要,thrift1的Hbase.thirft
thrift2的hbase.thirft
还需要生成的代码,和hbase所有的jar
hbase的thrift1 和thrift2生成的类不一样
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
如果是c++连接的话
官方只给了个java和py的例子,网上有很多thrift1的接口的,但是没找到thirft2 的
自己写了个客户端如下
c++代码如下
thrift -r --gen cpp hbase.thrift
Makefile如下
代码目录
[root@mytest HbaseThrift]# 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
[root@mytest HbaseThrift]#
[root@mytest HbaseThrift]# ls
gen-cpp HbaseClient HbaseClient.cpp hbase.thrift Makefile
hbase需要先建立:
hbase shell
create 'mytable','cf'
[root@mytest HbaseThrift]# ./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
[root@mytest HbaseThrift]#
注意设置
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib:/usr/local/thrift/lib
找thrift的so
插入的例子
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的话
hbase-daemon.sh start thrift2
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
[root@mytest HbaseThrift]# 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; } [root@mytest HbaseThrift]#
Makefile如下
[root@mytest HbaseThrift]# 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 [root@mytest HbaseThrift]#
代码目录
[root@mytest HbaseThrift]# 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
[root@mytest HbaseThrift]#
[root@mytest HbaseThrift]# ls
gen-cpp HbaseClient HbaseClient.cpp hbase.thrift Makefile
hbase需要先建立:
hbase shell
create 'mytable','cf'
[root@mytest HbaseThrift]# ./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
[root@mytest HbaseThrift]#
注意设置
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib:/usr/local/thrift/lib
找thrift的so
插入的例子
[root@mytest HbaseThrift]# 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; } [root@mytest HbaseThrift]#
- HbaseThrift.tar.gz (394.2 KB)
- 下载次数: 78
- thrift1.java.rar (2.4 MB)
- 下载次数: 45
- thrift2.java.rar (2 MB)
- 下载次数: 54
发表评论
-
gcc链接顺序
2019-10-12 18:25 679代码在 https://github.com/killinux ... -
hadoop复习 1搭建
2014-07-23 17:38 789参考http://hadoop.apache.org/docs ... -
hbase的安装和测试
2014-03-17 16:04 849使用hbase-0.94.17.tar.gz 如果想练机使用必 ... -
thrift的测试
2014-03-13 17:20 1982尝试失败的版本 0.9.1 要求autoconf是2.56以上 ... -
sqoop从mysql到数据到hadoop中
2014-03-11 13:54 2698groupadd hadoop useradd hadoop ...
相关推荐
Python连接HBase的过程主要分为以下几个步骤: 1. **安装依赖**:在Python环境中,你需要安装`happybase`库,它是一个基于Thrift的Python库,使得与HBase的交互变得更加简单。你可以通过`pip`来安装: ``` pip ...
在Python连接HBase的情况下,Thrift充当了Python与HBase之间的桥梁,允许Python程序通过Thrift调用HBase的API。 对于Python连接HBase的lib,通常我们需要一个名为`happybase`的Python库。这个库是一个易于使用的...
一旦thrift安装完成,开发者就可以开始编写服务定义和客户端代码,连接并操作HBase。 需要注意的是,操作过程中可能由于OCR扫描识别错误或者漏识别的情况,导致部分文字不准确。在实际操作中,应当根据提示和命令...
在表面之下, ehbase使用 Erlang Thrift 库通过其 Thrift 网关连接到 HBase,该网关包含在标准 HBase 0.9x 版本中。 另外, ehbase使用进行连接的池封装。 但是我使用了,它支持更多功能,并且出于某种原因暂时不向...
- **HBase MapReduce示例**:提供了一些使用HBase进行MapReduce编程的例子。 - **跨表访问**:在一个MapReduce作业中访问多个HBase表的方法。 #### 八、HBase安全 - **客户端访问控制**:确保只有授权用户可以访问...
第一印象——两个简单的例子 17 2.1.1 简单的位置偏好数据集 17 2.1.2 存储汽车品牌和型号数据 22 2.2 使用多种语言 30 2.2.1 MongoDB驱动 30 2.2.2 初识Thrift 33 2.3 小结 34 第3章 NoSQL接口与交互 36 ...
2.4.2 Avro,REST,以及Thrift 2.5 示例 2.5.1 模式 2.5.2 加载数据 2.5.3 Web查询 2.6 HBase和RDBMS的比较 2.6.1 成功的服务 2.6.2 HBase 2.6.3 实例:HBase在Streamy.com的使用 2.7 Praxis 2.7.1 版本 2.7.2 HDFS ...
2.4.2 Avro,REST,以及Thrift 2.5 示例 2.5.1 模式 2.5.2 加载数据 2.5.3 Web查询 2.6 HBase和RDBMS的比较 2.6.1 成功的服务 2.6.2 HBase 2.6.3 实例:HBase在Streamy.com的使用 2.7 Praxis ...
- **数据传输**: 介绍了如何使用Hadoop管道来连接不同的MapReduce任务,实现数据的高效传输。 #### 三、Hadoop分布式文件系统(HDFS) **3.1 HDFS的设计** - **设计原则**: HDFS的设计着重于提供高吞吐量的数据访问...