Hbase的API入门的增加,查询,删除数据详细解读和代码展示

以下是Hbase的入门详细解读:
一,从外部的连接上hbase:
1,首先获取hbase的配置信息—-Configuration。
2,给Configuration设置你的zookeeper以及虚拟机的地址。
3,通过连接工厂ConnectionFactory,创建连接。
4,通过连接拿到管理员即admin用来后面操作hbase。

二,建立表:
1,首先可以通过TableName创建表名对象。
2,通过admin判断改表是否存在,再做其他操作。
3,如果不存在就创建表结构对象,HTableDescriptor。
4,创建列族,并将列族添加到表结构对象,HColumnDescriptor。
5,使用admin将表创建。

三,向表中添加数据:
1,创建Put,并指定对应的行健。
2,给put指定列族,还可以指定修饰名,如果不指定列族就默认成了修饰名。
3,通过连接拿到表,再通过表将数据插入。

四,scan读取数据:
1,创建一个scan。
2,通过链接拿到表,通过表和scan,取到一个ResultScanner结果集。
3,循环这个结果集,转换成单个的结果。Result。
4,将这个Result转换成一个List。
5,循环这个List就可以拿到对应的值了。

五,get读取数据:
get其实和scan还是很像的,不过通过get是拿到的单个结果,少了一次循环,其他就大致相同了。

六,删除数据:
这个也简单了,创建一个Delete,指定对应的行健和列族,修饰名,通过拿到的表就可以完成这步操作了。

详细代码展示:

import java.util.Arrays;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
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.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;
public class HbaseApi2 {
    public static void main(String[] args) {
        Admin admin = null;
        Connection con = null;
        try {
            Configuration conf = HBaseConfiguration.create();// 获取hbase配置文件
            conf.set("hbase.zookeeper.quorum", "192.168.61.128");
            con = ConnectionFactory.createConnection(conf);
            admin=con.getAdmin();//获得管理员
            TableName tn=TableName.valueOf("scores2");
            if(admin.tableExists(tn)){//判断表是否存在
                admin.disableTable(tn);//使表失效
                admin.deleteTable(tn);//删除该表
                System.out.println("你要创建的表已存在,已经将其删除!");
            }
            System.out.println("要创建的表没有重复,正在创建!");
            HTableDescriptor htd=new HTableDescriptor(tn);//创建表结构对象
            HColumnDescriptor hcd1=new HColumnDescriptor("grade");//创建列族
            HColumnDescriptor hcd2=new HColumnDescriptor("coures");
            htd.addFamily(hcd1);//将列族添加到表结构
            htd.addFamily(hcd2);
            admin.createTable(htd);//创建表

            System.out.println("创建表成功!");

            //向表中插入数据
            Put put=new Put(Bytes.toBytes("tg"));
            put.addColumn(Bytes.toBytes("grade"),null,Bytes.toBytes("5"));//列族就是列
            put.addColumn(Bytes.toBytes("coures"),Bytes.toBytes("art"),Bytes.toBytes("98"));//在列族下面,创建列
            put.addColumn(Bytes.toBytes("coures"),Bytes.toBytes("math"),Bytes.toBytes("91"));
            Table table=con.getTable(tn);
            table.put(put);

            //向表中批量插入数据
            Put put1=new Put(Bytes.toBytes("tg2"));
            put1.addColumn(Bytes.toBytes("grade"),null,Bytes.toBytes("4"))
            .addColumn(Bytes.toBytes("coures"),Bytes.toBytes("art"),Bytes.toBytes("88"))
            .addColumn(Bytes.toBytes("coures"),Bytes.toBytes("math"),Bytes.toBytes("85"));

            Put put2=new Put(Bytes.toBytes("tg3"));
            put2.addColumn(Bytes.toBytes("grade"),null,Bytes.toBytes("3"))
            .addColumn(Bytes.toBytes("coures"),Bytes.toBytes("art"),Bytes.toBytes("77"))
            .addColumn(Bytes.toBytes("coures"),Bytes.toBytes("math"),Bytes.toBytes("65"));

            List<Put> puts= Arrays.asList(put1,put2);//将数组转为集合
            Table table2=con.getTable(tn);
            table2.put(puts);

            //读取操作
            //scan
            System.out.println("==================scan查询======================");
            Scan scan=new Scan();
            Table table3=con.getTable(tn);
            ResultScanner resultScanner=table3.getScanner(scan);//获得scan结果集
            for(Result rs:resultScanner){
                List<Cell> cs=rs.listCells();//将到的每一个结果,转成list的形式
                for(Cell cell:cs){
                    String rowkey=Bytes.toString(CellUtil.cloneRow(cell));//取到行键
                    long timestamp=cell.getTimestamp();//取时间戳
                    String fname=Bytes.toString(CellUtil.cloneFamily(cell));//取到列族名
                    String qualifier=Bytes.toString(CellUtil.cloneQualifier(cell));//取修饰名,即列名
                    String value=Bytes.toString(CellUtil.cloneValue(cell)); //取值
                    System.out.println("rowkey=="+rowkey+"---timestamp=="+timestamp+"---qualifier=="+fname+"=>"+qualifier+"---value=="+value);
                }
            }

        //get查询数据
        System.out.println("================================================================");
        System.out.println("========================get查询的数据==============================");   
        System.out.println("-----------------------取到的该行所有数据----------------------");   
        Get get=new Get(Bytes.toBytes("tg"));//指定行
        Table table4=con.getTable(tn);
        Result rt=table4.get(get);
        List<Cell> cs=rt.listCells();
        for(Cell cell:cs){
            String rowkey=Bytes.toString(CellUtil.cloneRow(cell));//取到行键
            long timestamp=cell.getTimestamp();//取时间戳
            String fname=Bytes.toString(CellUtil.cloneFamily(cell));//取到列族名
            String qualifier=Bytes.toString(CellUtil.cloneQualifier(cell));//取修饰名,即列名
            String value=Bytes.toString(CellUtil.cloneValue(cell)); //取值
            System.out.println("rowkey=="+rowkey+"---timestamp=="+timestamp+"---qualifier=="+fname+"=>"+qualifier+"---value=="+value);
        }

        System.out.println("====================get取指定行列数据===================");
        Get get1=new Get(Bytes.toBytes("tg"));
        get1.addColumn(Bytes.toBytes("coures"), Bytes.toBytes("art"));//指定列族和修饰名
        Table table5=con.getTable(tn);
        Result rt1=table5.get(get1);
        List<Cell> cs1=rt1.listCells();
        for(Cell cell:cs1){
            String rowkey=Bytes.toString(CellUtil.cloneRow(cell));//取到行键
            long timestamp=cell.getTimestamp();//取时间戳
            String fname=Bytes.toString(CellUtil.cloneFamily(cell));//取到列族名
            String qualifier=Bytes.toString(CellUtil.cloneQualifier(cell));//取修饰名,即列名
            String value=Bytes.toString(CellUtil.cloneValue(cell)); //取值
            System.out.println("rowkey=="+rowkey+"---timestamp=="+timestamp+"---qualifier=="+fname+"=>"+qualifier+"---value=="+value);
        }

        //删除数据
        System.out.println("********************************************************");
        System.out.println("******************************删除数据******************************");
        System.out.println("删除tg2这行所有数据");
        Delete delete=new Delete(Bytes.toBytes("tg2"));
        Table table6=con.getTable(tn);
        table6.delete(delete);
        System.out.println("删除tg3的art列");
        Delete delete1=new Delete(Bytes.toBytes("tg3"));
        delete1.addColumn(Bytes.toBytes("coures"), Bytes.toBytes("art"));
        Table table7=con.getTable(tn);
        table7.delete(delete1);

        System.out.println("========================================================");
        System.out.println("==================删除操作后的scan查询======================");
        Scan scan1=new Scan();
        Table table8=con.getTable(tn);
        ResultScanner resultScanner1=table8.getScanner(scan1);//获得scan结果集
        for(Result rs:resultScanner1){
            List<Cell> cs2=rs.listCells();//将到的每一个结果,转成list的形式
            for(Cell cell:cs2){
                String rowkey=Bytes.toString(CellUtil.cloneRow(cell));//取到行键
                long timestamp=cell.getTimestamp();//取时间戳
                String fname=Bytes.toString(CellUtil.cloneFamily(cell));//取到列族名
                String qualifier=Bytes.toString(CellUtil.cloneQualifier(cell));//取修饰名,即列名
                String value=Bytes.toString(CellUtil.cloneValue(cell)); //取值
                System.out.println("rowkey=="+rowkey+"---timestamp=="+timestamp+"---qualifier=="+fname+"=>"+qualifier+"---value=="+value);
            }
        }




            if (admin != null){
                admin.close();
            }
            if(con != null){
                con.close();
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
  • 6
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值