1

我在 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 中找到有关相同的适当文档。谁能帮帮我吗?

4

1 回答 1

1

我通过以下代码从 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);
于 2018-09-18T07:37:15.890 回答