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

时间: 2024-07-30 13:38:23

hBase-thrift 实践(java)的相关文章

【转】HBase业务实践

原文链接 http://rdc.taobao.org/?p=457 HBase业务实践 2013/07/10 by 师允 · Leave a comment 源作者:我们团队最闷骚的武祖哥 适合读者 2012年因为业务需求,我们的底层数据库从Mysql迁移到HBase上面,正好也亲身经历了HBase-Client从0.92到0.94变化.我们总结了一些业务上面使用HBase的办法,希望本文能够对业务上面刚刚使用HBase的人一些帮助,降低入门门槛. 准备工作 HBase Toturial,需要对

Apache Thrift with Java Quickstart(thrift入门及Java实例)

thrift是一个软件框架,用来进行可扩展且跨语言的服务的开发.它结合了功能强大的软件堆栈和代码生成引擎,以构建在 C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, Smalltalk, and OCaml 这些编程语言间无缝结合的.高效的服务. 1. 概述 Thrift最初由facebook开发,07年四月开放源码,08年5月进入apache孵化器.thrift允许你定义一个

Atitit. 解压缩zip文件 的实现最佳实践 java c# .net php

Atitit. 解压缩zip文件 的实现最佳实践 java c# .net php 1. Jdk zip 跟apache ant zip 1 2. Apache Ant包进行ZIP文件压缩,upzip 大概流程.. 1 3. 读文件名称ok,但是cant读取到input说NPE.. 2 4. Ant1.8.2.jar 2 5. #---详细code 2 6. 参考 4 1.  Jdk zip 跟apache ant zip 下面实现的功能是zip文件中的图像文件解压到当前目录下,用jdk自带的处

atitit.人脸识别的应用场景and使用最佳实践 java .net php

atitit.人脸识别的应用场景and使用最佳实践 java .net php 1. 人脸识别的应用场景 1 2. 框架选型 JNI2OpenCV.dll and JavaCV 1 3. Url api 法 1 4. 使用法 2 5. 问题解决 2 6. 测试main修改 2 7. 主要的code 2 8. 参考 3 1. 人脸识别的应用场景 图片库清理...不个要保持的图片(有人脸/ngaiz,呵呵)跟个马用的图片分割开... 人物识别::::  不同样的人脸可以识别... 物体识别::: O

HBase的常用Java API

1. 创建HBase表的对象 HBase表的对项名字叫HTable,创建它的方法有很多,常见的有如下: org.apache.hadoop.hbase.client.HTable hTable = new HTable(org.apache.hadoop.hbase.HBaseConfiguration conf, String tableName); 或 org.apache.hadoop.hbase.client.HTable hTable = new HTable(org.apache.h

Thrift-java实例

?更多技术干货请戳:听云博客 Thrift实例1 功能描述:客户端与服务器端分别是两个应用,先启动服务器端,再启动客户端,实现执行客户端运行服务器端的加法方法. 源码截图(源码在附件中): 客户端: TestThriftClientServlet: SendRequestController: Pom.xml: 服务端: TestThriftServlet: ThriftServerController: IThriftServer:由thrift工具编译生成 ThriftServerServi

atitit.压缩算法 ZLib ,gzip ,zip 最佳实践 java .net php

atitit.压缩算法 ZLib ,gzip ,zip   最佳实践  java .net php 1. 压缩算法的归类::: 纯算法,带归档算法 1 2. zlib(适合字符串压缩) 1 3. gzip( 适合单个的文件) 1 4. zip 2 5. java jdk 给zlib,gzip,zip的支持 2 6. zlib---gzip 压缩在后长度比较 2 7. 别的bzip,,tar 2 8. 参考 3 1. 压缩算法的归类::: 纯算法,带归档算法 ZIP.RAR等归档算法 ZLib可以

Thrift的java和php数据交互

Thrift是一个软件框架(远程过程调用框架),用来进行可扩展且跨语言的服务的开发,封装了数据传输格式(二进制.json)和网络通信的服务框架,提供多语言(C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, Smalltalk, and OCaml)的网络服务器端和客户端程序组件 适用于搭建大型数据交换及存储的通用工具,对于大型系统中的内部数据传输相对于JSON和xml无论在性能

吐槽net下没有靠谱的FastDFS的sdk之使用thrift实现JAVA和C#互通

原文:吐槽net下没有靠谱的FastDFS的sdk之使用thrift实现JAVA和C#互通 事情是这样的,在一个新项目中引入了fastdfs,用这玩意做一些小数据的存储还是很方便的,然后在nuget上就找到了一个FastDFS的sdk,如下图: 一眼就看到了这个top1的sdk,应该会比较靠谱...简单的在项目中应用了一下没啥问题就忽悠上线了,然后就悲剧了,测试那边反馈说上传了一个 人群,拉下来的时候少了几个人,我的使用方式是将一批customerid按照bitmap的形式存到byte[]数组传

19 HBase SHELL、 JAVA 和 Thrift 客户端

HBase 由 Java 语言实现,同时他也是最主要最高效的客户端. 相关的类在org.apache.hadoop.hbase.client 包中.涵盖所有 增删改查 API . 主要的类包含: HTable.HBaseAdmin.Put.Get.Scan.Increment 和 Delete 等.         HBase 是一个 NoSQL数据库.JAVA客户端提供了增删改查表的DDL操作,同时提供了一些工具.类似合并.分裂 Region 等. 使用 HBase 原生客户端需要创建 配置类