Last active
July 8, 2016 20:46
-
-
Save haginara/6f36c164983a09fb8bce to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import print_function | |
import json | |
import sys | |
import pprint | |
import argparse | |
import cStringIO | |
import tarfile | |
parser = argparse.ArgumentParser() | |
parser.add_argument("-g", "--grep") | |
parser.add_argument("-f", "--field") | |
parser.add_argument("-z", "--gunzip") | |
parser.add_argument("-r", "--raw", action='store_true') | |
args = parser.parse_args() | |
def _json_print(s): | |
print(json.dumps(s)) | |
json_print = _json_print if args.raw else pprint.pprint | |
for line in sys.stdin: | |
try: | |
jsonstream = json.loads(line) | |
if args.grep: | |
if args.grep in line: | |
json_print(jsonstream) | |
elif args.field: | |
if '=' in args.field: | |
kv = args.field.split('=') | |
if len(kv) != 2: | |
raise Exception("No key-value") | |
k, v = kv | |
data = jsonstream | |
data = data[k] | |
if data == v: | |
json_print(jsonstream) | |
else: | |
fields = args.field.split('.') | |
data = jsonstream | |
for field in fields: | |
data = data[field] | |
json_print(data) | |
else: | |
json_print(jsonstream) | |
except Exception as e: | |
continue |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
usage
Get size of the specific index on Elasticsearch