Getting latest data from AWS custom Cloudwatch in Java

2.7k views Asked by At

I have a custom metric in AWS cloudwatch and i am putting data into it through 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);


    }

Now i want to be able to fetch the latest data entered for a particular namespace, metric name and dimention combination through Java API. I am not able to find appropriate documenation from AWS regarding the same. Can anyone please help me?

1

There are 1 answers

1
codeguru On

I got the results from cloudwatch by the below code.\

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);