设为首页 加入收藏

TOP

Hbase编程封装实例
2019-05-02 01:46:49 】 浏览:50
Tags:Hbase 编程 封装 实例

1. HbaseConfiguration
关系:org.apache.hadoop.hbase.HBaseConfiguration
作用:通过此类可以对HBase进行配置
2.HBaseAdmin
关系:org.apache.hadoop.hbase.client.HBaseAdmin
作用:提供一个接口来管理HBase数据库中的表信息。它提供创建表、删除表等方法。
3.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) 设置属性的值
4.HColumnDescriptor
关系:org.apache.hadoop.hbase.client.HColumnDescriptor
作用:维护关于列的信息。提供的方法有
byte[] getName() 获取列族的名字
byte[] getValue() 获取对应的属性的值
void setValue(String key,String value) 设置对应属性的值
5.HTable
关系:org.apache.hadoop.hbase.client.HTable
作用:用户与HBase表进行通信。此方法对于更新操作来说是非线程安全的,如果启动多个线程尝试与单个HTable实例进行通信,那么写缓冲器可能会崩溃。
6.Put
关系:org.apache.hadoop.hbase.client.Put
作用:用于对单个行执行添加操作
7.Get
关系:org.apache.hadoop.hbase.client.Get
作用:用于获取单个行的相关信息
8.Result
关系:org.apache.hadoop.hbase.client.Result
作用:存储Get或Scan操作后获取的单行值。
9.ResultScanner
关系:Interface
作用:客户端获取值的接口。

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

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.Table;
import org.apache.hadoop.hbase.util.Bytes;

public class HBaseUtil {
    private static Configuration conf;
    private static Connection con;
    // 初始化连接
    static {
        conf = HBaseConfiguration.create(); // 获得配制文件对象
        conf.set("hbase.zookeeper.quorum", "192.168.52.140");
        try {
            con = ConnectionFactory.createConnection(conf);// 获得连接对象
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 获得连接
    public static Connection getCon() {
        if (con == null || con.isClosed()) {
            try {
                con = ConnectionFactory.createConnection(conf);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return con;
    }

    // 关闭连接
    public static void close() {
        if (con != null) {
            try {
                con.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    // 创建表
    public static void createTable(String tableName, String... FamilyColumn) {
        TableName tn = TableName.valueOf(tableName);
        try {
            Admin admin = getCon().getAdmin();
            HTableDescriptor htd = new HTableDescriptor(tn);
            for (String fc : FamilyColumn) {
                HColumnDescriptor hcd = new HColumnDescriptor(fc);
                htd.addFamily(hcd);
            }
            admin.createTable(htd);
            admin.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 删除表
    public static void dropTable(String tableName) {
        TableName tn = TableName.valueOf(tableName);
        try {
            Admin admin = con.getAdmin();
            admin.disableTable(tn);
            admin.deleteTable(tn);
            admin.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 插入或者更新数据
    public static boolean insert(String tableName, String rowKey,
            String family, String qualifier, String value) {
        try {
            Table t = getCon().getTable(TableName.valueOf(tableName));
            Put put = new Put(Bytes.toBytes(rowKey));
            put.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier),
                    Bytes.toBytes(value));
            t.put(put);
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            HBaseUtil.close();
        }
        return false;
    }

    // 删除
    public static boolean del(String tableName, String rowKey, String family,
            String qualifier) {
        try {
            Table t = getCon().getTable(TableName.valueOf(tableName));
            Delete del = new Delete(Bytes.toBytes(rowKey));

            if (qualifier != null) {
                del.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier));
            } else if (family != null) {
                del.addFamily(Bytes.toBytes(family));
            }
            t.delete(del);
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            HBaseUtil.close();
        }
        return false;
    }
    //删除一行
    public static boolean del(String tableName, String rowKey) {
        return del(tableName, rowKey, null, null);
    }
    //删除一行下的一个列族
    public static boolean del(String tableName, String rowKey, String family) {
        return del(tableName, rowKey, family, null);
    }

    // 数据读取
    //取到一个值
    public static String byGet(String tableName, String rowKey, String family,
            String qualifier) {
        try {
            Table t = getCon().getTable(TableName.valueOf(tableName));
            Get get = new Get(Bytes.toBytes(rowKey));
            get.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier));
            Result r = t.get(get);
            return Bytes.toString(CellUtil.cloneva lue(r.listCells().get(0)));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    //取到一个族列的值
        public static Map<String, String> byGet(String tableName, String rowKey, String family) {
            Map<String, String> result = null ;
            try {
                Table t = getCon().getTable(TableName.valueOf(tableName));
                Get get = new Get(Bytes.toBytes(rowKey));
                get.addFamily(Bytes.toBytes(family));
                Result r = t.get(get);
                List<Cell> cs = r.listCells();
                result = cs.size() > 0  new HashMap<String, String>() : result;
                for (Cell cell : cs) {
                    result.put(Bytes.toString(CellUtil.cloneQualifier(cell)), Bytes.toString(CellUtil.cloneva lue(cell)));
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return result;
        }
    //取到多个族列的值
        public static Map<String, Map<String, String>> byGet(String tableName, String rowKey) {
            Map<String, Map<String, String>> results = null ;
            try {
                Table t = getCon().getTable(TableName.valueOf(tableName));
                Get get = new Get(Bytes.toBytes(rowKey));
                Result r = t.get(get);
                List<Cell> cs = r.listCells();
                results = cs.size() > 0  new HashMap<String, Map<String, String>> () : results;
                for (Cell cell : cs) {
                    String familyName = Bytes.toString(CellUtil.cloneFamily(cell));
                    if (results.get(familyName) == null)
                    {
                        results.put(familyName, new HashMap<String,  String> ());
                    }
                    results.get(familyName).put(Bytes.toString(CellUtil.cloneQualifier(cell)), Bytes.toString(CellUtil.cloneva lue(cell)));
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return results;
        }
}

看看测试类

package com.yc.hbase.util;

import static org.junit.Assert.*;

import java.util.Map;

import org.junit.Test;

public class HBaseUtilTest {

    @Test
    public void testCreateTable() {
        //创建表
        HBaseUtil.createTable("myTest", "myfc1", "myfc2", "myfc3");
        HBaseUtil.close();
        HBaseUtil.createTable("myTest02", "myfc1", "myfc2", "myfc3");
        HBaseUtil.close();
    }

    @Test
    public void testDropTable() {
        //删除表
        HBaseUtil.dropTable("myTest");
        HBaseUtil.dropTable("myTest02");
    }

    @Test
    public void testInsert(){
        //插入数据
        HBaseUtil.insert("myTest", "1", "myfc1", "sex", "men");
        HBaseUtil.insert("myTest", "1", "myfc1", "name", "xiaoming");
        HBaseUtil.insert("myTest", "1", "myfc1", "age", "32");
        HBaseUtil.insert("myTest", "1", "myfc2", "name", "xiaohong");
        HBaseUtil.insert("myTest", "1", "myfc2", "sex", "woman");
        HBaseUtil.insert("myTest", "1", "myfc2", "age", "23");
    }

    @Test
    public void testByGet(){
        //得到一行下一个列族下的某列的数据
        String result = HBaseUtil.byGet("myTest", "1", "myfc1", "name");
        System.out.println("结果是的: " + result);
        assertEquals("xiaosan", result);
    }

    @Test
    public void testByGet02(){
        //得到一行下一个列族下的所有列的数据
        Map<String, String> result = HBaseUtil.byGet("myTest", "1", "myfc1");
        System.out.println("结果是的: " + result);
        assertNotNull(result);
    }

    @Test
    public void testByGet03(){
        //得到一行的所有列族的数据
        Map<String, Map<String, String>> result = HBaseUtil.byGet("myTest", "1");

        System.out.println("所有列族的数据是:  "+result);
        System.out.println("结果是的: " + result.get("myfc1"));
        assertNotNull(result);
    }
}
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇HBase shell 命令入门 下一篇HBase的基本结构

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目