利用游标从es导出数据

#!/bin/bash
# 存放文件路径
FILE="./temp_es_data.txt"
# ES访问地址
HOST="xxx.xxx.xxx.xxx:11414"
# ES index
INDEX="api-gateway-prd-error-2023.02.19"
# ES type
TYPE="_doc"
#scroll数据
function scroll(){
    search_dsl='{
        "query": {
            "bool": {
                "must": [
                    {
                    "match": {
                        "app_id.keyword": "xxxxxx"
                    }
                    }
                ]
            }
        },
        "size": 100
    }'

    url="$HOST/$INDEX/_search?scroll=1m"
    json=$(curl -s -XGET $url -H "Content-Type: application/json" -d "$search_dsl")
    scroll_id=$(echo $json | jq -c -r "._scroll_id")
    hits=$(echo $json | jq -c ".hits.hits")
    length=$(echo $hits | jq length)
    echo "hits length is "$length

    for i in $(seq 1 $length)
    do
        index=$i-1
        message=$(echo $hits | jq -c ".[$index]._source.message")
        echo $message
        echo $message >> $FILE
    done

    scroll_dsl='{"scroll_id": "'"$scroll_id"'"}'


    counter=1
    while [ $counter -lt 100 ];
    do
        hits=$(curl -s -XGET "$HOST/_search/scroll?scroll=1m" -H "Content-Type: application/json" -d "$scroll_dsl" | jq -c ".hits.hits")

        if [ "$hits" == "" ];
        then
            echo "exit"
            exit
        fi

        length=$(echo $hits | jq length)
        echo "hits length is "$length
        for i in $(seq 1 $length)
        do
            index=$i-1
            message=$(echo $hits | jq -c ".[$index]._source.message")
            echo $message >> $FILE
        done

        echo "counter $counter"
        echo `date +%Y/%m/%d_%k:%M:%S`
        let counter+=1
    done
}


scroll
posted @ 2021-08-18 19:34  Sincere_Ye  阅读(165)  评论(0编辑  收藏  举报