Hbase-thrift Practice (Java)

Source: Internet
Author: User

Reference website:

Http://wiki.apache.org/hadoop/Hbase/ThriftApi

Environment: hbase-0.98.1-cdh5.1.0,hadoop-2.3.0-cdh5.1.0,centos6.5 x64,thrift2


1. Introducing MAVEN Dependencies

HBase has integrated thrift, and if Java does not have to install thrift to generate the server code, only the following dependencies are introduced:

<dependency><groupid>org.apache.hbase</groupid><artifactid>hbase-thrift</artifactid ><version>0.98.1-cdh5.1.0</version></dependency>

2. Turn on Hbase-thrift service

The use of THRIFT2,THRIFT2 is an upgraded version of thrift.

[Hbase-root]/bin/hbase thrift2 start

The default port is 9090


3. Writing the Client sample program

Implemented a new record to query a record

/** * * 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 * under the Apache License, Version 2.0 (The * "License");  You are not a use of this file except in compliance * with the License. Obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * unless required by applicab Le law or agreed into writing, software * Distributed under the License is distributed on a "as is" BASIS, * without WAR Ranties 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 has 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 (Resultco      Lumnvalue.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. Running Results

row = 102,family = Article,qualifier = Article,value = Change Thirft,timestamp = 1423496756997





Hbase-thrift Practice (Java)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.