# Bucket aggregation with java api and export in csv

**URL:** <https://discuss.elastic.co/t/bucket-aggregation-with-java-api-and-export-in-csv/314935>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [September 22, 2022, 11:04am UTC](https://discuss.elastic.co/t/bucket-aggregation-with-java-api-and-export-in-csv/314935 "2022-09-22T11:04:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rain\_Water](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rain_water/32/98764_2.png) [@Rain\_Water](https://discuss.elastic.co/u/Rain_Water)\
**Post date:** [September 22, 2022, 11:04am UTC](https://discuss.elastic.co/t/bucket-aggregation-with-java-api-and-export-in-csv/314935/1 "2022-09-22T11:04:50Z")

</div>

I have query below which i want to buckets result, i created the query with the help of grafana but im not able to convert it into java program.

```auto
GET /_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "@timestamp": {
              "gte": 1663666935141,
              "lte": 1663738634000,
              "format": "epoch_millis"
            }
          }
        },
        {
          "query_string": {
            "analyze_wildcard": true,
            "query": "service.name:*itm*"
          }
        }
      ]
    }
  },
  "aggs": {
    "terms_by": {
      "terms": {
        "field": "labels.client_id",
        "size": 10000,
        "order": {
          "_key": "desc"
        },
        "min_doc_count": 1
      },
      "aggs": {
        "sum_rowcount": {
          "sum": {
            "field": "labels.row_count"
          }
        }
      }
    }
  }
}

```

an example output i want is

```auto
{
   "client_id": ...,
   "row_count": ...,
   "DateTime": ...
}

```

Basically i want to convert this query into java program which help me export the data and write it into csv  
Thanks

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [October 20, 2022, 11:05am UTC](https://discuss.elastic.co/t/bucket-aggregation-with-java-api-and-export-in-csv/314935/2 "2022-10-20T11:05:43Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
