스프링에서 하둡 연동

스프링에서 하둡 연동

현재 hadoop은 버전 2.7까지 나와있지만, 스프링에서는 높은 버전을 쓸 수 없다. 현재 hadoop-core 라이브러리(스프링에서 하둡을 바로 붙을 수 있도록 지원)가 1.2.1 까지 밖에 지원이 안된다. 하둡을 1.2.1로 깔고 그것에 나머지 하둡 에코시스템을 맞춰야하는 한계점이 있다.

1. hadoop-core 

<!– http://mvnrepository.com/artifact/org.apache.hadoop/hadoop-core –>
<dependency>
    <groupId>org.apache.hadoop</groupId>
    <artifactId>hadoop-core</artifactId>
    <version>1.2.1</version>
</dependency>
 


2. app.xml

<?xml version=”1.0″ encoding=”UTF-8″?>
<beans xmlns=”http://www.springframework.org/schema/beans
    xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance
    xmlns:aop=”http://www.springframework.org/schema/aop
    xmlns:context=”http://www.springframework.org/schema/context
    xmlns:hadoop=”http://www.springframework.org/schema/hadoop
    xmlns:p=”http://www.springframework.org/schema/p
    xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/hadoop http://www.springframework.org/schema/hadoop/spring-hadoop.xsd“>

    <!– 메모리 할당 –>
    <context:annotation-config/>
    <context:component-scan base-package=”com.com.*”></context:component-scan>
   
    <!– 하둡 설정 –>
    <!– private Configuration conf; –>
    <hadoop:configuration id=”hadoopConf”>
        fs.default.name=hdfs://localhost:9000
    </hadoop:configuration>

   
</beans>

3. FileSystemMain.java

package com.sist.movie;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

@Component(“fsm”)
public class FileSystemMain {
   
    @Autowired
    private Configuration conf;
   
    public static void main(String[] args) {
        //Component: 자동메모리 할당
        //Autowired : 주소값 설정
       
        try{
           
            ApplicationContext app = new ClassPathXmlApplicationContext(“app.xml”);
           
            FileSystemMain fm = (FileSystemMain) app.getBean(“fsm”);
           
            FileSystem fs = FileSystem.get(fm.conf);
           
            1) 로컬에서 하둡으로 파일 보내기
            fs.copyFromLocalFile(new Path(“/home/sist/emp.csv”),
                                    new Path(“/input/emp.csv”) );
           
            //(2) 하둡에서 로컬로 파일 보내기
//            fs.copyToLocalFile(new Path(“/input/emp.csv”),
//                                    new Path(“./”) );
           
            //(3) 하둡 내의 파일 삭제하기
            //if( fs.exists(new Path(“/input/emp.csv”))){ //존재하면
            //    fs.delete(new Path(“/input/emp.csv”), true); //삭제

                // 두번째 매개변수에 true를 주면 안쪽 파일까지 삭제되는 것
                // ( 리눅스의 rm -rf와 같음. 하둡에서는 -rmr )
            //}
           
            fs.close();
           
            System.out.println(“파일전송완료”);
        }catch(Exception e){
            System.out.println(“main error :”);
            e.printStackTrace();
        }
    }
}