-
Notifications
You must be signed in to change notification settings - Fork 5
/
webhdfsconnector.cpp
841 lines (678 loc) · 27.2 KB
/
webhdfsconnector.cpp
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
/*##############################################################################
Copyright (C) 2012 HPCC Systems.
All rights reserved. This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
############################################################################## */
#include "webhdfsconnector.hpp"
int webhdfsconnector::reachWebHDFS()
{
int retval = RETURN_FAILURE;
if (!curl)
{
fprintf(stderr, "Could not reach WebHDFS\n");
return retval;
}
curl_easy_reset(curl);
string filestatusstr;
string getFileStatus;
getFileStatus.append(baseurl);
if (hasUserName())
{
getFileStatus.append("?user.name=");
getFileStatus.append(username);
getFileStatus.append("&op=GETFILESTATUS");
}
else
getFileStatus.append("?op=GETFILESTATUS");
fprintf(stderr, "Testing connection: %s\n", getFileStatus.c_str());
curl_easy_setopt(curl, CURLOPT_URL, getFileStatus.c_str());
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L);
//Dont' use default curl WRITEFUNCTION bc it outputs value to STDOUT.
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeToStrCallBackCurl);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &filestatusstr);
CURLcode res = curl_easy_perform(curl);
if (res == CURLE_OK)
retval = EXIT_SUCCESS;
else
fprintf(stderr, "Could not reach WebHDFS. Error code: %d\n", res);
return retval;
}
unsigned long webhdfsconnector::getFileSize(const char * fileurl)
{
HdfsFileStatus filestat;
if (getFileStatus(fileurl, &filestat) != RETURN_FAILURE)
return filestat.length;
else
return 0;
}
unsigned long webhdfsconnector::getFileSize()
{
return targetfilestatus.length;
}
int webhdfsconnector::getFileStatus(const char * fileurl, HdfsFileStatus * filestat)
{
int retval = RETURN_FAILURE;
if (!curl)
{
fprintf(stderr, "Could not connect to WebHDFS\n");
return retval;
}
string filestatusstr;
string requestheader;
string getFileStatus;
getFileStatus.append(fileurl);
if (hasUserName())
{
getFileStatus.append("?user.name=");
getFileStatus.append(username);
getFileStatus.append("&op=GETFILESTATUS");
}
else
getFileStatus.append("?op=GETFILESTATUS");
curl_easy_reset(curl);
fprintf(stderr, "Retrieving file status: %s\n", getFileStatus.c_str());
curl_easy_setopt(curl, CURLOPT_URL, getFileStatus.c_str());
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeToStrCallBackCurl);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &filestatusstr);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeToStrCallBackCurl);
curl_easy_setopt(curl, CURLOPT_WRITEHEADER, &requestheader);
CURLcode res = curl_easy_perform(curl);
if (res == CURLE_OK)
{
try
{
/*
* Not using JSON parser to avoid 3rd party deps
*/
filestat->accessTime = -1;
filestat->blockSize = -1;
filestat->group = "";
filestat->length = -1;
filestat->modificationTime = -1;
filestat->owner = "";
filestat->pathSuffix = "";
filestat->permission = "";
filestat->replication = -1;
filestat->type = "";
fprintf(stderr, "%s.\n", filestatusstr.c_str());
if (filestatusstr.find("FileStatus") >=0 )
{
int lenpos = filestatusstr.find("length");
if (lenpos >= 0)
{
int colpos = filestatusstr.find_first_of(':', lenpos);
if (colpos > lenpos)
{
int compos = filestatusstr.find_first_of(',', colpos);
if (compos > colpos)
{
filestat->length = atol(filestatusstr.substr(colpos+1,compos-1).c_str());
}
}
}
}
retval = EXIT_SUCCESS;
}
catch (...) {}
if (retval != EXIT_SUCCESS)
fprintf(stderr, "Error fetching HDFS file status.\n");
}
else
fprintf(stderr, "Error fetching HDFS file status. Error code: %d.\n", res);
return retval;
}
int webhdfsconnector::streamFlatFileOffset(unsigned long seekPos, unsigned long readlen, int maxretries)
{
int retval = RETURN_FAILURE;
if (!curl)
{
fprintf(stderr, "Could not connect to WebHDFS\n");
return retval;
}
curl_easy_reset(curl);
string readfileurl;
string requestheader;
readfileurl.append(targetfileurl).append("?");
if (hasUserName())
readfileurl.append("user.name=").append(username).append("&");
readfileurl.append("op=OPEN&offset=");
readfileurl.append(template2string(seekPos));
readfileurl.append("&length=");
readfileurl.append(template2string(readlen));
fprintf(stderr, "Reading file data: %s\n", readfileurl.c_str());
CURLcode res;
int failed_attempts = 0;
double dlsize = 0;
double tottime = 0;
double dlspeed = 0;
do
{
curl_easy_setopt(curl, CURLOPT_URL, readfileurl.c_str());
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, true);
curl_easy_setopt(curl, CURLOPT_VERBOSE, true);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, true);
curl_easy_setopt(curl, CURLOPT_MAXREDIRS, s_libcurlmaxredirs); //Default as reported by libcurl
//curl.haxx.se/docs/manpage.html#--max-redirs
//however the default seems to be 0 in
//at least one platform (CentOS), therefore
//Explicitly setting default to 50.
res = curl_easy_perform(curl);
if (res == CURLE_OK)
{
curl_easy_getinfo(curl, CURLINFO_SIZE_DOWNLOAD, &dlsize);
curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &tottime);
curl_easy_getinfo(curl, CURLINFO_SPEED_DOWNLOAD, &dlspeed);
}
else
{
failed_attempts++;
fprintf(stderr, "Error attempting to read from HDFS file: \n\t%s. Error code %d \n", readfileurl.c_str(), res);
}
}
while(res != CURLE_OK && failed_attempts <= maxretries);
if (res == CURLE_OK)
{
retval = EXIT_SUCCESS;
fprintf(stderr, "\nPipe in FLAT file results:\n");
fprintf(stderr, "Read time: %.3f secs\t ", tottime);
fprintf(stderr, "Read speed: %.0f bytes/sec\n", dlspeed);
fprintf(stderr, "Read size: %.0f bytes\n", dlsize);
}
return retval;
}
int webhdfsconnector::streamCSVFileOffset(unsigned long seekPos,
unsigned long readlen, const char * eolseq, unsigned long bufferSize, bool outputTerminator,
unsigned long recLen, unsigned long maxLen, const char * quote, int maxretries)
{
fprintf(stderr, "CSV terminator: \'%s\' and quote: \'%c\'\n", eolseq, quote[0]);
unsigned long recsFound = 0;
unsigned eolseqlen = strlen(eolseq);
if (seekPos > eolseqlen)
seekPos -= eolseqlen; //read back sizeof(EOL) in case the seekpos happens to be a the first char after an EOL
bool withinQuote = false;
string bufferstr ="";
bufferstr.resize(bufferSize);
bool stopAtNextEOL = false;
bool firstEOLfound = seekPos == 0;
unsigned long currentPos = seekPos;
fprintf(stderr, "--Start looking: %ld--\n", currentPos);
unsigned long bytesLeft = readlen;
const char * buffer;
curl_easy_reset(curl);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, true);
curl_easy_setopt(curl, CURLOPT_MAXREDIRS, s_libcurlmaxredirs); //Default as reported by libcurl
//curl.haxx.se/docs/manpage.html#--max-redirs
//however the default seems to be 0 in
//at least one platform (CentOS), therefore
//Explicitly setting default to 50.
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeToStrCallBackCurl);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &bufferstr);
while(bytesLeft > 0)
{
bufferstr.clear();
double num_read_bytes = readTargetFileOffsetToBuffer(currentPos, bytesLeft > bufferSize ? bufferSize : bytesLeft, maxretries);
if (num_read_bytes <= 0)
{
fprintf(stderr, "\n--Hard Stop at: %ld--\n", currentPos);
break;
}
buffer = bufferstr.c_str();
for (int bufferIndex = 0; bufferIndex < num_read_bytes; bufferIndex++, currentPos++)
{
char currChar = buffer[bufferIndex];
if (currChar == EOF)
break;
if (currChar == quote[0])
{
fprintf(stderr, "found quote char at pos: %ld\n", currentPos);
withinQuote = !withinQuote;
}
if (currChar == eolseq[0] && !withinQuote)
{
bool eolfound = true;
double extraNumOfBytesRead = 0;
string tmpstr("");
if (eolseqlen > 1)
{
int eoli = bufferIndex;
while (eoli < num_read_bytes && eoli - bufferIndex < eolseqlen)
{
tmpstr.append(1, buffer[eoli++]);
}
if (eoli == num_read_bytes && tmpstr.size() < eolseqlen)
{
//looks like we have to do a remote read, but before we do, let's make sure the substring matches
if (strncmp(eolseq, tmpstr.c_str(), tmpstr.size())==0)
{
string tmpbuffer = "";
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &tmpbuffer);
//TODO have to make a read... of eolseqlen - tmpstr.size is it worth it?
//read from the current position scanned on the file (currentPos) + number of char looked ahead for eol (eoli - bufferIndex)
//up to the necessary chars to determine if we're currently scanning the EOL sequence (eolseqlen - tmpstr.size()
extraNumOfBytesRead = readTargetFileOffsetToBuffer(currentPos + (eoli - bufferIndex), eolseqlen - tmpstr.size(), maxretries);
for(int y = 0; y < extraNumOfBytesRead; y++)
tmpstr.append(1, tmpbuffer.at(y));
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &bufferstr);
}
}
if (strcmp(tmpstr.c_str(), eolseq) != 0)
eolfound = false;
}
if (eolfound)
{
if (!firstEOLfound)
{
bufferIndex = bufferIndex + eolseqlen - 1;
currentPos = currentPos + eolseqlen - 1;
bytesLeft = bytesLeft - eolseqlen;
fprintf(stderr, "\n--Start reading: %ld --\n", currentPos);
firstEOLfound = true;
continue;
}
if (outputTerminator)
{
fprintf(stdout, "%s", eolseq);
bufferIndex += eolseqlen;
currentPos += eolseqlen;
bytesLeft -= eolseqlen;
}
recsFound++;
if (stopAtNextEOL)
{
fprintf(stderr, "\n--Stop piping: %ld--\n", currentPos);
bytesLeft = 0;
break;
}
if (bufferIndex < num_read_bytes)
currChar = buffer[bufferIndex];
else
break;
}
}
//don't pipe until we're beyond the first EOL (if offset = 0 start piping ASAP)
if (firstEOLfound)
{
fprintf(stdout, "%c", currChar);
bytesLeft--;
}
else
{
fprintf(stderr, "%c", currChar);
bytesLeft--;
if(maxLen > 0 && currentPos-seekPos > maxLen * 10)
{
fprintf(stderr, "\nFirst EOL was not found within the first %ld bytes", currentPos-seekPos);
return EXIT_FAILURE;
}
}
if (stopAtNextEOL)
fprintf(stderr, "%c", currChar);
// If bytesLeft <= 0 at this point, but he haven't encountered
// the last EOL, need to continue piping untlil EOL is encountered.
if (bytesLeft <= 0 && currChar != eolseq[0])
{
if(!firstEOLfound)
{
fprintf(stderr, "\n--Reached end of readlen before finding first record start at: %ld (breaking out)--\n", currentPos);
break;
}
if (stopAtNextEOL)
{
fprintf(stderr, "Could not find last EOL, breaking out a position %ld\n", currentPos);
break;
}
fprintf(stderr, "\n--Looking for Last EOL: %ld --\n", currentPos);
bytesLeft = maxLen; //not sure how much longer until next EOL read up max record len;
stopAtNextEOL = true;
}
}
}
fprintf(stderr, "\nCurrentPos: %ld, RecsFound: %ld\n", currentPos, recsFound);
if (currentPos == seekPos && readlen > 0 )
{
fprintf(stderr, "\n--ERROR 0 Bytes Fetched--\n");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
double webhdfsconnector::readTargetFileOffsetToBuffer(unsigned long seekPos, unsigned long readlen, int maxretries)
{
double returnval = 0;
if (!curl)
{
fprintf(stderr, "Could not connect to WebHDFS");
return returnval;
}
char readfileurl [1024];
if (hasUserName())
sprintf(readfileurl, "%s?user.name=%s&op=OPEN&offset=%lu&length=%lu",targetfileurl.c_str(), username.c_str(), seekPos,readlen);
else
sprintf(readfileurl, "%s?op=OPEN&offset=%lu&length=%lu",targetfileurl.c_str(), seekPos,readlen);
curl_easy_setopt(curl, CURLOPT_URL, readfileurl);
CURLcode res;
int failed_attempts = 0;
do
{
res = curl_easy_perform(curl);
if (res == CURLE_OK)
{
double dlsize;
curl_easy_getinfo(curl, CURLINFO_SIZE_DOWNLOAD, &dlsize);
if (dlsize > readlen)
fprintf(stderr, "Warning: received incorrect number of bytes from HDFS.\n");
else
returnval = dlsize;
}
else
{
failed_attempts++;
fprintf(stderr, "Error attempting to read from HDFS file: \n\t%s\n\tError code: %d", readfileurl, res);
}
}
while(res != CURLE_OK && failed_attempts <= maxretries);
return returnval;
}
unsigned long webhdfsconnector::getTotalFilePartsSize(unsigned clustercount)
{
unsigned long totalSize = 0;
for (unsigned node = 0; node < clustercount; node++)
{
char filepartname[1024];
memset(&filepartname[0], 0, sizeof(filepartname));
sprintf(filepartname,"%s-parts/part_%d_%d", targetfileurl.c_str(), node, clustercount);
unsigned long partFileSize = getFileSize(filepartname);
if (partFileSize <= 0)
{
fprintf(stderr,"Error: Could not find part file: %s", filepartname);
return 0;
}
totalSize += partFileSize;
}
return totalSize;
}
unsigned long webhdfsconnector::appendBufferOffset(long blocksize, short replication, int buffersize, unsigned char * buffer)
{
//curl -i -X POST "http://<HOST>:<PORT>/webhdfs/v1/<PATH>?op=APPEND[&buffersize=<INT>]"
unsigned long retval = RETURN_FAILURE;
if (!curl)
{
fprintf(stderr, "Could not connect to WebHDFS");
return retval;
}
curl_easy_reset(curl);
char openfileurl [1024];
if (hasUserName())
sprintf(openfileurl, "%s?user.name=%s&op=APPEND&buffersize=%d",targetfileurl.c_str(), username.c_str(), buffersize);
else
sprintf(openfileurl, "%s?op=APPEND&buffersize=%d",targetfileurl.c_str(), buffersize);
curl_easy_setopt(curl, CURLOPT_READFUNCTION, continueCallBackCurl);
curl_easy_setopt(curl, CURLOPT_URL, openfileurl);
curl_easy_setopt(curl, CURLOPT_POST, true);
curl_easy_setopt(curl, CURLOPT_VERBOSE, false);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, true);
curl_easy_setopt(curl, CURLOPT_MAXREDIRS, s_libcurlmaxredirs); //Default as reported by libcurl
//curl.haxx.se/docs/manpage.html#--max-redirs
//however the default seems to be 0 in
//at least one platform (CentOS), therefore
//Explicitly setting default to 50.
string header;
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeToStrCallBackCurl);
curl_easy_setopt(curl, CURLOPT_WRITEHEADER, &header);
CURLcode res = curl_easy_perform(curl);
if (res == CURLE_OK)
{
size_t found;
found = header.find("307 TEMPORARY_REDIRECT");
if (found!=string::npos)
{
found=header.find("Location:",found+22);
if (found!=string::npos)
{
size_t eolfound =header.find(EOL,found);
string tmp = header.substr(found+10,eolfound-(found+10) - strlen(EOL));
curl_easy_setopt(curl, CURLOPT_URL, tmp.c_str());
curl_easy_setopt(curl, CURLOPT_POST, true);
curl_easy_setopt(curl, CURLOPT_READFUNCTION, readBufferCallBackCurl);
curl_easy_setopt(curl, CURLOPT_READDATA, buffer);
curl_easy_setopt(curl, CURLOPT_BUFFERSIZE, buffersize);
curl_easy_setopt(curl, CURLOPT_VERBOSE, false);
header.clear();
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeToStrCallBackCurl);
curl_easy_setopt(curl, CURLOPT_WRITEHEADER, &header);
res = curl_easy_perform(curl);
fprintf(stderr, "Response: %s\n", header.c_str());
}
}
}
if (res != CURLE_OK)
fprintf(stderr, "Error transferring file. Curl error code: %d\n", res);
else
retval = buffersize;
return retval;
}
bool webhdfsconnector::connect ()
{
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
if (!curl)
{
fprintf(stderr, "Could not connect to WebHDFS\n");
return false;
}
baseurl.clear();
targetfileurl.clear();
hasusername = false;
baseurl.append(hadoopHost);
baseurl.append(":");
baseurl.append(template2string(hadoopPort));
baseurl.append(WEBHDFS_VER_PATH);
targetfileurl.assign(baseurl.c_str());
baseurl.append("/");
if (fileName[0] != '/')
targetfileurl.append("/");
targetfileurl.append(fileName);
if (strlen(hdfsuser)>0)
{
username.assign(hdfsuser);
hasusername = true;
}
webhdfsreached = (reachWebHDFS() == EXIT_SUCCESS);
if (webhdfsreached)
getFileStatus(targetfileurl.c_str(), &targetfilestatus);
return webhdfsreached;
};
int webhdfsconnector::execute ()
{
int returnCode = EXIT_FAILURE;
if (action == HCA_STREAMIN)
{
returnCode = streamFileOffset();
}
else if (action == HCA_STREAMOUT || action == HCA_STREAMOUTPIPE)
{
returnCode = writeFlatOffset();
}
else if (action == HCA_MERGEFILE)
{
returnCode = mergeFile();
}
else
{
fprintf(stderr, "\nNo action type detected, exiting.");
}
return returnCode;
};
int webhdfsconnector::streamInFile(const char * rfile, int bufferSize)
{
return 0;
}
int webhdfsconnector::mergeFile()
{
fprintf(stderr, "Merging of file parts not supported by WEBHDFS version of HDFSConnector!\n");
return RETURN_FAILURE;
}
int webhdfsconnector::writeFlatOffset()
{
int retval = RETURN_FAILURE;
if (strlen(pipepath) <= 0)
{
fprintf(stderr, "Bad data pipe or file name found");
return retval;
}
if (!curl)
{
fprintf(stderr, "Could not connect to WebHDFS");
return retval;
}
curl_easy_reset(curl);
char openfileurl [1024];
if (hasUserName())
sprintf(openfileurl, "%s-parts/part_%d_%d?user.name=%s&op=CREATE&replication=%d&overwrite=true",targetfileurl.c_str(),nodeID, clusterCount,username.c_str(), 1);
else
sprintf(openfileurl, "%s-parts/part_%d_%d?op=CREATE&replication=%d&overwrite=true",targetfileurl.c_str(),nodeID, clusterCount, 1);
_IO_FILE * datafileorpipe;
datafileorpipe = fopen(pipepath, "rb");
fprintf(stderr, "Setting up new HDFS file: %s\n", openfileurl);
curl_easy_setopt(curl, CURLOPT_READFUNCTION, continueCallBackCurl);
curl_easy_setopt(curl, CURLOPT_READDATA, datafileorpipe);
curl_easy_setopt(curl, CURLOPT_URL, openfileurl);
curl_easy_setopt(curl, CURLOPT_PUT, true);
curl_easy_setopt(curl, CURLOPT_VERBOSE, true);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, false);
string header;
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeToStrCallBackCurl);
curl_easy_setopt(curl, CURLOPT_WRITEHEADER, &header);
CURLcode res = curl_easy_perform(curl);
if (res == CURLE_OK)
{
size_t found;
found = header.find_first_of("307 TEMPORARY_REDIRECT");
// HTTP/1.1 100 Continue\\r\\n\\r\\nHTTP/1.1 307 TEMPORARY_REDIRECT\\r\\nContent-Type: application/json\\r\\nExpires: Thu, 01-Jan-1970 00:00:00 GMT\\r\\nSet-Cookie: hadoop.auth=\\"u=hadoop&p=hadoop&t=simple&e=1345504538799&s=
if (found!=string::npos)
{
found=header.find("Location:",found+22);
if (found!=string::npos)
{
size_t eolfound =header.find(EOL,found);
string tmp = header.substr(found+10,eolfound-(found+10) - strlen(EOL));
fprintf(stderr, "Redirect location: %s\n", tmp.c_str());
curl_easy_setopt(curl, CURLOPT_URL, tmp.c_str());
curl_easy_setopt(curl, CURLOPT_UPLOAD, true);
curl_easy_setopt(curl, CURLOPT_READFUNCTION, readFileCallBackCurl);
curl_easy_setopt(curl, CURLOPT_READDATA, datafileorpipe);
string errorbody;
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeToStrCallBackCurl);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &errorbody);
res = curl_easy_perform(curl);
if (res == CURLE_OK)
{
if (errorbody.length() > 0)
fprintf(stderr, "Error transferring file: %s\n", errorbody.c_str());
else
retval = EXIT_SUCCESS;
}
else
{
fprintf(stderr, "Error transferring file. Curl error code: %d\n", res);
}
}
}
}
else
{
fprintf(stderr, "Error setting up file: %s.\n", openfileurl);
}
return retval;
}
int webhdfsconnector::streamFileOffset()
{
int returnCode = RETURN_FAILURE;
fprintf(stderr, "\nStreaming in %s...\n", fileName);
unsigned long fileSize = getFileSize();
if (fileSize != RETURN_FAILURE)
{
if (strcmp(format.c_str(), "FLAT") == 0)
{
unsigned long recstoread = getRecordCount(fileSize, clusterCount, recLen, nodeID);
if (recstoread != RETURN_FAILURE)
{
unsigned long totRecsInFile = fileSize / recLen;
unsigned long offset = nodeID * (totRecsInFile / clusterCount) * recLen;
unsigned long leftOverRecs = totRecsInFile % clusterCount;
if (leftOverRecs > 0)
{
if (leftOverRecs > nodeID)
offset += nodeID * recLen;
else
offset += leftOverRecs * recLen;
}
fprintf(stderr, "fileSize: %lu offset: %lu size bytes: %lu, recstoread:%lu\n", fileSize, offset,
recstoread * recLen, recstoread);
if (offset < fileSize)
returnCode = streamFlatFileOffset(offset, recstoread * recLen, maxRetry);
}
else
fprintf(stderr, "Could not determine number of records to read");
}
else if (strcmp(format.c_str(), "CSV") == 0)
{
fprintf(stderr, "Filesize: %ld, Offset: %ld, readlen: %ld\n", fileSize,
(fileSize / clusterCount) * nodeID, fileSize / clusterCount);
returnCode = streamCSVFileOffset((fileSize / clusterCount) * nodeID,
fileSize / clusterCount, terminator.c_str(), bufferSize, outputTerminator, recLen, maxLen,
quote.c_str(), maxRetry);
}
else
fprintf(stderr, "Unknown format type: %s(%s)", format.c_str(), foptions.c_str());
}
else
fprintf(stderr, "Could not determine HDFS file size: %s", fileName);
return returnCode;
}
long webhdfsconnector::getRecordCount(long fsize, int clustersize, int reclen, int nodeid)
{
long readSize = fsize / reclen / clustersize;
if (fsize % reclen)
{
fprintf(stderr, "filesize (%lu) not multiple of record length(%d)", fsize, reclen);
return RETURN_FAILURE;
}
if ((fsize / reclen) % clustersize > nodeid)
{
readSize++;
fprintf(stderr, "\nThis node will pipe one extra rec\n");
}
return readSize;
}
int main(int argc, char **argv)
{
int returnCode = EXIT_FAILURE;
webhdfsconnector * connector = new webhdfsconnector();
if (connector)
{
if (connector->parseInParams(argc, argv))
{
if (connector->validateParameters())
{
if (connector->connect())
{
returnCode = connector->execute();
}
}
if (connector)
delete (connector);
}
}
else
fprintf(stderr, "\nError: Could not create webhdfs connector");
return returnCode;
}