HBase Java API 练习

0.072字数 542阅读 1319

1 环境准备

1)成功搭建Hadoop-2.2.0开发环境
2)成功启动HBase,通过HBase Shell进行测试
3)使用MyEclipse作为开发工具
4)使用Maven构建项目

2 项目创建

2.1 创建Project

HBase-api-创建项目

由于我已经创建了项目,所以这里会报错,只要改下Artifact Id就可以。而你只需要输入没有使用过的Artifact Id。

HBase-api-创建项目2.png

2.2 pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.szh</groupId>
  <artifactId>HBase</artifactId>
  <version>0.0.1</version>
  <packaging>jar</packaging>

  <name>HBase</name>
  <url>http://maven.apache.org</url>

 <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <hadoop.version>2.2.0</hadoop.version>
  </properties>

  <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <version>${hadoop.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-hdfs</artifactId>
            <version>${hadoop.version}</version>
        </dependency>
         <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>${hadoop.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <version>0.98.23-hadoop2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-common</artifactId>
            <version>0.98.23-hadoop2</version>
        </dependency>
        
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-server</artifactId>
            <version>0.98.23-hadoop2</version>
        </dependency>
        <dependency>  
            <groupId>jdk.tools</groupId>  
            <artifactId>jdk.tools</artifactId>  
            <version>1.7</version>  
            <scope>system</scope>  
            <systemPath>${JAVA_HOME}/lib/tools.jar</systemPath>  
        </dependency>  
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.4.1</version>
                <executions>
                    <!-- Run shade goal on package phase -->
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <!-- add Main-Class to manifest file -->
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.szh.hbase.MyDriver</mainClass>
                                </transformer>
                            </transformers>
                            <createDependencyReducedPom>false</createDependencyReducedPom>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

在上面的pom.xml中我们导入了hadoop和hbase开发所需要的包,待会我们还要学习如何开发MapReduce和HBase的交互,所以先做好准备,使用Maven开发最好要保证网速,下载真的很慢!!!等待下载完毕....

2.3 MyHbase实例

使用Java API和使用HBase shell本质上是没有任何区别的,都是HBase里面的DDL操作,所以我们本次使用的例子跟HBase Shell 练习里面的例子是一样的,在这里我们先贴出局部代码,大家不用着急,最后会把项目代码给大家的。

1)创建HBaseConfiguration来获取HBase的配置文件信息

    static Configuration conf = null;
    static {
        conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "szh");
    }

2)创建表格

     /**
     * 创建表格
     * @param tbName 表名
     * @param cf 列族名
     * @throws IOException 
     * @throws ZooKeeperConnectionException 
     * @throws MasterNotRunningException 
     */
    @SuppressWarnings({ "all" })
    public static void createTable(String tbName,String[] cf) throws MasterNotRunningException, ZooKeeperConnectionException, IOException{
        //用于执行操作
        HBaseAdmin admin = new HBaseAdmin(conf);
        HTableDescriptor desc = new HTableDescriptor(tbName);
        for(int i=0;i<cf.length;i++){
            desc.addFamily(new HColumnDescriptor(cf[i]));
        }
        //判断表格是否存在
        if(admin.tableExists(tbName)){
            System.out.println(tbName+" exists");
            System.exit(0);
        }else{
            admin.createTable(desc);
            System.out.println("create table success");
        }
    }

3)删除表格

public static void deleteTable(String tableName) throws MasterNotRunningException, ZooKeeperConnectionException, IOException{
        //用于执行操作
        HBaseAdmin admin = new HBaseAdmin(conf);
        HTableDescriptor desc = new HTableDescriptor(tableName);
        //判断表格是否存在
        if(admin.tableExists(tableName)){
            admin.disable(tableName);
            admin.drop(tableName);
            System.out.println("delete table success");
        }else{
            System.out.println("table is not exist");
        }
    }

4)插入数据

@SuppressWarnings("all")
    public static void add(String tableName,String rowKey,String family,String qualifier,String value) throws IOException{
        Put put = new Put(Bytes.toBytes(rowKey));
        HTable table = new HTable(conf, tableName);
        put.add(Bytes.toBytes(family), Bytes.toBytes(qualifier), Bytes.toBytes(value));
        table.put(put);
        System.out.println("add data success");
        System.out.println("===========");
    }

执行main函数:

public static void main(String[] args) {
        // TODO Auto-generated method stub
        String tablebName = "student";
        String rowKey = "xiaoming";
        String[] family = {"info","address","score"};
        try {
//          createTable(tablebName, family);
            add(tablebName,rowKey,family[0],"age","18");
            add(tablebName,rowKey,family[0],"birthday","1990-12-12");
            add(tablebName,rowKey,family[0],"school","beijingdaxue");
            add(tablebName,rowKey,family[1],"country","china");
            add(tablebName,rowKey,family[1],"province","guangdong");
            add(tablebName,rowKey,family[1],"city","shenzhen");
            add(tablebName,rowKey,family[1],"yuwen","99");
            add(tablebName,rowKey,family[1],"shuxue","98");
            add(tablebName,rowKey,family[1],"yingyu","100");
        } catch (MasterNotRunningException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ZooKeeperConnectionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

使用HBase Shell查询结果,如下:

执行插入数据.png

5)删除数据

    public static void deleteAppointColumn(String tableName,String rowKey,String family,String qualifier) throws IOException{
        HTable table = new HTable(conf,Bytes.toBytes(tableName));
        Delete delete = new Delete(Bytes.toBytes(rowKey));
        delete.deleteColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier));
        table.delete(delete);
        System.out.println("delete data success");
        System.out.println("===========");
    }

执行main函数:

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String tablebName = "student";
        String rowKey = "xiaoming";
        String[] family = {"info","address","score"};
        try {
            deleteAppointColumn(tablebName,rowKey,family[1],"yuwen");
            deleteAppointColumn(tablebName,rowKey,family[1],"shuxue");
            deleteAppointColumn(tablebName,rowKey,family[1],"yingyu");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

使用 HBase Shell 查询结果,如下:

执行删除后结果.png

6)更新数据

    public static void update(String tableName,String rowKey,String family,String qualifier,String value) throws IOException{
        Put put = new Put(Bytes.toBytes(rowKey));
        HTable table = new HTable(conf, tableName);
        put.add(Bytes.toBytes(family), Bytes.toBytes(qualifier), Bytes.toBytes(value));
        table.put(put);
        System.out.println("update data success");
        System.out.println("===========");
    }

执行main函数:

public static void main(String[] args) {
        // TODO Auto-generated method stub
        String tablebName = "student";
        String rowKey = "xiaoming";
        String[] family = {"info","address","score"};
        try {
            update(tablebName,rowKey,family[0],"age","100");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

使用HBase Shell查询,结果如下:

执行更新结果.png

7)查询数据
7.1)全盘扫描

 public static void scanAll(String tableName,String startRow,String stopRow) throws IOException{
        
        HTable table = new HTable(conf, tableName);
        Scan scan = new Scan();
        scan.setStartRow(Bytes.toBytes(startRow));
        scan.setStopRow(Bytes.toBytes(stopRow));
        ResultScanner rs = null;
        try{
            System.out.println("全盘扫描");
            rs = table.getScanner(scan);
            for(Result r:rs){
                for(KeyValue kv:r.list()){
                    System.out.println("rowKey: "+ Bytes.toString(kv.getRow())
                            +" family: "+ Bytes.toString(kv.getFamily())
                            +" qualifiter: "+Bytes.toString(kv.getQualifier())
                            +" value "+Bytes.toString(kv.getValue())
                            );
                }
                
            }
            
        }finally{
            if(rs!=null){
                rs.close();
            }
        }
        System.out.println("===========");
    }

7.2)查询某一列族

public static void scanByFamily(String tableName,String startRow,String stopRow,String family) throws IOException{
        
        HTable table = new HTable(conf, tableName);
        Scan scan = new Scan();
        scan.setStartRow(Bytes.toBytes(startRow));
        scan.setStopRow(Bytes.toBytes(stopRow));
        scan.addFamily(Bytes.toBytes(family));
        ResultScanner rs = null;
        try{
            System.out.println("查询某一列族");
            rs = table.getScanner(scan);
            for(Result r:rs){
                for(KeyValue kv:r.list()){
                    System.out.println("rowKey: "+ Bytes.toString(kv.getRow())
                            +" family: "+ Bytes.toString(kv.getFamily())
                            +" qualifiter: "+Bytes.toString(kv.getQualifier())
                            +" value "+Bytes.toString(kv.getValue())
                            );
                }
                
            }
            
        }finally{
            if(rs!=null){
                rs.close();
            }
        }
        System.out.println("===========");
    }

7.3)查询某一列

public static void scanByColumns(String tableName,String startRow,String stopRow,String family,String qualifiter) throws IOException{
        
        HTable table = new HTable(conf, tableName);
        Scan scan = new Scan();
        scan.setStartRow(Bytes.toBytes(startRow));
        scan.setStopRow(Bytes.toBytes(stopRow));
        scan.addColumn(Bytes.toBytes(family),Bytes.toBytes(qualifiter));
        ResultScanner rs = null;
        try{
            System.out.println("查询某一列");
            rs = table.getScanner(scan);
            for(Result r:rs){
                for(KeyValue kv:r.list()){
                    System.out.println("rowKey: "+ Bytes.toString(kv.getRow())
                            +" family: "+ Bytes.toString(kv.getFamily())
                            +" qualifiter: "+Bytes.toString(kv.getQualifier())
                            +" value "+Bytes.toString(kv.getValue())
                            );
                }
                
            }
            
        }finally{
            if(rs!=null){
                rs.close();
            }
        }
        System.out.println("===========");
    }

7.4)查询某一列的多个版本

    public static void getByVersion(String tableName,String rowKey,String family,String qualifier) throws IOException{
        HTable table = new HTable(conf, Bytes.toBytes(tableName));  
        Get get = new Get(Bytes.toBytes(rowKey));   
        get.setMaxVersions(5);  
        Result result = table.get(get);
        System.out.println("查询某一列多版本");
        List<KeyValue> list = (List<KeyValue>) result.getColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier));
        for (KeyValue kv : list) {  
            System.out.println("rowKey: "+ Bytes.toString(kv.getRow())
                    +" family: "+ Bytes.toString(kv.getFamily())
                    +" qualifiter: "+Bytes.toString(kv.getQualifier())
                    +" value "+Bytes.toString(kv.getValue())
                    +" TimeStamp: "+kv.getTimestamp()
                    );
        }  
        System.out.println("===========");
    }

这里需要注意的是,0.98版本的HBase默认VERSIONS为1,所以如果你想要获取某一列多个版本数据的话,首先得去修改HBase该列所属的列族的VERSIONS,举个例子:

a. 进入HBase Shell
b. 输入一下命令

alter 'student',NAME=>'info',VERSIONS => 5

查询修改结果:

describe 'student'

更多HBase Shell细节 可查看HBase Shell 练习
7.5)查询某一行的数据

    public static void getByRowKey(String tableName,String rowKey) throws IOException{
        HTable table = new HTable(conf,Bytes.toBytes(tableName));
        Get get = new Get(Bytes.toBytes(rowKey));
        Result result = table.get(get);
         System.out.println("查询某一行");
        for (KeyValue kv : result.list()) {  
            System.out.println("rowKey: "+ Bytes.toString(kv.getRow())
                    +" family: "+ Bytes.toString(kv.getFamily())
                    +" qualifiter: "+Bytes.toString(kv.getQualifier())
                    +" value "+Bytes.toString(kv.getValue())
                    +" TimeStamp: "+kv.getTimestamp()
                    );
        }  
        System.out.println("===========");
    }

执行main函数:

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String tablebName = "student";
        String rowKey = "xiaoming";
        String[] family = {"info","address","score"};
        try {
            scanAll(tablebName,rowKey,rowKey);
            scanByFamily(tablebName,rowKey,rowKey,family[0]);
            scanByColumns(tablebName,rowKey,rowKey,family[0],"age");
            getByVersion(tablebName,rowKey,family[0],"age");
            getByRowKey(tablebName,rowKey);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

查看Console:

五种查询结果.png

推荐阅读更多精彩内容