HBASE(三)

今天看了一个人的博客 返现自己对HBASE了解还是太out了

下面补充一些概念 和新 API

**1.**NameSpace:相当于数据库 以前一直好奇 为什么HBASE 只有表 现在明白了 还有表空间 就相当于数据库 对应hadoop目录下的一个文件

//创建表空间 
create_namespace '名字'
//删除表空间   表空间中必须是空的
drop_namespace '名字'

新的API 获取 连接的方式也不同

//我手打的  粘贴有错自己改:
//创建conf
Configuration conf = HBaseConfiguration.create();
 conf.set("hbase.zookeeper.quorum","hadoop01:2181,hadoop02:2181,hadoop03:2181");
//获取连接 
Connection conn = ConnectionFactory.createConnection(conf);


这个conn 就能获取admin  
Admin admin = conn.getAdmin();

admin 可以根据表名获得表     创建表空间   等等 就和旧API一样了
conn 也能获的表  获得admin

创建表空间:

  /**
     * 创建命名空间
     * @throws Exception
     */
    @Test
    public void createNameSpace() throws Exception{
        Admin admin = conn.getAdmin();
        NamespaceDescriptor namespace = NamespaceDescriptor.create("NAMESPACE").build();
        admin.createNamespace(namespace);
        admin.close();
    }

2.创建表的新API 采用TableName

  /**
     * 创建表
     */
    @Test
    public void createTable() throws Exception{
        Admin admin = conn.getAdmin();
        TableName tableName = TableName.valueOf("NAMESPACE","TABLE1");
        HTableDescriptor table = new HTableDescriptor(tableName);
        HColumnDescriptor colfam1 = new HColumnDescriptor("colfam1");
        HColumnDescriptor colfam2 = new HColumnDescriptor("colfam2");
        table.addFamily(colfam1);
        table.addFamily(colfam2);
        admin.createTable(table);
        admin.close();

    }

命令行的形式:

 create 'NAMESPACE:TABLE2','colfam1','colfam2'

3.查看数据库信息

命令行是 list

api:
  @Test
    public void list() throws Exception{
        Admin admin = conn.getAdmin();
        //获取所有的命名空间
        NamespaceDescriptor[] namespaceDescriptors = admin.listNamespaceDescriptors();
        for (NamespaceDescriptor namespaceDescriptor : namespaceDescriptors) {
            System.out.println(namespaceDescriptor.getName());
        }
    }
     System.out.println("------------------------");
//获取 给定命名空间下的表
        TableName[] tableNames = admin.listTableNamesByNamespace("NAMESPACE");
        for (TableName tableName : tableNames) {
            System.out.println(tableName.getNameAsString());
        }
   System.out.println("------------------------");
   //利用正则获得表
        Pattern pattern = Pattern.compile("NAMESPACE:.*");
        HTableDescriptor[] hTableDescriptors = admin.listTables(pattern);
        for (HTableDescriptor hTableDescriptor : hTableDescriptors) {
            System.out.println(hTableDescriptor);
        }

4.获取集群状态

   ClusterStatus status = admin.getClusterStatus();

5. 14个过滤器
5.1 RowFilter

  /**
     * 行键过滤 RowFilter
     */
    @Test
    public void RowFilter() throws Exception {
        //建立与user表的连接
        Table user = conn.getTable(TableName.valueOf("user"));
//        CompareFilter.CompareOp 包含小于  等于 大于  <=  >=

        // 1.BinaryComparator  当前的值
        RowFilter rowFilter1 = new RowFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("813782218261011172")));

//2.正则匹配RegexStringComparator
    RowFilter rowFilter2 = new RowFilter(CompareFilter.CompareOp.EQUAL, new RegexStringComparator("^.*1$"));

//3.包含子串 
  RowFilter rowFilter3 = new RowFilter(CompareFilter.CompareOp.EQUAL, new SubstringComparator("3782"));




        Scan scan = new Scan();
        scan.addColumn(Bytes.toBytes("info"),Bytes.toBytes("age"));
        scan.setFilter(rowFilter1);
        ResultScanner scanner = user.getScanner(scan);
        for (Result result : scanner) {
            byte[] value = result.getValue(Bytes.toBytes("info"), Bytes.toBytes("age"));
            System.out.println(new String(value));
        }


    }

5.2 FamilyFilter 列族过滤器

  FamilyFilter kk = new FamilyFilter(CompareFilter.CompareOp.GREATER, new BinaryComparator(Bytes.toBytes("kk")));

5.3 DependentColumnFilter 参考列过滤器
以这一列为参考 在同列族中查找跟参考列时间戳一样的列

@Test
    public void depentCol() throws Exception{
        Table user = conn.getTable(TableName.valueOf("user"));
        Scan scan = new Scan();
        DependentColumnFilter filter = new DependentColumnFilter(Bytes.toBytes("info"), Bytes.toBytes("phone"), true, CompareFilter.CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("13213921424")));
        scan.setFilter(filter);
        ResultScanner scanner = user.getScanner(scan);
        for (Result result : scanner) {
            System.out.println(result);
        }
    }

5.4 PrefixFilter 前缀过滤器 根据行键的前缀进行过滤

 /**
     * PrefixFilter 前缀过滤器  根据行键的前缀进行过滤
     */
    @Test
    public void prefix() throws Exception{
        Table user = conn.getTable(TableName.valueOf("user"));
        Scan scan = new Scan();
        PrefixFilter prefixFilter = new PrefixFilter(Bytes.toBytes("2243"));
        scan.setFilter(prefixFilter);
        ResultScanner scanner = user.getScanner(scan);
        for (Result result : scanner) {
            System.out.println(result);
        }


    }

5.5 分页过滤器

 /**
     * 分页过滤器 PageFilter
     */
    @Test
    public void pageFilter() throws IOException {
        Table user = conn.getTable(TableName.valueOf("user"));
        Scan scan = new Scan();
        PageFilter pageFilter = new PageFilter(2);
        scan.setFilter(pageFilter);
        ResultScanner scanner = user.getScanner(scan);
        for (Result result : scanner) {
            System.out.println(result);
        }
    }

5.6 KeyOnlyFilter 行键过滤器

//只返回行键  值都为空
   KeyOnlyFilter keyOnlyFilter = new KeyOnlyFilter();

5.7 FirstKeyOnlyFilter 首次行键过滤器 只返回第一列

FirstKeyOnlyFilter firstKeyOnlyFilter = new FirstKeyOnlyFilter();

5.8 InclusiveStopFilter 包含结束的过滤器

    InclusiveStopFilter inclusiveStopFilter = new InclusiveStopFilter(Bytes.toBytes("510824118261011172"));

5.9 ColumnCountGetFilter 列计数过滤器
给定列数
从行第一列开始扫描 如果这一行 列数小于给定列数 继续扫描 否则程序结束

   ColumnCountGetFilter columnCountGetFilter = new ColumnCountGetFilter(1);

5.10 ColumnPrefixFilter 列前缀过虑器

Filter cpf = new ColumnPrefixFilter(Bytes.toBytes("qual1")); // OK 筛选出前缀匹配的列  

5.11 MultipleColumnPrefixFilter 多个列前缀过滤器

Filter filter = new MultipleColumnPrefixFilter(new byte[][] {Bytes.toBytes("a"),Bytes.toBytes("h")});

5.12 组合的filter

 List list = new ArrayList();

        FilterList f1 = new FilterList(FilterList.Operator.MUST_PASS_ALL,list);

还有很多 具体碰到了再查

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