hBase-thrift 实践(java)
参考官网:
http://wiki.apache.org/hadoop/Hbase/ThriftApi
环境:hbase-0.98.1-cdh5.1.0,hadoop-2.3.0-cdh5.1.0,centos6.5 x64,thrift2
1.引入maven依赖
hbase已整合了thrift,如果是java不用再安装thrift产生服务端代码,只引入下面依赖:
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-thrift</artifactId>
<version>0.98.1-cdh5.1.0</version>
</dependency>
2.开启hbase-thrift服务
这里采用thrift2,thrift2是thrift的升级版。
[hbase-root]/bin/hbase thrift2 start
默认端口是9090
3.编写客户端示例程序
实现了新增一条记录,查询一条记录
/**
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jamesfen.hbase; import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List; import org.apache.hadoop.hbase.thrift2.generated.TColumnValue;
import org.apache.hadoop.hbase.thrift2.generated.TGet;
import org.apache.hadoop.hbase.thrift2.generated.THBaseService;
import org.apache.hadoop.hbase.thrift2.generated.TIOError;
import org.apache.hadoop.hbase.thrift2.generated.TPut;
import org.apache.hadoop.hbase.thrift2.generated.TResult;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport; public class DemoClient {
public static void main(String[] args) throws TIOError, TException {
System.out.println("Thrift2 Demo");
System.out.println("Usage: DemoClient [host=localhost] [port=9090]");
System.out.println("This demo assumes you have a table called \"example\" with a column family called \"family1\""); String host = "192.168.58.101";
int port = 9090; // use passed in arguments instead of defaults
if (args.length >= 1) {
host = args[0];
}
if (args.length >= 2) {
port = Integer.parseInt(args[1]);
} int timeout = 10000;
boolean framed = false; TTransport transport = new TSocket(host, port, timeout);
if (framed) {
transport = new TFramedTransport(transport);
}
TProtocol protocol = new TBinaryProtocol(transport);
// This is our thrift client.
THBaseService.Iface client = new THBaseService.Client(protocol); // open the transport
transport.open(); ByteBuffer table = ByteBuffer.wrap("blog".getBytes()); TPut put = new TPut();
put.setRow("103".getBytes()); TColumnValue columnValue = new TColumnValue();
columnValue.setFamily("article".getBytes());
columnValue.setQualifier("title,".getBytes());
columnValue.setValue("change thirft".getBytes());
List<TColumnValue> columnValues = new ArrayList<TColumnValue>();
columnValues.add(columnValue);
put.setColumnValues(columnValues); client.put(table, put); TGet get = new TGet();
get.setRow("102".getBytes()); TResult result = client.get(table, get); System.out.print("row = " + new String(result.getRow()));
for (TColumnValue resultColumnValue : result.getColumnValues()) {
System.out.print(",family = " + new String(resultColumnValue.getFamily()));
System.out.print(",qualifier = " + new String(resultColumnValue.getFamily()));
System.out.print(",value = " + new String(resultColumnValue.getValue()));
System.out.print(",timestamp = " + resultColumnValue.getTimestamp());
} transport.close();
}
}
4.运行结果
row = 102,family = article,qualifier = article,value = change thirft,timestamp = 1423496756997
版权声明:本文为博主原创文章,未经博主允许不得转载。
hBase-thrift 实践(java)的更多相关文章
- Thrift实践
Thrift实践:(一)安装 -- 未完待续 1. 新建一个目录,C:\test\thrift-test,里面建2个子文件夹,client-node和sever-csharp,然后把Thrift官 ...
- hbase thrift 定义
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agre ...
- HBase & thrift & C++编程
目录 目录 1 1. 前言 1 2. 启动和停止thrift2 1 2.1. 启动thrift2 1 2.2. 停止thrift2 1 2.3. 启动参数 2 3. hbase.thrift 2 3. ...
- 【Hbase三】Java,python操作Hbase
Java,python操作Hbase 操作Hbase python操作Hbase 安装Thrift之前所需准备 安装Thrift 产生针对Python的Hbase的API 启动Thrift服务 执行p ...
- Atitit.http httpclient实践java c# .net php attilax总结
Atitit.http httpclient实践java c# .net php attilax总结 1. Navtree>> net .http1 2. Httpclient理论1 2. ...
- Thrift-java实例
➠更多技术干货请戳:听云博客 Thrift实例1 功能描述:客户端与服务器端分别是两个应用,先启动服务器端,再启动客户端,实现执行客户端运行服务器端的加法方法. 源码截图(源码在附件中): 客户端: ...
- Atitit. 解压缩zip文件 的实现最佳实践 java c# .net php
Atitit. 解压缩zip文件 的实现最佳实践 java c# .net php 1. Jdk zip 跟apache ant zip 1 2. Apache Ant包进行ZIP文件压缩,upzip ...
- atitit.压缩算法 ZLib ,gzip ,zip 最佳实践 java .net php
atitit.压缩算法 ZLib ,gzip ,zip 最佳实践 java .net php 1. 压缩算法的归类::: 纯算法,带归档算法 1 2. zlib(适合字符串压缩) 1 3. gz ...
- atitit.人脸识别的应用场景and使用最佳实践 java .net php
atitit.人脸识别的应用场景and使用最佳实践 java .net php 1. 人脸识别的应用场景 1 2. 框架选型 JNI2OpenCV.dll and JavaCV 1 3. Url ap ...
- 吐槽net下没有靠谱的FastDFS的sdk之使用thrift实现JAVA和C#互通
事情是这样的,在一个新项目中引入了fastdfs,用这玩意做一些小数据的存储还是很方便的,然后在nuget上就找一个对接FastDFS的sdk,如下图: 一眼就看到了这个top1的sdk,应该会比较靠 ...
随机推荐
- mysql 数据备份及pymysql模块
一 IDE工具介绍 生产环境还是推荐使用mysql命令行,但为了方便我们测试,可以使用IDE工具 下载链接:https://pan.baidu.com/s/1bpo5mqj 掌握: #1. 测试+链接 ...
- 原生JDBC的使用
public class ConnDB { private Connection ct = null; //驱动程序名 String driver = "com.mysql.jdbc.Dri ...
- 寻找节点d=n的节点算法
这里的算法是毕设过程中,自己想到的,也不知道有不有人提出过.这里就记录下发现的过程的具体的算法,以后会用到 背景描述 毕设做的是「社交网络中病毒传播的预测」,前期过程主要是模拟几个网络的数据,然后从一 ...
- CATransform3D 矩阵变换之立方体旋转实现细节 (转)
原文地址 http://blog.csdn.net/ch_soft/article/details/7351896 第一部分.前几天做动画,使用到了CATransform3D ,由于没有学过计算机图形 ...
- Broken Keyboard(模拟数组或者双重链表的运用)
这题我是大写的服气,辛辛苦苦搞了个双重链表结果还一直不对,不对就算了,书上源代码打进去还是不对,我能怎么办我也很无奈.不过这题还是让我对双重链表更加了解和运用了!还是可以的! You’re typin ...
- unity 查看打包资源占用
想要压缩包大小,首先得知道打包出来的各个资源的大小,明确知道哪些资源占用大,可以通过如下操作打开Editor.log(可能需要先输出一遍安卓包) 1.在Unity Console界面右上角点开Open ...
- Httpclient远程调用WebService示例
我们将Web Service发布在Tomcat或者其他应用服务器上后,有很多方法可以调用该Web Service,常用的有两种: 1.通过浏览器HTTP调用,返回规范的XML文件内容 2.通 ...
- J2EE分布式服务基础之RPC
一.RPC介绍 什么是RPC 远程过程调用(RPC)是一个协议,程序可以使用这个协议请求网络中另一台计算机上某程序的服务而不需知道网络细节. RPC模型 C/S模式 基于传输层协议 (例如 TCP/I ...
- 两种设置WebLogic启动内存的方法
我们经常在使用WebLoigc部署应用程序后,发现程序运行速度并不是很快,遇到这种情况我们可以尝试调整启动时分配的内存,设置方法有两种: 一.在..\domain\startWebLoigc.* ...
- 蓝桥杯 算法训练 ALGO-118 连续正整数的和
算法训练 连续正整数的和 时间限制:1.0s 内存限制:256.0MB 问题描述 78这个数可以表示为连续正整数的和,1+2+3,18+19+20+21,25+26+27. 输入一个正整数 n ...