Configuration and use of Libhdfs

Source: Internet
Author: User

Test environment: centos6.10,hadoop2.7.3,jdk1.8

Test code: HDFSCSAMPLE.C

#include "hdfs.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) {

    hdfsFS fs = hdfsConnect("default", 0);
    const char* writePath = "/tmp/testfile.txt";
    hdfsFile writeFile = hdfsOpenFile(fs, writePath, O_WRONLY |O_CREAT, 0, 0, 0);
    if(!writeFile) {
          fprintf(stderr, "Failed to open %s for writing!\n", writePath);
          exit(-1);
    }
    char* buffer = "Hello, World!";
    tSize num_written_bytes = hdfsWrite(fs, writeFile, (void*)buffer, strlen(buffer)+1);
    if (hdfsFlush(fs, writeFile)) {
           fprintf(stderr, "Failed to ‘flush‘ %s\n", writePath);
          exit(-1);
    }
    hdfsCloseFile(fs, writeFile);
}

Compile script:

compile.sh


#!/bin/bash
export JAVA_HOME=/root/softs/jdk1.8.0_172
export HADOOP_HOME=/root/softs/hadoop-2.7.3/

gcc -I$HADOOP_HOME/include -L$HADOOP_HOME/lib/native -lhdfs -L$JAVA_HOME/jre/lib/amd64/server -ljvm HDFSCSample.c

Execute command:

# chmod +x compile.sh

#./compile.sh

Execute script:

execute.sh


#!/bin/bash
export JAVA_HOME=/root/softs/jdk1.8.0_172
export HADOOP_HOME=/root/softs/hadoop-2.7.3/
export LD_LIBRARY_PATH=$HADOOP_HOME/lib/native:$JAVA_HOME/jre/lib/amd64/server/

CLASSPATH=./
for f in $HADOOP_HOME/share/hadoop/common/*.jar; do
  CLASSPATH=${CLASSPATH}:$f;
done
for f in $HADOOP_HOME/share/hadoop/common/lib/*.jar; do
  CLASSPATH=${CLASSPATH}:$f;
done
for f in $HADOOP_HOME/share/hadoop/hdfs/*.jar; do
  CLASSPATH=${CLASSPATH}:$f;
done
for f in $HADOOP_HOME/share/hadoop/hdfs/lib/*.jar; do
  CLASSPATH=${CLASSPATH}:$f;
done
export CLASSPATH=$CLASSPATH

./a.out

Execute command:

# chmod +x execute.sh

#./execute.sh

Open/tmp/testfile.txt, you can see the inside write a Hello, world!

Configuration and use of Libhdfs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.