Hbase进阶与API

/**
    java操作数据的步骤:
    1、注册驱动
    2、创建数据库连接对象
    3、创建数据库操作对象
    4、进行增删改查
    5、如果时查询的话,可以对查询的结果进行分析
    6、释放资源


    hbase基础中要做的需求:
        1、如何创建一张表
        2、如何删除一张表
        3、如何向一张表中添加一条数据
        4、如何向一张表中同时添加一批数据
        5、如何获取一条数据
        6、如果获取一批数据
        7、如何创建预分region表

*/

@Before
    public void connection() {

        try {
            //创建hbase的运行环境对象
            //旧版本的写法,已弃用
            //HBaseConfiguration conf = new HBaseConfiguration();

            //新版本的创建配置文件对象的方式
            Configuration conf = HBaseConfiguration.create();

            //设置zookeeper的节点信息
            conf.set("hbase.zookeeper.quorum", "master:2181,node1:2181,node2:2181");

            //创建hbase连接对象
            conn = ConnectionFactory.createConnection(conf);

            //创建数据库操作对象
            admin = conn.getAdmin();

            System.out.println("成功获取数据库连接对象" + conn);
            System.out.println("成功获取数据库管理对象" + admin);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    
    一、如何创建一张表
     create 'students','info'  必须要有表名和列簇的名
     
  @Test
    public void createOneTable() {   
 //1、创建一个表的描述对象    
 TableName name = TableName.valueOf("students");
 TableDescriptorBuilder test2 = TableDescriptorBuilder.newBuilder(name);
 //2、创建一个列簇描述器对象
 ColumnFamilyDescriptor info = ColumnFamilyDescriptorBuilder.of("info");
 //3、将列簇添加到表中
 test2.setColumnFamily(info);
 //4、admin对象调用方法创建一张表
 try {
            admin.createTable(test2.build());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        System.out.println(Bytes.toString((test2.build().getTableName().getName()+"表创建 成功 SUCCEED!").getBytes()));
        System.out.println(Bytes.toString((test2.build().getTableName().getName()+"表创建 失败 fAILED!").getBytes()));
    }
}

  //3.如何向表中添加一列数据
    @Test
    public void putOneColData() {
        try {
            TableName name = TableName.valueOf("students");
            if (!admin.tableExists(name)) {
                System.out.println(Bytes.toString(name.getName()) + "表不存在");
                return;
            }
            Table test2 = conn.getTable(name);
            Put put = new Put(Bytes.toBytes("1500100001"));
            KeyValue keyValue = new KeyValue(Bytes.toBytes("1500100001"),
                    Bytes.toBytes("info"),
                    Bytes.toBytes("age"),
                    Bytes.toBytes("22"));
            put.add(keyValue);
            test2.put(put);
            System.out.println("一列数据添加完毕");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
如何获取一条数据 
@Test
    public void getOneData() {
        try {

            TableName tableName = TableName.valueOf("students");
            if (!admin.tableExists(tableName)) {
                System.out.println(Bytes.toString(tableName.getName()) + "表不存在,创建失败");
                return;}
            Table students = conn.getTable(tableName);
            Get get = new Get(Bytes.toBytes("1500100001"));
            Result result = students.get(get);
            
            List<Cell> cells = result.listCells();
            for (Cell cell : cells) {
                String id = Bytes.toString(CellUtil.cloneRow(cell));
                String cf = Bytes.toString(CellUtil.cloneFamily(cell));
                String colName = Bytes.toString(CellUtil.cloneQualifier(cell));
                String colValue = Bytes.toString(CellUtil.cloneValue(cell));
                System.out.println("行健" + id + "列簇" + cf + "列名" + colName + "列值" + colValue);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
@After
public void close() {
    if (admin != null) {
        try {
            admin.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    if (conn != null) {
        try {
            conn.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值