0

below is my java client query in which im trying write my data in csv file i'm stuck on for loop not able to understand what should i parse in for loop can Below is my code

    public class ElasticQ {

    public static void main(String[] args) {
        RestClient restClient = RestClient.builder(
                new HttpHost("host", 1212, "http")
        ).build();

        ElasticsearchTransport transport = new RestClientTransport(
                restClient, new JacksonJsonpMapper());

        ElasticsearchClient client = new ElasticsearchClient(transport);


        try {
            var sub3TermAggregation = new Aggregation.Builder()
                    .sum(new SumAggregation.Builder()
                            .field("labels.row_count").build())
                    .build();

            var sub4Aggregation = new Aggregation.Builder()
                    .terms(new TermsAggregation.Builder()
                            .field("labels.job_id")
                            .size(100000)
                            //   .order(List.of("_key", SortOrder.Desc))
                            .minDocCount(1)
                            .build())
                    .aggregations("3", sub3TermAggregation)
                    .build();

            var aggregation = new Aggregation.Builder()
                    .terms(new TermsAggregation.Builder()
                            .field("labels.client_id")
                            .size(100000)
                            //     .order(List.of("_key", SortOrder.Desc))
                            .minDocCount(1)
                            .build())
                    .aggregations("4", sub4Aggregation)
                    .build();

            var boolquery = BoolQuery
                    .of(b -> b.filter(Query
                            .of(q -> q.range(RangeQuery
                                    .of(r -> r.field("@timestamp")
                                            .gte(JsonData.of("1664755200000"))
                                            .lte(JsonData.of("1664787600000"))
                                            .format("epoch_millis")
                                    ))
                            )).filter(f -> f.queryString(QueryStringQuery.of(qs -> qs
                            .query("service.name:*itm*").analyzeWildcard(true))))
                    );
            var query = Query.of(q -> q.bool(boolquery));

            var searchRequest = new SearchRequest.Builder()
                    .index("*itm*")
                    .size(0)
                    .query(query)
                    .aggregations("2", aggregation)
                    .build();

            var response = client.search(searchRequest, Void.class);    
            var aggs2 = response.aggregations().get("2");

            if (Objects.nonNull(aggs2)) {
                var buckets = aggs2.sterms().buckets().array();

                for (StringTermsBucket bucket : buckets) {
                    var subAggs4 = bucket.aggregations().get("4");



                    subAggs4.sterms().buckets().array().forEach(agg3Bucket -> {


                        try {
                            FileWriter fWriter = new FileWriter(
                                    "/home/rain/Downloads/ElasticNestedQuery-main/demo.csv");


                            for () {
                                System.out.println(bucket.key() + bucket.docCount() + ","
                                        + agg3Bucket.key() + agg3Bucket.docCount() + ","
                                        + agg3Bucket.aggregations().get("3").sum().value());

                            }

                            fWriter.close();
                        } catch (IOException e) {
                            throw new RuntimeException(e);
                        }

                        System.out.println(bucket.key() + bucket.docCount() + "," +
                                 agg3Bucket.key() + agg3Bucket.docCount() + ","  + agg3Bucket.aggregations().get("3").sum().value());
                    });
                }

            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

this is how my result of query look like

86206181,12157261,2.0 86206181,12157271,3318.0 86206181,12157301,3369.0 86206181,12157311,3376.0

now i just need to write it down in csv file can some one help me here

Rain
  • 1
  • There are many CSV parser library available in Java (example: Apache Commons CSV, OpenCSV, jackson-dataformat-csv, SimpleFlatMapper CSV parser, uniVocity-parsers, deephaven-csv) that you can use to write CSV file. – Paul Marcelin Bejan Oct 28 '22 at 10:00

0 Answers0