一个简单的HbaseHelper类

记得要创建一个dbinfo.properties文件,内容格式为key=value格式,具体放在哪里由你决定。里面放置了连接Hbase所需的参数。
使用这个类目的就是让使用者更方便、更安全的对数据库的操作,既是除了在Helper类以外的所有类将不用引用对数据库操作的任何类与语句,无须担心数据库的连接与关闭的问题。

package Hbase_tolist;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.DriverManager;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
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.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
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.client.backoff.ClientBackoffPolicy;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.PageFilter;
import org.apache.hadoop.hbase.util.Bytes;
/**
 * Created by root on 5/17/16.
 */

public class HbsHelper {

    private static final String Connection_KEY_QUORUM ="hbase.zookeeper.quorum";
    private static final String Connection_KEY_clientport ="hbase.zookeeper.property.clientport";

    private static String quorum = "";
    private static String clientport = "";

    private static Connection conn = null;
    private static Admin admin=null;

    private static Properties pp = null;
    private static FileInputStream fis = null;

    // 加载驱动,只需要一次
    static {
        try {
            // 从配置文件dbinfo.properties中读取配置信息
            pp = new Properties();
            fis = new FileInputStream("/root/Desktop/dbinfo.properties");
            pp.load(fis);
            quorum = pp.getProperty("quorum").trim();
            clientport = pp.getProperty("clientport").trim();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fis != null)
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            fis = null;
        }
    }

    public HbsHelper(){}

    // 得到连接
    public static Connection getConnection() {
        try {
            Configuration conf = HBaseConfiguration.create();
            conf.set(Connection_KEY_QUORUM, quorum);
            conf.set(Connection_KEY_clientport, clientport);
            conn = ConnectionFactory.createConnection(conf);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }


    //list tables
    public static void listTables () throws IOException {

        try{
            conn = getConnection();
            admin = conn.getAdmin();

            TableName [] names = admin.listTableNames();
            for (TableName tableName : names) {
                System.out.println("Table Name is : " + tableName.getNameAsString());
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        } finally {
            // 关闭资源
            close(admin,conn);
        }
    }

    //create table
    public static Boolean createTable (HashMap parameter) throws IOException {
        boolean exists = false;
        try {
            // 获得连接
            conn = getConnection();
            admin = conn.getAdmin();

            String table_name = parameter.get("table_name").toString();
            String cf_name = parameter.get("cf_name").toString();
            TableName tableName = TableName.valueOf(table_name);

            exists = admin.tableExists(tableName);
            if (!exists) {
                HTableDescriptor tableDesc = new HTableDescriptor(tableName);
                HColumnDescriptor columnDesc = new HColumnDescriptor(cf_name);
                tableDesc.addFamily(columnDesc);
                admin.createTable(tableDesc);
            }

        } catch (Exception e) {
            e.printStackTrace();
            //throw new RuntimeException(e.getMessage());
        } finally {
            // 关闭资源
            close(admin,conn);
        }
        return !exists;
    }

    public static void putDatas (HashMap parameter) throws IOException {

        try {
            // 获得连接
            conn = getConnection();
            admin = conn.getAdmin();
            //createTable
            String table_name = parameter.get("table_name").toString();
            String cf_name = parameter.get("cf_name").toString();
            String row_name = parameter.get("raw_name").toString();

            String [] columns = (String [])parameter.get("columns");
            String [] value = (String [])parameter.get("value");

            Table table = conn.getTable(TableName.valueOf(table_name));
            Put put = new Put(Bytes.toBytes(row_name));

            for (int j = 0; j < columns.length; j++) {

                put.addColumn(Bytes.toBytes(cf_name), Bytes.toBytes(columns[j]), Bytes.toBytes(value[j]));

            }
            table.put(put);
            table.close();

        } catch (Exception e) {
            e.printStackTrace();
            //throw new RuntimeException(e.getMessage());
        } finally {
            // 关闭资源
            close(admin,conn);
        }
    }

    public static HashMap <String, String> getData(HashMap parameter)throws IOException{
        HashMap para = new HashMap();
        try{
            // 获得连接
            conn = getConnection();
            admin = conn.getAdmin();

            String table_name = parameter.get("table_name").toString();
            String cf_name = parameter.get("cf_name").toString();
            String row = parameter.get("raw_name").toString();

            TableName tableName = TableName.valueOf(table_name);
            Table table = conn.getTable(tableName);
            Get get = new Get(Bytes.toBytes(row));
            get.addFamily(Bytes.toBytes(cf_name));
            Result result = table.get(get);
            List<Cell> cells = result.listCells();
            for (Cell cell : cells) {

                String qualifier = new String(CellUtil.cloneQualifier(cell));
                String value = new String(CellUtil.cloneValue(cell), "UTF-8");
                // LOG.info(cell.getQualifier() + "\t" + cell.getValue());
                para.put(qualifier,value);
                //System.out.println(qualifier + "\t" + value);
            }

        } catch (Exception e) {
            e.printStackTrace();
            //throw new RuntimeException(e.getMessage());
        } finally {
            // 关闭资源
            close(admin,conn);
            return para;
        }

    }

    public static void deleteDatas(HashMap parameter) throws IOException {
        try {
            // 获得连接
            conn = getConnection();
            admin = conn.getAdmin();
            Table table =null;

            String table_name = parameter.get("table_name").toString();
            String cf_name = parameter.get("cf_name").toString();
            String row = parameter.get("raw_name").toString();

            TableName tableName = TableName.valueOf(table_name);
            Delete delete = new Delete(Bytes.toBytes(row));

            String [] columns = (String [])parameter.get("columns");
            for (int j = 0; j < columns.length; j++) {
                delete.addColumn(Bytes.toBytes(cf_name), Bytes.toBytes(columns[j]));
                table = conn.getTable(tableName);
                table.delete(delete);
            }
            // @deprecated Since 1.0.0. Use {@link #(byte[])}
            // delete.addFamily(family);                            // 删除某个列族

            table.close();

        } catch (Exception e) {
            e.printStackTrace();
            //throw new RuntimeException(e.getMessage());
        } finally {
            // 关闭资源
            close(admin,conn);
        }
    }

    public static void close(Admin ad, Connection conn) {
        if (ad != null)
            try {
                ad.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

        if (conn != null)
            try {
                conn.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
    }
}

调用函数大概为:

package Hbase_tolist;

import java.io.IOException;
import java.util.HashMap;

/**
 * Created by root on 5/18/16.
 */
public class TestHelper {
    public static void main(String [] args) throws IOException {

        HashMap<String, Object> parameters = new HashMap<>();
        parameters.put("table_name","m_domain");
        parameters.put("cf_name","cf");
        parameters.put("raw_name","baidu.com_19991011_20151011");
        parameters.put("columns", new String[]{"owner", "access_server"});
        parameters.put("value",new String[]{"ShangHai Splunk Technology Co.", "ShangHai",});

        //HbsHelper manageMain = new HbsHelper();

        //addatble
        //System.out.print(HbsHelper.createTable(parameters));
        //add data
        HbsHelper.putDatas(parameters);


        //HbsHelper.deleteDatas(parameters);

        //search get data
        //System.out.print(HbsHelper.getData(parameters).get("owner"));

        //list table
        //HbsHelper.listTables();

    }
}

代码参考了一个网友写的SQLHelper类:http://blog.csdn.net/liugenghao3048/article/details/9029403

猜你喜欢

转载自blog.csdn.net/u010030977/article/details/51456083