• 学习进度(2)


    时间:2020.10.11~2020.10.25

    学习了Hbase,zookeeper的安装和Hbase的shell命令,以及通过java API操作HBase数据库

    代码量:400行  学习时间:8小时

    记录下HBase的DML和DDL操作

    对数据的操作:

    import java.awt.List;
    import java.util.ArrayList;
    import java.util.Iterator;
    
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.hbase.Cell;
    import org.apache.hadoop.hbase.CellScanner;
    import org.apache.hadoop.hbase.HBaseConfiguration;
    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.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.util.Bytes;
    import org.junit.Before;
    import org.junit.Test;
    
    public class HbaseClientDML {
        
        public static Connection getConn() throws Exception{
            // 构建一个连接对象
            Connection conn = null;
            Configuration conf = HBaseConfiguration.create(); // 会自动加载hbase-site.xml
            conf.set("hbase.zookeeper.quorum", "hdp-01:2181,hdp-02:2181,hdp-03:2181");
            
            return  conn = ConnectionFactory.createConnection(conf);
        }
        
        
        /**
         * 增
         * 改:put来覆盖
         * @throws Exception 
         */
        public void testPut(Connection con) throws Exception{
            
            // 获取一个操作指定表的table对象,进行DML操作
            Table table = con.getTable(TableName.valueOf("user_info"));
            
            // 构造要插入的数据为一个Put类型(一个put对象只能对应一个rowkey)的对象
            Put put = new Put(Bytes.toBytes("001"));
            put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("username"), Bytes.toBytes("张三"));
            put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("age"), Bytes.toBytes("18"));
            put.addColumn(Bytes.toBytes("extra_info"), Bytes.toBytes("addr"), Bytes.toBytes("北京"));
            
            
            Put put2 = new Put(Bytes.toBytes("002"));
            put2.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("username"), Bytes.toBytes("李四"));
            put2.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("age"), Bytes.toBytes("28"));
            put2.addColumn(Bytes.toBytes("extra_info"), Bytes.toBytes("addr"), Bytes.toBytes("上海"));
        
            
            ArrayList puts = new ArrayList();
            puts.add(put);
            puts.add(put2);
            
            
            // 插进去
            table.put(puts);
            
            table.close();
            con.close();
            
        }
        
        
        /**
         * 循环插入大量数据
         * @throws Exception 
         */
        
        public void testManyPuts(Connection con) throws Exception{
            
            Table table = con.getTable(TableName.valueOf("user_info"));
            ArrayList puts = new ArrayList();
            
            for(int i=0;i<100000;i++){
                Put put = new Put(Bytes.toBytes(""+i));
                put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("username"), Bytes.toBytes("张三"+i));
                put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("age"), Bytes.toBytes((18+i)+""));
                put.addColumn(Bytes.toBytes("extra_info"), Bytes.toBytes("addr"), Bytes.toBytes("北京"));
                
                puts.add(put);
            }
            
            table.put(puts);
            
        }
        
        /**
         * 删
         * @throws Exception 
         */
        public void testDelete(Connection con) throws Exception{
            Table table = con.getTable(TableName.valueOf("user_info"));
            
            // 构造一个对象封装要删除的数据信息
            Delete delete1 = new Delete(Bytes.toBytes("001"));
            
            Delete delete2 = new Delete(Bytes.toBytes("002"));
            delete2.addColumn(Bytes.toBytes("extra_info"), Bytes.toBytes("addr"));
            
            ArrayList dels = new ArrayList();
            dels.add(delete1);
            dels.add(delete2);
            
            table.delete(dels);
            
            
            table.close();
            con.close();
        }
        
        /**
         * 查
         * @throws Exception 
         */
        public void testGet(Connection conn ) throws Exception{
            
            Table table = conn.getTable(TableName.valueOf("user_info"));
            
            Get get = new Get("002".getBytes());
            
            Result result = table.get(get);
            
            // 从结果中取用户指定的某个key的value
            byte[] value = result.getValue("base_info".getBytes(), "age".getBytes());
            System.out.println(new String(value));
            
            System.out.println("-------------------------");
            
            // 遍历整行结果中的所有kv单元格
            CellScanner cellScanner = result.cellScanner();
            while(cellScanner.advance()){
                Cell cell = cellScanner.current();
                
                byte[] rowArray = cell.getRowArray();  //本kv所属的行键的字节数组
                byte[] familyArray = cell.getFamilyArray();  //列族名的字节数组
                byte[] qualifierArray = cell.getQualifierArray();  //列名的字节数据
                byte[] valueArray = cell.getValueArray(); // value的字节数组
                
                System.out.println("行键: "+new String(rowArray,cell.getRowOffset(),cell.getRowLength()));
                System.out.println("列族名: "+new String(familyArray,cell.getFamilyOffset(),cell.getFamilyLength()));
                System.out.println("列名: "+new String(qualifierArray,cell.getQualifierOffset(),cell.getQualifierLength()));
                System.out.println("value: "+new String(valueArray,cell.getValueOffset(),cell.getValueLength()));
                
            }
            
            table.close();
            conn.close();
            
        }
        
        
        /**
         * 按行键范围查询数据
         * @throws Exception 
         */
        public void testScan(Connection conn ) throws Exception{
            
            Table table = conn.getTable(TableName.valueOf("user_info"));
            
            // 包含起始行键,不包含结束行键,但是如果真的想查询出末尾的那个行键,那么,可以在末尾行键上拼接一个不可见的字节(00)
            Scan scan = new Scan("10".getBytes(), "1000001".getBytes());
            
            ResultScanner scanner = table.getScanner(scan);
            
            Iterator iterator = scanner.iterator();
          //  ResultScanner resutScanner = table.getScanner(scan);
    
            while(iterator.hasNext()){
                
                Result result = (Result) iterator.next();
                // 遍历整行结果中的所有kv单元格
                CellScanner cellScanner = result.cellScanner();
                while(cellScanner.advance()){
                    Cell cell = cellScanner.current();
                    
                    byte[] rowArray = cell.getRowArray();  //本kv所属的行键的字节数组
                    byte[] familyArray = cell.getFamilyArray();  //列族名的字节数组
                    byte[] qualifierArray = cell.getQualifierArray();  //列名的字节数据
                    byte[] valueArray = cell.getValueArray(); // value的字节数组
                    
                    System.out.println("行键: "+new String(rowArray,cell.getRowOffset(),cell.getRowLength()));
                    System.out.println("列族名: "+new String(familyArray,cell.getFamilyOffset(),cell.getFamilyLength()));
                    System.out.println("列名: "+new String(qualifierArray,cell.getQualifierOffset(),cell.getQualifierLength()));
                    System.out.println("value: "+new String(valueArray,cell.getValueOffset(),cell.getValueLength()));
                }
                System.out.println("----------------------");
            }
        }
        
        public void test(){
            String a = "000";
            String b = "000";
            
            System.out.println(a);
            System.out.println(b);
            
            
            byte[] bytes = a.getBytes();
            byte[] bytes2 = b.getBytes();
            
            System.out.println("");
            
        }
        
        public static void main(String []args)
        {
            
        }
        
    
    }

    对表的操作

    import java.io.IOException;
    
    import org.apache.hadoop.conf.Configuration;
    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.regionserver.BloomType;
    import org.junit.Before;
    import org.junit.Test;
    
    
    /**
     *  
     *  1、构建连接
     *  2、从连接中取到一个表DDL操作工具admin
     *  3、admin.createTable(表描述对象);
     *  4、admin.disableTable(表名);
        5、admin.deleteTable(表名);
        6、admin.modifyTable(表名,表描述对象);    
     *  
     * @author hunter.d
     *
     */
    public class HbaseClientDDL {
        Connection conn = null;
        
        public void getConn() throws Exception{
            // 构建一个连接对象
            Configuration conf = HBaseConfiguration.create(); // 会自动加载hbase-site.xml
            conf.set("hbase.zookeeper.quorum", "hdp-01:2181,hdp-02:2181,hdp-03:2181");
            
            conn = ConnectionFactory.createConnection(conf);
        }
        
        
        
        /**
         * DDL
         * @throws Exception 
         */
        public void testCreateTable() throws Exception{
    
            // 从连接中构造一个DDL操作器
            Admin admin = conn.getAdmin();
            
            // 创建一个表定义描述对象
            HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf("user_info"));
            
            // 创建列族定义描述对象
            HColumnDescriptor hColumnDescriptor_1 = new HColumnDescriptor("base_info");
            hColumnDescriptor_1.setMaxVersions(3); // 设置该列族中存储数据的最大版本数,默认是1
            
            HColumnDescriptor hColumnDescriptor_2 = new HColumnDescriptor("extra_info");
            
            // 将列族定义信息对象放入表定义对象中
            hTableDescriptor.addFamily(hColumnDescriptor_1);
            hTableDescriptor.addFamily(hColumnDescriptor_2);
            
            
            // 用ddl操作器对象:admin 来建表
            admin.createTable(hTableDescriptor);
            
            // 关闭连接
            admin.close();
            conn.close();
            
        }
        
        
        /**
         * 删除表
         * @throws Exception 
         */
        public void testDropTable() throws Exception{
            
            Admin admin = conn.getAdmin();
            
            // 停用表
            admin.disableTable(TableName.valueOf("user_info"));
            // 删除表
            admin.deleteTable(TableName.valueOf("user_info"));
            
            
            admin.close();
            conn.close();
        }
        
        // 修改表定义--添加一个列族
        public void testAlterTable() throws Exception{
            
            Admin admin = conn.getAdmin();
            
            // 取出旧的表定义信息
            HTableDescriptor tableDescriptor = admin.getTableDescriptor(TableName.valueOf("user_info"));
            
            
            // 新构造一个列族定义
            HColumnDescriptor hColumnDescriptor = new HColumnDescriptor("other_info");
            hColumnDescriptor.setBloomFilterType(BloomType.ROWCOL); // 设置该列族的布隆过滤器类型
            
            // 将列族定义添加到表定义对象中
            tableDescriptor.addFamily(hColumnDescriptor);
            
            
            // 将修改过的表定义交给admin去提交
            admin.modifyTable(TableName.valueOf("user_info"), tableDescriptor);
            
            
            admin.close();
            conn.close();
            
        }
        
        
        /**
         * DML -- 数据的增删改查
         */
        
        public static void main(String []args)
        {
            //
            Connection conn = null;
            Configuration conf = HBaseConfiguration.create(); // 会自动加载hbase-site.xml
            conf.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
            
            try {
                
                conn = ConnectionFactory.createConnection(conf);
                
                // 从连接中构造一个DDL操作器
                Admin admin = conn.getAdmin();
                
                // 创建一个表定义描述对象
                HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf("result"));
                
                // 创建列族定义描述对象
                HColumnDescriptor hColumnDescriptor_1 = new HColumnDescriptor("info");
                hColumnDescriptor_1.setMaxVersions(3); // 设置该列族中存储数据的最大版本数,默认是1
                
                HColumnDescriptor hColumnDescriptor_2 = new HColumnDescriptor("extra_info");
                
                // 将列族定义信息对象放入表定义对象中
                hTableDescriptor.addFamily(hColumnDescriptor_1);
                //hTableDescriptor.addFamily(hColumnDescriptor_2);
                
                
                // 用ddl操作器对象:admin 来建表
                admin.createTable(hTableDescriptor);
                
                // 关闭连接
                admin.close();
                conn.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
        }
    
    }
  • 相关阅读:
    最高效的无限级菜单生成方法
    MySQL性能优化的最佳20+条经验(转)
    Web应用中的轻量级消息队列
    Delphi Setlength 内存释放总结
    Delphi 的TSpeedButton按下和弹起效果
    Delphi存取图像完整解决方案
    delphi 开机自动运行代码
    有关时间限制使用和软件注册的设计(delphi)
    Linux环境thinkphp配置以及数据源驱动改动
    Dalvik和ART简单介绍
  • 原文地址:https://www.cnblogs.com/ywqtro/p/13923428.html
Copyright © 2020-2023  润新知