Skip to content

Instantly share code, notes, and snippets.

@Tagar
Created September 11, 2017 23:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tagar/7c42f82f5538004bda2f4ea0b0e0adc5 to your computer and use it in GitHub Desktop.
Save Tagar/7c42f82f5538004bda2f4ea0b0e0adc5 to your computer and use it in GitHub Desktop.
hdfs_write_example.c using JNI
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "hdfs.h"
void logging (const char *msg)
{
fprintf(stderr, "%s\n", msg);
}
int main(int argc, char **argv) {
logging("hdfsConnect()..");
hdfsFS fs = hdfsConnect("pc1udatahad121", 8020);
logging("hdfsConnect()ed");
const char* writePath = "/tmp/testfile.txt";
hdfsFile writeFile = hdfsOpenFile(fs, writePath, O_WRONLY |O_CREAT, 0, 0, 0);
logging("HDFS file opened for writing");
if(!writeFile) {
fprintf(stderr, "Failed to open %s for writing!\n", writePath);
exit(10);
}
char* buffer = "Hello, World!";
tSize num_written_bytes = hdfsWrite(fs, writeFile, (void*)buffer, strlen(buffer)+1);
logging("hdfsWrite() complete; not flushed yet");
if (hdfsFlush(fs, writeFile)) {
fprintf(stderr, "Failed to 'flush' %s\n", writePath);
exit(10);
}
logging("about to close the file");
hdfsCloseFile(fs, writeFile);
logging("hdfsCloseFile() complete");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment