#!/bin/bash start_time=$(date +%s.%3N) if [[ $1 =~ ^select.* ]]; then json='{"query": "'$1'", "fetch_size": '$2' }' else json='{"query": "select * from '$1'", "fetch_size": '$2' }' fi res=`curl -s -XPOST http://localhost:9200/_plugins/_sql -H 'Content-Type: application/json' -d "$json"` requests=1 totalRows=0 for (( ; ; )) do rows=$(($(echo $res | jq '.datarows' | grep -Fc '[') - 1)) totalRows=$((totalRows + $rows)) cursor=$(echo $res | jq -r .cursor) if [ $cursor = 'null' ]; then break; fi res=`curl -s -XPOST http://localhost:9200/_plugins/_sql -H 'Content-Type: application/json' -d '{"cursor": "'$cursor'" }'` requests=$((requests + 1)) echo Got $rows rows done end_time=$(date +%s.%3N) elapsed=$(echo "scale=3; $end_time - $start_time" | bc) echo Fetched $totalRows rows in $requests requests in ${elapsed}s