HBase & thrift & C ++ Programming

Source: Internet
Author: User

HBase & thrift & C ++ Programming
HBase & thrift & csf-program examples directory

Directory 1

1. Preface 1

2. Start and Stop thrift2 1

2. 1. Start thrift2 1

2. Stop thrift2 1

2. 3. startup parameter 2

3. hbase. thrift2

3. 1. Compile hbase. thrift 2

4. thrift_helper.h3

5. Sample Code 4

1. Preface

This document describes how to operate HBase using C ++. Since HBase0.94, thrift2 has been added to HBase. This article only introduces and discusses thrift2. The thrift used by the hbase-1.1.2 is estimated to be thrift-0.9.0.

2. Start and Stop thrift22.1. start thrift2

Log on to the HBasemaster machine and run the following command to start thrift2: hbase-daemon.shstartthrift2.

The default listening port of thrift is 9090. You can use the "-p" parameter to specify other ports. The default Server is TThreadPoolServer. The default Protocol is TBinaryProtocol.

Note that the Protocol and Transport used by the client must be consistent with those used by the server. Otherwise, the client may encounter errors such as "EAGAIN (timedout)" during the call.

2. Stop thrift2

Hbase-daemon.shstopthrift2

2. 3. startup parameters

When using hbase-daemon.shstartthrift2, you can also include the following parameters:

Parameter Name

Default?

Parameter description

-H, -- help

Show Help Information

-B, -- bind

The specified address is bound, but the TNonblockingServer and THsHaServer are not supported. The two always use "0.0.0.0"

-P, -- port

9090

The specified port used to bind the room. The default port is 9090.

-F, -- framed

Use TFramedTransport

-C, -- compact

Use TCompactProtocol. The default value is TBinaryProtocol.

-Threadpool

Yes

Use TThreadPoolServer, which is the default Server

-Nonblocking

Use TNonblockingServer that implements FramedTransport

-Hsha

Use THsHaServer that implements FramedTransport

The Transport and Protocol of the client and hbasethrift2 must be consistent. For example, if the client is FramedTransport, you must start hbasethrift2 with "-f.

Otherwise, the client may encounter errors such as "EAGAIN (timedout)" during the call.

The startup parameter information comes from the official website page:

Bytes.

The above parameters are not used for the hbase-daemon.sh, but by hbasethrifle T2 use, you can browse the relevant source code to understand the details:

Hbase-thrift \ src \ main \ java \ org \ apache \ hadoop \ hbase \ thrit2 \ ThriftServer. java

Hbase-thrift \ src \ main \ java \ org \ apache \ hadoop \ hbase \ thrit2 \ thrithbaseservicehandler. java

? Startup example:

Hbase-daemon.shstartthrift2 -- framed-nonblocking

3. hbase. thrift

Location of hbaser. thrift file in hbase source code package (take hbase-1.1.2 as an example:

Hbase-thrift \ src \ main \ resources \ org \ apache \ hadoop \ hbase \ thrit2 \ hbase. thrift

3. 1. Compile hbase. thrift

Keep thrift installed on the machine (tested hbase-1.1.2 and thrift-0.9.0 compatible), then compile with the following command: thrift -- gencpp-out.hbase.thrift, after compilation successful, the following five files are generated in the directory specified by "-out:

THBaseService. h

THBaseService. cpp

Hbase_types.h

Hbase_types.cpp

Hbase_constants.h

Hbase_constants.cpp

The class THBaseServiceClient located in the THBaseService. h file is used by the client.

4. thrift_helper.h

To simplify the programming of the C ++ client, you can use thrift_helper.h:

Bytes:

// Thrift client helper class

//

// Example:

// Mooon: net: cthrifle tclienthelper <ExampleServiceClient> client (rpc_server_ip, rpc_server_port );

// Try

//{

// Client. connect ();

// Client-> foo ();

//}

// Catch (apache: thrift: transport: TTransportException & ex)

//{

// MYLOG_ERROR ("thritexception :( % d) % s \ n", ex. getType (), ex. what ());

//}

// Catch (apache: thrift: transport: TApplicationException & ex)

//{

// MYLOG_ERROR ("thritexception: % s \ n", ex. what ());

//}

// Catch (apache: thrift: TException & ex)

//{

// MYLOG_ERROR ("thritexception: % s \ n", ex. what ());

//}

// In addition to the default TFramedTransport (TBufferTransports. h), you can also select:

// TBufferedTransport (TBufferTransports. h)

// THttpTransport

// TZlibTransport

// TFDTransport (TSimpleFileTransport)

//

// In addition to the default apache: thrift: Protocol: TBinaryProtocol, you can also select:

// TCompactProtocol

// TJSONProtocol

// TDebugProtocol

Template <classthrifle tclient,

ClassProtocol = apache: thrift: protocol: TBinaryProtocol,

ClassTransport = apache: thrift: transport: TFramedTransport>

Classcthritclienthelper

5. Sample Code

// HBasethrift2C ++ programming example

# Include "THBaseService. h"

# Include <inttypes. h> // PRIu64

# Include <mooon/net/thrift_helper.h>

# Include <mooon/utils/args_parser.h>

# Include <vector>

// Note that the Transport and Protocol of the thrift used by the client must be consistent with that of the hbasethrit2 server,

// Otherwise, timeout or other errors may always be reported during the call !!!

//

// Before running, create a table through HBaseshell: create 'test', 'cf1', 'cf2'

// Or specify the number of VERSIONS: create 'test', {NAME => 'cf1', VERSIONS => 2}, {NAME => 'cf2', VERSIONS => 3}

// Delete the table and execute the following two HBaseshell commands in sequence:

// Disable 'test'

// Drop 'test'

STRING_ARG_DEFINE (hbase_ip, "192.168.0.1", "hbasethrifle tip ");

INTEGER_ARG_DEFINE (uint16_t, hbase_port, 9090,1000,50000, "hbasethriftport ");

Intmain (intargc, char * argv [])

{

Std: stringerrmsg;

If (! Mooon: utils: parse_arguments (argc, argv, & errmsg ))

{

Fprintf (stderr, "parametererror: % s \ n", errmsg. c_str ());

Exit (1 );

}

Usingnamespaceapache;

Usingnamespaceapache: hadoop;

Std: stringhbase_ip = mooon: argument: hbase_ip-> value ();

Uint16_thbase_port = mooon: argument: hbase_port-> value ();

Mooon: net: cthrifle tclienthelper

Try

{

Hbase_client.connect (); // connect to hbasethrift2server

Fprintf (stdout, "connect % s: % dok \ n", hbase_ip.c_str (), hbase_port );

Std: stringtablename = "test"; // table name. Make sure that you have created a table before running.

Std: stringrowkey = "row1"; // row Key

Std: stringfamily = "cf1"; // instance family name

Std: stringcolumnname = "f1"; // Instance name

Std: stringcolumnvalue = "value1"; // Instance value

// Insert parameter settings

Std: vector

Columns_value [0]. _ set_family (family );

Columns_value [0]. _ set_qualifier (columnname );

Columns_value [0]. _ set_value (columnvalue );

Hbase: thrift2: TPutput;

Put. _ set_row (rowkey );

Put. _ set_columnValues (columns_value );

Hbase_client-> put (tablename, put); // insert. An error is thrown. hbase: thrift2: TIOError

// Query parameter settings

Hbase: thrift2: TGetinput;

Input. _ set_row (rowkey );

Hbase: thrift2: TResultresult; // the query result is stored here.

Hbase_client-> get (result, tablename, input); // query, throwing an error hbase: thrift2: TIOError

// Display query results

For (inti = 0; I <static_cast <int> (result. columnValues. size (); ++ I)

{

Consthbase: thrift2: TColumnValue & column_value_ref = result. columnValues [I];

Fprintf (stdout, "family [% s]/qualifier [% s]/timestamp [%" PRIu64 "]: % s \ n", column_value_ref.family.c_str (),

Column_value_ref.qualifier.c_str (),

Column_value_ref.timestamp,

Column_value_ref.value.c_str ());

}

}

Catch (hbase: thrift2: TIOError & ex)

{

Fprintf (stderr, "IOError: % s \ n", ex. what ());

}

Catch (apache: thrift: transport: TTransportException & ex)

{

// If it is different from the Transport and Protocol on the server, the error here is "EAGAIN (timedout )"

Fprintf (stderr, "(% d) % s \ n", ex. getType (), ex. what ());

}

Catch (apache: thrift: TApplicationException & ex)

{

Fprintf (stderr, "% s \ n", ex. what ());

}

Catch (thrift: TException & ex)

{

Fprintf (stderr, "% s \ n", ex. what ());

}

Return0;

}

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.