Cloudera Manager API调用实例(JAVA版)

1、下载github上的api项目,含pom依赖关系

https://github.com/cloudera/cm_api

2、下载的api项目只是纯粹的api说明和工具类,没有调用实例,以maven格式导入到eclipse项目中

3、增加调用实例类,如下

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.ws.rs.core.Response;

import com.cloudera.api.ApiRootResource;
import com.cloudera.api.ApiUtils;
import com.cloudera.api.ClouderaManagerClientBuilder;
import com.cloudera.api.DataView;
import com.cloudera.api.model.ApiHost;
import com.cloudera.api.model.ApiTimeSeriesRequest;
import com.cloudera.api.v10.HostsResourceV10;
import com.cloudera.api.v11.RootResourceV11;
import com.cloudera.api.v11.TimeSeriesResourceV11;

import net.sf.json.JSONObject;

public class T {

	private static String cdhApiPath = "172.31.2.90";
	private static String cdhApiPort = "7180";
	private static String cdhApiUserName = "admin";
	private static String cdhApiPassword = "admin";

	/**
	 * 按照时间范围,查询图表数据
	 * 
	 * @param query
	 * @param from
	 * @param to
	 * @return
	 * @throws ParseException
	 */
	public static JSONObject getTimeSeriesResponse(String query, String from, String to) throws ParseException {
		SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date fromDate = dfs.parse(from);
		Date toDate = dfs.parse(to);
		// SimpleDateFormat rfs = new
		// SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'+0800'");
		// String fromformat2 = rfs.format(fromDate);
		// String toformat2 = rfs.format(toDate);
		String fromformat = ApiUtils.printDate(fromDate);
		String toformat = ApiUtils.printDate(toDate);

		long between = (toDate.getTime() - fromDate.getTime());
		int minutes = (int) (between / (1000 * 60));
		String desire = "RAW";
		if (minutes <= 30) {
			desire = "RAW";

		} else if (minutes > 30 && minutes < 300) {
			desire = "TEN_MINUTELY";
		} else if (minutes >= 300 && minutes < 1800) {
			desire = "HOURLY";
		} else if (minutes >= 1800 && minutes < 10800) {
			desire = "SIX_HOURLY";
		} else {
			desire = "DAILY";
		}

		System.out.println("desire" + desire);
		System.out.println("fromformat" + fromformat);
		System.out.println("toformat" + toformat);
		ApiTimeSeriesRequest atsr = new ApiTimeSeriesRequest();
		atsr.setQuery(query);
		atsr.setFrom(fromformat);
		atsr.setTo(toformat);
		atsr.setDesiredRollup(desire);
		atsr.setMustUseDesiredRollup(true);
		ApiRootResource root = new ClouderaManagerClientBuilder().withHost(cdhApiPath)
				.withPort(Integer.parseInt(cdhApiPort)).withUsernamePassword(cdhApiUserName, cdhApiPassword).build();

		RootResourceV11 v11 = root.getRootV11();

		TimeSeriesResourceV11 t11 = v11.getTimeSeriesResource();
		Response res = t11.queryTimeSeries(atsr);
		String jsonResponse = res.readEntity(String.class);
		JSONObject obj = JSONObject.fromObject(jsonResponse);

		return obj;
	}

	/**
	 * 查询节点信息
	 * 
	 * @return
	 */
	public static JSONObject getHost() {
		ApiRootResource root = new ClouderaManagerClientBuilder().withHost(cdhApiPath)
				.withPort(Integer.parseInt(cdhApiPort)).withUsernamePassword(cdhApiUserName, cdhApiPassword).build();

		RootResourceV11 v11 = root.getRootV11();
		HostsResourceV10 hosts = v11.getHostsResource();
		List<ApiHost> hostList = hosts.readHosts(DataView.SUMMARY).getHosts();
		for (ApiHost apiHost : hostList) {
			System.out.println(apiHost.getIpAddress());
		}
		JSONObject obj = new JSONObject();

		return obj;
	}

	public static void main(String[] args) throws Exception {
		String query = "SELECT total_read_bytes_rate_across_datanodes WHERE entityName = \"hdfs\" AND category = SERVICE";
		String from = "2019-03-29 21:20:00";
		String end = "2019-03-29 21:45:00";
		JSONObject json = getTimeSeriesResponse(query, from, end);
		System.out.println(json.toString());
		getHost();
	}

	/**
	 * 把CM的日期转换成正常的日期 CM的时间和正常的时间格式有区别,需要转换(相差8小时)
	 * 
	 * @param cmdatestr
	 * @return
	 */
	public static Date transCMDateToNormal(String cmdatestr) {
		return ApiUtils.newDateFromString(cmdatestr);
	}

	/**
	 * 把正常的日期转换成CM的日期,用于传参数 CM的时间和正常的时间格式有区别,需要转换(相差8小时)
	 * 
	 * @param cmdatestr
	 * @return
	 */
	public static String transNormalDateToCMDate(Date date) {
		return ApiUtils.printDate(date);
	}
}

注意事项:

有个坑,就是CM时间格式和我们正常的格式不一致,相差8小时,需要用上面类中的工具方法转换。

最后附上我试验成功的eclipse项目下载地址:https://download.csdn.net/download/gongchengshiv/11076580

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值