-
Notifications
You must be signed in to change notification settings - Fork 0
/
webhdfs_lib.sh
1684 lines (1576 loc) · 50.2 KB
/
webhdfs_lib.sh
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
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
CONF_FILE="/home/oracle/webhdfs/settings.conf"
if [ -f "$CONF_FILE" ]
then
. "$CONF_FILE"
else
echo "Config-file ${CONF_FILE} was not found;"
exit 1
fi
if [ ! -e "$LOG_FILE" ]
then
touch "$LOG_FILE" 1>/dev/null 2>&1
if [ "$?" -ne "0" ]
then
echo "Can not create log-file: ${LOG_FILE}"; exit 1;
fi
fi
RUNID=`date +%s.%N`
[ ! -z "$SILENT" ] && CONSOLE_OUTPUT="N"
V_PWD=$($OPR -r "$V_OPRATTR" "$V_LOGIN" | tr -d [:cntrl:])
[ "$?" -ne "0" ] && {
echo "can not obtain passwd for logopass to hdfs;"
echo "obtaining was executed as: ${OPR} -r ${V_OPRATTR} ${V_LOGIN}"
echo "exiting with error"
return 1
}
#====================================================
#== Misc ==
log_info() {
local datetime=`date +%Y.%m.%d:%H.%M.%S`
local data_source="$2"
local v_x
if [ "$data_source" == "logfile" ]
then
[ "$CONSOLE_OUTPUT" == "Y" ] && cat "$1" | awk -v runid=$RUNID '{print runid": "$0}'
cat "$1" | awk -v runid=$RUNID '{print runid": "$0}' >> $LOG_FILE
else
v_x=${1//${V_PWD}/"..."}
[ -e "$LOG_FILE" ] && echo "${RUNID}:${datetime}: ${v_x}" >> $LOG_FILE
[ "$CONSOLE_OUTPUT" == "Y" ] && echo "${RUNID}:${datetime}: ${v_x}"
fi
if [ "$FOLLOW_LINE_LIMITS" == "Y" ]
then
v_x=`cat $LOG_FILE | wc -l`
if [ "$v_x" -gt "$LOG_FILE_LINES_LIMIT" ]
then
v_x=`echo $v_x-$LOG_FILE_LINES_LIMIT | bc`
sed -i "1,${v_x}d" $LOG_FILE
fi
fi
}
#== Python modules =======================================================
parse_liststatus() {
local v_module="parse_liststatus"
#log_info "${v_module} 1: >${1}<"
if [ ! -z "$1" ]
then
$PYTHON << __EOF__
import sys, json
json_string="""$1"""
parsed_string = json.loads(json_string)
if 'FileStatuses' in parsed_string:
parsed_string=parsed_string['FileStatuses']['FileStatus']
for i in range(len(parsed_string)):
if i == 0:
print('%s\t%s\t%s\t%s\t%s' % ("type", "permission", "mTime", "bytes", "name"))
f=parsed_string[i]
itype="FILE"
if f['type'] == "DIRECTORY":
itype="DIR"
print('%s %s %s %s %s' % (itype, f['permission'], f['modificationTime'], f['length'], f['pathSuffix']))
sys.exit(0)
elif 'RemoteException' in parsed_string:
parsed_string=parsed_string['RemoteException']
msg="RemoteException"
if 'exception' in parsed_string:
msg=msg+': '+parsed_string['exception']
if 'message' in parsed_string:
msg=msg+' '+parsed_string['message']
print('%s' % (msg))
sys.exit(1)
else:
msg="Unknown json from hdfs"
print('%s' % (msg))
sys.exit(2)
__EOF__
return "$?"
fi
return "0"
}
parse_getfilestatus() {
local rc v_msg
if [ ! -z "$1" ]
then
$PYTHON << __EOF__
import sys, json
json_string="""$1"""
parsed_string = json.loads(json_string)
if "RemoteException" in parsed_string:
msg="error: "
if "exception" in parsed_string['RemoteException']:
msg=msg+" "+parsed_string['RemoteException']['exception']
if "message" in parsed_string['RemoteException']:
msg=msg+parsed_string['RemoteException']['message']
print('%s' % (msg))
sys.exit(1)
elif 'FileStatus' in parsed_string:
parsed_string=parsed_string['FileStatus']
msg=""
for k, v in parsed_string.iteritems():
msg=msg+" "+str(k)+": "+str(v)+"\n"
print('%s' % (msg))
sys.exit(0)
else:
msg="Unknown json-structure"
print('%s' % (msg))
sys.exit(2)
__EOF__
return "$?"
fi
return 0
}
parse_boolean_response() {
local v_module="parse_delete_response"
#log_info "${v_module} 1: >${1}<"
if [ ! -z "$1" ]
then
$PYTHON << __EOF__
import sys, json
json_string="""$1"""
parsed_string = json.loads(json_string)
if 'boolean' in parsed_string:
msg="executed successfully"
print('%s' % (msg))
sys.exit(0)
elif 'RemoteException' in parsed_string:
parsed_string=parsed_string['RemoteException']
msg="RemoteException"
if 'exception' in parsed_string:
msg=msg+': '+parsed_string['exception']
if 'message' in parsed_string:
msg=msg+' '+parsed_string['message']
print('%s' % (msg))
sys.exit(1)
else:
msg="Unknown json-format from hdfs"
print('%s' % (msg))
sys.exit(2)
__EOF__
return "$?"
fi
return "0"
}
parse_getxattrs_response() {
local v_module="parse_getxattrs_response"
if [ ! -z "$1" ]
then
$PYTHON << __EOF__
import sys, json
json_string="""$1"""
parsed_string = json.loads(json_string)
if "XAttrs" in parsed_string:
parsed_string=parsed_string['XAttrs']
if len(parsed_string) == 0:
sys.exit(0)
else:
for i in range(len(parsed_string)):
f=parsed_string[i]
msg=f['value']
if msg is None:
msg="null"
print('%s %s' % (f['name'], msg))
sys.exit(0)
elif 'RemoteException' in parsed_string:
parsed_string=parsed_string['RemoteException']
msg="RemoteException"
if 'exception' in parsed_string:
msg=msg+': '+parsed_string['exception']
if 'message' in parsed_string:
msg=msg+' '+parsed_string['message']
print('%s' % (msg))
sys.exit(1)
else:
msg="Unknown json-format from hdfs"
print('%s' % (msg))
sys.exit(2)
__EOF__
return "$?"
fi
return "0"
}
parse_error_response() {
local v_module="parse_error_response"
[ -z "$1" ] && return 1
$PYTHON << __EOF__
import sys, json
json_string="""$1"""
parsed_string = json.loads(json_string)
if "RemoteException" in parsed_string:
parsed_string=parsed_string['RemoteException']
msg="RemoteException"
if 'exception' in parsed_string:
msg=msg+': '+parsed_string['exception']
if 'message' in parsed_string:
msg=msg+' '+parsed_string['message']
print('%s' % (msg))
sys.exit(2)
else:
msg="Unknown json-format from hdfs"
print('%s' % (msg))
sys.exit(3)
__EOF__
return "$?"
}
#============================================================================================
create_file_usage() {
printf "%s\n" "Usage: create_file [options]
Here the options are:
-n|--name [file name] - Name of file to create in hdfs; Mandatory;
-l|--local [file name] - Name of local-file, which content'll be uploaded to hdfs with name from -n option; Mandatory;
-o|--overwrite [true|false|t|f] - Do (t|true) of do not (f|false - default) rewtite hdfs-file, if there is file with given name;
-p|--permission [0-1777] - Permission for new file, default: 644;
-m|--maxtime [int] - Limit for uploading time; In curl's term it's value for --max-time option; Default 10 seconds;
-r|--replication [int] - Replication factor for file in hdfs; Default: 2, valid value: [1-9]{1}
-h|--help - This help
"
}
delete_usage() {
printf "%s\n" "Usage: del_file [options]
Here the options are:
-n|--name [name] - Name of file|directory to delete in hdfs; Mandatory;
-r|--recursive - Do delete recursively; Default: non-recursively; Optional;
-h|--help - This help
"
}
getxattr_usage() {
printf "%s\n" "Usage: getxattr [options]
Here the options are:
-n|--name [name] - Name of hdfs-attribute (file|folder) which attribute you want to get; Optional
- In case it not set: root point of your hdfs-hierarchy will be processed;
-a|--attribute [name] - Name of attribute to get it out from hdfs; Optional;
In case it empty: all attributes of a given hdfs-item will be retrieved (if they are);
Please note, you have to prefix attribute name with 'user.';
And it'll be retrieved with that prefix;
See https://hadoop.apache.org/docs/current/hadoop-project-dist/hadoop-hdfs/ExtendedAttributes.html
-e|--encoding - How to encode value of retrieved attribue; Optional; Def: text; Allowed: text|hex|base64;
-b|--base64 - In case you asking hdfs about value of an one xattr, and you made the xattr earlier through setxattr -b,
You can orderm by -b|--base64 decoding this value of this xattr from base64;
-h|--help - This help
"
}
setxattr_usage() {
printf "%s\n" "Usage: setxattr [options]
Here the options are:
-n|--name [name] - Name of hdfs-attribute (file|folder) which attribute you want to get; Optional
- In case it not set: root point of your hdfs-hierarchy will be processed;
-a|--attribute [name] - Name of attribute to set; Mandatory;
- Please note, you have to prefix attribute name with 'user.';
- See https://hadoop.apache.org/docs/current/hadoop-project-dist/hadoop-hdfs/ExtendedAttributes.html
-v|--value - Value of attribute; Optional;
- In case it not set, or set as \"\" hdfs'll return it as 'null'
- Please avoid to use space and|or unprintable and|or special characters, like quotes;
- In case you really need to set such complex string as attr-value: use -b option
-b|--base64 - If that parameter is set: attr-value will be encoded in base64, before posting it to hdfs;
-f|--flag - create|replace attribute; Mandatory; create - should not be used for attributes which already exist;
- Def.: create
-h|--help - This help
"
}
rmxattr_usage() {
printf "%s\n" "Usage: rmxattr [options]
Here the options are:
-n|--name [name] - Name of hdfs-attribute (file|folder) which attribute you want to remove; Optional
- In case it not set: root point of your hdfs-hierarchy will be processed;
-a|--attribute [name] - Name of attribute to set; Mandatory;
- Please note, you have to prefix attribute name with 'user.';
- See https://hadoop.apache.org/docs/current/hadoop-project-dist/hadoop-hdfs/ExtendedAttributes.html
-h|--help - This help
"
}
mk_dir_usage() {
printf "%s\n" "Usage: mk_dir [options]
Here the options are:
-n|--name [name] - Name of directory to create in hdfs; Mandatory; No error if dir alreay is;
- path with subfolders allowed;
-p|--permission [0-1777] - Permission for new dir, default: 755;
-h|--help - This help
"
}
ls_dir_usage() {
printf "%s\n" "Usage: ls_dir [options]
Here the options are:
-n|--name [name] - Name of file|folder in hdfs to list; Optional;
- In case -n ommited: root-point of your hdfs-path is supposed;
-h|--help - This help
"
}
item_status_usage() {
printf "%s\n" "Usage: item_status [options]
Here the options are:
-n|--name [name] - Name of file|folder in hdfs to list; Optional;
- In case -n ommited: root-point of your hdfs-path is supposed;
-h|--help - This help
"
}
getfile_usage() {
printf "%s\n" "Usage: getfile [options]
Here the options are:
-n|--name [name] - Name of file in hdfs to download; Mandatory; It has to be file;
-l|--local [name] - Name of local file, where to save content of the hdfs-file, which given by '-n' option; Mandatory;
- If this localfile already is: it'll be overvriten;
-m|--maxtime [int] - Limit for downloading time; In curl's term it's value for --max-time option; Default 10 seconds;
-h|--help - This help
Note: offset, length, buffersize parameter of rest-api OPEN request - currentlly not supported, by this bash-library wrapper;
"
}
appendtofile_usage() {
printf "%s\n" "Usage: appendtofile [options]
Here the options are:
-n|--name [name] - Name of hdfs-file, to which append data; Mandatory;
-l|--local [name] - Name of local file, from where to read content for appending the hdfs-file; Mandatory;
-m|--maxtime [int] - Limit for downloading time; In curl's term it's value for --max-time option; Default 10 seconds;
-h|--help - This help
Note: buffersize parameter of rest-api APPEND request - currentlly not supported, by this bash-library wrapper;
"
}
renamefile_usage() {
printf "%s\n" "Usage: renamefile [options]
Here the options are:
-n|--name [name] - Name of hdfs-item, which you want to rename; Mandatory;
-d|--dest [name] - New name of for renaming; Mandatory; It has to be absolute path;
-h|--help - This help
"
}
setmod() {
printf "%s\n" "Usage: renamefile [options]
Here the options are:
-n|--name [name] - Name of hdfs-item, for which you want to change permissions; Mandatory;
-p|--permission [int] - The permission of a file/directory, octal, valid value: 0 - 1777; Mandatory;
-h|--help - This help
"
}
usage() {
printf "%s\n" "HDFS-interface's subprograms brief desc:
mk_dir - Make hdfs-directory with given name and permission mode.
Default permission is 644 for files, 755 for directories; Valid Values 0 - 1777
ls_dir - Listing items in given hdfs-directory, or show some info about given hdfs-file;
itemstatus - Show info about given at hdfs-item
In case of success it returns block of new-line delimited lines, with hdfs-item attributes, in key-valu form;
see FileStatus JSON object in webhdfs doc;
createfile - Make file at hdfs, by uploading there given local file;
delete - Delete file|directory at hdfs, optionally - recursively;
getfile - Get given file from hdfs
appendtofile - Append content of given local-file to given hdfs-file;
renamefile - Renaming
setmod - Set permission of a file/directory;
getxattr - Get out from hdfs extended attribute(s), of the given file|folder
setxattr - Set an extended-attribute to the given file|folder in hdfs
rmxattr - Remove given extended-attribute of the given file|folder in hdfs
All subprograms have -h|--help call option
In all subprograms value for option: -n|--name - has to be an absolute path
--------------------------------------------------------------------------------------
ENV:
SILENT - In case it non-zero: turns off output of library-subprogram messages to stdout;
Messages still will be made by the subproc, but will be written to logfile only;
---------------------------------------------------------------------------------------
Logdile: - ${LOG_FILE}
Config: - ${CONF_FILE}"
}
grep_json() {
local v_output=""
v_output=$(cat "$1" | egrep -v "^[\>\<\*] .*") #in case curl was called with -v option
v_output=${v_output//\\/\\\\}
v_output=${v_output%%[[:control:]]}
printf -v "$2" %s "$v_output"
}
prefix_hdfspath() {
local v_path="$1"
if [ "${v_path}" == "/" ]
then
echo -n ""
return 0
fi
if [ "${#v_path}" -gt "0" ]
then
if [ "${v_path:0:1}" != "/" ]
then
v_path="/"${v_path}
fi
fi
echo -n "$v_path"
}
empty_args_notallowed() {
local v_args="$1"
local v_procname="$2"
if [ -z "$v_args" ]
then
log_info "${v_procname} you should not launch this routine without any args"
return 1
fi
return 0
}
#== HDSF ====================================================================================================================
setmod() {
local v_module="setmod"
local v_count v_cmd rc v_x v_y V_URL=""
local v_operation="?op=SETPERMISSION"
local v_hdfspath=""
local v_permission=""
log_info "${v_module} starting"
empty_args_notallowed "$1" "$v_module"
if [ "$?" -ne "0" ]
then
setmod_usage; return 1
fi
while [ "$1" != "" ]
do
case "$1" in
"-h"|"--help")
setmod_usage; return 0
;;
"-n"|"--name")
if [ -z "$2" ]
then
log_info "${v_module} you have to specify value for -n|--name option;"
setmod_usage; return 1
fi
v_hdfspath="$2"
shift 2
;;
"-p"|"--permission")
if [ -z "$2" ]
then
log_info "${v_module} you have to specify value for -p|--permission option;"
setmod_usage; return 1
fi
v_permission="$2"
shift 2
;;
*)
log_info "${v_module} ${1} is not an option;"
setmod_usage; return 1
;;
esac
done
if [ -z "$v_hdfspath" -o -z "$v_permission" ]
then
log_info "${v_module} you have to set name and permission-mode"
setmod_usage; return 1
fi
if [[ ! "$v_permission" =~ [0-9]{3,4} ]]
then
log_info "${v_module} ${v_permission} is a wrong value for -p|--permission option;"
setmod_usage; return 1
fi
if [ "$v_permission" -gt "1777" -o "$v_permission" -lt "0" ]
then
log_info "${v_module} ${v_permission} is a wrong value for -p|--permission option;"
setmod_usage; return 1
fi
v_hdfspath=$(prefix_hdfspath "$v_hdfspath")
v_operation="${v_operation}&permission=${v_permission}"
V_URL="https://${WEBHDFS_SERVER}:${WEBHDFS_PORT}/gateway/default/webhdfs/v1/${WH_PATH}${v_hdfspath}${v_operation}"
V_URL=${V_URL%$'\r'}
v_cmd="${CURL} -X PUT ${GENERIC_OPTION} -u \"${V_LOGIN}:${V_PWD}\" --connect-timeout ${CONNECT_TIMEOUT} --max-time ${MAX_TIME} \"$V_URL\" 1>\"$TEMP_FILE\" 2>/dev/null"
log_info "$v_module ${v_cmd}"
v_count="1"
while [ "$v_count" -lt "$RETRY_LIMIT" ]
do
cat /dev/null > "$TEMP_FILE"
eval "$v_cmd"
rc="$?"
if [ "$rc" -eq "0" ]
then
log_info "${v_module} hdfs-response was obtained successfully"
log_info "$TEMP_FILE" "logfile"
v_x=""
grep_json "$TEMP_FILE" v_x
if [ -z "$v_x" ]
then
return 0
else
v_x=$(parse_error_response "$v_x"); v_y="$?"
echo "$v_x"
return "$v_y"
fi
else
log_info "${v_module} attempt ${v_count} fail ${rc}"
fi
((v_count++))
done
log_info "${v_module} done"
return "$?"
}
renamefile() {
local v_module="renamefile"
local v_count v_cmd rc v_x v_y V_URL=""
local v_operation="?op=RENAME"
local v_hdfspath=""
local v_newname=""
log_info "${v_module} starting"
empty_args_notallowed "$1" "$v_module"
if [ "$?" -ne "0" ]
then
renamefile_usage; return 1
fi
while [ "$1" != "" ]
do
case "$1" in
"-h"|"--help")
renamefile_usage; return 0
;;
"-n"|"--name")
if [ -z "$2" ]
then
log_info "${v_module} you have to specify value for -n|--name option;"
appendtofile_usage; return 1
fi
v_hdfspath="$2"
shift 2
;;
"-d"|"--dest")
if [ -z "$2" ]
then
log_info "${v_module} you have to specify value for -d|--dest option;"
appendtofile_usage; return 1
fi
v_newname="$2"
shift 2
;;
*)
log_info "${v_module} ${1} is not an option;"
renamefile_usage; return 1
;;
esac
done
if [ -z "$v_newname" -o -z "$v_hdfspath" ]
then
log_info "${v_module} current name and|or new-name is|are not set"
renamefile_usage; return 1
fi
v_hdfspath=$(prefix_hdfspath "$v_hdfspath")
v_newname=$(prefix_hdfspath "$v_newname")
v_newname="/${WH_PATH}"${v_newname}
v_operation="${v_operation}&destination=${v_newname}"
V_URL="https://${WEBHDFS_SERVER}:${WEBHDFS_PORT}/gateway/default/webhdfs/v1/${WH_PATH}${v_hdfspath}${v_operation}"
V_URL=${V_URL%$'\r'}
v_cmd="${CURL} -X PUT ${GENERIC_OPTION} -u \"${V_LOGIN}:${V_PWD}\" --connect-timeout ${CONNECT_TIMEOUT} --max-time ${MAX_TIME} \"$V_URL\" 1>\"$TEMP_FILE\" 2>/dev/null"
log_info "$v_module ${v_cmd}"
v_count="1"
while [ "$v_count" -lt "$RETRY_LIMIT" ]
do
cat /dev/null > "$TEMP_FILE"
eval "$v_cmd"
rc="$?"
if [ "$rc" -eq "0" ]
then
log_info "${v_module} hdfs-response was obtained successfully"
log_info "$TEMP_FILE" "logfile"
v_x=""
grep_json "$TEMP_FILE" v_x
if [ -z "$v_x" ]
then
return 0
else
v_x=$(parse_boolean_response "$v_x"); v_y="$?"
echo "$v_x"
return "$v_y"
fi
else
log_info "${v_module} attempt ${v_count} fail ${rc}"
fi
((v_count++))
done
log_info "${v_module} done"
return "$?"
}
appendtofile() {
local v_module="appendtofile"
local v_count v_cmd rc v_x v_y V_URL=""
local v_operation="?op=APPEND"
local v_hdfspath=""
local v_buffersize=""
local v_noredirect="false"
local v_localfile=""
local v_location=""
local v_maxtime="10"
log_info "${v_module} starting"
empty_args_notallowed "$1" "$v_module"
if [ "$?" -ne "0" ]
then
appendtofile_usage; return 1
fi
while [ "$1" != "" ]
do
case "$1" in
"-h"|"--help")
appendtofile_usage; return 0
;;
"-m"|"--maxtime")
if [ -z "$2" ]
then
log_info "${v_module} you have to specify value for -m|--maxtime option;"
appendtofile_usage; return 1
fi
if [[ ! "$2" =~ [0-9]+ ]]
then
log_info "${v_module} value for -m|--maxtime option should be int-digit;"
appendtofile_usage; return 1
fi
v_maxtime="$2"
if [ "$v_maxtime" -lt "10" ]
then
log_info "${v_module} value for -m|--maxtime option is too small: ${v_maxtime};"
log_info "${v_module} This value: ignored, default value '10' will be used;"
v_maxtime="10"
fi
shift 2
;;
"-n"|"--name")
if [ -z "$2" ]
then
log_info "${v_module} you have to specify value for -n|--name option;"
appendtofile_usage; return 1
fi
v_hdfspath="$2"
shift 2
;;
"-l"|"--local")
if [ -z "$2" ]
then
log_info "${v_module} you have to specify value for -l|--local option;"
appendtofile_usage; return 1
fi
if [ ! -f "$2" -a -r "$2" ]
then
log_info "${v_module} object, which setted in -l|--local option: ${2} is not a file and|or is not readable;"
return 2
fi
v_localfile="$2"
shift 2
;;
*)
log_info "${v_module} ${1} is not an option;"
appendtofile_usage; return 1
;;
esac
done
if [ -z "$v_localfile" -o -z "$v_hdfspath" ]
then
log_info "${v_module} you have to set hdfs-file to write-append to and local-file to read from"
appendtofile_usage; return 1
fi
v_hdfspath=$(prefix_hdfspath "$v_hdfspath")
[ ! -z "$v_buffersize" ] && v_operation="${v_operation}&buffersize=${v_buffersize}"
[ ! -z "$v_noredirect" ] && v_operation="${v_operation}&noredirect=${v_noredirect}"
V_URL="https://${WEBHDFS_SERVER}:${WEBHDFS_PORT}/gateway/default/webhdfs/v1/${WH_PATH}${v_hdfspath}${v_operation}"
V_URL=${V_URL%$'\r'}
#### -v option and 2>&1 is essential here ###
v_cmd="${CURL} -X POST -v ${GENERIC_OPTION} -u \"${V_LOGIN}:${V_PWD}\" --connect-timeout ${CONNECT_TIMEOUT} --max-time ${MAX_TIME} \"$V_URL\" 1>\"$TEMP_FILE\" 2>&1"
#############################################
log_info "$v_module ${v_cmd}"
v_count="1"
while [ "$v_count" -lt "$RETRY_LIMIT" ]
do
cat /dev/null > "$TEMP_FILE"
eval "$v_cmd"
rc="$?"
if [ "$rc" -eq "0" ]
then
v_location=`cat $TEMP_FILE | egrep "Location: " | awk '{printf "%s", $NF;}'`
v_location=${v_location%$'\r'}
v_count="$RETRY_LIMIT"
log_info "${v_module} success, locaton is: ${v_location}"
else
#sleep 1
log_info "$v_module attempt ${v_count} fail ${rc}"
fi
((v_count++))
done
if [ -z "$v_location" ]
then
log_info "${v_module} well, so sorry but inode-location for your file was not provided by hdfs; exiting"
return 1
else
v_cmd="${CURL} -X POST -f ${GENERIC_OPTION} -u \"${V_LOGIN}:${V_PWD}\" -T ${v_localfile} --connect-timeout ${CONNECT_TIMEOUT} --max-time ${v_maxtime} ${v_location}"
log_info "$v_module try to append date from ${v_localfile} to ${v_hdfspath}; Max-time limit is: ${v_maxtime}"
log_info "$v_module ${v_cmd}"
v_count="1"
while [ "$v_count" -lt "$RETRY_LIMIT" ]
do
eval "$v_cmd"
rc="$?"
if [ "$rc" -eq "0" ]
then
log_info "${v_module} success"
return 0
else
log_info "${v_module} attempt ${v_count} fail ${rc}"
fi
((v_count++))
done
return "$rc"
fi
}
getfile() {
local v_module="getfile"
local v_count v_cmd rc v_x v_y V_URL=""
local v_operation="?op=OPEN"
local v_hdfspath=""
local v_offset=""
local v_length=""
local v_buffersize=""
local v_noredirect="false"
local v_localfile=""
local v_location=""
local v_maxtime="10"
log_info "${v_module} starting"
empty_args_notallowed "$1" "$v_module"
if [ "$?" -ne "0" ]
then
getfile_usage; return 1
fi
while [ "$1" != "" ]
do
case "$1" in
"-h"|"--help")
getfile_usage; return 0
;;
"-m"|"--maxtime")
if [ -z "$2" ]
then
log_info "${v_module} you have to specify value for -m|--maxtime option;"
getfile_usage; return 1
fi
if [[ ! "$2" =~ [0-9]+ ]]
then
log_info "${v_module} value for -m|--maxtime option should be int-digit;"
getfile_usage; return 1
fi
v_maxtime="$2"
if [ "$v_maxtime" -lt "10" ]
then
log_info "${v_module} value for -m|--maxtime option is too small: ${v_maxtime};"
log_info "${v_module} This value: ignored, default value '10' will be used;"
v_maxtime="10"
fi
shift 2
;;
"-n"|"--name")
if [ -z "$2" ]
then
log_info "${v_module} you have to specify value for -n|--name option;"
getfile_usage; return 1
fi
v_hdfspath="$2"
shift 2
;;
"-l"|"--local")
if [ -z "$2" ]
then
log_info "${v_module} you have to specify value for -l|--local option;"
getfile_usage; return 1
fi
if [ ! -f "$2" -a -w "$2" ]
then
log_info "${v_module} object, which setted in -l|--local option: ${2} is not a file and|or is not writable;"
return 2
fi
v_localfile="$2"
shift 2
;;
*)
log_info "${v_module} ${1} is not an option;"
getfile_usage; return 1
;;
esac
done
if [ -z "$v_localfile" -o -z "$v_hdfspath" ]
then
log_info "${v_module} you have to set hdfs-file to read from and local-file to save read data"
getfile_usage; return 1
fi
v_hdfspath=$(prefix_hdfspath "$v_hdfspath")
[ ! -z "$v_offset" ] && v_operation="${v_operation}&offset=${v_offset}"
[ ! -z "$v_length" ] && v_operation="${v_operation}&length=${v_length}"
[ ! -z "$v_buffersize" ] && v_operation="${v_operation}&buffersize=${v_buffersize}"
[ ! -z "$v_noredirect" ] && v_operation="${v_operation}&noredirect=${v_noredirect}"
V_URL="https://${WEBHDFS_SERVER}:${WEBHDFS_PORT}/gateway/default/webhdfs/v1/${WH_PATH}${v_hdfspath}${v_operation}"
V_URL=${V_URL%$'\r'}
#### -v option and 2>&1 is essential here ###
v_cmd="${CURL} -X GET -v ${GENERIC_OPTION} -u \"${V_LOGIN}:${V_PWD}\" --connect-timeout ${CONNECT_TIMEOUT} --max-time ${MAX_TIME} \"$V_URL\" 1>\"$TEMP_FILE\" 2>&1"
#############################################
log_info "$v_module ${v_cmd}"
v_count="1"
while [ "$v_count" -lt "$RETRY_LIMIT" ]
do
cat /dev/null > "$TEMP_FILE"
eval "$v_cmd"
rc="$?"
if [ "$rc" -eq "0" ]
then
v_location=`cat $TEMP_FILE | egrep "Location: " | awk '{printf "%s", $NF;}'`
v_location=${v_location%$'\r'}
v_count="$RETRY_LIMIT"
log_info "${v_module} success, locaton is: ${v_location}"
else
#sleep 1
log_info "$v_module attempt ${v_count} fail ${rc}"
fi
((v_count++))
done
if [ -z "$v_location" ]
then
log_info "${v_module} well, so sorry but inode-location for your file was not provided by hdfs; exiting"
return 1
else
cat /dev/null > "$v_localfile"
v_cmd="${CURL} -X GET -f ${GENERIC_OPTION} -u \"${V_LOGIN}:${V_PWD}\" -o ${v_localfile} --connect-timeout ${CONNECT_TIMEOUT} --max-time ${v_maxtime} ${v_location}"
log_info "$v_module try to download ${v_hdfspath} as ${v_localfile}; Max-time limit is: ${v_maxtime}"
log_info "$v_module ${v_cmd}"
v_count="1"
while [ "$v_count" -lt "$RETRY_LIMIT" ]
do
eval "$v_cmd"
rc="$?"
if [ "$rc" -eq "0" ]
then
log_info "${v_module} success"
return 0
else
log_info "${v_module} attempt ${v_count} fail ${rc}"
fi
((v_count++))
done
return "$rc"
fi
}
rmxattr() {
local v_module="rmxattr"
local v_count v_cmd rc v_x v_y V_URL=""
local v_operation="?op=REMOVEXATTR"
local v_hdfspath=""
local v_xattrname=""
log_info "${v_module} starting"
empty_args_notallowed "$1" "$v_module"
if [ "$?" -ne "0" ]
then
rmxattr_usage; return 1
fi
while [ "$1" != "" ]
do
case "$1" in
"-h"|"--help")
rmxattr_usage; return 0
;;
"-n"|"--name")
if [ -z "$2" ]
then
log_info "${v_module} you have to specify file or folder name in -n|--name option;"
rmxattr_usage; return 1
fi
v_hdfspath="$2"
shift 2
;;
"-a"|"--attribute")
if [ -z "$2" ]
then
log_info "${v_module} you have to specify attribute name in -a|--attribute option;"
rmxattr_usage; return 1
fi
v_xattrname="$2"
shift 2
;;
*)
log_info "${v_module} ${1} is not an option;"
setxattr_usage; return 1
;;
esac
done
if [ -z "$v_xattrname" ]
then
log_info "${v_module} name of extended-attribute should not be empty"
setxattr_usage; return 1
fi
v_hdfspath=$(prefix_hdfspath "$v_hdfspath")
v_operation="${v_operation}&xattr.name=${v_xattrname}"
V_URL="https://${WEBHDFS_SERVER}:${WEBHDFS_PORT}/gateway/default/webhdfs/v1/${WH_PATH}${v_hdfspath}${v_operation}"
V_URL=${V_URL%$'\r'}
v_cmd="${CURL} -X PUT ${GENERIC_OPTION} -u \"${V_LOGIN}:${V_PWD}\" --connect-timeout ${CONNECT_TIMEOUT} --max-time ${MAX_TIME} \"$V_URL\" 1>\"$TEMP_FILE\" 2>/dev/null"
log_info "$v_module ${v_cmd}"
v_count="1"
while [ "$v_count" -lt "$RETRY_LIMIT" ]
do
cat /dev/null > "$TEMP_FILE"
eval "$v_cmd"
rc="$?"
if [ "$rc" -eq "0" ]
then
log_info "${v_module} hdfs-response was obtained successfully"
log_info "$TEMP_FILE" "logfile"
v_x=""
grep_json "$TEMP_FILE" v_x
if [ -z "$v_x" ]
then
return 0
else
v_x=$(parse_error_response "$v_x"); v_y="$?"
echo "$v_x"
return "$v_y"
fi
else
log_info "${v_module} attempt ${v_count} fail ${rc}"
fi
((v_count++))
done
log_info "${v_module} done"
return "$rc"
}
setxattr() {
local v_module="setxattrs"
local v_count v_cmd rc v_x v_y V_URL=""
local v_operation="?op=SETXATTR"
local v_hdfspath=""
local v_xattrname=""
local v_xattrval=""
local v_flag="CREATE" # https://hadoop.apache.org/docs/current/hadoop-project-dist/hadoop-hdfs/WebHDFS.html#XAttr_set_flag
local v_base64=""
log_info "${v_module} starting"
empty_args_notallowed "$1" "$v_module"