4.2HBase 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 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.gzCreateMyTable.javaDeleteMyTable.javaGetData.javaPutData.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的表。

 

程序代码

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中。

 

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

1. hbase shell  

执行list,列出当前HBase中的表。

1. list  

 

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

1. describe 'mytb'  

 

7.删除表的API

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

 

程序代码

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中。

 

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表中。

 

程序代码如下:

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中。

 

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

1. scan 'buyer'  

 

9.查询数据的API

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

 

程序代码如下:

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中。

 

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

 

至此,本次实验结束。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值