通过Java Api与HBase交互

HBase提供了Java Api的访问接口,掌握这个就跟Java应用使用RDBMS时需要JDBC一样重要,下面看一下Java api创建、删除表,及记录的增删改查操作:

package com;



import java.io.IOException;  
import java.util.ArrayList;  
import java.util.List;  
  
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.KeyValue;  
import org.apache.hadoop.hbase.MasterNotRunningException;  
import org.apache.hadoop.hbase.ZooKeeperConnectionException;  
import org.apache.hadoop.hbase.client.Delete;  
import org.apache.hadoop.hbase.client.Get;  
import org.apache.hadoop.hbase.client.HBaseAdmin;  
import org.apache.hadoop.hbase.client.HTable;  
import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.client.HTablePool;  
import org.apache.hadoop.hbase.client.Put;  
import org.apache.hadoop.hbase.client.Result;  
import org.apache.hadoop.hbase.client.ResultScanner;  
import org.apache.hadoop.hbase.client.Scan;  
import org.apache.hadoop.hbase.filter.Filter;  
import org.apache.hadoop.hbase.filter.FilterList;  
import org.apache.hadoop.hbase.filter.PrefixFilter;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;  
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;  
import org.apache.hadoop.hbase.util.Bytes;  


/**
 * 
 * @author zhang
 *
 */
public class zhangTest
{  
/**
* class Configuration 被HbaseConfiguration继承
*/
    private static Configuration configuration;  
    
    static 
    {  
    /*
* HbaseConfiguration
      关系:org.apache.hadoop.hbase.HBaseConfiguration 继承Configuration
      作用:通过此类可以对HBase进行配置
*/
        configuration = HBaseConfiguration.create(); 


//与hbase/conf/hbase-site.xml中hbase.zookeeper.property.clientPort配置的值相同  
        configuration.set("hbase.zookeeper.property.clientPort", "2181"); 


//与hbase/conf/hbase-site.xml中hbase.zookeeper.quorum配置的值相同  
        configuration.set("hbase.zookeeper.quorum", "192.168.0.145");

        configuration.set("hbase.master", "192.168.0.145:600000");  
    }  
  
    
  
    /** 
     * 创建表 
     * @param tableName 
     */  
    public static void createTable(String tableName) 
    {  
        System.out.println("start create table ......");  
        try 
        {  
        /*
    * .HBaseAdmin
          关系:org.apache.hadoop.hbase.client.HBaseAdmin
          作用:提供一个接口来管理HBase数据库中的表信息。它提供创建表、删除表等方法。
    */
            HBaseAdmin hBaseAdmin = new HBaseAdmin(configuration);  
            
         // 如果存在要创建的表,那么先删除,再创建  
            if (hBaseAdmin.tableExists(tableName)) 
            {
           
                hBaseAdmin.disableTable(tableName);  
                hBaseAdmin.deleteTable(tableName);  
                System.out.println(tableName + " is exist,detele....");  
            } 
            
            /*
    * HTableDescriptor
          关系:org.apache.hadoop.hbase.client.HTableDescriptor
          作用:包含了表的名字及其对应列族。 提供的方法有
            void                addFamily(HColumnDescriptor)              添加一个列族
            HColumnDescriptor   removeFamily(byte[] column)               移除一个列族
            byte[]              getName()                                 获取表的名字
            byte[]              getValue(byte[] key)                      获取属性的值
            void                setValue(String key,String value)         设置属性的值
    */
            
//获取表的信息
            HTableDescriptor tableDescriptor = new HTableDescriptor(tableName);
            
            
            /*HColumnDescriptor
          关系:org.apache.hadoop.hbase.client.HColumnDescriptor
          作用:维护关于列的信息。提供的方法有
          byte[]              getName()                                 获取列族的名字
             byte[]              getValue()                                获取对应的属性的值
             void                setValue(String key,String value)         设置对应属性的值
    */
            //添加列
            //tableDescriptor.addFamily(new HColumnDescriptor("id"));
            
            //tableDescriptor.addFamily(new HColumnDescriptor("username"));
            
            //tableDescriptor.addFamily(new HColumnDescriptor("hostelid"));
            
            tableDescriptor.addFamily(new HColumnDescriptor("hostelname"));
            
            tableDescriptor.addFamily(new HColumnDescriptor("time"));
            
            tableDescriptor.addFamily(new HColumnDescriptor("roomid"));
            
            tableDescriptor.addFamily(new HColumnDescriptor("address"));
            
            tableDescriptor.addFamily(new HColumnDescriptor("userid")); 
            
            hBaseAdmin.createTable(tableDescriptor);  
        } catch (MasterNotRunningException e) {  
            e.printStackTrace();  
        } catch (ZooKeeperConnectionException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
        System.out.println("end create table ......");  
    }  
  
    /** 
     * 插入数据 
     * @param tableName 
     */  
    public static void insertData(String tableName) 
    {  
        System.out.println("start insert data ......");
        Put put = null;
        HTablePool pool = new HTablePool(configuration, 1000);
        
        /*
         * HTable
      关系:org.apache.hadoop.hbase.client.HTable
      作用:用户与HBase表进行通信。此方法对于更新操作来说是非线程安全的,
      如果启动多个线程尝试与单个HTable实例进行通信,那么写缓冲器可能会崩溃。
         */
        //zj
        HTableInterface  table =  pool.getTable(tableName);  
        
        // 一个PUT代表一行数据,再NEW一个PUT表示第二行数据,每行一个唯一的ROWKEY,
        //此处rowkey为put构造方法中传入的值  
        
        /*
         * Put
      关系:org.apache.hadoop.hbase.client.Put
      作用:用于对单个行执行添加操作
         */
        
     for(int i=1;i<100;i++)
     {
    //id_username_hostelid
        put = new Put( ("id"+i+"_"+"username"+i+"_"+"hostelid"+i).getBytes());
        
        //zj
      //Put put = new Put("112233bbbcccc".getBytes());
       // put.add("id".getBytes(), null, (""+i).getBytes());// 本行数据的第一列  
      //  put.add("username".getBytes(), null, ("qwe"+i).getBytes());// 本行数据的第二列  
       // put.add("hostelid".getBytes(), null, (""+i).getBytes());// 本行数据的第三列
        
        put.add("hostelname".getBytes(), null, ("qwea"+i).getBytes());
        
        put.add("time".getBytes(), null, "2013-01-25 13:12:16".getBytes());
        
        put.add("roomid".getBytes(), null, ("10"+i).getBytes());
        
        put.add("address".getBytes(), null, ("qweaa"+i).getBytes());
        
        put.add("userid".getBytes(), null, ("14587965453512"+i).getBytes());
        
       
        try { 
            table.put(put);  
        } catch (IOException e) {  
            e.printStackTrace();  
        } 
      }
        System.out.println("end insert data ......");  
    }  
  
    /** 
     * 删除一张表 
     * @param tableName 
     */  
    public static void dropTable(String tableName) {  
        try {  
            HBaseAdmin admin = new HBaseAdmin(configuration);  
            admin.disableTable(tableName);  
            admin.deleteTable(tableName);  
        } catch (MasterNotRunningException e) {  
            e.printStackTrace();  
        } catch (ZooKeeperConnectionException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
  
    }  
    /** 
     * 根据 rowkey删除一条记录 
     * @param tablename 
     * @param rowkey 
     */  
     public static void deleteRow(String tablename, String rowkey)  {  
        try {  
            HTable table = new HTable(configuration, tablename);  
            List list = new ArrayList();  
            Delete d1 = new Delete(rowkey.getBytes());  
            list.add(d1);  
              
            table.delete(list);  
            System.out.println("删除行成功!");  
              
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
          
  
    }  
  
     /** 
      * 组合条件删除 
      * @param tablename 
      * @param rowkey 
      */  
     public static void deleteByCondition(String tablename, String rowkey) 
{  
            //目前还没有发现有效的API能够实现 根据非rowkey的条件删除 这个功能,
//还有清空表全部数据的API操作  
  
    }  
  
  
    /** 
     * 查询所有数据 
     * @param tableName 
     */  
    public static void QueryAll(String tableName) {  
        HTablePool pool = new HTablePool(configuration, 1000);  
        //HTable table = (HTable) pool.getTable(tableName);
        
        //zj
        HTableInterface  table = null; 
        
        //zj
        ResultScanner rs = null;
        try 
        {  
        //ResultScanner rs = table.getScanner(new Scan());
        //zj
table = pool.getTable(tableName);

        rs = table.getScanner(new Scan());
            
            for (Result r : rs) 
            {  
                System.out.println("获得到rowkey:" + new String(r.getRow()));  
                for (KeyValue keyValue : r.raw()) 
                {  
                    System.out.println("列:" + new String(keyValue.getFamily())  
                            + "====值:" + new String(keyValue.getValue()));  
                }  
            }
            
        } 
        catch (IOException e) 
        {  
            e.printStackTrace();  
        }
        finally
        {
        rs.close();// 最后还得关闭


//实际应用过程中,pool获取实例的方式应该抽取为单例模式的,
//不应在每个方法都重新获取一次(单例明白?
//就是抽取到专门获取pool的逻辑类中,
//具体逻辑为如果pool存在着直接使用,如果不存在则new)
try 
{
pool.putTable(table);
} catch (IOException e)
{
e.printStackTrace();
}

}


        
    }  
  
    /** 
     * 单条件查询,根据rowkey查询唯一一条记录 
     * @param tableName 
     */  
    public static void QueryByCondition1(String tableName) {  
  
        HTablePool pool = new HTablePool(configuration, 1000);  
        //HTable table = (HTable) pool.getTable(tableName);
        
        //zj
        HTableInterface table =  pool.getTable(tableName);  
        try { 
       
        /*
        * Get
      关系:org.apache.hadoop.hbase.client.Get
      作用:用于获取单个行的相关信息
        */
            Get scan = new Get("abcdef".getBytes());// 根据rowkey查询  
       
        //zj
            //Get scan = new Get("112233bbbcccc".getBytes());// 根据rowkey查询  
            /*
             * Result
      关系:org.apache.hadoop.hbase.client.Result
      作用:存储Get或Scan操作后获取的单行值。
             */
            Result r = table.get(scan);  
            System.out.println("获得到rowkey:" + new String(r.getRow()));  
            for (KeyValue keyValue : r.raw()) 
            {  
                System.out.println("列:" + new String(keyValue.getFamily())  
                        + "====值:" + new String(keyValue.getValue()));  
            }  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  
  
    /** 
     * 单条件按查询,查询多条记录 
     * @param tableName 
     */  
    public static void QueryByCondition2(String tableName) {  
  
        try {  
            HTablePool pool = new HTablePool(configuration, 1000);  
            
//            HTable table = (HTable) pool.getTable(tableName);
            
            //zj
            HTableInterface table =  pool.getTable(tableName); 
            
            // 当列column1的值为aaa时进行查询 
            Filter filter = new SingleColumnValueFilter(Bytes  
                    .toBytes("id"), null, CompareOp.EQUAL, Bytes  
                    .toBytes("1")); 
            Scan s = new Scan();  
            s.setFilter(filter);  
            
            /*
             * ResultScanner
      关系:Interface
      作用:客户端获取值的接口。
             */
            ResultScanner rs = table.getScanner(s);
            
            for (Result r : rs) 
            {  
                System.out.println("获得到rowkey:" + new String(r.getRow()));  
                for (KeyValue keyValue : r.raw()) 
                {  
                    System.out.println("列:" + new String(keyValue.getFamily())  
                            + "====值:" + new String(keyValue.getValue()));  
                }  
            }  
        } 
        catch (Exception e) 
        {  
            e.printStackTrace();  
        }  
    }  
  
    /** 
     * 组合条件查询 
     * @param tableName 
     */  
    public static void QueryByCondition3(String tableName) 
    {  
  
        try 
        {  
            HTablePool pool = new HTablePool(configuration, 1000);  
            
            //zj
            HTableInterface table =  pool.getTable(tableName);
            
            List<Filter> filters = new ArrayList<Filter>();  
  
            Filter filter1 = new SingleColumnValueFilter(Bytes  
                    .toBytes("id"), null, CompareOp.EQUAL, Bytes  
                    .toBytes("99"));  
            filters.add(filter1);  
  
            Filter filter2 = new SingleColumnValueFilter(Bytes  
                    .toBytes("address"), null, CompareOp.EQUAL, Bytes  
                    .toBytes("qweaa99"));  
            filters.add(filter2);  
  
            Filter filter3 = new SingleColumnValueFilter(Bytes  
                    .toBytes("username"), null, CompareOp.EQUAL, Bytes  
                    .toBytes("qwe99"));  
            filters.add(filter3);  
  
            FilterList filterList1 = new FilterList(filters);  
  
            Scan scan = new Scan();  
            
            scan.setFilter(filterList1);  
            
            ResultScanner rs = table.getScanner(scan);  
            
            for (Result r : rs) 
            {  
                System.out.println("获得到rowkey:" + new String(r.getRow()));  
                for (KeyValue keyValue : r.raw()) {  
                    System.out.println("列:" + new String(keyValue.getFamily())  
                            + "====值:" + new String(keyValue.getValue()));  
                }  
            }  
            rs.close();  
  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
  
    }
    
  //------------------------条件 查询-------------
    public static void selectFilter(String tablename,List<String> arr) throws IOException
    {  
        HTable table=new HTable(configuration,tablename);  
        
        Scan scan = new Scan();//实例化一个遍历器  
        
        FilterList filterList = new FilterList();   //过滤器List
     
        for(String v:arr)
        { //下标0为列簇,1为列名,2为条件
            String [] wheres=v.split("_");  
           
            filterList.addFilter( new SingleColumnValueFilter(//过滤器
              wheres[0].getBytes(),
              wheres[1].getBytes(),
              
              CompareOp.EQUAL,//各个条件之间是" and "的关系                 
              wheres[2].getBytes())
            );
        }  
        
        scan.setFilter(filterList);  
        
        ResultScanner ResultScannerFilterList = table.getScanner(scan);
        
        for(Result rs=ResultScannerFilterList.next(); rs!=null; rs=ResultScannerFilterList.next())
        {  
            for(KeyValue kv:rs.list())
            {  
             System.out.println("--------------------"+new String(kv.getRow())+"----------------------------");
             System.out.println("Column Family: "+new String(kv.getFamily()));
             System.out.println("Column       :"+new String(kv.getQualifier())+"\t");
             System.out.println("value        : "+new String(kv.getValue()));  
            }  
        }  
    }
    
    
  //hbase通过row key 的前缀查询记录
    public static void scaneByPrefixFilter(String tablename, String rowPrifix) 
    {
      try {
          HTable table = new HTable(configuration, tablename);
          
          Scan s = new Scan();
          
          s.setFilter(new PrefixFilter(rowPrifix.getBytes()));
          
          ResultScanner rs = table.getScanner(s);
          
          for (Result r : rs) 
          {
             KeyValue[] kv = r.raw();
             for (int i = 0; i < kv.length; i++) 
             {
                 System.out.print(new String(kv[i].getRow()) + "  ");
                 System.out.print(new String(kv[i].getFamily()) + ":");
                 System.out.print(new String(kv[i].getQualifier()) + "  ");
                 System.out.print(kv[i].getTimestamp() + "  ");
                 System.out.println(new String(kv[i].getValue()));
             }
         }
      }
      catch (IOException e) 
      {
            e.printStackTrace();
      }
    }
    
    /**
     *主方法 
     */
    public static void main(String[] args) {  
        // createTable("wujintao");  
         //createTable("usera");  
        // insertData("wujintao");  
        //insertData("usera");  
        // QueryAll("wujintao");  
       //QueryAll("usera");  
        // QueryByCondition1("wujintao");  
        // QueryByCondition2("wujintao");  
        // QueryByCondition2("user");  
        //QueryByCondition3("wujintao");  
    //QueryByCondition3("user");  
       // deleteRow("wujintao","abcdef");  
       // deleteByCondition("wujintao","abcdef");
   
   
    System.out.println("------------------------条件 查询----------------------------------"); 
      
    List<String> arr=new ArrayList<String>();
     
    arr.add("hostelname_qwe99_qwe99");
    //arr.add("id98_username98_hostelid98"); 
   
      
    try 
    {
selectFilter("user",arr);
} catch (IOException e) 
{
// TODO Auto-generated catch block
e.printStackTrace();
}
    //hbase通过row key 的前缀查询记录
    scaneByPrefixFilter("usera","id98_username98_hostelid98");
    }  
  

>>>>>>>>>>>>>>>原创文章,小部分来自网络整理(如果有什么问题,请您及时联系我naxiaohuo@163.com),转载请标明出处。  

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值