• Hbase之API基本操作


    API之框架

    private static Admin admin = null;
    private static Connection connection = null;
    private static Configuration conf = null;
    
    static {
        // hbase的配置文件
        conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "192.168.11.11");
        conf.set("hbase.zookeeper.property.clientPort", "2181");
    
        // 获取hbase的管理员
        try {
            connection = ConnectionFactory.createConnection(conf);
            admin = connection.getAdmin();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    // 进行核心代码编写
    
    // 释放hbase的资源
    private void close(Connection connection, Admin admin) {
        if (connection != null) {
            try {
                connection.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (admin != null) {
            try {
                admin.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    View Code

    判断表是否存在

    // 判断表是否存在
    public static boolean tableExists(String tableName) throws IOException {
        // 执行操作
        boolean tableExists = admin.tableExists(TableName.valueOf(tableName));
        return tableExists;
    }
    
    public static void main(String[] args) throws IOException {
            // 查看表是否存在
            System.out.println(tableExists("student"));
    }
    View Code

    创建表

    // 创建表
    public static void createTable(String tableName, String... cfs) throws IOException {
        if (tableExists(tableName)) {
            System.out.println(tableName + "已存在");
            return;
        }
        // 表需要表描述器
        HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
    
        // 添加列族,列族可以有多个,so  for{ }
        for (String cf : cfs) {
            // 列族需要列描述器
            HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(cf);
            hColumnDescriptor.setMaxVersions(1);// 添加版本号
            hTableDescriptor.addFamily(hColumnDescriptor);
        }
        //创建表
        admin.createTable(hTableDescriptor);
        System.out.println(tableName + "创建成功!!!");
    }
    
    public static void main(String[] args) throws IOException {
            /*// 创建表*/
           createTable("student1", "info");
           System.out.println(tableExists("student1"));
    }
    View Code

    删除表

    // 删除表
    public static void deleteTable(String tableName) throws IOException {
        if (tableExists(tableName)) {
            // 使得表下线(使表不可用)
            admin.disableTable(TableName.valueOf(tableName));
            // 删除表
            admin.deleteTable(TableName.valueOf(tableName));
            System.out.println(tableName + "删除成功!!!");
        } else System.out.println(tableName + "不存在!!!");
    }
    
    public static void main(String[] args) throws IOException {
    /*// 删除表*/
     deleteTable("student1");
     System.out.println(tableExists("student1")); 
    }
    View Code

    添加数据

    // 增加数据//修改数据
    public static void putData(String tableName, String rowKey, String cf, String cn, String value) throws IOException {
        if (tableExists(tableName)) {
            // 获取table对象
            Table table = connection.getTable(TableName.valueOf(tableName));
            ArrayList<Put> list = new ArrayList<Put>();
            Put put = null;
            for (int i = 0; i <= rowKey.length(); i++) {
                put = new Put(Bytes.toBytes(rowKey));
                put.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn), Bytes.toBytes(value));
                list.add(put);
                if (i % 100 == 0) {
                    // 开始执行添加数据操作
                    table.put(list);
                    list.clear();
                }
            }
            // 关闭table
            table.close();
        } else System.out.println(tableName + "不存在!!!");
    }
    
    public static void main(String[] args) throws IOException {
    /*// 添加数据*/
    for (int i = 0; i <= 1000; i++) {
        putData("student", "100" + i, "info", "email", "tao" + i + "qq.com");
    }
    
    }
    View Code

    删除数据

     // 删除表
        public static void delete(String tableName, String rowkey, String cf, String cn) throws IOException {
            if (tableExists(tableName)) {
                // 获取table对象
                Table table = connection.getTable(TableName.valueOf(tableName));
                // 创建delete对象
                Delete delete = new Delete(Bytes.toBytes(rowkey));
    
                // 删除所有版本的数据,公司常用的
                delete.addColumns(Bytes.toBytes(cf), Bytes.toBytes(cn));
                /**
                 * 如果是多个版本的数据,删除最新版本的数据,之前的版本会出来顶替值
                 * 在公司慎用
                 * 可以更细粒度是控制版本  timestamp
                 */
    //            delete.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn));
    
                // 开始执行删除操作
                table.delete(delete);
                // 关闭table
                table.close();
            } else System.out.println(tableName + "不存在!!!");
        }
    
    public static void main(String[] args) throws IOException {
    /*// 删除数据*/
    delete("student", "1001", "info", "name");
    }
    View Code

    全表扫描

    // 全表扫描
        public static void scan(String tableName, String startRow) throws IOException {
            // 创建表对象
            Table table = connection.getTable(TableName.valueOf(tableName));
            // 创建scan对象  可以设置多条件   eg:startRow,stopRow
            Scan scan = new Scan();
    //        scan.getStartRow(Bytes.toBytes(startRow));
    //        scan.getStopRow();
    
            // 开始扫描数据
            ResultScanner resultScanner = table.getScanner(scan);
            // 表有多个rowkey
            for (Result result : resultScanner) {
                Cell[] cells = result.rawCells();
                // 每个rowkey有多个单元格数据
                for (Cell cell : cells) {
                    System.out.println("RK-->" + Bytes.toString(CellUtil.cloneRow(cell))
                            + ",CF-->" + Bytes.toString(CellUtil.cloneFamily(cell))
                            + ",CN-->" + Bytes.toString(CellUtil.cloneQualifier(cell))
                            + ",VALUE-->" + Bytes.toString(CellUtil.cloneValue(cell)));
                }
            }
        }
    
    public static void main(String[] args) throws IOException {
    /*// 全表扫描数据*/
    scan("student", "1002");
    }
    View Code

    根据rowkey获取数据

    // 获取指定数据  可以指定cf  cn
    public static void getData(String tableName, String rowkey, String cf, String cn) throws IOException {
        // 获取表对象
        Table table = connection.getTable(TableName.valueOf(tableName));
        // 获取get对象
        Get get = new Get(Bytes.toBytes(rowkey));
        get.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn));
        get.addFamily(Bytes.toBytes(cf));
        get.setMaxVersions();// 默认是1版本
        // 执行查找数据操作
        Result result = table.get(get);
    
        // 开始遍历操作
        Cell[] cells = result.rawCells();
    
        // 每个rowkey下都有很多数据
        for (Cell cell : cells) {
            System.out.println("RK-->" + Bytes.toString(CellUtil.cloneRow(cell))
                    + ",CF-->" + Bytes.toString(CellUtil.cloneFamily(cell))
                    + ",CN-->" + Bytes.toString(CellUtil.cloneQualifier(cell))
                    + ",VALUE-->" + Bytes.toString(CellUtil.cloneValue(cell)));
        }
    }
    
    public static void main(String[] args) throws IOException {
    // 根据rowkey获取数据
    getData("student", "1002", "info", "name");
    }
    View Code
    做自己的太阳,成为别人的光!
  • 相关阅读:
    二叉查找树
    huffman coding
    普通树与二叉树
    递归转循环的通法
    尾递归和JAVA
    编译器和解释器
    开天辟地-用visualstudio2010编写helloworld
    Android app targetSdk升级到27碰到的一个bug补充说明
    Android Studio修改Apk打包生成名称
    Glide3升级到Glide4碰到的问题汇总以及部分代码修改
  • 原文地址:https://www.cnblogs.com/botaoli/p/13859348.html
Copyright © 2020-2023  润新知