HBase JAVA API
字体:
实验目的
1.了解HBase语言的基本语法
2.了解HBase开发的原理
3.了解HBase Java API的使用
实验原理
HBase与Hadoop一样,都是用Java编写的,所以HBase对Java支持是必须的,HBase Java API核心类介绍如下:
1.HBaseConfiguration类
HBaseConfiguration是每一个HBase Client都会使用到的对象,它代表HBase配置信息,有两种构造方式:
①public HBaseConfiguration()
②public HBaseConfiguration(final Configuration c)
2.创建表
创建表通过HBaseAdmin对象操作。HBaseAdmin负责META表信息的处理。
HBaseAdmin提供了createTable方法。
public void createTable(HTableDescriptor desc)
HTableDescriptor表示表的Schema,提供的常用方法有以下两个:
①setMaxFileSize:指定最大的Region大小。
②setMemStoreFlushSize:指定MemStore Flush到HDFS的文件大小。
3.删除表
删除表也是通过HBaseAdmin来操作,删除表之前首先要disable表。这是一个非常耗时的操作,所以不建议频繁删除表。
disable Table和deleteTable分别用来执行disable和delete操作。
4.插入数据
HTable通过put方法插入数据。可以传递单个put对象或List put对象分别实现单条插入和批量插入。
①public void put(final Put put) throws IOException
②public void put(final List<Put>puts) throws IOException
Put提供三种构造方式。
①public Put (byte [] row)
②public Put (byte [] row,RowLock rowLock)
③public Put(Put putToCopy)
5.查询数据
查询分为单条随机查询和批量查询。单条查询通过Row Key在Table中查询某一行的数据,HTable提供了get方法完成单条查询。批量查询通过制定一段Row Key的范围来查询,HTable提供了getScanner方法完成批量查询。
实验环境
Linux Ubuntu 16.04
jdk-1.8.0_151-linux-x64
hbase-1.2.6
hadoop-2.6.5
hadoop-2.6.5-eclipse
eclipse-java-juno-SR2-linux-gtk-x86_64
实验内容
使用Java API对HBase表的基本操作,主要包含以下四个部分:
1.编写Java代码,实现创建HBase表的操作。
2.编写Java代码,实现删除HBase表的操作。
3.编写Java代码,实现写数据到HBase表中的操作。
4.编写Java代码,实现读取HBase表中数据的操作。
实验步骤
1.首先检查Hadoop相关进程,是否已经启动。若未启动,切换到/usr/apps/hadoop/sbin目录下,启动Hadoop。
1.jps
2.cd/usr/apps/hadoop/sbin
3../start-all.sh
当Hadoop相关进程启动后,进入HBase的bin目录下,启动HBase服务。
1.cd/usr/apps/hbase/bin/
2../start-hbase.sh
2.切换到/usr/data/hbase2目录下,如不存在需提前创建hbase2文件夹。
1.mkdir-p/usr/data/hbase2
2.cd/usr/data/hbase2
3.将实验四文件目录中的hbasedemo.tar.gz、CreateMyTable.java、DeleteMyTable.java、GetData.java、PutData.java文件拷贝到hbase2目录下。
4.打开Eclipse,创建java项目,名为hbasedemo。
在hbasedemo项目下,创建包,包名为myhbase。
添加项目依赖的jar包,右击hbasedemo,选择import。
进入下面界面,选择General中的File System,点击Next。
进入以下界面,选择/usr/apps/hbase/lib目录下的所有jar包,并勾选Create top-level folder,点击Finish。
然后,选中hbasedemolib里面的所有文件,单击右键Build Path=>Add to Build Path选项,就将所有jar包加载到项目里面了。
6.创建表的API
创建类,名为CreateMyTable,功能为在HBase中创建名为mytb,列族为mycf的表。
程序代码
packagemyhbase;
importjava.io.IOException;
importorg.apache.hadoop.conf.Configuration;
importorg.apache.hadoop.hbase.HBaseConfiguration;
importorg.apache.hadoop.hbase.HColumnDescriptor;
importorg.apache.hadoop.hbase.HTableDescriptor;
importorg.apache.hadoop.hbase.MasterNotRunningException;
importorg.apache.hadoop.hbase.ZooKeeperConnectionException;
importorg.apache.hadoop.hbase.client.HBaseAdmin;
publicclassCreateMyTable{
publicstaticvoidmain(String[]args)throwsMasterNotRunningException,ZooKeeperConnectionException,IOException{
StringtableName="mytb";
StringcolumnFamily="mycf";
create(tableName,columnFamily);
}
publicstaticConfigurationgetConfiguration(){
Configurationconf=HBaseConfiguration.create();
conf.set("hbase.rootdir","hdfs://localhost:9000/hbase");
conf.set("hbase.zookeeper.quorum","localhost");
returnconf;
}
publicstaticvoidcreate(StringtableName,StringcolumnFamily)
throwsMasterNotRunningException,ZooKeeperConnectionException,
IOException{
HBaseAdminhBaseAdmin=newHBaseAdmin(getConfiguration());
if(hBaseAdmin.tableExists(tableName)){
System.err.println("Tableexists!");
}else{
HTableDescriptortableDesc=newHTableDescriptor(tableName);
tableDesc.addFamily(newHColumnDescriptor(columnFamily));
hBaseAdmin.createTable(tableDesc);
System.err.println("CreateTableSUCCESS!");
}
}
}
在Eclipse中执行程序代码,在CreateMyTable类文件中,单击右键=>Run As=>Run on Hadoop选项,将任务提交到Hadoop中。
然后查看HBase中新创建的mytb表,先启动hbase shell命令行模式。
1.hbaseshell
执行list,列出当前HBase中的表。
1.list
执行以下命令,查看创建的表结构。
1.describe'mytb'
7.删除表的API
创建类,命名为DeleteMyTable,功能为将HBase中表mytb删除。
程序代码
packagemyhbase;
importjava.io.IOException;
importorg.apache.hadoop.conf.Configuration;
importorg.apache.hadoop.hbase.HBaseConfiguration;
importorg.apache.hadoop.hbase.client.HBaseAdmin;
publicclassDeleteMyTable{
publicstaticvoidmain(String[]args)throwsIOException{
StringtableName="mytb";
delete(tableName);
}
publicstaticConfigurationgetConfiguration(){
Configurationconf=HBaseConfiguration.create();
conf.set("hbase.rootdir","hdfs://localhost:9000/hbase");
conf.set("hbase.zookeeper.quorum","localhost");
returnconf;
}
publicstaticvoiddelete(StringtableName)throwsIOException{
HBaseAdminhAdmin=newHBaseAdmin(getConfiguration());
if(hAdmin.tableExists(tableName)){
try{
hAdmin.disableTable(tableName);
hAdmin.deleteTable(tableName);
System.err.println("DeletetableSuccess");
}catch(IOExceptione){
System.err.println("DeletetableFailed");
}
}else{
System.err.println("tablenotexists");
}
}
}
在DeleteMyTable类文件中,单击右键=>Run As=>Run on Hadoop选项,将任务提交到Hadoop中。
在Eclipse中执行完成,然后在hbase中查看结果, 查看mytb表是否被删除。
1.list
8.写入数据的API
某电商网站,后台有买家信息表buyer,每注册一名新用户网站后台会产生一条日志,并写入HBase中。
数据格式为:用户ID(buyer_id),注册日期(reg_date),注册IP(reg_ip),卖家状态(buyer_status,0表示冻结 ,1表示正常),以“\t”分割,数据内容如下:
1.用户ID注册日期注册IP卖家状态
2.20385,2010-05-04,124.64.242.30,1
3.20386,2010-05-05,117.136.0.172,1
4.20387,2010-05-06,114.94.44.230,1
将数据以buyer_id作为行键写入到HBase的buyer表中,插入之前,需确保buyer表已存在,若不存在,提前创建。
1.create'buyer','reg_date'
创建类,名为PutData,功能为将以上三条数据写入到buyer表中。
程序代码如下:
packagemyhbase;
importjava.io.IOException;
importorg.apache.hadoop.conf.Configuration;
importorg.apache.hadoop.hbase.HBaseConfiguration;
importorg.apache.hadoop.hbase.MasterNotRunningException;
importorg.apache.hadoop.hbase.ZooKeeperConnectionException;
importorg.apache.hadoop.hbase.client.HTable;
importorg.apache.hadoop.hbase.client.Put;
importorg.apache.hadoop.hbase.util.Bytes;
publicclassPutData{
publicstaticvoidmain(String[]args)throwsMasterNotRunningException,
ZooKeeperConnectionException,IOException{
StringtableName="buyer";
StringcolumnFamily="reg_date";
put(tableName,"20385",columnFamily,"2010-05-04:reg_ip","124.64.242.30");
put(tableName,"20385",columnFamily,"2010-05-04:buyer_status","1");
put(tableName,"20386",columnFamily,"2010-05-05:reg_ip","117.136.0.172");
put(tableName,"20386",columnFamily,"2010-05-05:buyer_status","1");
put(tableName,"20387",columnFamily,"2010-05-06:reg_ip","114.94.44.230");
put(tableName,"20387",columnFamily,"2010-05-06:buyer_status","1");
}
publicstaticConfigurationgetConfiguration(){
Configurationconf=HBaseConfiguration.create();
conf.set("hbase.rootdir","hdfs://localhost:9000/hbase");
conf.set("hbase.zookeeper.quorum","localhost");
returnconf;
}
publicstaticvoidput(StringtableName,Stringrow,StringcolumnFamily,
Stringcolumn,Stringdata)throwsIOException{
HTabletable=newHTable(getConfiguration(),tableName);
Putput=newPut(Bytes.toBytes(row));
put.add(Bytes.toBytes(columnFamily),
Bytes.toBytes(column),
Bytes.toBytes(data));
table.put(put);
System.err.println("SUCCESS");
}
}
在Eclipse中执行程序代码,在PutData类文件中,右键并点击=>Run As=>Run on Hadoop选项,将任务提交到Hadoop中。
执行完成后,进入HBase中查看buyer表结果。
1.scan'buyer'
9.查询数据的API
创建类GetData,功能为查询HBase的buyer表中rowkey为20386的数据。
程序代码如下:
packagemyhbase;
importjava.io.IOException;
importorg.apache.hadoop.conf.Configuration;
importorg.apache.hadoop.hbase.HBaseConfiguration;
importorg.apache.hadoop.hbase.client.Get;
importorg.apache.hadoop.hbase.client.HTable;
importorg.apache.hadoop.hbase.client.Result;
importorg.apache.hadoop.hbase.util.Bytes;
publicclassGetData{
publicstaticvoidmain(String[]args)throwsIOException{
StringtableName="buyer";
get(tableName,"20386");
}
publicstaticConfigurationgetConfiguration(){
Configurationconf=HBaseConfiguration.create();
conf.set("hbase.rootdir","hdfs://localhost:9000/hbase");
conf.set("hbase.zookeeper.quorum","localhost");
returnconf;
}
publicstaticvoidget(StringtableName,Stringrowkey)throwsIOException{
HTabletable=newHTable(getConfiguration(),tableName);
Getget=newGet(Bytes.toBytes(rowkey));
Resultresult=table.get(get);
byte[]value1=result.getValue("reg_date".getBytes(),"2010-05-05:reg_ip".getBytes());
byte[]value2=result.getValue("reg_date".getBytes(),"2010-05-05:buyer_status".getBytes());
System.err.println("line1:SUCCESS");
System.err.println("line2:"
+newString(value1)+"\t"
+newString(value2));
}
}
在Eclipse中执行程序代码,在GetData类文件中,单击右键=>Run As=>Run on Hadoop选项,将任务提交到Hadoop中。
执行完成后,可以在Eclipse中的console界面查看到执行结果为:
至此,本次实验结束。