hbase java api指定访问用户名

在默认情况情况,我们使用hbase的java api去访问hbase的服务时,使用的hbase的服务的用户名为启动java程序的系统用户名。

在有些情况下,我们要指定用户。如何指定呢,就是本文的内容。

		//指定用户名为hbase的用户去访问hbase服务
        UserGroupInformation userGroupInformation = UserGroupInformation.createRemoteUser("hbase");
        connection = ConnectionFactory.createConnection(configuration, User.create(userGroupInformation));

全文代码:

HBaseTest.java

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.ConnectionFactory;
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.client.Table;
import org.apache.hadoop.hbase.security.User;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.security.UserGroupInformation;

public class HBaseTest {
    private org.apache.hadoop.hbase.client.Connection connection = null;
    public void init() throws Exception {
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "192.168.13.133,192.168.13.134,192.168.13.135");//zookeeper的ip
        configuration.set("hbase.zookeeper.property.clientPort", "2181");// zookeeper端口

        configuration.set("zookeeper.znode.parent", "/hbase-unsecure");
        configuration.set("hbase.rpc.timeout", "2000");
        configuration.set("hbase.client.scanner.timeout.period", "3000");
//        configuration.set("hadoop.home.dir","F:\\hadoop");

        //默认使用的是启动该java程序的系统用户名去连接hbase,
//        connection = ConnectionFactory.createConnection(configuration);

        //指定用户名为hbase的用户去访问hbase服务
        UserGroupInformation userGroupInformation = UserGroupInformation.createRemoteUser("hbase");
        connection = ConnectionFactory.createConnection(configuration, User.create(userGroupInformation));
    }

    public void doScan() {
        try {
            Table table = connection.getTable(TableName.valueOf("student"));
            Scan scan = new Scan();
            scan.setStartRow(Bytes.toBytes("001"));
            scan.setStopRow(Bytes.toBytes("002"));//不包含这条
            ResultScanner rs = table.getScanner(scan);
            int num = 0;
            for (Result r : rs) {
                System.out.println("获得到rowkey:" + new String(r.getRow()));
                num++;
            }
            System.out.print(num);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        HBaseTest hBaseTest = new HBaseTest();
        try {
            hBaseTest.init();
            hBaseTest.doScan();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Hbase 基本操作类 static { //此处可以使用hbase的配置文件,也可以通过代码来实例化hbase连接 /* * Configuration HBASE_CONFIG = new Configuration(); * HBASE_CONFIG.set("hbase.zookeeper.quorum", "10.229.171.45"); * HBASE_CONFIG.set("hbase.zookeeper.property.clientPort", "2181"); * HBASE_CONFIG.set("hbase.zookeeper.quorum","10.233.92.85,10.233.92.86,10.233.92.88"); * HBASE_CONFIG.set("hbase.zookeeper.property.clientPort", "2181"); * HBASE_CONFIG.set("zookeeper.znode.parent", "/hbase-unsecure"); conf = HBaseConfiguration.create(HBASE_CONFIG); */ conf = HBaseConfiguration.create(); if("kerberos".equals(conf.get("hbase.security.authentication"))){ try { //设置hadoop.security.authentication为kerberos conf.set("hadoop.security.authentication", "kerberos"); //获取kerberos配置文件路径(krb为kerberos配置文件) String krbStr=Thread.currentThread().getContextClassLoader().getResource("krb").getFile(); //获取用户票据hezhong路径(hezhong为给合众分配的用户配置文件) String keyStr=Thread.currentThread().getContextClassLoader().getResource("jdzy").getFile(); //初始化配置文件 System.setProperty("java.security.krb5.conf", krbStr); //使用用户hezhong登录 UserGroupInformation.setConfiguration(conf); UserGroupInformation.loginUserFromKeytab("jdzy/[email protected]", keyStr); } catch (IOException e) { e.printStackTrace(); } } tablePool = new HTablePool(conf, poolsize); logger.debug("create hbase connection success"); // System.out.println("create hbase connection success"); }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值