[zz] HDFS 파일 읽 기와 쓰기 c api 사용

5270 단어 hdfs
1. haddop 에서 제공 하 는 C API to HDFS 로 파일 을 HDFS 에 기록 합 니 다.그 과정 에서 주로 환경 설정 에 시간 이 좀 걸 렸 어 요.
참조 홈 페이지: http://hadoop.apache.org/common/docs/r0.20.203.0/libhdfs.html
다시 컴 파일 하지 않 고 $HADOOPHOME / c + + / Linux - linux - amd64 - 64 / lib 를 컴 파일 하면 됩 니 다. libhdfs 를 컴 파일 하려 면 hadop 맨 위 디 렉 터 리 에서 실행 하 십시오: ant copile - c + - libhdfs - Dislibhdfs = true
API 는 주로 hadop 패키지 압축 해제 디 렉 터 리 에서 hdfs. h 정의 가 실 현 된 함수 들 을 볼 수 있 습 니 다.
한 언어의 초학 입문 예 는 일반적으로 "hello, World" 입 니 다. 다음은 hdfs 파일 코드, 파일 hello 를 봅 니 다.hdfs.c
 1 #include "hdfs.h"
 2 
 3  int main(int argc, char **argv) {
 4 
 5     hdfsFS fs = hdfsConnect("127.0.0.1", 9000);
 6     const char* writePath = "/tmp/testfile.txt";
 7     hdfsFile writeFile = hdfsOpenFile(fs, writePath, O_WRONLY|O_CREAT, 0, 0, 0);
 8     if(!writeFile) {
 9           fprintf(stderr, "Failed to open %s for writing!
", writePath); 10 exit(-1); 11 } 12 char* buffer = "Hello, World!"; 13 tSize num_written_bytes = hdfsWrite(fs, writeFile, (void*)buffer, strlen(buffer)+1); 14 if (hdfsFlush(fs, writeFile)) { 15 fprintf(stderr, "Failed to 'flush' %s
", writePath); 16 exit(-1); 17 } 18 hdfsCloseFile(fs, writeFile); 19 return 0; 20 }

 
컴 파일 명령:
gcc hello_hdfs.c -I${HADOOP_HOME}/src/c++/libhdfs -I${JAVA_HOME}/include -I${JAVA_HOME}/include/linux  -L${HADOOP_HOME}/c++/Linux-Linux-amd64-64/lib -lhdfs -L${JAVA_HOME}/jre/lib/amd64/server -ljvm -o hello_hdfs
실행 중 보고: error while loading shared libraries: libjvm. so: cannot open shared object file: No such file or directory 이 건 LDLIBRARY_PATH 가 설정 되 어 있 지 않 음:
이것 은 내 컴퓨터 의 설정 입 니 다: / etc / profile (Ubuntu 에 bug 가 있 습 니 다. 여기에 설 치 된 모든 환경 변 수 는 all user wide 입 니 다. LD LIBRARY PATH 를 제외 하고 다시 source 해 야 합 니 다)
export JAVA_HOME=/usr/lib/jvm/java-6-sunexport LD_LIBRARY_PATH=$JAVA_HOME/jre/lib/amd64/serverexport PATH=$PATH:$JAVA_HOME/binexport HADOOP_HOME=/home/hadoop/hadoop-0.20.203.0export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$HADOOP_HOME/hadoop-core-0.20.203.0.jar:$HADOOP_HOME/lib/commons-logging-1.1.1.jar:$HADOOP_HOME/lib/commons-configuration-1.6.jar:$HADOOP_HOME/lib/commons-lang-2.4.jar
파일 기록 여 부 를 보 려 면 bin / hadop fs - ls / tmp 를 보 거나 로 컬 로 직접 복사 하 십시오: bin / hadop fs - get / tmp / testfile. txt / home / test
기록 에 성공 하지 못 하면 hadop 을 시작 하 는 것 이 이 프로그램 을 실행 하 는 것 과 같은 사용자 인지 확인 해 야 합 니 다!

좋은 웹페이지 즐겨찾기