java调用HbaseAPI管理Hbase权限

来源:互联网 发布:织梦 编辑器字体修改 编辑:程序博客网 时间:2024/04/25 21:37

导入依赖

<dependency>            <groupId>log4j</groupId>            <artifactId>log4j</artifactId>            <version>1.2.17</version>        </dependency>        <!--jersey-->        <dependency>            <groupId>com.sun.jersey</groupId>            <artifactId>jersey-bundle</artifactId>            <version>1.19.1</version>        </dependency>        <dependency>            <groupId>com.sun.jersey</groupId>            <artifactId>jersey-json</artifactId>            <version>1.19</version>        </dependency>        <dependency>            <groupId>com.sun.jersey</groupId>            <artifactId>jersey-servlet</artifactId>            <version>1.19.1</version>        </dependency>        <!-- hbase -->        <dependency>            <groupId>org.apache.hbase</groupId>            <artifactId>hbase-client</artifactId>            <version>1.3.1</version>        </dependency>        <dependency>            <groupId>org.apache.hbase</groupId>            <artifactId>hbase-common</artifactId>            <version>1.3.1</version>        </dependency>        <dependency>            <groupId>commons-logging</groupId>            <artifactId>commons-logging</artifactId>            <version>1.1.1</version>        </dependency>        <dependency>            <groupId>com.google.guava</groupId>            <artifactId>guava</artifactId>            <version>12.0.1</version>        </dependency>        <dependency>            <groupId>commons-collections</groupId>            <artifactId>commons-collections</artifactId>            <version>3.2.2</version>        </dependency>        <dependency>            <groupId>com.google.protobuf</groupId>            <artifactId>protobuf-java</artifactId>            <version>2.5.0</version>        </dependency>        <dependency>            <groupId>commons-lang</groupId>            <artifactId>commons-lang</artifactId>            <version>2.6</version>        </dependency>        <dependency>            <groupId>commons-configuration</groupId>            <artifactId>commons-configuration</artifactId>            <version>1.6</version>        </dependency>        <dependency>            <groupId>org.apache.hadoop</groupId>            <artifactId>hadoop-core</artifactId>            <version>1.2.1</version>        </dependency>        <dependency>            <groupId>org.cloudera.htrace</groupId>            <artifactId>htrace-core</artifactId>            <version>2.04</version>        </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<String> 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<HColumnDescriptor> collection = desc.getFamilies();        List<String> list = new ArrayList<String>();        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<String> queryTable(Configuration configuration) throws IOException {        if (logger.isInfoEnabled()) {            logger.info("HBaseServiceImpl queryTable begin");        }        HBaseAdmin admin = new HBaseAdmin(configuration);        TableName[] names = admin.listTableNames();        List<String> list = new ArrayList<String>();        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<Put> putList = new ArrayList<Put>();        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<String> 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<String> 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<String> 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<String> 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<String> 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<String> actions = new ArrayList<String>();        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<String> 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;    }}
0 0