java调用HbaseAPI管理Hbase权限

导入依赖

<dependency>
            <groupId>log4jgroupId>
            <artifactId>log4jartifactId>
            <version>1.2.17version>
        dependency>

        
        <dependency>
            <groupId>com.sun.jerseygroupId>
            <artifactId>jersey-bundleartifactId>
            <version>1.19.1version>
        dependency>
        <dependency>
            <groupId>com.sun.jerseygroupId>
            <artifactId>jersey-jsonartifactId>
            <version>1.19version>
        dependency>
        <dependency>
            <groupId>com.sun.jerseygroupId>
            <artifactId>jersey-servletartifactId>
            <version>1.19.1version>
        dependency>
        
        <dependency>
            <groupId>org.apache.hbasegroupId>
            <artifactId>hbase-clientartifactId>
            <version>1.3.1version>
        dependency>
        <dependency>
            <groupId>org.apache.hbasegroupId>
            <artifactId>hbase-commonartifactId>
            <version>1.3.1version>
        dependency>
        <dependency>
            <groupId>commons-logginggroupId>
            <artifactId>commons-loggingartifactId>
            <version>1.1.1version>
        dependency>
        <dependency>
            <groupId>com.google.guavagroupId>
            <artifactId>guavaartifactId>
            <version>12.0.1version>
        dependency>
        <dependency>
            <groupId>commons-collectionsgroupId>
            <artifactId>commons-collectionsartifactId>
            <version>3.2.2version>
        dependency>
        <dependency>
            <groupId>com.google.protobufgroupId>
            <artifactId>protobuf-javaartifactId>
            <version>2.5.0version>
        dependency>
        <dependency>
            <groupId>commons-langgroupId>
            <artifactId>commons-langartifactId>
            <version>2.6version>
        dependency>
        <dependency>
            <groupId>commons-configurationgroupId>
            <artifactId>commons-configurationartifactId>
            <version>1.6version>
        dependency>
        <dependency>
            <groupId>org.apache.hadoopgroupId>
            <artifactId>hadoop-coreartifactId>
            <version>1.2.1version>
        dependency>
        <dependency>
            <groupId>org.cloudera.htracegroupId>
            <artifactId>htrace-coreartifactId>
            <version>2.04version>
        dependency>

service服务类

package com.ultrapower.framework.module.bigdata.dirver.hbase.service.impl;

import com.ultrapower.framework.module.bigdata.dirver.hadoop.util.StrUtil;
import com.ultrapower.framework.module.bigdata.dirver.hbase.service.IHBaseService;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.security.access.AccessControlClient;
import org.apache.hadoop.hbase.security.access.Permission;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.log4j.Logger;

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

/**
 * HBase实现类
 * Created by shiyufeng on 2017/4/25.
 */
public class HBaseServiceImpl implements IHBaseService {
    private static final Logger logger = Logger.getLogger(HBaseServiceImpl.class);
    static Configuration configuration = null;

//    static {
//        configuration = HBaseConfiguration.create();
//        configuration.set("hbase.zookeeper.quorum", "192.168.187.209");
//    }

    /**
     * 根据表名,查询列族
     *
     * @param configuration
     * @param tableName
     * @return
     */
    @Override
    public List queryFamilies(Configuration configuration, String tableName) throws IOException {
        if (logger.isInfoEnabled()) {
            logger.info("HBaseServiceImpl queryFamilies begin");
        }
        HTable table = new HTable(configuration, Bytes.toBytes(tableName));
        HTableDescriptor desc = table.getTableDescriptor();
        Collection collection = desc.getFamilies();
        List list = new ArrayList();
        for (HColumnDescriptor hColumnDescriptor : collection) {
//            System.out.println(hColumnDescriptor.getNameAsString());
            list.add(hColumnDescriptor.getNameAsString());
        }
        if (logger.isInfoEnabled()) {
            logger.info("HBaseServiceImpl queryFamilies end");
        }
        return list;
    }

    /**
     * 查询所有表
     *
     * @param configuration
     */
    @Override
    public List queryTable(Configuration configuration) throws IOException {
        if (logger.isInfoEnabled()) {
            logger.info("HBaseServiceImpl queryTable begin");
        }
        HBaseAdmin admin = new HBaseAdmin(configuration);
        TableName[] names = admin.listTableNames();
        List list = new ArrayList();
        for (TableName name : names) {
            System.out.println(name.getNameAsString());
            list.add(name.getNameAsString());
        }
        if (logger.isInfoEnabled()) {
            logger.info("HBaseServiceImpl queryTable end");
        }
        return list;
    }

    /**
     * 授予用户权限
     *
     * @param userName 用户
     * @param actions  权限
     */
    @Override
    public void grant(Configuration configuration, String tableName,String userName, String family,String qual,Permission.Action... actions) throws Throwable {
        if (logger.isInfoEnabled()) {
            logger.info("HBaseServiceImpl grant begin");
        }
        HTable table = new HTable(configuration, Bytes.toBytes(tableName));
        TableName tableName1 = table.getName();
        AccessControlClient.grant(configuration, tableName1, userName, Bytes.toBytes(family), Bytes.toBytes(qual), actions);
        if (logger.isInfoEnabled()) {
            logger.info("HBaseServiceImpl grant end");
        }
    }

    /**
     * 回收用户权限
     *
     * @param userName 用户
     * @param actions  权限
     */
    @Override
    public void revoke(Configuration configuration, String tableName,String userName, String family,String qual,Permission.Action... actions) throws Throwable {
        if (logger.isInfoEnabled()) {
            logger.info("HBaseServiceImpl revoke begin");
        }
        HTable table = new HTable(configuration, Bytes.toBytes(tableName));
        TableName tableName1 = table.getName();
        AccessControlClient.revoke(configuration, tableName1, userName, Bytes.toBytes(family), Bytes.toBytes(qual), actions);
        if (logger.isInfoEnabled()) {
            logger.info("HBaseServiceImpl revoke end");
        }
    }

    /**
     * 添加hbase配置
     *
     * @param ip
     */
    @Override
    public void addHbaseConfig(String ip) {

    }

    /**
     * 修改hbase配置
     *
     * @param ip
     */
    @Override
    public void updateHbaseConfig(String ip) {

    }

    /**
     * 添加/更新数据
     *
     * @param tableName 表名称
     * @param rowKey    唯一标识
     * @param family    列族
     * @param qualifier 列
     * @param value     值
     */
    @Override
    public void add(String tableName, String rowKey, String family, String qualifier, String value) throws IOException {
        HTable table = new HTable(configuration, Bytes.toBytes(tableName));
        // 需要插入数据库的数据集合
        List putList = new ArrayList();
        Put put = new Put(Bytes.toBytes(rowKey));
        // 列族、列、值
        put.add(Bytes.toBytes(family), Bytes.toBytes(qualifier), Bytes.toBytes(value));
        putList.add(put);
        // 将数据集合插入到数据库
        table.put(putList);
        System.out.println("---------------插入数据 END-----------------");
    }

    /**
     * 删除指定列的所有值
     *
     * @param tableName 表名称
     * @param rowKey    唯一标识
     * @throws java.io.IOException
     */
    @Override
    public void delete(String tableName, String rowKey) throws IOException {
        HTable table = new HTable(configuration, Bytes.toBytes(tableName));
        Delete deleteAll = new Delete(Bytes.toBytes(rowKey));
        table.delete(deleteAll);
        System.out.println("all columns are deleted!");
    }

    /**
     * 查询列
     *
     * @param tableName 表名称
     * @param rowKey    唯一标识
     * @param family    列族
     * @param qualifier 列
     * @throws java.io.IOException
     */
    @Override
    public Result find(String tableName, String rowKey, String family, String qualifier, String value) throws IOException {
        HTable table = new HTable(configuration, Bytes.toBytes(tableName));
        // 根据tableName查询
        if (StrUtil.isNullStr(rowKey) && StrUtil.isNullStr(family) && StrUtil.isNullStr(qualifier)) {
            ResultScanner rs = table.getScanner(new Scan());
            for (Result r : rs) {
                System.out.println("获得到rowkey:" + new String(r.getRow()));
                for (KeyValue keyValue : r.raw()) {
                    System.out.println("列族:" + new String(keyValue.getFamily())
                            + "====" + new String(keyValue.getQualifier())
                            + "====值:" + new String(keyValue.getValue()));
                }
            }
        }
        //根据tableName,rowKey查询
        if (!StrUtil.isNullStr(rowKey) && StrUtil.isNullStr(family) && StrUtil.isNullStr(qualifier)) {
            Get scan = new Get(rowKey.getBytes());
            Result r = table.get(scan);
            System.out.println("获得到rowkey:" + new String(r.getRow()));
            for (KeyValue keyValue : r.raw()) {
                System.out.println("列族:" + new String(keyValue.getFamily())
                        + "====" + new String(keyValue.getQualifier())
                        + "====值:" + new String(keyValue.getValue()));
            }
        }
        return null;
    }

    /**
     * 查询列
     *
     * @param tableName 表名称
     * @param rowKey    唯一标识
     * @param family    列族
     * @param qualifier 列
     * @throws java.io.IOException
     */
    @Override
    public Result findByQualifier(String tableName, String rowKey, String family, String qualifier) throws IOException {
        HTable table = new HTable(configuration, Bytes.toBytes(tableName));
        Get get = new Get(Bytes.toBytes(rowKey));
        get.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier)); // 获取指定列族和列修饰符对应的列
        Result result = table.get(get);
        for (KeyValue kv : result.list()) {
            System.out.println("family:" + Bytes.toString(kv.getFamily()));
            System.out.println("qualifier:" + Bytes.toString(kv.getQualifier()));
            System.out.println("value:" + Bytes.toString(kv.getValue()));
            System.out.println("Timestamp:" + kv.getTimestamp());
            System.out.println("-------------------------------------------");
        }
        return result;
    }

    @Override
    public Result findByFamily(Configuration configuration,String tableName, String rowKey, String family) throws IOException {
        HTable table = new HTable(configuration, Bytes.toBytes(tableName));
        Get get = new Get(Bytes.toBytes(rowKey));
        get.addFamily(Bytes.toBytes(family)); // 获取指定列族和列修饰符对应的列
        Result result = table.get(get);
        for (KeyValue kv : result.list()) {
            System.out.println("family:" + Bytes.toString(kv.getFamily()));
            System.out.println("qualifier:" + Bytes.toString(kv.getQualifier()));
            System.out.println("value:" + Bytes.toString(kv.getValue()));
            System.out.println("Timestamp:" + kv.getTimestamp());
            System.out.println("-------------------------------------------");
        }
        return result;
    }

    /**
     * 根据rwokey查询
     *
     * @param tableName
     * @param rowKey
     */
    @Override
    public Result findByRowKey(String tableName, String rowKey) throws IOException {
        HTable table = new HTable(configuration, Bytes.toBytes(tableName));// 获取表
        Get get = new Get(Bytes.toBytes(rowKey));
        Result result = table.get(get);
        for (KeyValue kv : result.list()) {
            System.out.println("family:" + Bytes.toString(kv.getFamily()));
            System.out.println("qualifier:" + Bytes.toString(kv.getQualifier()));
            System.out.println("value:" + Bytes.toString(kv.getValue()));
            System.out.println("Timestamp:" + kv.getTimestamp());
            System.out.println("-------------------------------------------");
        }
        return result;
    }

    /**
     * 创建表
     *
     * @param tableName 表名称
     * @param family    列族
     */
    @Override
    public boolean creatTable(String tableName, List family) throws IOException {
        boolean flag = false;
        HBaseAdmin admin = new HBaseAdmin(configuration);
        HTableDescriptor desc = new HTableDescriptor(tableName);
        for (int i = 0; i < family.size(); i++) {
            desc.addFamily(new HColumnDescriptor(family.get(i)));
        }
        if (admin.tableExists(tableName)) {
            System.out.println("table Exists!");
            flag = false;
            return flag;
        } else {
            admin.createTable(desc);
            System.out.println("create table Success!");
            flag = true;
            return flag;
        }
    }
}

rest接口调用

package com.ultrapower.framework.rest;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ultrapower.framework.module.bigdata.dirver.hadoop.util.StrUtil;
import com.ultrapower.framework.module.bigdata.dirver.hbase.domain.UserAuthz;
import com.ultrapower.framework.module.bigdata.dirver.hbase.service.IHBaseService;
import com.ultrapower.framework.module.bigdata.dirver.hbase.service.impl.HBaseServiceImpl;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.security.access.Permission;
import org.apache.log4j.Logger;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * hbase Rest服务
 * Created by shiyufeng on 2017/5/2.
 */
@Path("/hbase")
public class HbaseRestService {
    private static final Logger logger = Logger.getLogger(HbaseRestService.class);

    //    static Configuration configuration = null;
    static {
//        configuration = HBaseConfiguration.create();
//        configuration.set("hbase.zookeeper.quorum", "192.168.187.209");
    }

    @POST
    @Path("grant")
    public String grant(String data) {
        if (logger.isDebugEnabled()) {
            logger.debug("HbaseRestService grant begin");
            logger.debug("HbaseRestService grant received :【" + data + "】");
        }

        JSONObject result = new JSONObject();
        if (StrUtil.isNullStr(data)) {
            result.put("success", false);
            result.put("errorMsg", "param is null");
            return result.toJSONString();
        }
        UserAuthz userAuthz = JSON.parseObject(data, UserAuthz.class);
        userAuthz.setQual("");// 传入"" 防止空指针异常
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "192.168.187.209");

        List actions = userAuthz.getActions();

        Permission.Action[] actions1 = this.fillActions(actions);
        IHBaseService service = new HBaseServiceImpl();
        try {
            service.grant(configuration, userAuthz.getTableName(), userAuthz.getUserName(), userAuthz.getFamily(), userAuthz.getQual(), actions1);
            result.put("success", true);
        } catch (Throwable throwable) {
            result.put("errorMsg", throwable.getMessage());
            result.put("success", false);
            throwable.printStackTrace();
        }

        if (logger.isDebugEnabled()) {
            logger.debug("HbaseRestService grant end");
        }
        return result.toJSONString();
    }

    @POST
    @Path("revoke")
    public String revoke(String data) {
        if (logger.isDebugEnabled()) {
            logger.debug("HbaseRestService revoke begin");
            logger.debug("HbaseRestService revoke received :【" + data + "】");

        }

        JSONObject result = new JSONObject();
        if (StrUtil.isNullStr(data)) {
            result.put("success", false);
            result.put("errorMsg", "param is null");
            return result.toJSONString();
        }
        UserAuthz userAuthz = JSON.parseObject(data, UserAuthz.class);
        userAuthz.setQual("");// 传入"" 防止空指针异常

        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "192.168.187.209");

        List actions = userAuthz.getActions();

        Permission.Action[] actions1 = this.fillActions(actions);
        IHBaseService service = new HBaseServiceImpl();
        try {
            service.grant(configuration, userAuthz.getTableName(), userAuthz.getUserName(), userAuthz.getFamily(), userAuthz.getQual(), actions1);
            result.put("success", true);
        } catch (Throwable throwable) {
            result.put("success", false);
            throwable.printStackTrace();
        }

        if (logger.isDebugEnabled()) {
            logger.debug("HbaseRestService revoke end");
        }
        return result.toJSONString();
    }

    @POST
    @Path("queryFamilies")
    public String queryFamilies(String data) {
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "192.168.187.209");
        if (logger.isDebugEnabled()) {
            logger.debug("HbaseRestService queryFamilies begin");
            logger.debug("HbaseRestService queryFamilies received :【" + data + "】");

        }

        JSONObject result = new JSONObject();
        result.put("success", true);
        if (StrUtil.isNullStr(data)) {
            result.put("success", false);
            result.put("errorMsg", "param is null");
            return result.toJSONString();
        }

        JSONObject jsonObject = JSON.parseObject(data);

        String tableName = (String) jsonObject.get("tableName");
        IHBaseService service = new HBaseServiceImpl();
        List list;
        try {
            list = service.queryFamilies(configuration, tableName);
            JSONArray fsArr = new JSONArray();
            for (String name : list) {
                JSONObject json = new JSONObject();
                json.put("familyName", name);
                fsArr.add(json);
            }
            result.put("result", fsArr);
        } catch (IOException e) {
            logger.error(e.getMessage());
            result.put("success", false);
            result.put("errorMsg", e.getMessage());
        }
        if (logger.isDebugEnabled()) {
            logger.debug("HbaseRestService queryFamilies end");
        }
        return result.toJSONString();
    }

    @POST
    @Path("queryTable")
    public String queryTable(String data) {
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "192.168.187.209");
        if (logger.isDebugEnabled()) {
            logger.debug("HbaseRestService queryTable begin");
            logger.debug("HbaseRestService queryTable received :【" + data + "】");

        }

        JSONObject result = new JSONObject();
        result.put("success", true);
        if (StrUtil.isNullStr(data)) {
            result.put("success", false);
            result.put("errorMsg", "param is null");
            return result.toJSONString();
        }

        JSONObject jsonObject = JSON.parseObject(data);

        IHBaseService service = new HBaseServiceImpl();
        List list;
        try {
            list = service.queryTable(configuration);
            JSONArray fsArr = new JSONArray();
            for (String name : list) {
                JSONObject json = new JSONObject();
                json.put("tableName", name);
                fsArr.add(json);
            }
            result.put("result", fsArr);
        } catch (IOException e) {
            logger.error(e.getMessage());
            result.put("errorMsg", e.getMessage());
        }
        if (logger.isDebugEnabled()) {
            logger.debug("HbaseRestService queryTable end");
        }
        return result.toJSONString();
    }


    public static void main(String[] args) {
        UserAuthz userAuthz = new UserAuthz();
        userAuthz.setTableName("student");
        userAuthz.setUserName("syf");
        userAuthz.setFamily("studentAddress");
        userAuthz.setQual("");
        List actions = new ArrayList();
        actions.add("read");
        actions.add("write");
        userAuthz.setActions(actions);
        String json = JSONObject.toJSONString(userAuthz);
        System.out.println(json);
    }

    /**
     * 创建Actions
     *
     * @param actions
     * @return
     */
    public Permission.Action[] fillActions(List actions) {
        Permission.Action[] actions1 = new Permission.Action[actions.size()];
        for (int i = 0; i < actions.size(); i++) {
            if ("read".equals(actions.get(i))) {
                actions1[i] = Permission.Action.READ;
            }
            if ("write".equals(actions.get(i))) {
                actions1[i] = Permission.Action.WRITE;
            }
            if ("exec".equals(actions.get(i))) {
                actions1[i] = Permission.Action.EXEC;
            }
            if ("create".equals(actions.get(i))) {
                actions1[i] = Permission.Action.CREATE;
            }
            if ("admin".equals(actions.get(i))) {
                actions1[i] = Permission.Action.ADMIN;
            }
        }
        return actions1;
    }
}

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