MAPREDUCE服务 MRS-使用代理用户访问FlinkServer REST API样例程序(Java):代码样例

时间:2024-10-18 17:22:19

代码样例

以租户用户为“test92”,租户ID为“92”,获取具有FlinkServer管理员权限的用户名为“flinkserveradmin”的代理访问API为例,以下代码为完整示例。

public class TestCreateTenants {
    public static void main(String[] args) {
        ParameterTool paraTool = ParameterTool.fromArgs(args);
        final String hostName = paraTool.get("hostName");    // 修改hosts文件,使用主机名
        final String keytab = paraTool.get("keytab");        // user.keytab路径
        final String krb5 = paraTool.get("krb5");            // krb5.conf路径
        final String principal = paraTool.get("principal");  // 认证用户

        System.setProperty("java.security.krb5.conf", krb5);
        String url = "https://"+hostName+":28943/flink/v1/tenants";
        String jsonstr = "{" +
                "\n\t \"tenantId\":\"92\"," +
                "\n\t \"tenantName\":\"test92\"," +
                "\n\t \"remark\":\"test tenant remark1\"," +
                "\n\t \"updateUser\":\"test_updateUser1\"," +
                "\n\t \"createUser\":\"test_createUser1\"" +
                "\n}";

        try {
            LoginClient.getInstance().setConfigure(url, principal, keytab, "");
            LoginClient.getInstance().login(); // 先使用flinkserver管理员用户登录

            String proxyUrl =  "https://"+hostName+":28943/flink/v1/proxyUserLogin"; // 调用代理用户接口,获取普通用户token
            String result = HttpClientUtil.doPost(proxyUrl, "{\n" +
                    "\t\"realUser\": \"flinkserveradmin\"\n" +
                    "}", "utf-8", true);
            Gson gson = new Gson();
            JsonObject jsonObject = gson.fromJson(result, JsonObject.class);
            String token = jsonObject.get("result").toString();
            token = "hadoop_auth=" + token;

            System.out.println(HttpClientUtil.doPost(url, jsonstr, "utf-8", true , token));
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}
support.huaweicloud.com/devg-lts-mrs/mrs_07_260074.html