hbaseAPI 针对表操作 ddl


    public static void main(String[] args) throws IOException {
        System.setProperty("hadoop.home.dir", "D:\\hadoop-2.6.0-cdh5.15.0");

        Configuration conf = new Configuration();
        conf.set("zookeeper.znode.parent", "/hbase");
        conf.set("hbase.zookeeper.quorum", "can");
        conf.set("hbase.zookeeper.property.clientPort", "2181");

        Connection connection =
                ConnectionFactory.createConnection(conf);

        Admin admin = connection.getAdmin();

        String name = "hadoop:table";
        String cf1 = "f1";
        String cf2 = "f2";
        String cf3 = "f3";

        //判断表是否存在
        TableName tableName = TableName.valueOf(name);
        boolean b = admin.tableExists(tableName);
        System.out.println(b);

        //HTableDescriptor  创建表
        if(!admin.tableExists(tableName)) {
            HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);

            //创建表时候 至少需要 需要指定一个列族
            HColumnDescriptor hColumnDescriptor1 = new HColumnDescriptor(cf1);
            HColumnDescriptor hColumnDescriptor2 = new HColumnDescriptor(cf2);
            HColumnDescriptor hColumnDescriptor3 = new HColumnDescriptor(cf3);

            hTableDescriptor.addFamily(hColumnDescriptor1);
            hTableDescriptor.addFamily(hColumnDescriptor2);
            hTableDescriptor.addFamily(hColumnDescriptor3);

            admin.createTable(hTableDescriptor);
        }else {
            System.out.println("already create");
        }

        //admin.createTable();
        //修改列族属性
//        if(admin.tableExists(tableName)) {
//            if( admin.isTableEnabled(tableName))
//                admin.disableTable(tableName); //首先关闭表格
//            //HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
//
//            //增加一个列族
//            //hTableDescriptor.addFamily(new HColumnDescriptor("f4"));
//            //如果new HTableDescriptor  这个HTableDescriptor 是空的
//
//            //获取该表  HTableDescriptor
//            //HTableDescriptor tableDescriptor = admin.getTableDescriptor(tableName);
//            //tableDescriptor.addFamily(new HColumnDescriptor("f3"));
//
//            //修改某一个列族
            HTableDescriptor tableDescriptor = admin.getTableDescriptor(tableName);
            HColumnDescriptor family = tableDescriptor.getFamily(Bytes.toBytes("f4"));

            //修改family属性
            family.setBlocksize(1024*1024*16);//设置缓冲大小 1K ~ 16m
            // Block size for column family f4  must be between 1K and 16MB. Set hbase.table.sanity.checks to false at conf or table descript
            family.setTimeToLive(60);
            family.setVersions(1,5);
            family.setInMemory(true);

            tableDescriptor.modifyFamily(family);
            admin.modifyTable(tableName, tableDescriptor);
//
//            //删除一个列族
//            HTableDescriptor tableDescriptor = admin.getTableDescriptor(tableName);
//
//            tableDescriptor.removeFamily(Bytes.toBytes("f3"));
//            admin.modifyTable(tableName, tableDescriptor);
//            admin.enableTable(tableName);
//        }else  {
//            System.out.println("table not create");
//        }

        //删除表
//        if (admin.tableExists(tableName)) {
//            admin.disableTable(tableName);
//            admin.deleteTable(tableName);
//           // admin.enableTable(tableName);
//        }else {
//            System.out.println("table not create");
//        }


        //admin.getTableDescriptor(tableName);
        HTableDescriptor tableDescriptor1 = admin.getTableDescriptor(tableName);
        Collection families = tableDescriptor1.getFamilies();

        for (HColumnDescriptor tmp:families) {
            System.out.println(tmp.getMaxVersions() + ":" + new String(tmp.getName()));
        }

        //list
        NamespaceDescriptor[] namespaceDescriptors = admin.listNamespaceDescriptors();

        for (NamespaceDescriptor nsdt:namespaceDescriptors) {
            HTableDescriptor[] hTableDescriptors = admin.listTableDescriptorsByNamespace(nsdt.getName());

            for (HTableDescriptor tmp:hTableDescriptors) {
                System.err.println(tmp);
            }
        }
    }

 

你可能感兴趣的:(hbase)