-
Notifications
You must be signed in to change notification settings - Fork 3
/
load-es.py
executable file
·152 lines (119 loc) · 3.55 KB
/
load-es.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/env python
"""Load json data into elasticsearch
Should run this first:
curl -XDELETE 'http://localhost:9200/pivot-demo/'
curl -XPUT "http://localhost:9200/pivot-demo/" -d'
{
"mappings": {
"entry": {
"properties": {
"gender": {
"type": "string"
},
"age": {
"type": "integer"
},
"household_income": {
"type": "integer"
},
"favorite_color": {
"type": "string",
"index": "not_analyzed"
},
"QS6_6": {
"type": "string",
"index": "not_analyzed"
},
"Q30b": {
"type": "string",
"index": "not_analyzed"
},
"Q31_1": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}'
After loading data, a sample query:
curl -XPOST "http://localhost:9200/pivot-demo/entry/_search" -d'{
"query": {
"filtered" : {
"filter" : {
"and" : [
{
"range" : {"age" : {"from" : "18", "to" : "20"} }
},
{
"match" : { "gender" : "F" }
}
]
}
}
},
"aggregations": {
"favorite_colors": {
"terms": {
"field": "favorite_color"
}
}
}
}' | jq .
"""
import sys
from optparse import OptionParser
import fileinput
import logging
from elasticsearch import Elasticsearch, helpers
import json
# Default paramaters to be used by option parser
DEFAULT_INDEX = "pivot-demo"
# Set up logging
log = logging.getLogger(__name__)
hdlr = logging.StreamHandler()
log_formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
hdlr.setFormatter(log_formatter)
log.addHandler(hdlr)
# Function and Classes go here
def set_log_level(verbosity):
"""
Set the log level based on an integer between 0 and 2
"""
log_level = logging.WARNING # default
if verbosity == 1:
log_level = logging.INFO
elif verbosity >= 2:
log_level = logging.DEBUG
log.setLevel(level=log_level)
def get_options():
"""
Returns the options and arguments passed to this script on the commandline
@return: (options,args)
"""
usage = "usage: %prog [options] file1 file2 ..."
parser = OptionParser(usage)
parser.add_option("-i", "--index-a", dest="index", default=DEFAULT_INDEX,
help="The name of the index Default: [default: %default]")
parser.add_option('-v', '--verbose', dest='verbose', action='count',
help="Increase verbosity (specify multiple times for more)")
options, args = parser.parse_args()
return (options, args)
def main(args):
(options, args) = get_options()
set_log_level(options.verbose)
log.debug("Starting with options: %s" % (options))
entries = []
# Accept lines of input from a file specified in the args, or stdin
for line in (l.strip() for l in fileinput.input(args)):
entry = json.loads(line)
# Modify the entry for elasticsearch
entry['_type'] = "entry"
entry['_index'] = options.index
entry['_id'] = entry['id']
del entry['id']
entries.append(entry)
es = Elasticsearch()
helpers.bulk(es, entries)
if __name__ == '__main__':
sys.exit(main(sys.argv[1:]))