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:
- # Include "hdfs. h"
- IntMain (IntArgc,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: 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:
- LIB =-L $ (HADOOP_INSTALL)/c ++/Linux-i386-32/lib/
- Libjvm =/usr/lib/jvm/java-6-openjdk/jre/lib/i386/client/libjvm. so
The complete makefile is:
- HADOOP_INSTALL =/home/fzuir/hadoop-0.20.203.0
- PLATFORM = Linux-i386-32
- JAVA_HOME =/usr/lib/jvm/java-6-openjdk/
- CPPFLAGS =-I $ (HADOOP_INSTALL)/src/c ++/libhdfs
- LIB =-L $ (HADOOP_INSTALL)/c ++/Linux-i386-32/lib/
- Libjvm =/usr/lib/jvm/java-6-openjdk/jre/lib/i386/client/libjvm. so
- LDFLAGS + =-lhdfs
- TestHdfs: testHdfs. c
- Gcc testHdfs. c $ (CPPFLAGS) $ (LIB) $ (LDFLAGS) $ (libjvm)-o testHdfs
- Clean:
- 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.