HBase Admin 新增和修改ColumnFamily

HBase 版本: 1.2.1

package com.feng.hbase;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
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;

/**
 * 增加和修改columnFamily
 * 
 * @author feng
 *
 */
public class AddAndModifyColumn {

    public static void main(String[] args) {

        String sTalbeName = "test1";
        String columnFamily = "cfadd-";
        String columnFamily1 = "cf11";

        columnFamily = columnFamily + Math.random();

        Connection connection = null;
        Admin admin = null;
        try {
            Configuration config = HBaseConfiguration.create();
            config.set("hbase.zookeeper.quorum", "localhost");
            connection = ConnectionFactory.createConnection(config);
            admin = connection.getAdmin();

            TableName tableName = TableName.valueOf(sTalbeName);

            // 判断是否存在此表
            boolean ifexists = admin.tableExists(tableName);

            if (ifexists) {
                // 判断是否可以获取
                if (admin.isTableAvailable(tableName)) {

                    if (!admin.isTableDisabled(tableName)) {
                        // 如果没关闭则关闭
                        admin.disableTable(tableName);
                        System.out.println(tableName + " disable...");
                    }

                    HTableDescriptor tableDesc = admin
                            .getTableDescriptor(tableName);

                    try {
                        // adding new ColumnFamily
                        HColumnDescriptor cf = new HColumnDescriptor(
                                columnFamily);
                        admin.addColumn(tableName, cf);
                        System.out.println(cf.getNameAsString()
                                + " add column success!");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    try {
                        HColumnDescriptor tempColumnDesc = tableDesc
                                .getFamily(columnFamily1.getBytes());
                        if(tempColumnDesc == null){
                            System.out.println(columnFamily1 + " is null column ");
                        }else{
                            // modifying existing ColumnFamily
                            HColumnDescriptor cf2 = new HColumnDescriptor(
                                    columnFamily1);
                            admin.modifyColumn(tableName, cf2);
                            System.out.println(cf2.getNameAsString()
                                    + "  modify column success!");
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    if (!admin.isTableEnabled(tableName)) {
                        // 如果没有打开则打开表
                        admin.enableTable(tableName);
                        System.out.println(tableName + " enable...");
                    }

                    System.out.println(sTalbeName
                            + " add And modify column success!");
                } else {
                    System.out.println(sTalbeName + " is not available!");
                }

            } else {
                System.out.println("There is not " + sTalbeName);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (admin != null) {
                try {
                    admin.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

}

你可能感兴趣的:(HBase,NOSQL)