Bucket aggregation with java api and export in csv

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.

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

{
   "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