HBase JAVA API

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 Listputs) 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-7u75-linux-x64
hbase-1.0.0-cdh5.4.5
hadoop-2.6.0-cdh5.4.5
hadoop-2.6.0-eclipse-cdh5.4.5.jar
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相关进程,是否已经启动。若未启动,切换到/apps/hadoop/sbin目录下,启动Hadoop。

jps
cd /apps/hadoop/sbin
./start-all.sh

当Hadoop相关进程启动后,进入HBase的bin目录下,启动HBase服务。

cd /apps/hbase/bin/
./start-hbase.sh

2.切换到/data/hbase2目录下,如不存在需提前创建hbase2文件夹。

mkdir -p /data/hbase2
cd /data/hbase2

3.使用wget命令,下载http://192.168.19.201:60000/allfiles/hbase2中的文件。

wget http://192.168.19.201:60000/allfiles/hbase2/hbasedemolib.tar.gz
wget http://192.168.19.201:60000/allfiles/hbase2/hbasedemo.tar.gz
wget http://192.168.19.201:60000/allfiles/hbase2/CreateMyTable.java
wget http://192.168.19.201:60000/allfiles/hbase2/DeleteMyTable.java
wget http://192.168.19.201:60000/allfiles/hbase2/GetData.java
wget http://192.168.19.201:60000/allfiles/hbase2/PutData.java

4.解压/data/hbase2中的hbasedemolib.tar.gz包到/data/hbase2中。

tar zxvf hbasedemolib.tar.gz

5.打开Eclipse,创建java项目,名为hbasedemo。

02.png

03.png

在hbasedemo项目下,创建包,包名为myhbase。

04.png

05.png

添加项目依赖的jar包,右击hbasedemo,选择import。

06.png

进入下面界面,选择General中的File System,点击Next。

07.png

进入以下界面,选择/data/hbase2中的hbasedemolib文件夹,并勾选Create top-level folder,点击Finish。

08.png

然后,选中hbasedemolib里面的所有文件,单击右键Build Path=>Add to Build Path选项,就将所有jar包加载到项目里面了。

09.png

6.创建表的API

创建类,名为CreateMyTable,功能为在HBase中创建名为mytb,列族为mycf的表。

10.png

程序代码

package myhbase;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.HBaseAdmin;
public class CreateMyTable {
	public static void main(String[] args) throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
		String tableName = "mytb";
		String columnFamily = "mycf";
		create(tableName, columnFamily);
	}

	public static Configuration getConfiguration() {
		Configuration conf = HBaseConfiguration.create();
		conf.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
		conf.set("hbase.zookeeper.quorum", "localhost");
		return conf;
	}
	public static void create(String tableName, String columnFamily)
			throws MasterNotRunningException, ZooKeeperConnectionException,
			IOException {
		HBaseAdmin hBaseAdmin = new HBaseAdmin(getConfiguration());
		if (hBaseAdmin.tableExists(tableName)) {
			System.err.println("Table exists!");
		} else {
			HTableDescriptor tableDesc = new HTableDescriptor(tableName);
			tableDesc.addFamily(new HColumnDescriptor(columnFamily));
			hBaseAdmin.createTable(tableDesc);
			System.err.println("Create Table SUCCESS!");
		}
	}
}

在Eclipse中执行程序代码,在CreateMyTable类文件中,单击右键=>Run As=>Run on Hadoop选项,将任务提交到Hadoop中。

11.png

然后查看HBase中新创建的mytb表,先启动hbase shell命令行模式。
hbase shell
执行list,列出当前HBase中的表。

list

12.png

执行以下命令,查看创建的表结构。

describe 'mytb'

13.png

7.删除表的API

创建类,命名为DeleteMyTable,功能为将HBase中表mytb删除。

14.png

程序代码

package myhbase;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HBaseAdmin;
public class DeleteMyTable {
	public static void main(String[] args) throws IOException {
		String tableName = "mytb";
		delete(tableName);
	}

	public static Configuration getConfiguration() {
		Configuration conf = HBaseConfiguration.create();
		conf.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
		conf.set("hbase.zookeeper.quorum", "localhost");
		return conf;
	}

	public static void delete(String tableName) throws IOException {
		HBaseAdmin hAdmin = new HBaseAdmin(getConfiguration());
		if(hAdmin.tableExists(tableName)){
			try {
				hAdmin.disableTable(tableName);
				hAdmin.deleteTable(tableName);
				System.err.println("Delete table Success");
			} catch (IOException e) {
				System.err.println("Delete table Failed ");
			}
		}else{
			System.err.println("table not exists");
		}
	}
}

在DeleteMyTable类文件中,单击右键=>Run As=>Run on Hadoop选项,将任务提交到Hadoop中。

15.png

在Eclipse中执行完成,然后在hbase中查看结果, 查看mytb表是否被删除。
list

16.png

8.写入数据的API

某电商网站,后台有买家信息表buyer,每注册一名新用户网站后台会产生一条日志,并写入HBase中。数据格式为:用户ID(buyer_id),注册日期(reg_date),注册IP(reg_ip),卖家状态(buyer_status,0表示冻结 ,1表示正常),以“\t”分割,数据内容如下:

用户ID   注册日期  注册IP   卖家状态
20385,2010-05-04,124.64.242.30,1
20386,2010-05-05,117.136.0.172,1
20387,2010-05-06 ,114.94.44.230,1

将数据以buyer_id作为行键写入到HBase的buyer表中,插入之前,需确保buyer表已存在,若不存在,提前创建。

create 'buyer','reg_date'

17.png

创建类,名为PutData,功能为将以上三条数据写入到buyer表中。

18.png

程序代码如下:

package myhbase;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;
public class PutData {
	public static void main(String[] args) throws MasterNotRunningException,
			ZooKeeperConnectionException, IOException {
		String tableName = "buyer";
		String columnFamily = "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");

	}
	public static Configuration getConfiguration() {
		Configuration conf = HBaseConfiguration.create();
		conf.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
		conf.set("hbase.zookeeper.quorum", "localhost");
		return conf;
	}
	public static void put(String tableName, String row, String columnFamily,
			String column, String data) throws IOException {
		HTable table = new HTable(getConfiguration(), tableName);
		Put put = new Put(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中。

19.png

执行完成后,进入HBase中查看buyer表结果。

scan 'buyer'

20.png

9.查询数据的API

创建类GetData,功能为查询HBase的buyer表中rowkey为20386的数据。

21.png

程序代码如下:

package myhbase;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.util.Bytes;
public class GetData {
	public static void main(String[] args) throws IOException {
		String tableName = "buyer";
		get(tableName, "20386");
	}
	public static Configuration getConfiguration() {
		Configuration conf = HBaseConfiguration.create();
		conf.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
		conf.set("hbase.zookeeper.quorum", "localhost");
		return conf;
	}
	public static void get(String tableName, String rowkey) throws IOException {
		HTable table = new HTable(getConfiguration(), tableName);
		Get get = new Get(Bytes.toBytes(rowkey));
		Result result = 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:"
		        + new String(value1) + "\t"
				+ new String(value2));
	}
}

在Eclipse中执行程序代码,在GetData类文件中,单击右键=>Run As=>Run on Hadoop选项,将任务提交到Hadoop中。

22.png

执行完成后,可以在Eclipse中的console界面查看到执行结果为:

23.png

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值