MAPREDUCE服务 MRS-访问HBase ThriftServer认证:样例代码

时间:2024-06-13 09:39:23

样例代码

  • 代码认证
    以下代码在“hbase-thrift-example”样例工程的“com.huawei.bigdata.hbase.examples”包的“TestMain”类中。
        private static void init() throws IOException {
            // Default load from conf directory
            conf = HBaseConfiguration.create();
    
            String userdir = TestMain.class.getClassLoader().getResource("conf").getPath() + File.separator;[1]
            //In Linux environment
            //String userdir = System.getProperty("user.dir") + File.separator + "conf" + File.separator;
            conf.addResource(new Path(userdir + "core-site.xml"), false);
            conf.addResource(new Path(userdir + "hdfs-site.xml"), false);
            conf.addResource(new Path(userdir + "hbase-site.xml"), false);
    }
    

    [1]userdir获取的是编译后资源路径下conf目录的路径。初始化配置用到的core-site.xml、hdfs-site.xml、hbase-site.xml文件和用于安全认证的用户凭证文件,需要放置到"src/main/resources/conf"的目录下。

  • 安全登录
    请根据实际情况,修改“userName”为实际用户名,例如“developuser”。
        private static void login() throws IOException {
            if (User.isHBaseSecurityEnabled(conf)) {
                userName = " developuser ";
    
                //In Windows environment
                String userdir = TestMain.class.getClassLoader().getResource("conf").getPath() + File.separator;
                //In Linux environment
                //String userdir = System.getProperty("user.dir") + File.separator + "conf" + File.separator;
    
                userKeytabFile = userdir + "user.keytab";
                krb5File = userdir + "krb5.conf";
     
                /*
                 * if need to connect zk, please provide jaas info about zk. of course,
                 * you can do it as below:
                 * System.setProperty("java.security.auth.login.config", confDirPath +
                 * "jaas.conf"); but the demo can help you more : Note: if this process
                 * will connect more than one zk cluster, the demo may be not proper. you
                 * can contact us for more help
                 */
                LoginUtil.setJaasConf(ZOOKEEPER_DEFAULT_ LOG IN_CONTEXT_NAME, userName, userKeytabFile);
                LoginUtil.login(userName, userKeytabFile, krb5File, conf);
            }
    }
  • 连接ThriftServer实例
        try {    
            test = new ThriftSample();    
            test.test("10.120.16.170", THRIFT_PORT, conf);[2]
        } catch (TException | IOException e) {
            LOG.error("Test thrift error", e);
        }

    [2]test.test()传入参数为待访问的ThriftServer实例所在节点ip地址,需根据实际运行集群情况进行修改,且该节点ip需要配置到运行样例代码的本机hosts文件中。

    “THRIFT_PORT”为ThriftServer实例的配置参数"hbase.regionserver.thrift.port"对应的值。

support.huaweicloud.com/devg-lts-mrs/mrs_07_080064.html