登录cloudera manager,并查询impala query历史java示例

通过java登录cloudera manager,并进行impala query历史查询示例!

package commons;

import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.slf4j.Logger;

public class HttpAuthen {
    static Logger logger = org.slf4j.LoggerFactory.getLogger(HttpAuthen.class);

    public static void main(String[] args) {
        //hostip和账户替换为你自己的
        String loginUrl = "http://hostip:7180/j_spring_security_check";
        String dataUrl = "http://hostip:7180/api/v11/clusters/Cluster1/services/impala/impalaQueries";

        // 需登陆后访问的 Url
        System.out.println(getHttpClient(loginUrl,dataUrl,"j_username","j_password","user", "user"));
        System.out.println(getHttpClient(loginUrl,dataUrl,"j_username","j_password","user", "passwd"));

    }
//   java  -cp commons.HttpAuthen
    public static String getHttpClient(String loginUrl,String dataUrl,String userLabel,String passwdLabel,String userName,String passwd) {
        HttpClient httpClient = new HttpClient();
        // 模拟登陆,按实际服务器端要求选用 Post 或 Get 请求方式
        PostMethod postMethod = new PostMethod(loginUrl);

        NameValuePair[] data = { new NameValuePair(userLabel, userName), new NameValuePair(passwdLabel, passwd) };
        postMethod.setRequestBody(data);
        try {
            // 设置 HttpClient 接收 Cookie,用与浏览器一样的策略
            httpClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
            httpClient.executeMethod(postMethod);
            // 获得登陆后的 Cookie
            Cookie[] cookies = httpClient.getState().getCookies();
            StringBuffer tmpcookies = new StringBuffer();
            for (Cookie c : cookies) {
                tmpcookies.append(c.toString() + ";");
            }
            GetMethod getMethod = new GetMethod(dataUrl);
            getMethod.setRequestHeader("cookie", tmpcookies.toString());
            postMethod.setRequestHeader("Referer", "http://www.cc");
            postMethod.setRequestHeader("User-Agent", "www Spot");
            httpClient.executeMethod(getMethod);
            String text = getMethod.getResponseBodyAsString();
            return text;
        } catch (Exception e) {
            logger.error("",e);
        }
        return null;
    }
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,关于使用Java API操作Impala以及Cloudera Manager API操作Yarn/Impala队列资源,我可以给您一些指导。 首先,对于ImpalaJava API操作,可以使用ImpalaJDBCConnector库来实现。这个库提供了一个JDBC驱动程序,使得您可以使用Java来连接Impala,并执行SQL查询。这个库可以在Maven中央仓库中找到。以下是一个使用ImpalaJDBCConnector库的示例代码: ```java Class.forName("com.cloudera.impala.jdbc41.Driver"); Connection conn = DriverManager.getConnection("jdbc:impala://localhost:21050/default", "", ""); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM my_table"); while (rs.next()) { System.out.println("Count: " + rs.getInt(1)); } ``` 对于Cloudera Manager API操作Yarn/Impala队列资源,您可以使用Cloudera ManagerJava API来实现。Cloudera Manager提供了一个REST API,可以使用Java来调用。以下是一个使用Cloudera Manager Java API的示例代码: ```java ApiRootResource root = new ClouderaManagerClientBuilder() .withHost("localhost") .withUsernamePassword("admin", "admin") .build().getRootV19(); ApiClusterTemplateResourceV19 clusterTemplateResource = root.getClusterTemplateResource(); ApiClusterTemplateList clusterTemplateList = clusterTemplateResource.readClusterTemplates(DataView.SUMMARY); for (ApiClusterTemplate template : clusterTemplateList) { System.out.println(template.getName()); } ``` 以上是一些示例代码,您可以根据具体场景进行调整和修改。希望能对您有所帮助!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值