1 | /** |
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one |
---|
3 | * or more contributor license agreements. See the NOTICE file |
---|
4 | * distributed with this work for additional information |
---|
5 | * regarding copyright ownership. The ASF licenses this file |
---|
6 | * to you under the Apache License, Version 2.0 (the |
---|
7 | * "License"); you may not use this file except in compliance |
---|
8 | * with the License. You may obtain a copy of the License at |
---|
9 | * |
---|
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
11 | * |
---|
12 | * Unless required by applicable law or agreed to in writing, software |
---|
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
15 | * See the License for the specific language governing permissions and |
---|
16 | * limitations under the License. |
---|
17 | */ |
---|
18 | |
---|
19 | #include "hdfs.h" |
---|
20 | |
---|
21 | int main(int argc, char **argv) { |
---|
22 | |
---|
23 | if (argc != 4) { |
---|
24 | fprintf(stderr, "Usage: hdfs_read <filename> <filesize> <buffersize>\n"); |
---|
25 | exit(-1); |
---|
26 | } |
---|
27 | |
---|
28 | hdfsFS fs = hdfsConnect("default", 0); |
---|
29 | if (!fs) { |
---|
30 | fprintf(stderr, "Oops! Failed to connect to hdfs!\n"); |
---|
31 | exit(-1); |
---|
32 | } |
---|
33 | |
---|
34 | const char* rfile = argv[1]; |
---|
35 | tSize fileTotalSize = strtoul(argv[2], NULL, 10); |
---|
36 | tSize bufferSize = strtoul(argv[3], NULL, 10); |
---|
37 | |
---|
38 | hdfsFile readFile = hdfsOpenFile(fs, rfile, O_RDONLY, bufferSize, 0, 0); |
---|
39 | if (!readFile) { |
---|
40 | fprintf(stderr, "Failed to open %s for writing!\n", rfile); |
---|
41 | exit(-2); |
---|
42 | } |
---|
43 | |
---|
44 | // data to be written to the file |
---|
45 | char* buffer = malloc(sizeof(char) * bufferSize); |
---|
46 | if(buffer == NULL) { |
---|
47 | return -2; |
---|
48 | } |
---|
49 | |
---|
50 | // read from the file |
---|
51 | tSize curSize = bufferSize; |
---|
52 | for (; curSize == bufferSize;) { |
---|
53 | curSize = hdfsRead(fs, readFile, (void*)buffer, curSize); |
---|
54 | } |
---|
55 | |
---|
56 | |
---|
57 | free(buffer); |
---|
58 | hdfsCloseFile(fs, readFile); |
---|
59 | hdfsDisconnect(fs); |
---|
60 | |
---|
61 | return 0; |
---|
62 | } |
---|
63 | |
---|
64 | /** |
---|
65 | * vim: ts=4: sw=4: et: |
---|
66 | */ |
---|
67 | |
---|