// 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; } |