首页 理论教育 HBase常用JavaAPI详解

HBase常用JavaAPI详解

时间:2023-06-26 理论教育 版权反馈
【摘要】:HBase主要包括5大类操作:HBase的配置、HBase表的管理、列族的管理、列的管理、数据操作等。该接口用来管理HBase数据库的表信息。HTableDescriptor包含了表的详细信息。获取表中数据,示例如下:Configuration configuration=HBaseConfiguration.create();Connection connection=ConnectionFactory.createConnection;Table table=connection.getTable();ResultScanner scanner=table.getScanner;org.apache.hadoop.hbase.client.Put。向表里添加数据的示例如下:Configuration configuration=HBaseConfiguration.create();Connection connection=ConnectionFactory.createConnection;Table table=connection.getTable();Put put=new Put;//—个Put代表一行数据,行键为构造方法中传入的值put.addColumn;//本行数据的第一列put.addColumn;//本行数据的第二列put.addColumn;//本行數据的第三列put.add;//本行数据的第四列table.put;org.apache.hadoop.hbase.client.Get。

HBase常用JavaAPI详解

HBase主要包括5大类操作:HBase的配置、HBase表的管理、列族的管理、列的管理、数据操作等。

(1)org.apache.hadoop.hbase.HBaseConfiguration。

HBaseConfiguration类用于管理HBase的配置信息,使用举例如下。

static Configuration cfg=HBaseConfiguration.create();

(2)org.apache.hadoop.hbase.client.Admin。

Admin是Java接口类型,不能直接用该接口来实例化一个对象,而是必须通过调用Connection.getAdmin()方法,来调用返回子对象的成员方法。该接口用来管理HBase数据库的表信息。它提供的方法包括创建表,删除表,列出表项,使表有效或无效,以及添加或删除表列族成员等。

例如,通过Connection.getAdmin()方法实例化一个对象并创建表示例如下:

(3)org.apache.hadoop.hbase.HTableDescriptor。

HTableDescriptor包含了表的详细信息。创建表时添加列族使用的例子如下。

HTableDescriptor tableDescriptor=new HTableDescriptor(tableName);//表的数据模式

tableDescriptor.addFamily(new HColumnDescriptor("name"));//增加列族

tableDescriptor.addFamily(new HColumnDescriptor("age"));

tableDescriptor.addFamily(new HColumnDescriptor("gender"));

admin.createTable(tableDescriptor);

(4)org.apache.hadoop.hbase.HColumnDescriptor。

HColumnDescriptor类维护着关于列族的信息,如版本号、压缩设置等。它通常在创建表或者为表添加列族的时候使用。列族被创建后不能直接修改,只能通过删除然后重新创建的方式。列族被删除的时候,列族里面的数据也会同时被删除。

创建表时添加列族的例子如下。

HTableDescriptor tableDescriptor=new HTableDescriptor(tableName);//表的数据模式

HColumnDescriptor hcd=HColumnDescriptor("name".getBytes.());//构造列族

hcd.setValue("firstName".getBytes(),"John".getBytes());//给列族添加列

hcd.setValue("lastName".getBytes(),"Bates".getBytes());//给列族添加列

tableDescriptor.addFamily(hcd);//把列族加入表描述

(5)org.apache.hadoop.hbase.client.Table。

Table是Java接口类型,不可以用Table直接实例化一个对象,而是必须通过调用connection.getTable()的一个子对象,来调用返回子对象的成员方法。这个接口可以用来和HBase表直接通信,可以从表中获取数据、添加数据、删除数据和扫描数据。

获取表中数据,示例如下:

Configuration configuration=HBaseConfiguration.create();

Connection connection=ConnectionFactory.createConnection(configuration);

Table table=connection.getTable();

ResultScanner scanner=table.getScanner(family);

(6)org.apache.hadoop.hbase.client.Put。(www.xing528.com)

Put类用来对单元执行添加数据操作。向表里添加数据的示例如下:

Configuration configuration=HBaseConfiguration.create();

Connection connection=ConnectionFactory.createConnection(configuration);

Table table=connection.getTable();

Put put=new Put("∗1111".getBytes());//—个Put代表一行数据,行键为构造方法中传入的值

put.addColumn("name".getBytes(),null,"Ghander".getBytes());//本行数据的第一列

put.addColumn("age".getBytes(),null,"20".getBytes());//本行数据的第二列

put.addColumn("gender".getBytes(),null,"male".getBytes());//本行數据的第三列

put.add("score".getBytes(),"Math".getBytes(),"99".getBytes());//本行数据的第四列

table.put(put);

(7)org.apache.hadoop.hbase.client.Get。

Get类用来获取单行的数据。获取指定单元的数据的示例如下:

Configuration configuration=HBaseConfiguration.create();

Connection connection=ConnectionFactory.createConnection(configuration);

Table table=connection.getTable();

Get g=new Get(rowKey.getBytes());

Result rs=table.get(g);

(8)org.apache.hadoop.hbase.client.Result。

Result类用来存放Get或Scan操作后的查询结果,并以<key,value〉的格式存储在映射表中。

获取指定单元的数据的示例如下:

(9)org.apache.hadoop.hbase.client.Scan。

Scan类可以用来限定需要查找的数据,如版本号、起始行号、终止行号、列族、列限定符、返回值的数量的上限等。设置Scan的列族、时间戳的范围和每次最多返回的单元数目的例子如下。

Scan scan=new Scan();

scan.addFamily(Bytes.toBytes("columnFamily1"));

scan.setTimeRange(1,3);

scan.setBatch(1000);

(10)org.apache.hadoop.hbase.client.ResultScanner。

ResultScanner类是客户端获取值的接口,可以用来限定需要查找的数据,如版本号、起始行号、终止行号、列族、列限定符、返回值的数量的上限等。

获取指定单元的数据的示例如下:

免责声明:以上内容源自网络,版权归原作者所有,如有侵犯您的原创版权请告知,我们将尽快删除相关内容。

我要反馈