Skip to content

Instantly share code, notes, and snippets.

@erasmas
Last active September 21, 2016 17:46
Show Gist options
  • Save erasmas/259ab184707f0463ace5fcaa3cad7df4 to your computer and use it in GitHub Desktop.
Save erasmas/259ab184707f0463ace5fcaa3cad7df4 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -e
PN=`basename "$0"`
host=localhost
port=8983
collection=
inputfile=
usage () {
echo "$PN - upload JSON file to Solr
Usage: $PN -f filename -c collection [-h host] [-p port]"
exit 1
}
msg () {
for i
do echo "$PN: $i" >&2
done
}
fatal () { msg "$@"; exit 1; }
validate_args() {
# validate input file log
[ -n "$inputfile" ] || fatal "please provide input JSON file"
[ -r "$inputfile" ] || fatal "cannot read input file: $inputfile"
[ -n "$collection" ] || fatal "please provide collection name"
}
if [ $# -eq 0 ]
then usage
fi
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-h|--host)
host="$2"; shift;;
-p|--port)
port="$2"; shift;;
-c|--collection)
collection=$"$2"; shift;;
-f|--file)
inputfile="$2"; shift;;
--help)
usage;;
-*)
usage;;
*) break;;
esac
shift
done
validate_args
echo "Uploading JSON to Solr ..."
echo "Query: http://$host:$port/solr/$collection/update/json/docs"
echo "File: $inputfile"
echo
curl --fail "http://$host:$port/solr/$collection/update/json/docs" \
-H 'Content-type:application/json' \
--data-binary @"$inputfile"
echo
echo "Done!"
#!/usr/bin/env bash
set -e
PN=`basename "$0"`
host=localhost
port=8983
limit=1000
collection=
outfile=
usage () {
echo "$PN - exports documents from Solr collection in JSON format
Usage: $PN -o filename -c collection [-h host] [-p port]"
exit 1
}
msg () {
for i
do echo "$PN: $i" >&2
done
}
fatal () { msg "$@"; exit 1; }
is_pos_integer () {
case "$1" in
^[0-9]+$)
return 0;;
*) return 1;;
esac
}
validate_args () {
# validate input file log
[ -n "$outfile" ] || fatal "please provide output JSON filename"
[ -n "$collection" ] || fatal "please provide collection name"
[ "$limit" -gt 0 ] || fatal "invalid limit: $limit"
}
if [ $# -eq 0 ]
then usage
fi
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-h|--host)
host="$2"; shift;;
-p|--port)
port="$2"; shift;;
-c|--collection)
collection="$2"; shift;;
-l|--limit)
limit="$2"; shift;;
-o|--file)
outfile="$2"; shift;;
--help)
usage;;
-*)
usage;;
*) break;;
esac
shift
done
validate_args
echo
echo "Exporting Solr collection ..."
echo "Query: http://$host:$port/solr/$collection/select?q=*%3A*&wt=json&indent=true&rows=$limit"
echo
response=$(curl --fail -sS "http://$host:$port/solr/$collection/select?q=*%3A*&wt=json&indent=true&rows=$limit")
if [[ $? != 0 ]]
then
echo "Export failed: $response"
exit 1
fi
echo $response | jq '.response.docs' > $outfile
num=$(cat $outfile | jq 'length')
echo
echo "Exported $num documents to $outfile."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment