千家信息网

HBase基本API操作之CRUD-Util怎么用

发表于:2026-11-02 作者:千家信息网编辑
千家信息网最后更新 2026年11月02日,这篇文章将为大家详细讲解有关HBase基本API操作之CRUD-Util怎么用,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。一:创建HBaseUtil。public
千家信息网最后更新 2026年11月02日HBase基本API操作之CRUD-Util怎么用

这篇文章将为大家详细讲解有关HBase基本API操作之CRUD-Util怎么用,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

一:创建HBaseUtil。

public class HBaseUtil {        private static Configuration conf;        private static Connection con;                //初始化联接        static{                //获得配置文件对象:                conf = HBaseConfiguration.create();                conf.set("hbase.zookeeper.quorum", "192.168.226.129");                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 closeCon(){                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 table = con.getTable(TableName.valueOf(tableName));                        Put put = new Put(Bytes.toBytes(rowKey));                        put.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier), Bytes.toBytes(value));                        table.put(put);                        return true;                } catch (IOException e) {                        e.printStackTrace();                }finally{                //      HBaseUtil.closeCon();                }                return false;        }        //删除数据记录        public static boolean delete(String tableName,String rowKey,String family,String qualifier){                try {                        Table table = con.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) );                        }                        table.delete(del);                        return true;                } catch (IOException e) {                        e.printStackTrace();                }finally{                        //HBaseUtil.closeCon();                }                return false;        }                //删除整行的数据记录        public static boolean delete(String tableName,String rowKey){                return delete(tableName, rowKey, null, null);        }                //删除某行某列的数据记录        public static boolean delete(String tableName, String rowKey, String family){                return delete(tableName, rowKey, family, null);        }                //数据读取        //取到一个值        public static String byGet(String tableName,String rowKey,String family,String qualifier){                try {                        Table table = con.getTable(TableName.valueOf(tableName));                        Get get = new Get(Bytes.toBytes(rowKey));                        get.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier));                        Result result = table.get(get);                        return Bytes.toString(CellUtil.cloneValue( result.listCells().get(0)));                } catch (IOException e) {                        e.printStackTrace();                }                return null;        }                //取到一个族列的值        public static Map byGet(String tableName,String rowKey, String family ){                Map map = null;                try {                        Table table = getCon().getTable( TableName.valueOf(tableName));                        Get get = new Get(Bytes.toBytes(rowKey));                        get.addFamily( Bytes.toBytes( family ));                        Result result = table.get(get);                        List list = result.listCells();                        map =  (Map) (list.size() > 0 ? new HashMap() : result);                        for( Cell cell : list ){                                map.put(Bytes.toString(CellUtil.cloneQualifier(cell)), Bytes.toString(CellUtil.cloneValue(cell)));                        }                } catch (IOException e) {                        e.printStackTrace();                }                return map;        }                //取到多个列族的值        public static Map> byGet(String tableName,String rowKey){                Map> maps = null;                                try {                        Table table = con.getTable(TableName.valueOf(tableName));                        Get get = new Get(Bytes.toBytes(rowKey));                        Result result = table.get(get);                        List list = result.listCells();                        maps = (Map>) (list.size() >0 ? new HashMap>() : result);                        for( Cell cell : list){                                String familyName = Bytes.toString(CellUtil.cloneFamily(cell));                                if( maps.get(familyName) == null ){                                        maps.put(familyName, new HashMap() );                                }                                maps.get(familyName).put(Bytes.toString(CellUtil.cloneQualifier(cell)), Bytes.toString(CellUtil.cloneValue(cell)));                        }                } catch (IOException e) {                        e.printStackTrace();                }                return maps;        }}

二:单元测试类 TestHBaseJUnit。

public class TestHBaseJUnit {        //创建表并列出所有的表:        @Test        public void testCreateTable() throws IOException {                //创建两张表:  person 与 student                 HBaseUtil.createTable("person", "famcolumn1","famcolumn2","famcolumn3");                HBaseUtil.closeCon();                HBaseUtil.createTable("student", "famcolumn1","famcolumn2","famcolumn3");                HBaseUtil.closeCon();                //列出所有表:                Admin admin = HBaseUtil.getCon().getAdmin();                TableName[] tables = admin.listTableNames();                for (TableName tableName : tables) {                        System.out.println( "tableName: " + tableName );                }        }        ////判断数据表是否存在。        @Test        public void testTableIsExists() throws IOException{                TableName tn = TableName.valueOf("person"); //创建表名对象                Admin admin = HBaseUtil.getCon().getAdmin();                boolean isExists = admin.tableExists(tn);                System.out.println( "person is Exists: "+ isExists  );        }        //删除表        @Test        public void testDropTable() throws IOException{                Admin admin = HBaseUtil.getCon().getAdmin();                TableName tn = TableName.valueOf("student");                System.out.println( admin.isTableDisabled(tn) );                HBaseUtil.dropTable("student");                admin = HBaseUtil.getCon().getAdmin();                //判断数据表是否还存在。                boolean isExists = admin.tableExists(tn);                System.out.println( "student is Exists: "+ isExists  );        }        //对表插入数据记录        @Test        public void testInsert() throws TableNotFoundException, IOException{                HBaseUtil.insert("person", "row1", "famcolumn1", "name", "Berg");                HBaseUtil.insert("person", "row1", "famcolumn1", "age", "22");                HBaseUtil.insert("person", "row1", "famcolumn1", "sex", "male");                HBaseUtil.insert("person", "row1", "famcolumn2", "name", "BergBerg");                HBaseUtil.insert("person", "row1", "famcolumn2", "age", "21");                HBaseUtil.insert("person", "row1", "famcolumn2", "sex", "male");                HBaseUtil.insert("person", "row1", "famcolumn3", "name", "BergBergBerg");                HBaseUtil.insert("person", "row1", "famcolumn3", "age", "23");                HBaseUtil.insert("person", "row1", "famcolumn3", "sex", "famale");                Admin admin = HBaseUtil.getCon().getAdmin();                TableName tn = TableName.valueOf("person");                System.out.println( admin.getTableDescriptor(tn) );        }        //取到一个值        @Test        public void testByGet1(){                String result = HBaseUtil.byGet("person", "row1", "famcolumn1", "name");                System.out.println( " result:  " + result );        }        //取到一个族列的值        @Test        public void testByGet2(){                Map result = HBaseUtil.byGet("person", "row1", "famcolumn1");                System.out.println( " result:  " + result );        }        //取到多个列族的值        @Test        public void testByGet3(){                Map> result = HBaseUtil.byGet("person", "row1");                System.out.println( " result:  " + result );        }        //删除数据记录        @Test        public void testDelete1(){                HBaseUtil.delete("person", "row1", "famcolumn3", "age");                //删除数据后:                Map> result = HBaseUtil.byGet("person", "row1");                System.out.println( " result:  " + result );        }        //删除某列数据        @Test        public void testTelete2(){                HBaseUtil.delete("person", "row1", "famcolumn3");                //删除数据后:                Map> result = HBaseUtil.byGet("person", "row1");                System.out.println( " result:  " + result );        }                //删除整行的数据        @Test        public void testTelete3(){                HBaseUtil.delete("person", "row1");                //删除数据后:                Map> result = HBaseUtil.byGet("person", "row1");                System.out.println( " result:  " + result );        }}

关于"HBase基本API操作之CRUD-Util怎么用"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

数据 对象 篇文章 多个 数据表 更多 不错 实用 内容 单元 文件 文章 知识 参考 帮助 更新 有关 测试 配置 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 中关村互联网 科技集团 圆梦互联网科技 智网互联网科技 齐家互联网科技 云电互联网科技 汇久创客互联网科技 互联网科技logo设计 伴你行互联网科技有限公司 长亮科技 互联网金融 互联网科技大会案例 深圳前海聚择互联网科技有限公司 广州慧亚互联网科技怎么样 欧洲十大科技互联网公司 开一家科技互联网公司需要多少钱 亳州好赞互联网科技有限公司 互联网第三次科技革命特征 厦门江牛科技互联网 兰石爱特工业互联网科技分公司 银川新华互联网科技学校原画 恒生互联网科技趋势 百单网互联网科技有限公司怎么样 互联网佳科技有限公司 互联网科技公司名称 江苏南京365互联网科技 互联网科技革命什么开始 互联网科技研发水平 浙南科技城互联网大厦建设工程 南博望互联网科技有限公司 三亚科技创新互联网基地 山东亿川互联网科技有限责任公司
0