c++连接hbase指南

来源:互联网 发布:疯狂联盟升级数据 编辑:程序博客网 时间:2024/05/19 22:48

环境配置

1、  安装thrift

按照官网http://thrift.apache.org/要求及步骤配置安装thrift

2、  取出Hbase文件中的Hbase.thrift文件,是用thrift--gen <language> <Thrift filename>产生所需的cpp文件,将gen_cpp文件复制到工程目录下。其中Hbase.cpp是CPP版的thrift文件中的函数,Hbase_types.cpp是CPP版的thrift文件中的结构。

3、  Hbase启动thrift服务./bin/hbase-daemon.sh start thrift                默认监听地址9090

以上步骤做好之后就可使用C++连接hbase并对其中数据库进行操作了

 

操作步骤

1、  创建连接

boost::shared_ptr<TTransport>socket(new TSocket("host",port(int)));

boost::shared_ptr<TTransport>transport(new TBufferedTransport(socket));

boost::shared_ptr<TProtocol>protocol(new TBinaryProtocol(transport));

HbaseClient client(protocol);

transport->open();    失败返回0并抛出TException错误

 

2、  连接成功后,可以通过client调用Hbase.thrift中定义的函数对hbase进行操作

3、  断开连接

transport->close()     失败抛出TException错误

 

HbaseClient类中定义的结构及函数

这些结构和函数都是在Hbase.thrift中定义,C++定义是由thrift读取Hbase.thrift生成的并存放在生成的cpp文件中,若要是用需在工程中包含这些文件

一、          异常

前三个异常类都包含一个string成员message,不同的函数出错会抛出不同的错误

1、  IOError               操作过程中的一般性错误

2、  IllegalArgument        函数传入参数错误

3、  AlreadyExists     如建表表已存在

4、   TException                   thrift服务的异常类,前三个都是Hbase.thrift中定义,生成cpp文件的定义中前三个都是都是这个异常类的子类

 

二、          数据类(等于多少表示默认值)

1、  TCell

1: Stringvalue;   一般是对应列数据

2: I64timestame;

2、  ColumnDescriptor:包含数据库中表的某个列族的信息

  1:string  name,

  2:i32 maxVersions = 3,

  3:string compression = "NONE",

  4:bool inMemory = 0,

  5:string bloomFilterType = "NONE",

  6:i32 bloomFilterVectorSize = 0,

  7:i32 bloomFilterNbHashes = 0,

  8:bool blockCacheEnabled = 0,

  9:i32 timeToLive = -1

3、   TRegionInfo:包含表中某个区域的信息

 1:stringstartKey,

2:string endKey,

 3:i64 id,

 4:stringname,

 5:stringversion

4、   Mutation:         用来更新或删除一个column-value

 1:boolisDelete = 0,

 2:stringcolumn,

 3:stringvalue

5、   BatchMutation:       单列多个mutation的结构

 1:stringrow,

 2:vector<Mutation>mutations

6、   TRowResult:  

 1:stringrow,     rowkey

 2:map<Text, TCell> columns     对应column-value值,Text为列名

7、   TScan:    用以scanner相关函数

 1:optionalstring startRow,                  起始rowkey

 2:optionalstring stopRow,                  终止rowkey

 3:optionali64 timestamp,

 4:optionallist<string> columns,

 5:optionali32 caching,

 6:optionalstring filterString

8、  Typedef ScannerID i32

9、  Typedef    Text(Byte) string

 

三、          操作函数(除非注明,返回值都为void)

1、  enableTable(string tablename) throws(IOError);           启用一个表

2、  disableTable(string tablename) throws(IOError);           禁用一个表

3、  bool isTableEnabled(string tablename) throws(IOError); 判断表释放启用

4、  compact(string tablenameorregionname) throws(IOError)

5、  majorCompact(string tablenameorregionname) throws(IOError)

4,5调用使hbase合并相应小文件

6、   getTableNames(std::vector<string> &_return);   throws(IOError) 

将用户空间所有表存在_return中

7、   getColumnDescriptors(std::map<Text,ColumnDescriptor> & _return,constText& tableName); throws(IOError),

将表tableName中所有列族信息存在_return中

8、   getTableRegions(std::vector<TRegionInfo>& _return, const Text& tableName);  throws(IOError)

返回表tableName中的Regions信息,返回值在return中

9、   createTable(constText& tableName,conststd::vector<ColumnDescriptor> & columnFamilies);  throws (1:IOError io, 2:IllegalArgument ia,3:AlreadyExists exist)

创建一个表为为tableName,拥有列族columnFamilies的表

10、  deleteTable(constText& tableName);                  throws(IOError)

删除表

11、  get(std::vector<TCell> & _return, const Text& tableName,constText& row,const Text& column);   throws(IOError)

获得表中指定rowkey,指定行的TCell

12、  getVer(std::vector<TCell> & _return, const Text& tableName, constText& row, const Text& column, const int32_t numVersions); throws(IOError)

获得表中指定rowkey,指定列中numVersion个TCell

13、  getVerTs(std::vector<TCell> & _return, const Text& tableName, constText& row, const Text& column, const int64_t timestamp,constint32_t numVersions);         throws(IOError)

获得表中指定rowkey,指定列中numVersions个Tcell,其中Tcell的时间戳要小于或等于给定的时间戳,行不存在返回值空

14、  getRow(std::vector<TRowResult> &_return, const Text& tableName, const Text& row);  throws(IOError)

获得表中指定rowKey最近时间戳的所在行的所有列的数据

15、  getRowWithColumns(std::vector<TRowResult>& _return, const Text& tableName, const Text& row, conststd::vector<Text> & columns);            throws(IOError)

获得表中指定行最近时间戳的所在行的指定列的数据

16、  getRowTs(std::vector<TRowResult> &_return, const Text& tableName, const Text& row, constint64_t timestamp);         throws(IOError)

获得表中指定rowkey,指定时间戳的行的所有列数据

17、  getRowWithColumnsTs(std::vector<TRowResult>& _return, const Text& tableName, const Text& row, conststd::vector<Text> & columns, constint64_t timestamp);              throws(IOError)

获得表中指定rowkey,指定时间戳的行的指定列数据

18、  getRows(std::vector<TRowResult> &_return, const Text& tableName, const std::vector<Text> & rows); throws(IOError)

rows中元素为rowkey,返回表中含有rows中最近时间戳的行数据

19、  getRowsWithColumns(std::vector<TRowResult>& _return, const Text& tableName, const std::vector<Text> & rows,const std::vector<Text> & columns);   throws(IOError)

与18相似,此次返回指定列

20、  getRowsTs(std::vector<TRowResult> &_return, const Text& tableName, const std::vector<Text> & rows,const int64_t timestamp);           throws(IOError)

获得表中指定rowkey,指定时间戳的行

21、  getRowsWithColumnsTs(std::vector<TRowResult>& _return, const Text& tableName, const std::vector<Text> & rows,const std::vector<Text> & columns,const int64_t timestamp);     throws(IOError)

与20相似,此处增加指定列

22、  mutateRow(constText& tableName,const Text& row,const std::vector<Mutation> & mutations);             throws(1:IOError,  2:IllegalArgument)         throws(1:IOError,2:IllegalArgument)

更新或删除指定rowkey的指定列,其中数据使用当时时间作为时间戳,不同mutation插入后时间戳相同

23、  mutateRowTs(constText& tableName,const Text& row,const std::vector<Mutation> & mutations,const int64_t timestamp); throws(1:IOError,2:IllegalArgument)

与22相似,不同在于此处指定了时间戳

24、  mutateRows(constText& tableName,conststd::vector<BatchMutation> & rowBatches); throws(1:IOError, 2:IllegalArgument)

与22相似,输入参数变为可能有多个行

25、  mutateRowsTs(constText& tableName,conststd::vector<BatchMutation> & rowBatches,constint64_t timestamp);        throws(1:IOError, 2:IllegalArgument)

与23相似,输入参数变为可能有多个行

26、  int64_t atomicIncrement(constText& tableName,const Text& row,const Text& column,constint64_t value);           throws(1:IOError,2:IllegalArgument)

指定rowKey制定列的值增加value,返回增加后的值

27、  deleteAll(constText& tableName,const Text& row,const Text& column);                  throws(IOError)

删除表中符合row,column条件的cells

28、  deleteAllTs(constText& tableName,const Text& row,const Text& column,constint64_t timestamp);      throws(IOError)

删除表中符合row,column条件并且时间戳等于或早于给定时间戳的cells

29、  deleteAllRow(constText& tableName,const Text& row); throws(IOError)

完全删除指定rowkey的cell

30、  deleteAllRowTs(constText& tableName,const Text& row,const int64_t timestamp);        throws(IOError)

完全删除指定rowkey,并且时间戳等于或早于给定时间戳的cells

31、  ScannerID scannerOpenWithScan(const Text& tableName,constTScan& scan);      throws(IOError)

在表上打开一个scanner,scanner条件在一个TScan结构的传入参数定义,返回ScannerID

32、  ScannerID scannerOpen(constText& tableName,const Text& startRow,const std::vector<Text> & columns);           throws(IOError)

在表上打开一个scanner,输入参数为这个scanner的起始rowkey和需要访问的列,返回scannerID。

33、  ScannerID scannerOpenWithStop(const Text& tableName,constText& startRow,const Text& stopRow,const std::vector<Text> & columns);            throws(IOError)

与32相似,但是多指定了scanner的中止行

34、  ScannerID scannerOpenWithPrefix(const Text& tableName,constText& startAndPrefix,conststd::vector<Text> & columns)  throws(IOError)

这个scanner的rowkey有指定的前缀

35、  ScannerID scannerOpenTs(constText& tableName,const Text& startRow,const std::vector<Text> & columns,const int64_t timestamp);                  throws(IOError)

这个scanner只返回指定时间戳的数据

36、  ScannerID scannerOpenWithStopTs(const Text& tableName,constText& startRow,const Text& stopRow,const std::vector<Text> & columns,const int64_t timestamp);                       throws(IOError)

与33相似,但多了一个条件,这个scanner只返回指定时间戳的行数据

37、  scannerGet(std::vector<TRowResult> &_return, const ScannerID id);                throws(1:IOError,2:IllegalArgument)

获得ScannerID这个scanner的当前行数据并且让他指向下行,若没数据读了则运行之后_return为空

38、  scannerGetList(std::vector<TRowResult>& _return, const ScannerID id, const int32_t nbRows);               throws(1:IOError, 2:IllegalArgument)

与37相似,不过一次返回多行

39、  scannerClose(constScannerID id);

关闭一个scanner

原创粉丝点击