HBase API 详细例子(封装的DAO类)

本文介绍了在HBase 0.98.5版本中如何使用HBase API,并提供了详细的API实例,同时讲解了如何将HBase库集成到项目中。文章还提到了常用接口的运用,强调了在引用内容时需保留原创出处。
摘要由CSDN通过智能技术生成


HBase没有库的概念

HBase lib目录下所有JAR包复制到项目中,Hbase 版本0.98.5

package com.zxing.imgQRCode;

import java.io.IOException;
import java.util.LinkedList;
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.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HConnection;
import org.apache.hadoop.hbase.client.HConnectionManager;
import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.io.compress.Compression.Algorithm;
import org.apache.hadoop.hbase.io.encoding.DataBlockEncoding;

public class HbaseConnection {
    private String rootDir;
    private String zkServer;
    private String port;
    private Configuration conf;
    private HConnection hConn=null;
    
    public HbaseConnection(String rootDir, String zkServer, String port) {
        super();
        this.rootDir = rootDir;
        this.zkServer = zkServer;
        this.port = port;
        conf=HBaseConfiguration.create();
        conf.set("hbase.rootdir", rootDir);
        conf.set("hbase.zookeeper.quorum ", zkServer);
        conf.set("hbase.zookeeper.property.clientPort", port);
        try {
            hConn=HConnectionManager.createConnection(conf);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    //创建表
    public void crateTable(String tableName,List<String> cols){
        try {
            HBaseAdmin admin=new HBaseAdmin(conf);
            if(admin.tableExists(tableName))
                throw new IOException("table exists");
            else{
                
                HTableDescriptor tableDesc=new HTableDescriptor(tableName);
                for(String col:cols){
                    
                    HColumnDescriptor colDesc=new HColumnDescriptor(col);
                    colDesc.setCompressionType(Algorithm.GZ);
                    colDesc.setDataBlockEncoding(DataBlockEncoding.DIFF);
                    tableDesc.addFamily(colDesc);
                }
                admin.createTable(tableDesc);
            }
            
        } 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();
        }
        
        
    }
    //插入数据
    public void saveData(String tableName,List<Put> puts){
        
        try {
            HTableInterface table =hConn.getTable(tableName);
            table.put(puts);
            table.setAutoFlush(false);
            table.flushCommits();
            
        } catch (IOException e) {
    
            e.printStackTrace();
        }
    }
    
    //得到数据
    public Result getData(String tableName,String rowkey){
        try {
            HTableInterface table =hConn.getTable(tableName);
            Get get=new Get(rowkey.getBytes());
            return table.get(get);
        } catch (IOException e) {
            
            e.printStackTrace();
        }
        return null;
        
    }     
    
    //输出result结果
    public void format(Result result){
        
        String rowkey=Bytes.toString(result.getRow());
        KeyValue[] kvs=result.raw();
        for (KeyValue kv:kvs){
                String family= Bytes.toString(kv.getFamily());
            String qualifier= Bytes.toString(kv.getQualifier());
            System.out.println("rowkey->"+rowkey+"family->"+family+"qualifier->"+qualifier);
        }
    }
    
    
    //全表扫描
    public void hbaseScan(String tableName){
        
        Scan scan=new Scan();//扫描器
        scan.setCaching(1000);//缓存1000条数据,一次读取1000条
        try {
            HTableInterface table =hConn.getTable(tableName);
            ResultScanner scanner=table.getScanner(scan);//返回迭代器
            for(Result res:scanner){
                format(res);
            }
            
        } catch (IOException e) {
            
            e.printStackTrace();
        }
    }
    
    
    //比较过滤器
    public void filterTest(String tableName){
        Scan scan=new Scan();//扫描器
        scan.setCaching(1000);//缓
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值