티스토리 뷰

   HDFS를 Java에서 사용하기 위해서는 먼저 프로젝트에 2개의 jar 파일을 추가해줘야 한다.


hadoop-core

commons-logging


   maven이 설치되어 있다면 간단하게 검색해서 추가할 수 있다.



   그리고 아래는 코드다.


import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.Path;

public class HdfsTest {

	public static final String inputPath = "hdfs://경로";
	public static final String inputmsg = "hello~~\n";
	
	/**
	 * 시험 삼아..
	 * @param args
	 */
	public static void main(String [] args) throws IOException {
		
		// hadoop의 configuration을 생성
		Configuration config = new Configuration();
		
		Path filenamePath = new Path(inputPath);
		
		// config를 HDFS로 parse
		FileSystem fs = filenamePath.getFileSystem(config);
		
		try {
		
			// inputmsg를 HDFS에 write
			FSDataOutputStream fin = fs.create(filenamePath);
			fin.writeUTF(inputmsg);
			fin.close();
			
			// filenamePath file을 읽어들임
			FSDataInputStream fout = fs.open(filenamePath);
			String msgIn = fout.readUTF();
			
			// 콘솔창에 출력
			System.out.println(msgIn);
			
			fout.close();
			fs.close();
		
		}catch(IOException ioe){
			System.err.println("IOException during operation " + ioe.toString());
			System.exit(1);
		}
		
	}
	
}


   보면 대충 어떻게 돌아가는지 이해할 수 있을 것이다.


   여러가지 사용 예를 보고 싶다면,

   이곳 참고☞ http://myjavanotebook.blogspot.kr/2008/05/hadoop-file-system-tutorial.html



댓글
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함