栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

Hadoop期末复习贴-Hbase

Hadoop期末复习贴-Hbase

若本文对你有帮助,请记得点赞、关注我喔!

Hbase是一个Hbase是一个分布式的、面向列的开源数据库

…,
讲完理论,现在进入Hbase用Java实现API
静态配置

  • 创建表
    public static void CreateTable(String tablename, String info) throws IOException {
        TableName tableName = TableName.valueOf(tablename);
        Admin admin = connection.getAdmin(); //建表操作一定要通过connection.getAdmin产生一个admin
        TableDescriptorBuilder tableDescriptor = TableDescriptorBuilder.newBuilder(tableName);//表构造器
        ColumnFamilyDescriptor family = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("info")).build();//列构造器
        tableDescriptor.setColumnFamily(family);//表构造器中添加列构造器
        admin.createTable(tableDescriptor.build());//!!!一定要有这句
        admin.close();
    }
  • 插入数据
    
    public static void InsertInfo(String tablename, String Row, String ColumnFamily, String Qualifier, String Value) throws IOException {
        TableName tableName = TableName.valueOf(tablename);
        Table table = connection.getTable(tableName);
        try {
            byte[] row = Bytes.toBytes(Row);
            Put put = new Put(row);
            put.addColumn(Bytes.toBytes(ColumnFamily), Bytes.toBytes(Qualifier), Bytes.toBytes(Value));
            table.put(put);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
  • 查询数据
    public static void queryTableInfo(String tablename, String Row) throws IOException {
        TableName tableName = TableName.valueOf(tablename);
        Table table = connection.getTable(tableName);
        try {
            Get get = new Get(Bytes.toBytes(Row));//通过某一行Bytes的产生的get对象
            Result result = table.get(get);
            //返回值是Bytes
            byte[] valueBytes = result.getValue(Bytes.toBytes("info"), Bytes.toBytes("name"));
            String valueStr = new String(valueBytes, "utf-8");
            System.out.println(valueStr);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
  • 删除表
    
    public static void deleteTable(String tablename) throws IOException {
        Admin admin = connection.getAdmin();
        TableName tableName = TableName.valueOf(tablename);
        admin.disableTable(tableName);
        admin.deleteTable(tableName);
    }
  • 获得多行数据
    
    public static void batchGet(String tablename) throws Exception {
        TableName tableName = TableName.valueOf(tablename);//?
        Table table = connection.getTable(tableName);//获取Table对象

        String[] rows = new String[]{"1001", "1002"};
        List gets = new ArrayList<>();
        for (String str : rows) {
            gets.add(new Get(Bytes.toBytes(str)));
        }
        //可以直接将Get对象集合传入
        Result[] results = table.get(gets);
        for (Result result:results){
            System.out.println("Row:"+Bytes.toString(result.getRow()));
            for (Cell kv:result.rawCells()){ //将同一个Row的值遍历完
                String family = Bytes.toString(CellUtil.cloneFamily(kv));
                String qualifier = Bytes.toString(CellUtil.cloneQualifier(kv));
                String value = Bytes.toString(CellUtil.clonevalue(kv));
                System.out.println(family + ":" + qualifier + "t" + value);
            }
        }
    }

下午补看书补理论hhhh

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/688058.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号