Hadoop accesses HDFS through C APIs

Source: Internet
Author: User
When HDFS is accessed through the Hadoop c api, many problems occur during compilation and running. Here, let's make a conclusion: System: Ubuntu11.04, the Hadoop-0.20.203.0 sample code is provided by reference to the official documentation: # include "hdfs. h "intmain (intargc, c

When HDFS is accessed through the Hadoop c api, many problems occur during compilation and running. Here, let's make a summary:

System: Ubuntu 11.04, Hadoop-0.20.203.0

The sample code is provided in the official documentation as follows:

  1. # Include "hdfs. h"
  2. IntMain (IntArgc,Char** Argv ){
  3. HdfsFS fs = hdfsConnect ("Default", 0 );
  4. Const Char* WritePath ="/Tmp/testfile.txt";
  5. HdfsFile writeFile = hdfsOpenFile (fs, writePath, O_WRONLY | O_CREAT, 0, 0, 0 );
  6. If(! WriteFile ){
  7. Fprintf (stderr,"Failed to open % s for writing! \ N", WritePath );
  8. Exit (-1 );
  9. }
  10. Char* Buffer ="Hello, World! ";
  11. TSize num_written_bytes = hdfsWrite (fs, writeFile ,(Void*) Buffer, strlen (buffer) + 1 );
  12. If(HdfsFlush (fs, writeFile )){
  13. Fprintf (stderr,"Failed to 'flush '% s \ n", WritePath );
  14. Exit (-1 );
  15. }
  16. HdfsCloseFile (fs, writeFile );
  17. }

Compile: This is the description on the official website.

See the Makefile for hdfs_test.c in the libhdfs source directory ($ {HADOOP_HOME}/src/c ++/libhdfs/Makefile) or something like:

Gcc above_sample.c-I $ {HADOOP_HOME}/src/c ++/libhdfs-L $ {HADOOP_HOME}/libhdfs-lhdfs-o above_sample

However, I tried both methods. No, but later I found that there were fewer methods:

  1. LIB =-L $ (HADOOP_INSTALL)/c ++/Linux-i386-32/lib/
  2. Libjvm =/usr/lib/jvm/java-6-openjdk/jre/lib/i386/client/libjvm. so

The complete makefile is:

  1. HADOOP_INSTALL =/home/fzuir/hadoop-0.20.203.0
  2. PLATFORM = Linux-i386-32
  3. JAVA_HOME =/usr/lib/jvm/java-6-openjdk/
  4. CPPFLAGS =-I $ (HADOOP_INSTALL)/src/c ++/libhdfs
  5. LIB =-L $ (HADOOP_INSTALL)/c ++/Linux-i386-32/lib/
  6. Libjvm =/usr/lib/jvm/java-6-openjdk/jre/lib/i386/client/libjvm. so
  7. LDFLAGS + =-lhdfs
  8. TestHdfs: testHdfs. c
  9. Gcc testHdfs. c $ (CPPFLAGS) $ (LIB) $ (LDFLAGS) $ (libjvm)-o testHdfs
  10. Clean:
  11. Rm testHdfs

Okay. The following error message is displayed when www.linuxidc.com is compiled:

Exception in thread "main" java. lang. NoClassDefFoundError: org/apache/commons/configuration/Configuration

...

Call to org. apache. hadoop. fs. Filesystem: get (URI, Configuration) failed!

Exception in thread "main" java. lang. NullPointerException

Call to get configuration object from filesystem failed!

Solution: add all HADOOP_HOME and HADOOP_HOME/lib to the jar package/usr/lib/jvm/java-6-openjdk/jre/lib/ext/(as long as you need to add, but I don't know what is needed)

Finally, congratulations, the problem has been solved.

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.