hbase--jdbc编程

要点如下:(使用idea +maven)

  1. 增、删、改数据
  2. 查询: 过滤器,比较器
  3. 协处理器: 模拟  'RMDB的触发器'

第一步:准备好环境

  1. 搭建好hdfs, hbase伪分布式或完全分布式
  2. 使用idea创建maven项目,在resources目录下: 导入依赖的配置文件( core-site.xml, hdfs-site.xml, hbase-site.xml)
  3. pom.xml: 添加依赖(对应所使用的hdfs,hbase版本)
        
            junit
            junit
            4.7
        

        
        
            org.apache.hbase
            hbase-client
            1.2.6
        

        
            org.apache.hbase
            hbase-server
            1.2.6
        

第二步:使用junit创建test方法,测试hbase的java api使用

//1, 创建库、表(namespace, table)

    @Before
    public void before() throws IOException {
         Configuration conf = HBaseConfiguration.create();
         conn = ConnectionFactory.createConnection(conf);

    }

    @Test
    public  void create() throws IOException {
        //创建库, 表
        Admin admin = conn.getAdmin();

        NamespaceDescriptor db1 = NamespaceDescriptor.create("db1").build();
        admin.createNamespace(db1);
        HTableDescriptor tbs = new HTableDescriptor(TableName.valueOf("db1:t1"));

        HColumnDescriptor f1 = new HColumnDescriptor("f1");
        HColumnDescriptor f2 = new HColumnDescriptor("f2");
        tbs.addFamily(f1);
        tbs.addFamily(f2);

        admin.createTable(tbs);

        conn.close();
        admin.close();
    }

//2, 插入多条数据: 批量插入的优化

  1. 封装多个Put对象为一个集合:      List
  2. 使用Htable关闭自动刷新:           HTable tab = (HTable) table;  tab.setAutoFlush(false,true)
  3. 单个put对象跳过WAL:                  put.setDurability(Durability.SKIP_WAL);
    @Test
    public  void add() throws IOException {//增加,覆盖
        //获取表
        Table table = conn.getTable(TableName.valueOf("db1:t1"));

        Put put = new Put(Bytes.toBytes("row1"));
        put.addColumn(Bytes.toBytes("f1"), Bytes.toBytes("name"), Bytes.toBytes("lisi2"));
        put.addColumn(Bytes.toBytes("f1"), Bytes.toBytes("sex"), Bytes.toBytes("男"));
        put.addColumn(Bytes.toBytes("f1"), Bytes.toBytes("city"), Bytes.toBytes("北京"));

        table.put(put);
        //关闭资源
        table.close();
    }

    @Test
    public  void add2() throws IOException {//批量增加数据
        //获取表
        Table table = conn.getTable(TableName.valueOf("db1:t1"));

        ArrayList puts = new ArrayList();
        for (int i=2;i<100;i++){
            DecimalFormat format = new DecimalFormat("000");

            Put put = new Put(Bytes.toBytes("row"+format.format(i)));
            put.addColumn(Bytes.toBytes("f"+format.format(i)), Bytes.toBytes("age"), Bytes.toBytes(format.format(i%50)+""));
            put.addColumn(Bytes.toBytes("f2"), Bytes.toBytes("country"), Bytes.toBytes(format.format(i)+"国"));
            put.addColumn(Bytes.toBytes("f1"), Bytes.toBytes("sex"), Bytes.toBytes(i%2==0?"男":"女"));
           put.addColumn(Bytes.toBytes("f1"), Bytes.toBytes("city"), Bytes.toBytes(format.format(i)+"city"));
            puts.add(put);

        }

        table.put(puts);
        //关闭资源
        table.close();
    }

//3, 删除数据

     @Test
    public void del() throws IOException {//删除一个: 旧的版本数据
        Table table = conn.getTable(TableName.valueOf("db1:t1"));

        Delete delete = new Delete(Bytes.toBytes("row1"));
        delete.addColumn(Bytes.toBytes("f1"), Bytes.toBytes("name"));

        table.delete(delete);
    }

//4, 获取数据:get少量数据,   scan大量数据

    @Test
    public void get() throws IOException {
        Table table = conn.getTable(TableName.valueOf("db1:t1"));

        Get get = new Get(Bytes.toBytes("row1"));
        get.addColumn(Bytes.toBytes("f1"), Bytes.toBytes("name"));  //取出qualifier---》val
       //  get.addFamily(Bytes.toBytes("f1"));//取出列族的--->val

        Result result = table.get(get);
        List cells = result.listCells();
        //
        for (Cell c: cells){
            String row = Bytes.toString(CellUtil.cloneRow(c));
            String family = Bytes.toString(CellUtil.cloneFamily(c));

            String qual = Bytes.toString(CellUtil.cloneQualifier(c));
            String val = Bytes.toString(CellUtil.cloneValue(c));

            System.out.println(row+"/"+family+"/"+qual+"/"+val);

        }
    }

    @Test
    public void scan() throws IOException {
        Table table = conn.getTable(TableName.valueOf("db1:t1"));

        Scan scan = new Scan();
        scan.addColumn(Bytes.toBytes("f1"), Bytes.toBytes("age"));
        scan.addColumn(Bytes.toBytes("f1"), Bytes.toBytes("name"));


        ResultScanner scanner = table.getScanner(scan);
        Iterator it = scanner.iterator();
        while (it.hasNext()){
            Result result = it.next();
            List cells = result.listCells();

            Iterator iter = cells.iterator();
            while(iter.hasNext()){
                Cell c= iter.next();

                String row = Bytes.toString(CellUtil.cloneRow(c));
                String family = Bytes.toString(CellUtil.cloneFamily(c));

                String qual = Bytes.toString(CellUtil.cloneQualifier(c));
                String val = Bytes.toString(CellUtil.cloneValue(c));

                System.out.println(row+"/"+family+"/"+qual+"/"+val);
            }

        }
    }

//5,  数据的过滤: sql  where 从句的实现--->过滤器+ 比较器

    @Test//过滤: age<30
    public void scan2() throws IOException {
        Table table = conn.getTable(TableName.valueOf("db1:t1"));

        Scan scan = new Scan();
//        scan.addFamily(Bytes.toBytes("f1"));
//        scan.addFamily(Bytes.toBytes("f2"));
//        scan.setBatch(9);//默认扫描整个对象: 同一个row
        scan.setCaching(10);//一次扫描几个对象
        //scan.setFilter(new RowFilter(CompareFilter.CompareOp.LESS_OR_EQUAL, new BinaryComparator(Bytes.toBytes("row030"))));//前30行
//        scan.setFilter(new FamilyFilter(CompareFilter.CompareOp.EQUAL,  new BinaryComparator(Bytes.toBytes("f1"))));//过滤列族
//        scan.setFilter(new QualifierFilter(CompareFilter.CompareOp.EQUAL,  new RegexStringComparator("age")));
//        scan.setFilter(new ValueFilter(CompareFilter.CompareOp.LESS_OR_EQUAL,  new BinaryComparator(Bytes.toBytes("030"))));

        /*QualifierFilter quafilter = new QualifierFilter(CompareFilter.CompareOp.EQUAL, new RegexStringComparator("age"));
        ValueFilter valf = new ValueFilter(CompareFilter.CompareOp.LESS_OR_EQUAL, new BinaryComparator(Bytes.toBytes("030")));
        FamilyFilter famfilter = new FamilyFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("f1")));
        FilterList flist = new FilterList(quafilter, valf,famfilter);*/

        //值: 过滤--->显示同一列族值
        SingleColumnValueFilter singleF = new SingleColumnValueFilter(Bytes.toBytes("f1"), Bytes.toBytes("age"), CompareFilter.CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("020")));
        FamilyFilter famfilter = new FamilyFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("f1")));

//        FilterList flist = new FilterList( singleF,famfilter);
        FilterList flist = new FilterList(FilterList.Operator.MUST_PASS_ONE);
        flist.addFilter(singleF);
        flist.addFilter(famfilter);

        scan.setFilter(flist);
        ResultScanner scanner = table.getScanner(scan);
        Iterator it = scanner.iterator();
        while (it.hasNext()) {
            Result result = it.next();
            List cells = result.listCells();

            Iterator iter = cells.iterator();
            while (iter.hasNext()) {
                Cell c = iter.next();

                String row = Bytes.toString(CellUtil.cloneRow(c));
                String family = Bytes.toString(CellUtil.cloneFamily(c));

                String qual = Bytes.toString(CellUtil.cloneQualifier(c));
                String val = Bytes.toString(CellUtil.cloneValue(c));

                System.out.println(row + "/" + family + "/" + qual + "/" + val);
            }
            System.out.println("===========");
        }
    }

第三步: 创建客户端,服务端(模拟:粉丝表--关注表--->数据的自动同步:增删关注,增删粉丝)

//客户端
    public class Client {
    static final String guanzhu_tab = "weibo:guanzhu";

    //添加数据
    public static void put(String key, String val) throws IOException {
        //配置
        Configuration conf = HBaseConfiguration.create();
        //连接conn
        Connection conn = ConnectionFactory.createConnection(conf);
        //表
        key = key + "," + System.currentTimeMillis();
        Put put = new Put(key.getBytes());

        HTable table = (HTable) conn.getTable(TableName.valueOf(guanzhu_tab));
        put.addColumn("f1".getBytes(), "name".getBytes(), val.getBytes());

        table.put(put);
        table.close();
        conn.close();
    }

    public static void delete(String key,String val) throws Exception {
        Configuration conf = HBaseConfiguration.create();
        Connection conn = ConnectionFactory.createConnection(conf);
        //获取表
        Table table = conn.getTable(TableName.valueOf("weibo:guanzhu"));

        //查询: 完整key,  val
        Scan scan = new Scan();
        //   过滤器=   行 + 单列值
        //c,2343454 --->取消: llisi
        RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.EQUAL, new RegexStringComparator(key));
        SingleColumnValueFilter sing = new SingleColumnValueFilter("f1".getBytes(), "name".getBytes(),
                CompareFilter.CompareOp.EQUAL, new BinaryComparator(val.getBytes()));
        FilterList filter = new FilterList(rowFilter, sing);
        scan.setFilter(filter);

        ResultScanner result = table.getScanner(scan);
        Cell cell = result.next().listCells().get(0);

        //取出key
        String realKey = Bytes.toString(CellUtil.cloneRow(cell));

        //c,1534253779035 --->取消李四
        Delete delete = new Delete(realKey.getBytes());
        delete.addColumn("f1".getBytes(), "name".getBytes());
        delete.setAttribute("value",val.getBytes());
        table.delete(delete);

        //关闭资源
        table.close();
        conn.close();
    }

    //插入数据
    public static void main(String[] args) throws Exception {
        put("c6", "lisi6");
  //    delete("c6","lisi6");
    }
//服务端
public class Myobserve3 extends BaseRegionObserver {
   static FileOutputStream out=null;
   final  String fence_tab="weibo:fence";
   final String guanzhu_tab = "weibo:guanzhu";

    static{
        try {
             out = new FileOutputStream("/home/centos/myobserve.log",true);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void postPut(ObserverContext e, Put put, WALEdit edit, Durability durability) throws IOException {
        System.out.println("加入数据. .........");
            out.write("加入数据....".getBytes());
            out.flush();

        RegionCoprocessorEnvironment env = e.getEnvironment();
        String tbname = env.getRegionInfo().getTable().getNameAsString();

        if(tbname.equalsIgnoreCase(guanzhu_tab)){
            List value = put.getFamilyCellMap().firstEntry().getValue();
            Cell cell = value.get(0);
            //输入key,val===>      a,   b
            String[] keys = Bytes.toString(put.getRow()).split(",");
            String time=keys[1];

            String val= Bytes.toString(CellUtil.cloneValue(cell));
            val=val+","+time;

            //向:  粉丝表---》插入数据  b,a
            Table table = (Table) env.getTable(TableName.valueOf(fence_tab));
            Put put1 = new Put(val.getBytes());
            put1.addColumn(  "f1".getBytes(),"name".getBytes(), keys[0].getBytes());

            table.put(put1);
            table.close();
        }
    }


    //==============
    @Override
    public void postDelete(ObserverContext e, Delete delete, WALEdit edit, Durability durability) throws IOException {
        //获取: 环境
        RegionCoprocessorEnvironment env = e.getEnvironment();

        //从哪个表删除数据
        String tbname = env.getRegionInfo().getTable().getNameAsString();
        if(tbname.equalsIgnoreCase(guanzhu_tab)) {

            out.write("删除数据...".getBytes());
            out.flush();
            //delete对象获取数据
            List cells = delete.getFamilyCellMap().firstEntry().getValue();
            String val1= Bytes.toString(delete.getAttribute("value"));
            Cell cell = cells.get(0);

            //关注表:KEY: c2,1534255376535,      val: lisi
            //粉丝表:key==>(lisi,1534250643024,  val:c2)
            String key1 = Bytes.toString(CellUtil.cloneRow(cell));

            String[] keys = key1.split(",");
            String val = keys[0];
            String key = val1 + "," + keys[1];

            out.write(("输入key1..."+key1).getBytes());
            out.write(("输入val1..."+val1).getBytes());
            out.write(("输入key..."+key).getBytes());
            out.flush();
            //删除
            Delete delete1 = new Delete(Bytes.toBytes(key));
            delete1.addColumn(Bytes.toBytes("f1"), Bytes.toBytes("name"));

            Table table = (Table) env.getTable(TableName.valueOf("weibo:fence"));
            table.delete(delete1);

            //关闭资源
            table.close();
        }
    }
}

 

你可能感兴趣的:(大数据hadoop-hbase)