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_write <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* writeFileName = argv[1]; |
---|
35 | tSize fileTotalSize = strtoul(argv[2], NULL, 10); |
---|
36 | tSize bufferSize = strtoul(argv[3], NULL, 10); |
---|
37 | |
---|
38 | hdfsFile writeFile = hdfsOpenFile(fs, writeFileName, O_WRONLY, bufferSize, 0, 0); |
---|
39 | if (!writeFile) { |
---|
40 | fprintf(stderr, "Failed to open %s for writing!\n", writeFileName); |
---|
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 | int i = 0; |
---|
50 | for (i=0; i < bufferSize; ++i) { |
---|
51 | buffer[i] = 'a' + (i%26); |
---|
52 | } |
---|
53 | |
---|
54 | // write to the file |
---|
55 | tSize nrRemaining; |
---|
56 | for (nrRemaining = fileTotalSize; nrRemaining > 0; nrRemaining -= bufferSize ) { |
---|
57 | int curSize = ( bufferSize < nrRemaining ) ? bufferSize : (int)nrRemaining; |
---|
58 | hdfsWrite(fs, writeFile, (void*)buffer, curSize); |
---|
59 | } |
---|
60 | |
---|
61 | free(buffer); |
---|
62 | hdfsCloseFile(fs, writeFile); |
---|
63 | hdfsDisconnect(fs); |
---|
64 | |
---|
65 | return 0; |
---|
66 | } |
---|
67 | |
---|
68 | /** |
---|
69 | * vim: ts=4: sw=4: et: |
---|
70 | */ |
---|
71 | |
---|