복붙노트

[HADOOP] Hadoop Map 텍스트 파일 읽기 줄이기

HADOOP

Hadoop Map 텍스트 파일 읽기 줄이기

입력 파일을 읽고 다른 텍스트 파일에 출력을 쓸 수있는 MapReduce 프로그램을 작성하려고합니다. 이를 위해 BufferedReader 클래스를 사용할 계획입니다. 그러나 MapReduce 프로그램에서이를 사용하는 방법을 알지 못합니다.

다른 사람이 나에게 코드 스 니펫을 줄 수 있습니까?

추신 Hadoop과 MapReduce 프로그래밍에 완전히 익숙합니다. 그러니 나와 함께 견뎌주십시오.

미리 감사드립니다.

해결법

  1. ==============================

    1.아래 코드는 HDFS에서 파일을 읽고 콘솔에 내용을 표시하는 데 도움이됩니다.

    아래 코드는 HDFS에서 파일을 읽고 콘솔에 내용을 표시하는 데 도움이됩니다.

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.fs.FileSystem;
    import org.apache.hadoop.fs.Path;
    
    public class Cat{
        public static void main (String [] args) throws Exception{
            try{
                Path pt=new Path("hdfs:/path/to/file");//Location of file in HDFS
                FileSystem fs = FileSystem.get(new Configuration());
                BufferedReader br=new BufferedReader(new InputStreamReader(fs.open(pt)));
                String line;
                line=br.readLine();
                while (line != null){
                    System.out.println(line);
                    line=br.readLine();
                }
            }catch(Exception e){
            }
        }
    }
    

    편집하다

    운전사

    public class ReadFile {
    
        public static void main(String[] args) throws Exception {
            Configuration conf = new Configuration();
            Job job = new Job(conf, "Read a File");
    
    
            FileSystem fs = FileSystem.get(conf);
            job.setOutputKeyClass(Text.class);
            job.setOutputValueClass(IntWritable.class);
            if (fs.exists(new Path(args[1])))
                fs.delete(new Path(args[1]), true);
            job.setMapperClass(Map.class);
            job.setReducerClass(Reduce.class);
    
            job.setInputFormatClass(TextInputFormat.class);
            job.setOutputFormatClass(TextOutputFormat.class);
    
            FileInputFormat.addInputPath(job, new Path(args[0]));
            FileOutputFormat.setOutputPath(job, new Path(args[1]));
            job.setJarByClass(ReadFile.class);     
            job.waitForCompletion(true);
        }
    
    }
    

    매퍼

    public class Map extends Mapper<LongWritable, Text, Text, IntWritable> {
    
        public void setup(Context context) throws IOException{
            Path pt=new Path("hdfs:/path/to/file");//Location of file in HDFS
            FileSystem fs = FileSystem.get(new Configuration());
            BufferedReader br=new BufferedReader(new InputStreamReader(fs.open(pt)));
            String line;
            line=br.readLine();
            while (line != null){
                System.out.println(line);
                line=br.readLine();
            }
        }
        public void map(LongWritable key, Text value, Context context)
                throws IOException, InterruptedException {
          //as your wish
            }
        }
    }
    

    위의 코드는 HDFS에서 텍스트 파일을 읽는 데 도움이됩니다.

  2. from https://stackoverflow.com/questions/26209773/hadoop-map-reduce-read-a-text-file by cc-by-sa and MIT license