Java对Hbase表级的操作

 

package com.hbase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HConstants;
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.HBaseAdmin;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author qt
 * @description c
 * @date 2018/8/10 11:06
 */
public class HBaseTableTest {

    private static final String ZKADDRESS = "";

    private static Connection connection = null;
    private static Admin hBaseAdmin = null;
    static {
        Configuration config = HBaseConfiguration.create();
        config.set(HConstants.ZOOKEEPER_QUORUM, ZKADDRESS);
        try {
            connection = ConnectionFactory.createConnection(config);
            hBaseAdmin = connection.getAdmin();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 创建表, HColumnDescriptor包含有关列族的信息,例如版本数,压缩设置等。它在创建表或添加列时用作输入。
     * @param tableName
     * @param familyName
     * @throws Exception
     */
    public void create(String tableName, String familyName) throws Exception {
        HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
        tableDescriptor.addFamily(new HColumnDescriptor(familyName));
        System.out.print("Creating table. ");
        hBaseAdmin.createTable(tableDescriptor);
    }

    /**
     * 列出表
     * @return
     * @throws IOException
     */
    public static List<String> list() throws IOException {
        HTableDescriptor[] tableDescriptor = hBaseAdmin.listTables();
        List<String> list = new ArrayList<String>();
       for(HTableDescriptor x : tableDescriptor){
           System.out.println(x.getNameAsString());
           list.add(x.getNameAsString());
       }
        return list;
    }

    /**
     * 禁用表
     * @param tableName
     * @throws IOException
     */
    public static void disableTable(String tableName) throws IOException {
        Boolean bool = hBaseAdmin.isTableDisabled(TableName.valueOf(tableName));
        if(!bool){
            hBaseAdmin.disableTable(TableName.valueOf(tableName));
            System.out.println(tableName+"Table disabled");
        }
    }

    /**
     * 启用表
     * @param tableName
     * @throws Exception
     */
    public static void enableTable(String tableName) throws Exception {
        Boolean bool = hBaseAdmin.isTableEnabled(TableName.valueOf(tableName));
        if(!bool){
            hBaseAdmin.enableTable(TableName.valueOf(tableName));
            System.out.println(tableName+"Table Enabled");
        }
    }

    /**
     * 添加列族
     * @param tableName
     * @param familyNames
     * @throws IOException
     */
    public static void addFamily(String tableName, String familyNames) throws IOException {
        HColumnDescriptor columnDescriptor = new HColumnDescriptor(familyNames);
        hBaseAdmin.addColumn(TableName.valueOf(tableName), columnDescriptor);
    }

    /**
     * 删除列族
     * @param tableName
     * @param familyNames
     * @throws IOException
     */
    public static void deleteFamily(String tableName, String familyNames) throws IOException {
        hBaseAdmin.deleteColumn(TableName.valueOf(tableName), familyNames.getBytes());
    }

    /**
     * 表是否存在
     * @param tableName
     * @return
     * @throws IOException
     */
    public static boolean existsTab(String tableName) throws IOException {
        return hBaseAdmin.tableExists(TableName.valueOf(tableName));
    }

    /**
     * 删除表
     * @param tableName
     * @throws IOException
     */
    public static boolean deleteTable(String tableName) throws IOException {
        if(!existsTab(tableName)){
            return false;
        }
        disableTable(tableName);
        hBaseAdmin.deleteTable(TableName.valueOf(tableName));
        return true;
    }

     /**
     * 断开连接
     * @throws IOException
     */
    public static  void close() throws IOException {
        if(hBaseAdmin != null) {
            hBaseAdmin.close();
        }
        if(connection != null) {
            connection.close();
        }
    }

    public static void main(String[] args) throws Exception {
        HBaseTableTest.enableTable("test");
        HBaseTableTest.close();
    }

}

原文

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值