Skip to content

Instantly share code, notes, and snippets.

@saihoooooooo
Last active August 18, 2016 07:49
Show Gist options
  • Save saihoooooooo/1f016f0cb98f02666bf658419386b001 to your computer and use it in GitHub Desktop.
Save saihoooooooo/1f016f0cb98f02666bf658419386b001 to your computer and use it in GitHub Desktop.
kinesis-cliで指定したシーケンスを一件だけ取得
#!/bin/bash
PROGNAME=$(basename $0)
usage() {
echo "Usage: $PROGNAME <stream-name> <sequence-number> [<shard-id>] [--region <region>] [--profile <profile>]"
exit 1
}
REGION=
PROFILE=
for OPT in "$@"
do
case "$OPT" in
'-h'|'--help' )
usage
exit 1
;;
'--shard-id' )
if [[ -z "$2" ]] || [[ "$2" =~ ^-+ ]]; then
echo "エラー:「$1」オプションには引数が必要です" 1>&2
usage
exit 1
fi
SHARD_ID="$2"
shift 2
;;
'--region' )
if [[ -z "$2" ]] || [[ "$2" =~ ^-+ ]]; then
echo "エラー:「$1」オプションには引数が必要です" 1>&2
usage
exit 1
fi
REGION="--region $2"
shift 2
;;
'--profile' )
if [[ -z "$2" ]] || [[ "$2" =~ ^-+ ]]; then
echo "エラー:「$1」オプションには引数が必要です" 1>&2
usage
exit 1
fi
PROFILE="--profile $2"
shift 2
;;
-*)
;;
*)
if [[ ! -z "$1" ]] && [[ ! "$1" =~ ^-+ ]]; then
param+=( "$1" )
shift 1
fi
;;
esac
done
if [ "${#param[@]}" -lt 2 ]; then
echo "エラー:引数の数が正しくありません。" 1>&2
usage
exit 1
fi
STREAM_NAME="${param[0]}"
SEQUENCE_NUMBER="${param[1]}"
[ ! -z "${param[2]}" ] && SHARD_ID="${param[2]}" || SHARD_ID=shardId-000000000000
SHARD_ITERATOR=$(aws kinesis get-shard-iterator --stream-name $STREAM_NAME --shard-id $SHARD_ID --shard-iterator-type AT_SEQUENCE_NUMBER --starting-sequence-number $SEQUENCE_NUMBER --query 'ShardIterator' $REGION $PROFILE)
if [ -n "$SHARD_ITERATOR" ]; then
aws kinesis get-records --shard-iterator $SHARD_ITERATOR $REGION $PROFILE --limit 1 | jq .Records[].Data | xargs echo | base64 -d | jq .
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment