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





版权声明:本文为博主原创文章,未经博主允许不得转载。

转载于:https://www.cnblogs.com/jamesf/p/4751450.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值