-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathdatawave
executable file
·637 lines (528 loc) · 21.8 KB
/
datawave
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
#!/bin/bash
function setColors() {
readonly RED="\033[31m"
readonly GREEN="\033[32m"
readonly YELLOW="\033[33m"
readonly BLUE="\033[34m"
readonly RESET_COLOR="\033[m"
}
function printRed() {
echo -ne "${RED}${1}${RESET_COLOR}"
}
function printYellow() {
echo -ne "${YELLOW}${1}${RESET_COLOR}"
}
function printGreen() {
echo -ne "${GREEN}${1}${RESET_COLOR}"
}
function printBlue() {
echo -ne "${BLUE}${1}${RESET_COLOR}"
}
function info() {
log "[$( printGreen "DW-INFO" )] - ${1}"
}
function warn() {
log "[$( printYellow "DW-WARN" )] - ${1}"
}
function error() {
errlog "[$( printRed "DW-ERROR" )] - ${1}"
}
function fatal() {
errlog "[$( printRed "DW-FATAL" )] - ${1}"
errlog "Aborting $( basename "$0" )"
exit 1
}
function log() {
if [ "${QUIET_MODE}" != true ] ; then
echo -e "${1}"
fi
}
function errlog() {
if [ "${QUIET_MODE}" != true ] ; then
echo -e "${1}" >&2
fi
}
function setTestUserIdentity() {
# Converts DW_CLIENT_TEST_CERT (i.e., testUser.p12) to PEM
[ -z "${DW_CLIENT_TEST_CERT}" ] && error "DW_CLIENT_TEST_CERT variable must be set in order to configure curl" && return 1
[ ! -f "${DW_CLIENT_TEST_CERT}" ] && error "Certificate not found: ${DW_CLIENT_TEST_CERT}" && return 1
readonly TEST_PKI_DIR="${DW_CLIENT_PKI_DIR:-/tmp}"
readonly TEST_CERT="${TEST_PKI_DIR}/testUser.pem"
readonly TEST_KEY="${TEST_PKI_DIR}/testUser.key"
readonly TEST_KEY_RSA="${TEST_PKI_DIR}/testUser.key.rsa"
readonly TEST_CA="${TEST_PKI_DIR}/testUser.ca"
local OPENSSL="$( which openssl )" && [ -z "${OPENSSL}" ] && error "OpenSSL executable not found" && return 1
if [ ! -f "${TEST_KEY}" ] ; then
! ${OPENSSL} pkcs12 -passin "env:DW_CLIENT_TEST_CERT_PASS" -passout "env:DW_CLIENT_TEST_CERT_PASS" -in "${DW_CLIENT_TEST_CERT}" -out "${TEST_KEY}" -nocerts > /dev/null 2>&1 && error "Key creation failed!" && return 1
chmod 600 "${TEST_KEY}"
fi
if [ ! -f "${TEST_KEY_RSA}" ] ; then
! ${OPENSSL} rsa -passin "env:DW_CLIENT_TEST_CERT_PASS" -in "${TEST_KEY}" -out "${TEST_KEY_RSA}" > /dev/null 2>&1 && error "RSA key creation failed!" && return 1
chmod 600 "${TEST_KEY_RSA}"
fi
if [ ! -f "${TEST_CERT}" ] ; then
! ${OPENSSL} pkcs12 -passin "env:DW_CLIENT_TEST_CERT_PASS" -in "${DW_CLIENT_TEST_CERT}" -out "${TEST_CERT}" -clcerts -nokeys > /dev/null 2>&1 && error "Certificate creation failed!" && return 1
chmod 600 "${TEST_CERT}"
fi
if [ ! -f "${TEST_CA}" ] ; then
! ${OPENSSL} pkcs12 -passin "env:DW_CLIENT_TEST_CERT_PASS" -in "${DW_CLIENT_TEST_CERT}" -out "${TEST_CA}" -cacerts -nokeys > /dev/null 2>&1 && error "CA cert creation failed!" && return 1
chmod 600 "${TEST_CA}"
fi
readonly TEST_USER_CURL_CMD="${DW_CURL} --insecure --cert ${TEST_CERT} --key ${TEST_KEY_RSA} --cacert ${TEST_CA}}"
return 0
}
function setCurlConfig() {
DW_CURL=${DW_CURL:-$(which curl)}
DW_CLIENT_PKI_DIR="${DW_CLIENT_PKI_DIR:-${DW_CLIENT_HOME}/pki}"
if [ ! -d "${DW_CLIENT_PKI_DIR}" ] ; then
! mkdir -p "${DW_CLIENT_PKI_DIR}" && error "Failed to create client PKI directory" && return 1
chmod 700 "${DW_CLIENT_PKI_DIR}"
fi
readonly DW_CLIENT_TEST_USER_ENABLED=${DW_CLIENT_TEST_USER_ENABLED:-true}
if [ "${DW_CLIENT_TEST_USER_ENABLED}" == true ] ; then
setTestUserIdentity || return 1
fi
# If the test cert is being used, then the default 'testUser' curl command below will suffice.
# Otherwise, you'll need to override the DW_BASE_CURL_CMD variable for your own setup
if [[ -z "${DW_BASE_CURL_CMD}" && -z "${TEST_USER_CURL_CMD}" ]] ; then
error "Neither DW_BASE_CURL_CMD nor TEST_USER_CURL_CMD is set. Please resolve before continuing"
return 1
fi
readonly DW_BASE_CURL_CMD="${DW_BASE_CURL_CMD:-${TEST_USER_CURL_CMD}}"
# Additional curl options, required to reliably interpret web service responses
readonly CURL_CMD_FINAL="${DW_BASE_CURL_CMD} --silent --write-out 'HTTP_STATUS_CODE:%{http_code};TOTAL_TIME:%{time_total};CONTENT_TYPE:%{content_type}'"
return 0
}
function setEnv() {
readonly DW_CLIENT_HOME="${DW_CLIENT_HOME:-"$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"}"
[ ! -d "${DW_CLIENT_HOME}" ] && error "DW_CLIENT_HOME directory not found: ${DW_CLIENT_HOME}" && return 1
readonly CONTENT_TYPE_JSON=application/json
readonly CONTENT_TYPE_XML=application/xml
readonly ACCEPT_DEFAULT=${DW_CLIENT_ACCEPT_DEFAULT:-${CONTENT_TYPE_JSON}}
readonly DW_BASE_URI=${DW_BASE_URI:-"https://localhost:8443/DataWave"}
readonly DW_QUERY_URI=${DW_QUERY_URI:-${DW_BASE_URI}/Query}
readonly DW_CLIENT_DEFAULT_QUERY_AUTHS=${DW_CLIENT_DEFAULT_QUERY_AUTHS:-"BAD_AUTH1,BAD_AUTH2"}
defineQueryOptions
! setCurlConfig && error "Curl setup encountered errors. Please resolve this issue before continuing" && return 1
return 0
}
function urlencode() {
# Url encodes ${1}
local LANG=C i c e=''
for (( i=0; i < ${#1}; i++ )); do
c=${1:$i:1}
[[ "$c" =~ [a-zA-Z0-9\.\~\_\-] ]] || printf -v c '%%%02X' "'$c"
e+="$c"
done
echo "$e"
}
function autoClose204() {
logResponseMeta
if [ "${QUERY_OP}" != newQuery ] ; then
info "End of result set, as indicated by 204 response. Closing query automatically"
logCurlCommand
[ "${SHOW_CURL}" == true ] && local showCurlOpt="${SHOW_CURL_OPT}"
[ "${SHOW_META}" == true ] && local showMetaOpt="${SHOW_META_OPT}"
[ "${QUIET_MODE}" == true ] && local quietOpt="${QUIET_MODE_OPT}"
query --close ${QUERY_ID} ${showMetaOpt} ${showCurlOpt} ${quietOpt} || return 1
fi
return 0
}
function query() {
QUERY_LOGIC="EventQuery"
QUERY_AUTHS="${DW_CLIENT_DEFAULT_QUERY_AUTHS}"
QUERY_NAME="Query_$(date +%Y%m%d%H%M%S)"
QUERY_SYNTAX="LUCENE"
QUERY_BEGIN="19700101"
QUERY_END="20990101"
QUERY_LOG_VIZ="${QUERY_LOG_VIZ:-BAR&FOO}"
QUERY_PAGESIZE=10
QUERY_CREATE_MODE="createAndNext"
QUERY_OP="newQuery"
QUERY_EXPRESSION=""
QUERY_EXTRA_PARAMS=""
REQUEST_HEADERS=""
RESPONSE_BODY=""
RESPONSE_CODE=""
RESPONSE_TYPE=""
REQUEST_TOTAL_TIME=""
SHOW_CURL=false
SHOW_META=false
QUIET_MODE=false
configureRequest "${@}" || return $?
QUERY_API_CMD="$( eval ${QUERY_OP} )"
local dwResponse="$( eval "${QUERY_API_CMD}" )"
local curlrc=$?
if [ "${curlrc}" != "0" ] ; then
error "Curl command exited with non-zero status: ${curlrc}"
return ${curlrc}
fi
parseWebServiceResponse
if [ -n "${RESPONSE_BODY}" ] ; then
prettyPrintResponse
if [ "${QUERY_OP}" == newQuery ] ; then
logQueryId
fi
else
if [ "${RESPONSE_CODE}" == 204 ] ; then
autoClose204
return 0
fi
fi
logCurlCommand
logResponseMeta
if [[ ! "${RESPONSE_CODE}" =~ ^20[0,4]$ ]] ; then
warn "Unexpected response status: ${RESPONSE_CODE}"
return 1
fi
return 0
}
function queryIdIsValid() {
[ -z "${QUERY_ID}" ] && error "QUERY_ID cannot be null" && return 1
[ -z "$( echo "${QUERY_ID}" | grep -E '^[a-zA-Z0-9\-]+$' )" ] && error "'${QUERY_ID}' is not a valid query id" && return 1
return 0
}
function newQuery() {
echo "${CURL_CMD_FINAL} \
--header 'Content-Type: application/x-www-form-urlencoded' --header 'Accept: ${ACCEPT_DEFAULT}' \
${REQUEST_HEADERS} ${CURL_DATA} -X POST ${DW_BASE_URI}/Query/${QUERY_LOGIC}/${QUERY_CREATE_MODE}"
}
function closeQuery() {
echo "${CURL_CMD_FINAL} -X PUT ${DW_BASE_URI}/Query/${QUERY_ID}/close"
}
function nextPage() {
echo "${CURL_CMD_FINAL} --header 'Accept: ${ACCEPT_DEFAULT}' ${REQUEST_HEADERS} -X GET ${DW_BASE_URI}/Query/${QUERY_ID}/next"
}
function parseWebServiceResponse() {
RESPONSE_BODY=$( echo ${dwResponse} | sed -e 's/HTTP_STATUS_CODE\:.*//g' )
RESPONSE_CODE=$( echo ${dwResponse} | tr -d '\n' | sed -e 's/.*HTTP_STATUS_CODE://' | sed -e 's/;TOTAL_TIME\:.*//' )
RESPONSE_TYPE=$( echo ${dwResponse} | tr -d '\n' | sed -e 's/.*CONTENT_TYPE://' )
REQUEST_TOTAL_TIME=$( echo ${dwResponse} | tr -d '\n' | sed -e 's/.*TOTAL_TIME://' | sed -e 's/;CONTENT_TYPE\:.*//' )
}
function logCurlCommand() {
if [ "${SHOW_CURL}" == true ] ; then
log "$( printGreen "Curl Command" ): ${QUERY_API_CMD}"
fi
}
function logResponseMeta() {
if [ "${SHOW_META}" == true ] ; then
local responseCodeLabel="$( printGreen "Response Code" )"
if [[ ! "${RESPONSE_CODE}" =~ ^20[0,4]$ ]] ; then
responseCodeLabel="$( printRed "Response Code" )"
fi
log "$( printGreen "Time" ): ${REQUEST_TOTAL_TIME} ${responseCodeLabel}: ${RESPONSE_CODE} $( printGreen "Response Type" ): ${RESPONSE_TYPE:-N/A}"
fi
}
function logQueryId() {
QUERY_ID=""
if [ "${RESPONSE_TYPE}" == "${CONTENT_TYPE_JSON}" ] ; then
setQueryIdFromResponseJson
elif [ "${RESPONSE_TYPE}" == "${CONTENT_TYPE_XML}" ] ; then
setQueryIdFromResponseXml
else
warn "I don't know how to parse the query id from this response type: ${RESPONSE_TYPE}"
fi
if [ "${SHOW_META}" == true ] ; then
log "$( printGreen "Query ID" ): ${QUERY_ID:-N/A}"
fi
}
function setQueryIdFromResponseXml() {
local id="$( echo "${RESPONSE_BODY}" | sed -e 's~<[?]xml .*><QueryId>\(.*\)</QueryId>.*~\1~' | sed -e 's~<[?]xml .*><Result .*>\(.*\)</Result>.*~\1~' )"
id="$( echo ${id} | grep -E '^[a-zA-Z0-9\-]+$' )"
[ -n "${id}" ] && QUERY_ID=${id}
}
function setQueryIdFromResponseJson() {
local id="$( echo "${RESPONSE_BODY}" | sed -e 's~.*"QueryId":"\([a-zA-Z0-9\-]\+\)".*~\1~' | sed -e 's~.*"Result":"\([a-zA-Z0-9\-]\+\)"}.*~\1~' )"
id="$( echo ${id} | grep -E '^[a-zA-Z0-9\-]+$' )"
[ -n "${id}" ] && QUERY_ID=${id}
}
function prettyPrintResponse() {
if [ "${RESPONSE_TYPE}" == "${CONTENT_TYPE_JSON}" ] ; then
prettyPrintJson
elif [ "${RESPONSE_TYPE}" == "${CONTENT_TYPE_XML}" ] ; then
prettyPrintXml
else
echo "${RESPONSE_BODY}"
fi
}
function prettyPrintJson() {
local PY=$( which python )
if [ -n "${PY}" ] ; then
echo "${RESPONSE_BODY}" | ${PY} -c 'import sys,json;data=json.loads(sys.stdin.read()); print json.dumps(data, indent=2, sort_keys=True)'
local exitStatus=$?
if [ "${exitStatus}" != "0" ] ; then
echo "${RESPONSE_BODY}"
warn "Python encountered error (${exitStatus}). Printed response without formatting"
fi
else
echo "${RESPONSE_BODY}"
warn "Couldn't find python in your environment. Printed response without formatting"
fi
}
function prettyPrintXml() {
local XMLLINT=$( which xmllint )
if [ -n "${XMLLINT}" ] ; then
echo "${RESPONSE_BODY}" | ${XMLLINT} --format -
local exitStatus=$?
if [ "${exitStatus}" != "0" ] ; then
echo "${RESPONSE_BODY}"
warn "xmllint encountered error (${exitStatus}). Printed response without formatting"
fi
else
echo "${RESPONSE_BODY}"
warn "Couldn't find xmllint in your environment. Printed response without formatting"
fi
}
function setCurlData() {
CURL_DATA="" ; for param in "${@}" ; do
CURL_DATA="${CURL_DATA} -d ${param}"
done
}
function defineQueryOptions() {
readonly QUERY_EXPR_OPT="--expression"
readonly QUERY_EXPR_OPT_S="-E"
readonly QUERY_LOGIC_OPT="--logic"
readonly QUERY_LOGIC_OPT_S="-l"
readonly QUERY_BEGIN_OPT="--begin"
readonly QUERY_BEGIN_OPT_S="-b"
readonly QUERY_END_OPT="--end"
readonly QUERY_END_OPT_S="-e"
readonly QUERY_LOG_VIZ_OPT="--log-visibility"
readonly QUERY_AUTHS_OPT="--auths"
readonly QUERY_AUTHS_OPT_S="-a"
readonly QUERY_SYNTAX_OPT="--syntax"
readonly QUERY_SYNTAX_OPT_S="-s"
readonly QUERY_PAGESIZE_OPT="--pagesize"
readonly QUERY_PAGESIZE_OPT_S="-p"
readonly QUERY_CREATE_MODE_OPT="--create-only"
readonly QUERY_CREATE_MODE_OPT_S="-C"
readonly QUERY_NEXT_PAGE_OPT="--next"
readonly QUERY_NEXT_PAGE_OPT_S="-n"
readonly QUERY_CLOSE_OPT="--close"
readonly QUERY_CLOSE_OPT_S="-c"
readonly ADD_QUERY_PARAM_OPT="--param"
readonly ADD_QUERY_PARAM_OPT_S="-P"
readonly ADD_REQUEST_HEADER_OPT="--header"
readonly ADD_REQUEST_HEADER_OPT_S="-H"
readonly REQUEST_XML_OPT="--xml"
readonly REQUEST_XML_OPT_S="-x"
readonly QUERY_HELP_OPT="--help"
readonly QUERY_HELP_OPT_S="-h"
readonly SHOW_CURL_OPT="--show-curl"
readonly SHOW_META_OPT="--show-meta"
readonly QUIET_MODE_OPT="--quiet"
}
function configureRequest() {
while [ "${1}" != "" ]; do
case "${1}" in
${QUERY_EXPR_OPT} | ${QUERY_EXPR_OPT_S})
QUERY_EXPRESSION="$( urlencode "${2}" )"
shift
;;
${QUERY_LOGIC_OPT} | ${QUERY_LOGIC_OPT_S})
QUERY_LOGIC="${2}"
shift
;;
${QUERY_BEGIN_OPT} | ${QUERY_BEGIN_OPT_S})
QUERY_BEGIN="${2}"
shift
;;
${QUERY_END_OPT} | ${QUERY_END_OPT_S})
QUERY_END="${2}"
shift
;;
${QUERY_LOG_VIZ_OPT})
QUERY_LOG_VIZ="${2}"
shift
;;
${QUERY_AUTHS_OPT} | ${QUERY_AUTHS_OPT_S})
QUERY_AUTHS="${2}"
shift
;;
${QUERY_SYNTAX_OPT} | ${QUERY_SYNTAX_OPT_S})
QUERY_SYNTAX="${2}"
shift
;;
${QUERY_PAGESIZE_OPT} | ${QUERY_PAGESIZE_OPT_S})
QUERY_PAGESIZE="${2}"
shift
;;
${QUERY_CREATE_MODE_OPT} | ${QUERY_CREATE_MODE_OPT_S})
QUERY_CREATE_MODE=create
;;
${QUERY_NEXT_PAGE_OPT} | ${QUERY_NEXT_PAGE_OPT_S})
QUERY_ID="${2}"
QUERY_OP=nextPage
shift
;;
${QUERY_CLOSE_OPT} | ${QUERY_CLOSE_OPT_S})
QUERY_ID="${2}"
QUERY_OP=closeQuery
shift
;;
${ADD_QUERY_PARAM_OPT} | ${ADD_QUERY_PARAM_OPT_S})
QUERY_EXTRA_PARAMS="${QUERY_EXTRA_PARAMS} ${2%%=*}=$( urlencode "${2#*=}" )"
shift
;;
${ADD_REQUEST_HEADER_OPT} | ${ADD_REQUEST_HEADER_OPT_S})
REQUEST_HEADERS="${REQUEST_HEADERS} ${1} '${2}'"
shift
;;
${REQUEST_XML_OPT} | ${REQUEST_XML_OPT_S})
REQUEST_HEADERS="${REQUEST_HEADERS} --header 'Accept: ${CONTENT_TYPE_XML}'"
;;
${QUERY_HELP_OPT} | ${QUERY_HELP_OPT_S})
queryHelp && return 1
;;
${QUIET_MODE_OPT})
QUIET_MODE=true
;;
${SHOW_CURL_OPT})
SHOW_CURL=true
;;
${SHOW_META_OPT})
SHOW_META=true
;;
*)
error "Invalid argument passed to $( basename "$0" ) query: ${1}" && return 1
esac
shift
done
if [ "$QUERY_OP" == newQuery ] ; then
[ -z "${QUERY_EXPRESSION}" ] && error "Query expression is required" && return 1
setCurlData \
"query=${QUERY_EXPRESSION}" \
"queryName=${QUERY_NAME}" \
"auths=${QUERY_AUTHS}" \
"begin=${QUERY_BEGIN}" \
"end=${QUERY_END}" \
"pagesize=${QUERY_PAGESIZE}" \
"query.syntax=${QUERY_SYNTAX}" \
"columnVisibility=$( urlencode "${QUERY_LOG_VIZ}" )" \
${QUERY_EXTRA_PARAMS}
else
! queryIdIsValid && return 1
fi
return 0
}
function queryHelp() {
echo
echo " The $( printGreen "query" ) shell function provides a curl-based client for submitting queries to DataWave's"
echo " Rest API. It sets reasonable defaults for most curl- and DataWave-specific parameters"
echo
echo " The function will return zero if status code 200 or 204 is received from DataWave, non-zero otherwise"
echo
echo " E.g., \$ $( basename "$0" ) $( printGreen query ) ${QUERY_EXPR_OPT} \"PAGE_TITLE:*Computing\""
echo " \$ $( basename "$0" ) $( printGreen query ) ${QUERY_NEXT_PAGE_OPT} 09aa3d46-8aa0-49fb-8859-f3add48859b0"
echo " \$ $( basename "$0" ) $( printGreen query ) ${QUERY_CLOSE_OPT} 09aa3d46-8aa0-49fb-8859-f3add48859b0"
echo
echo " $( printGreen ${QUERY_EXPR_OPT_S} ),$( printGreen ${QUERY_EXPR_OPT} ) \"<EXPRESSION>\""
echo " Query expression. LUCENE syntax is assumed by default, but you may override via $( printGreen ${QUERY_SYNTAX_OPT} ),$( printGreen ${QUERY_SYNTAX_OPT_S} ) option"
echo
echo " $( printGreen ${QUERY_LOGIC_OPT_S} ),$( printGreen ${QUERY_LOGIC_OPT} ) <logicName>"
echo " Specify the logic name to utilize for the query. Defaults to \"${QUERY_LOGIC}\""
echo
echo " $( printGreen ${QUERY_BEGIN_OPT_S} ),$( printGreen ${QUERY_BEGIN_OPT} ) \"yyyyMMdd[ HHmmss.SSS]\""
echo " Begin date for query's shard date range. Defaults to ${QUERY_BEGIN}"
echo
echo " $( printGreen ${QUERY_END_OPT_S} ),$( printGreen ${QUERY_END_OPT} ) \"yyyyMMdd[ HHmmss.SSS]\""
echo " End date for query's shard date range. Defaults to ${QUERY_END}"
echo
echo " $( printGreen ${QUERY_AUTHS_OPT_S} ),$( printGreen ${QUERY_AUTHS_OPT} ) A1,A2,A3,..."
echo " List of Accumulo auths to enable for the query. Only data with these auths will be returned"
echo " Defaults to \"${QUERY_AUTHS}\" to match those used on DataWave's sample datasets"
echo
echo " $( printGreen ${QUERY_SYNTAX_OPT_S} ),$( printGreen ${QUERY_SYNTAX_OPT} ) <syntax>"
echo " Identifies the query expression syntax being used. E.g., LUCENE, JEXL, etc. Defaults to ${QUERY_SYNTAX}"
echo
echo " $( printGreen ${QUERY_PAGESIZE_OPT_S} ),$( printGreen ${QUERY_PAGESIZE_OPT} ) <int>"
echo " Sets the page size to be used for each page of query results. Defaults to ${QUERY_PAGESIZE}"
echo
echo " $( printGreen ${QUERY_LOG_VIZ_OPT} ) <visibility-expression>"
echo " Visibility expression to use when logging this query to Accumulo. Defaults to '${QUERY_LOG_VIZ}'"
echo
echo " $( printGreen ${QUERY_CREATE_MODE_OPT_S} ),$( printGreen ${QUERY_CREATE_MODE_OPT} )"
echo " Uses the 'Query/{logic}/create' endpoint, rather than the default, 'Query/{logic}/createAndNext' which creates"
echo " query and gets first page w/ one request"
echo
echo " $( printGreen ${QUERY_NEXT_PAGE_OPT_S} ),$( printGreen ${QUERY_NEXT_PAGE_OPT} ) <QueryID> "
echo " Gets the next page of results for the specified query id. Response code 204 indicates end of result set"
echo
echo " $( printGreen ${QUERY_CLOSE_OPT_S} ),$( printGreen ${QUERY_CLOSE_OPT} ) <QueryID> "
echo " Releases all server side resources being utilized for the specified query id"
echo
echo " $( printGreen ${ADD_QUERY_PARAM_OPT_S} ),$( printGreen ${ADD_QUERY_PARAM_OPT} ) \"paramName=paramValue\""
echo " Adds the specified query parameter name/value to the 'create' or 'createAndNext' request"
echo
echo " $( printGreen ${ADD_REQUEST_HEADER_OPT_S} ),$( printGreen ${ADD_REQUEST_HEADER_OPT} ) \"HeaderName: HeaderValue\""
echo " Adds specified name/value pair to the curl command as an HTTP request header. Note that"
echo " '$(printGreen "Accept"): ${CONTENT_TYPE_JSON}' is added by default, but may be overridden via this option"
echo
echo " $( printGreen ${REQUEST_XML_OPT_S} ),$( printGreen ${REQUEST_XML_OPT} )"
echo " Adds '$( printGreen Accept ): ${CONTENT_TYPE_XML}' as an HTTP request header to override the default JSON"
echo
echo " $( printGreen ${QUIET_MODE_OPT} )"
echo " Suppress all output except for web service response body. Supersedes $( printGreen ${SHOW_CURL_OPT} ) and $( printGreen ${SHOW_META_OPT} )"
echo
echo " $( printGreen ${SHOW_CURL_OPT} )"
echo " Display the curl command. Superseded by $( printGreen ${QUIET_MODE_OPT} ) flag"
echo
echo " $( printGreen ${SHOW_META_OPT} )"
echo " Display web service response meta (query id, status code, content type, time elapsed). Superseded by $( printGreen ${QUIET_MODE_OPT} ) flag"
echo
echo " $( printGreen ${QUERY_HELP_OPT_S} ),$( printGreen ${QUERY_HELP_OPT} )"
echo " Print this usage information and exit"
echo
}
function reloadDataWaveTableCache() {
CACHED_META_TABLES=${CACHED_META_TABLES:-"datawave.metadata datawave.queryMetrics_m datawave.error_m"}
# This function will force a refresh of certain Accumulo table caches in Wildfly, i.e., those used to cache
# DataWave's data element dictionaries. Otherwise, you might be forced to bounce Wildfly or wait for the
# caches to reload automatically at the configured refresh interval.
# This is particularly useful when you want to query newly ingested data and search on new field names, which
# haven't yet arrived in the Wildfly cache.
# Note that the reload endpoint may perform the work asynchronously, so there could still be some delay
# before the refresh takes effect
local cachedTables=(${CACHED_META_TABLES})
for table in "${cachedTables[@]}" ; do
info "Reloading cache for metadata table: ${table}"
local curlcmd="${CURL_CMD_FINAL} -X GET ${DW_BASE_URI}/Common/AccumuloTableCache/reload/${table}"
local dwResponse="$( eval "${curlcmd}" )"
local curlrc=$?
if [ "${curlrc}" != "0" ] ; then
error "Curl command exited with non-zero status: ${curlrc}. Failed to update table cache: ${table}"
return 1
else
parseWebServiceResponse
logResponseMeta
fi
done
return 0
}
function usage() {
echo
echo " $( printGreen Usage: ) $( basename "$0" ) [OPTIONS]"
echo " $( printGreen Options: ) (all options are mutually exclusive)"
echo " -h,--help | Print this usage info and exit"
echo " query [OPTIONS] | View all query options: $( printGreen "$( basename "$0" ) query -h,--help" )"
echo " reload-cache | Force Wildfly to reload cached DataWave metadata tables (i.e, data dictionaries)"
echo
}
setColors
[ -z "${1}" ] && usage && exit 1
! setEnv && error "Environment setup failed" && exit 1
case "${1}" in
query)
shift
query "$@" || exit 1
;;
reload-cache)
reloadDataWaveTableCache || exit 1
;;
-h|--help)
usage
;;
*)
error "Invalid argument passed to $( basename "$0" ): ${1}" && usage && exit 1
esac
exit 0