用JAVA连接Hbase的DBHelp类

来源:互联网 发布:手机关闭蜂窝移动数据 编辑:程序博客网 时间:2024/05/17 18:26

用JAVA语言连接Hbase数据库的DBHelp
如下所示:


静态方法:

import java.io.IOException;import java.util.List;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.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.HBaseAdmin;import org.apache.hadoop.hbase.client.HTable;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;/** * hbase 中的数据表操作方法 * @author bigdata * */public class DBHelp{    private Configuration conf=null;    private Connection conn=null;    private HTable table=null;    private ResultScanner scann=null;    private HBaseAdmin admin=null;    static String LOCALHOST="lacalhost";    static String LOCALPOST="2181";/** *初始化操作 *主机名为“ubuntu”,端口号为2181 */    public boolean init (String LOCALHOST,String  LOCALPOST) throws IOException {        conf = HBaseConfiguration.create();        conf.set("hbase.zookeeper.quorum", LOCALHOST);        conf.set("hbase.zookeeper.property.clientPort", LOCALPOST);        conn = ConnectionFactory.createConnection(conf); //创建链接        admin = (HBaseAdmin) conn.getAdmin();//创建admin        if(conf != null && conn != null && admin != null){            return true;        }else{            return false;        }    }/** * 数据表创建操作 * @param tableName:表名 * @param familyClum:列簇集合 */    public void createTable(String tableName,String [] familyClum) {        try {            HBaseAdmin admin = (HBaseAdmin) conn.getAdmin();            HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(tableName));            for(String family:familyClum){                desc.addFamily(new HColumnDescriptor(family));            }                if (admin.tableExists(tableName)) {                    System.out.println("table " + tableName + " is exists !");    //              System.exit(0);                } else {                    admin.createTable(desc);                    System.out.println("table " + tableName+ " created successfully.");                }        } catch (IOException e) {            e.printStackTrace();        }     }/** * 表插入操作 * @param tableName:表名 * @param puts:插入的信息集合 */    public void insertData(String tableName,List<Put> puts) {        try {            table = (HTable) conn.getTable(TableName.valueOf(tableName));            if(puts.size()!=0){                table.put(puts);    // 将数据加入表            }         } catch (Exception e) {            e.printStackTrace();        }     }/** * 表扫描操作 * @param tableName:表名 */    public void scanTable(String tableName) {        try {            table = (HTable) conn.getTable(TableName.valueOf(tableName));            scann = table.getScanner(new Scan());            for (Result rs : scann) {                System.out.println("该行数据的 familyClum为:" + new String(rs.getRow()));                for (Cell cell : rs.rawCells()) {                    System.out.println("列族:" + new String(CellUtil.cloneFamily(cell)) + "\t" + " 列 修 饰 符 : "                            + new String(CellUtil.cloneQualifier(cell)) + "\t" + "值:"                            + new String(CellUtil.cloneValue(cell)) + "\t" + "时间戳:" + cell.getTimestamp());                }                System.out.println("-----------------------------------------------");            }        } catch (Exception e) {            e.printStackTrace();        }    }/** * 表单行查询操作 * @param tableName:表名 * @param rowKey:行名 */    public void queryByRow(String tableName,String rowKey) {        try {            table = (HTable) conn.getTable(TableName.valueOf(tableName));            Get get = new Get(rowKey.getBytes());            Result rs = table.get(get);            System.out.println("表" + tableName + "中的行数据如下");            for (Cell cell : rs.rawCells()) {                System.out.println("列族:" + new String(CellUtil.cloneFamily(cell)) + "\t" + "列修饰符:"                        + new String(CellUtil.cloneQualifier(cell)) + "\t" + "值:"                        + new String(CellUtil.cloneValue(cell)) + "\t" + "时间戳:" + cell.getTimestamp());            }        } catch (Exception e) {            e.printStackTrace();        }     }/** * 表单行删除操作 * @param tableName:表名 * @param rowKey:行名 */    public void deleteRow(String tableName,String rowKey) {        try {            table = (HTable) conn.getTable(TableName.valueOf(tableName));            table.delete(new Delete(rowKey.getBytes()));        } catch (Exception e) {            e.printStackTrace();        }     }/** * 表列簇删除操作 * @param tableName:表名 * @param familyClum:列簇名 */    public void deleteFamily(String tableName,String familyClum) {        try {            admin.deleteColumn(tableName.getBytes(), familyClum);        } catch (Exception e) {            e.printStackTrace();        }     }/** * 表删除操作 * @param tableName:表名 */    public void deleteTable(String tableName) {        try {// 在删除一张表前,要先使其失效            admin.disableTable(tableName);            admin.deleteTable(tableName);        } catch (Exception e) {            e.printStackTrace();        }     }/** * 表操作销毁 */    public void nuinit(){        try {            if (table != null) {                    table.close();            }             if (conn != null) {                conn.close();            }        } catch (Exception e) {            e.printStackTrace();        }    }}

测试方法

import java.io.IOException;import java.util.ArrayList;import java.util.List;import org.apache.hadoop.hbase.client.Put;import org.apache.hadoop.hbase.util.Bytes;/** * hbase表操作测试 * @author bigdata * */public class DBText {    public static void main(String[] args) throws IOException {        DBHelp test = new DBHelp();        if (test.init("ubuntu","2181")) {            String [] family={"name","grade"};//列簇集合,形成需要的列            test.createTable("school", family);//创建表以及列簇            List<Put> puts = new ArrayList<Put>();   // 添加数据,一个 Put 代表一行,构造函数传入的是 RowKey            Put put1 = new Put(Bytes.toBytes("key1"));//第一行数据            put1.addColumn(Bytes.toBytes("name"), Bytes.toBytes("001"), Bytes.toBytes("Jom"));            put1.addColumn(Bytes.toBytes("grade"), Bytes.toBytes("001"), Bytes.toBytes("1"));            Put put2 = new Put(Bytes.toBytes("key2"));//第二行数据            put2.addColumn(Bytes.toBytes("name"), Bytes.toBytes("001"), Bytes.toBytes("Smith"));            put2.addColumn(Bytes.toBytes("grade"), Bytes.toBytes("001"), Bytes.toBytes("2"));            puts.add(put1);//集合压入            puts.add(put2);            test.insertData("school", puts);//插入操作            test.scanTable("school");//扫描操作//           test.queryByRow("school","key1");//查询操作//           test.deleteRow("school","key1");//删除行操作//           test.deleteFamily("school","name");//删除列操作//           test.deleteTable("school");//删除表操作            test.nuinit();//表操作终止        }else{            System.out.println(" 初始化失败,任务需重启! ");        }    }}
1 0
原创粉丝点击