HBase Java API 练习

1 环境准备

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

2 项目创建

2.1 创建Project

HBase Java API 练习_第1张图片
HBase-api-创建项目

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

HBase Java API 练习_第2张图片
HBase-api-创建项目2.png

2.2 pom.xml


  4.0.0

  com.szh
  HBase
  0.0.1
  jar

  HBase
  http://maven.apache.org

 
    UTF-8
    2.2.0
  

  
        
            junit
            junit
            3.8.1
            test
        
        
            org.apache.hadoop
            hadoop-common
            ${hadoop.version}
        
        
            org.apache.hadoop
            hadoop-hdfs
            ${hadoop.version}
        
         
            org.apache.hadoop
            hadoop-client
            ${hadoop.version}
        
        
            org.apache.hbase
            hbase-client
            0.98.23-hadoop2
        
        
            org.apache.hbase
            hbase-common
            0.98.23-hadoop2
        
        
        
            org.apache.hbase
            hbase-server
            0.98.23-hadoop2
        
          
            jdk.tools  
            jdk.tools  
            1.7  
            system  
            ${JAVA_HOME}/lib/tools.jar  
          
    
    
        
            
                org.apache.maven.plugins
                maven-shade-plugin
                2.4.1
                
                    
                    
                        package
                        
                            shade
                        
                        
                            
                                
                                
                                    com.szh.hbase.MyDriver
                                
                            
                            false
                        
                    
                
            
        
    

在上面的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

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查询结果,如下:

HBase Java API 练习_第3张图片
执行插入数据.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 查询结果,如下:

HBase Java API 练习_第4张图片
执行删除后结果.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 list = (List) 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:

HBase Java API 练习_第5张图片
五种查询结果.png

你可能感兴趣的:(HBase Java API 练习)