从Java中的AWS自定义Cloudwatch获取最新数据

时间:2018-09-17 11:13:43

标签: java amazon-cloudwatch amazon-cloudwatch-metrics

我在AWS cloudwatch中有一个自定义指标,并且正在通过AWS Java API将数据放入其中。

for(int i =0;i<collection.size();i++){
        String[] cell = collection.get(i).split("\\|\\|");
        List<Dimension> dimensions = new ArrayList<>();
        dimensions.add(new Dimension().withName(dimension[0]).withValue(cell[0]));
        dimensions.add(new Dimension().withName(dimension[1]).withValue(cell[1]));
        MetricDatum datum = new MetricDatum().withMetricName(metricName)
                                             .withUnit(StandardUnit.None)
                                             .withValue(Double.valueOf(cell[2]))
                                             .withDimensions(dimensions);
        PutMetricDataRequest request = new PutMetricDataRequest().withNamespace(namespace+"_"+cell[3]).withMetricData(datum);
        String response = String.valueOf(cw.putMetricData(request));

        GetMetricDataRequest res = new GetMetricDataRequest().withMetricDataQueries();
        //cw.getMetricData();
        com.amazonaws.services.cloudwatch.model.Metric m = new com.amazonaws.services.cloudwatch.model.Metric();
        m.setMetricName(metricName);
        m.setDimensions(dimensions);
        m.setNamespace(namespace);
        MetricStat ms = new MetricStat().withMetric(m);
        MetricDataQuery metricDataQuery = new MetricDataQuery();
        metricDataQuery.withMetricStat(ms);
        metricDataQuery.withId("m1");
        List<MetricDataQuery> mqList = new ArrayList<MetricDataQuery>();
        mqList.add(metricDataQuery);
        res.withMetricDataQueries(mqList);
        GetMetricDataResult result1=  cw.getMetricData(res);


    }

现在,我希望能够通过Java API提取针对特定名称空间,度量标准名称和尺寸组合输入的最新数据。我无法从AWS找到与此有关的适当文档。谁能帮我吗?

1 个答案:

答案 0 :(得分:0)

我通过以下代码从cloudwatch获得了结果。\

GetMetricDataRequest getMetricDataRequest = new GetMetricDataRequest().withMetricDataQueries();
    Integer integer = new Integer(300);

    Iterator<Map.Entry<String, String>> entries = dimensions.entrySet().iterator();
    List<Dimension> dList = new ArrayList<Dimension>();
    while (entries.hasNext()) {
        Map.Entry<String, String> entry = entries.next();
        dList.add(new Dimension().withName(entry.getKey()).withValue(entry.getValue()));
    }

    com.amazonaws.services.cloudwatch.model.Metric metric = new com.amazonaws.services.cloudwatch.model.Metric();
    metric.setNamespace(namespace);
    metric.setMetricName(metricName);
    metric.setDimensions(dList);
    MetricStat ms = new MetricStat().withMetric(metric)
                                    .withPeriod(integer)
                                    .withUnit(StandardUnit.None)
                                    .withStat("Average");
    MetricDataQuery metricDataQuery = new MetricDataQuery().withMetricStat(ms)
                                                            .withId("m1");

    List<MetricDataQuery> mqList = new ArrayList<>();
    mqList.add(metricDataQuery);
    getMetricDataRequest.withMetricDataQueries(mqList);
    long timestamp = 1536962700000L;
    long timestampEnd = 1536963000000L;
      Date d = new Date(timestamp );
      Date dEnd = new Date(timestampEnd );
    getMetricDataRequest.withStartTime(d);
    getMetricDataRequest.withEndTime(dEnd);
    GetMetricDataResult result1=  cw.getMetricData(getMetricDataRequest);