Hbase的API操作

1.创建maven工程导入依赖

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
		<!-- Apache HBase Client -->
		<dependency>
			<groupId>org.apache.hbase</groupId>
			<artifactId>hbase-client</artifactId>
			<version>1.3.1</version>
		</dependency>
		<dependency>
			<groupId>cglib</groupId>
			<artifactId>cglib</artifactId>
			<version>3.2.5</version>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.17</version>
		</dependency>
	</dependencies>

2.使用cglib动态代理实现在调用方法的前后自动开启关闭连接,下面是目标类。

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Delete;
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.client.Table;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.RegexStringComparator;
import org.apache.hadoop.hbase.filter.RowFilter;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;

public class HbaseTarget {
    public static Connection connection;
    public static Admin admin;
    /**
     * 打开连接
     * @throws IOException
     */
    public static void OpenConnection() throws IOException {
        Configuration conf = HBaseConfiguration.create();
        // zookeeper的配置信息
        conf.set("hbase.zookeeper.quorum", "master,slave1,slave2");// zookeeper节点信息
        conf.set("hbase.zookeeper.property.clientPort", "2181");// zookeeper端口
        connection = ConnectionFactory.createConnection(conf);
        admin = connection.getAdmin();
    }
    /**
     * 关闭连接
     * @throws IOException
     */
    public static void closeConnection() throws IOException {
        if (null != admin) {
            admin.close();
            admin=null;
        }
        if (null != connection) {
            connection.close();
            connection=null;
        }
    }
    
    /**
     * 查看已有表
     * @throws IOException
     */
    public void listTables() throws IOException {
        HTableDescriptor hTableDescriptors[] = admin.listTables();
        for (HTableDescriptor hTableDescriptor : hTableDescriptors) {
            System.out.println(hTableDescriptor.getNameAsString());
        }
    }
    /**
     * 通过列查询
     * @param tableName
     * @param rowKey
     * @throws IOException
     */
    public void scanByCell(String tableName,String rowKey) throws IOException {

        // 设置过滤器
        SingleColumnValueFilter filter = new SingleColumnValueFilter(Bytes.toBytes("f"), Bytes.toBytes("Date"),
                CompareOp.EQUAL, Bytes.toBytes(rowKey));
        // 设置全表扫描封装类
        Scan scan = new Scan();
        // 添加过滤器
        scan.setFilter(filter);
        // 扫描
        Table table = connection.getTable(TableName.valueOf(tableName));
        ResultScanner resultScanner = table.getScanner(scan);
        for (Result result : resultScanner) {
            showCell(result);
        }
        table.close();
    }

    /**
     * 通过正则--匹配行键
     * @param tableName
     * @param rowKey
     * @throws IOException
     */
    public void scanByRow(String tableName,String rowKey) throws IOException {

        RegexStringComparator re = new RegexStringComparator("^" + rowKey + "");
        Filter filter = new RowFilter(CompareOp.EQUAL, re);
        Scan scan = new Scan();
        // 添加过滤器
        scan.setFilter(filter);
        // 扫描
        Table table = connection.getTable(TableName.valueOf(tableName));
        ResultScanner resultScanner = table.getScanner(scan);
        for (Result result : resultScanner) {
            showCell(result);

        }
    }


    /**
     * 插入数据
     * @param tableName
     * @param rowkey
     * @param colFamily
     * @param col
     * @param val
     * @throws IOException
     */
    public void insterRow(String tableName, String rowkey, String colFamily, String col, String val)
            throws IOException {
        Table table = connection.getTable(TableName.valueOf(tableName));
        Put put = new Put(Bytes.toBytes(rowkey));
        put.addColumn(Bytes.toBytes(colFamily), Bytes.toBytes(col), Bytes.toBytes(val));
        table.put(put);
        // 批量插入
        /*
         * List<Put> putList = new ArrayList<Put>(); putList.add(put);
         * table.put(putList);
         */
        table.close();
    }

    /**
     * 格式化输出
     * @param result
     */
    public void showCell(Result result) {
        Cell[] cells = result.rawCells();
        for (Cell cell : cells) {
            System.out.println("RowName:" + new String(CellUtil.cloneRow(cell)) + " ");
            System.out.println("Timetamp:" + cell.getTimestamp() + " ");
            System.out.println("column Family:" + new String(CellUtil.cloneFamily(cell)) + " ");
            System.out.println("row Name:" + new String(CellUtil.cloneQualifier(cell)) + " ");
            System.out.println("value:" + new String(CellUtil.cloneValue(cell)) + " ");
        }
    }
    /**
     * 建表
     * @param tableNmae
     * @param cols
     * @throws IOException
     */
    public void createTable(String tableNmae, String[] cols) throws IOException {

        TableName tableName = TableName.valueOf(tableNmae);

        if (admin.tableExists(tableName)) {
            System.out.println("talbe is exists!");
        } else {
            HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
            for (String col : cols) {
                HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(col);
                hTableDescriptor.addFamily(hColumnDescriptor);
            }
            admin.createTable(hTableDescriptor);
        }

    }

    /**
     * 删表
     * @param tableName
     * @throws IOException
     */
    public void deleteTable(String tableName) throws IOException {
        TableName tn = TableName.valueOf(tableName);
        if (admin.tableExists(tn)) {
            admin.disableTable(tn);
            admin.deleteTable(tn);
        }
    }

    /**
     * 删除数据
     * @param tableName
     * @param rowkey
     * @param colFamily
     * @param col
     * @throws IOException
     */
    public void deleRow(String tableName, String rowkey, String colFamily, String col) throws IOException {
        Table table = connection.getTable(TableName.valueOf(tableName));
        Delete delete = new Delete(Bytes.toBytes(rowkey));
        // 删除指定列族
        // delete.addFamily(Bytes.toBytes(colFamily));
        // 删除指定列
        delete.addColumn(Bytes.toBytes(colFamily), Bytes.toBytes(col));
        table.delete(delete);
        // 批量删除
        /*
         * List<Delete> deleteList = new ArrayList<Delete>(); deleteList.add(delete);
         * table.delete(deleteList);
         */
        table.close();
    }

    protected HbaseTarget() {
        super();
        // TODO Auto-generated constructor stub
    }

}

3.编写代理类,在main方法中测试

import java.io.IOException;
import java.lang.reflect.Method;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

public class HbaseProxy extends HbaseTarget implements MethodInterceptor {
    //维护目标对象
    private HbaseTarget target;

    public HbaseProxy() {
         this.target = new HbaseTarget();
    }
    //给目标对象创建一个代理对象
    public HbaseTarget  getProxyInstance(){
        //1.工具类
        Enhancer en = new Enhancer();
        //2.设置父类
        en.setSuperclass(target.getClass());
        //3.设置回调函数
        en.setCallback(this);
        //4.创建子类(代理对象)
        return (HbaseTarget) en.create();

    }
    public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
            target.OpenConnection();
            //执行目标对象的方法
            Object returnValue = method.invoke(target, args);
            target.closeConnection();
            return returnValue; 
    }
    public static void main(String[] args) throws IOException {
        HbaseTarget target = new HbaseProxy().getProxyInstance();
        //target.scanByCell("t_user","1001");
        target.listTables();
    }

    
}

4.测试结果

posted on 2018-11-22 15:56  优品三悦  阅读(282)  评论(0编辑  收藏  举报

导航