How to generate csv response in elasticsearch?

后端 未结 2 1047
有刺的猬
有刺的猬 2020-12-20 05:01

As we know, the rest apis of Elasticsearch returns json response.But, I need CSV responses from those apis.

I

2条回答
  •  遥遥无期
    2020-12-20 05:52

    If you're open to use Logstash, then you can very easily do this with an elasticsearch input making the query and then a csv output for dumping the data into a CSV file. It'd look like this:

    input {
      elasticsearch {
        hosts => ["localhost:9200"]
        index => "your_index"
        query => '{"query": {"match_all": {}}}'
      }
    }
    output {
      csv {
        fields => ["field1", "field2", "field3"]
        path => "/path/to/file.csv"
      }
    }
    

    UPDATE

    If you need to invoke this dynamically, you could generate this logstash configuration dynamically based on a query that you'd give as input to the shell script:

    #!/bin/sh
    
    if [ -z "$LOGSTASH_HOME" ]; then
        echo "WARNING: The LOGSTASH_HOME environment variable is not set!"
        exit 0
    fi
    
    LS_CONF="input {
       elasticsearch {
         hosts => [\"localhost:9200\"]
         index => 'megacorp'
         query => '{\"query\":{\"query_string\": {\"query\": \"$1\"}}}'
       }
    }
    output {
       csv {
         fields => [$2]
         path => \"/path/to/file.csv\"
       }
    }"
    
    $LOGSTASH_HOME/bin/logstash -e "$LS_CONF"
    

    Then you can invoke that script with the query my_field:123456 like this

    ./es_to_csv.sh "my_field:123456" "field1,field2,field3"
    

    This will have the same effect as calling {{elasticUrl}}/_search?q=my_field:123456 and produce a CSV file with the columns field1,field2,field3

提交回复
热议问题