2015-01-09 7 views
0

저는 MapReduce를 사용하여 텍스트 파일을 시퀀스 파일로 변환합니다. 각 줄의 시작 부분에 숫자가 표시됩니다. 어떻게 그들을 제거하거나 내 산출물에서 오는 것을 막을 수 있습니까?MapReduce를 사용하여 텍스트를 시퀀스로 변환하면 정크 문자가 생성됩니다.

텍스트 :

d001 Marketing 

d002 Finance 

d003 Human Resources 

변환 된 시퀀스 파일 : 시퀀스 파일

0 d001 Marketing 

15 d002 Finance 

28 d003 Human Resources 

에서

0 d001 Marketing 

15 d002 Finance\n 

28 d003 Human Resources 

변환 된 텍스트는 내가 0 15 28 값을 제거해야합니다.

나는 다음과 같은 코드를 사용하고 있습니다 :

public class FormatConverterTextToSequenceDriver extends Configured implements Tool { 

    @Override 
    public int run(String[] args) throws Exception { 

    if (args.length != 2) { 
     System.out.printf("Two parameters are required for FormatConverterTextToSequenceDriver-<input dir> <output dir>\n"); 
     return -1; 
    } 

    Job job = new Job(getConf()); 
    job.setJarByClass(FormatConverterTextToSequenceDriver.class); 
    job.setJobName("Create Sequence File, from text file"); 

    FileInputFormat.setInputPaths(job, new Path(args[0])); 
    FileOutputFormat.setOutputPath(job, new Path(args[1])); 

    job.setMapperClass(FormatConverterMapper.class); 
    job.setOutputFormatClass(SequenceFileOutputFormat.class); 

    job.setNumReduceTasks(0); 

    boolean success = job.waitForCompletion(true); 
    return success ? 0 : 1; 
    } 
----------------------------------------------------------------- 
public class FormatConverterSequenceToTextDriver extends Configured implements Tool { 

    @Override 
    public int run(String[] args) throws Exception { 

    if (args.length != 2) { 
     System.out 
      .printf("Two parameters need to be supplied - <input dir> and <output dir>\n"); 
     return -1; 
    } 

    Job job = new Job(getConf()); 
    job.setJarByClass(FormatConverterSequenceToTextDriver.class); 
    job.setJobName("Convert Sequence File and Output as Text"); 

    FileInputFormat.setInputPaths(job, new Path(args[0])); 
    FileOutputFormat.setOutputPath(job, new Path(args[1])); 

    job.setInputFormatClass(SequenceFileInputFormat.class); 
    job.setMapperClass(FormatConverterMapper.class); 
    job.setNumReduceTasks(0); 

    boolean success = job.waitForCompletion(true); 
    return success ? 0 : 1; 
    } 
----------------------------------------------------------------- 
public class FormatConverterMapper extends 
    Mapper<LongWritable, Text, LongWritable, Text> { 

    @Override 
    public void map(LongWritable key, Text value, Context context) 
     throws IOException, InterruptedException { 
    context.write(key, value); 
    } 
} 

어떤 도움에 감사드립니다.

+2

이것은 정크가 아니므로'FormatConverterMapper'에 숫자를 쓰고 있습니다 (줄 번호 또는 바이트 오프셋과 같음). 이 클래스의 코드가 없으면 귀하를 도울 수 없습니다. –

+0

요청한 클래스의 코드를 질문에 추가하고 있습니다. –

답변

0

시퀀스 파일을 텍스트로 다시 변환 할 때 작성한 길이를 추가하지 않으려 고합니다. 쓰기 방법을 다음과 같이 조정하십시오 :

@Override 
public void map(LongWritable key, Text value, Context context) 
     throws IOException, InterruptedException { 
    context.write(value, null); 
    } 

출력은 값 자체 여야합니다.

+0

도움 주셔서 감사합니다 –

0

참조 용 작업 코드 작성. 텍스트 파일에서 시퀀스 파일을 생성 드라이버 코드 2 개 인수에 소요 소스 텍스트 파일의 경로와 대상 시퀀스 파일 경로

SequenceFileGenMapper -

4 개 클래스

SequenceFileGenDriver로 구성 - Text 파일을 Sequence File로 변환하는 Mapper입니다. 파일을

를 텍스트 시퀀스 파일 변환 매퍼 - 텍스트 파일 시퀀스 파일 변환 드라이버 코드, (2 개) 인수가 입력 시퀀스 파일 경로 및 출력 텍스트 파일 경로

TextFileGenMapper 소요 -가 TextFileGenDriver

public class SequenceFileGenDriver { public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); Job job = new Job(conf); job.setJarByClass(SequenceFileGenDriver.class); job.setMapperClass(SequenceFileGenMapper.class); job.setNumReduceTasks(0); job.setInputFormatClass(TextInputFormat.class); job.setOutputFormatClass(SequenceFileOutputFormat.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(NullWritable.class); TextInputFormat.addInputPath(job, new Path(args[0])); SequenceFileOutputFormat.setOutputPath(job, new Path(args[1])); job.waitForCompletion(true); } } public class SequenceFileGenMapper extends Mapper<LongWritable, Text, Text, NullWritable> { private final static NullWritable nullWritable = NullWritable.get(); public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { context.write(value, nullWritable); } } public class TextFileGenDriver { public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); Job job = new Job(conf); job.setJarByClass(TextFileGenDriver.class); job.setMapperClass(TextFileGenMapper.class); job.setInputFormatClass(SequenceFileInputFormat.class); job.setOutputFormatClass(TextOutputFormat.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(NullWritable.class); job.setNumReduceTasks(0); SequenceFileInputFormat.addInputPath(job, new Path(args[0])); TextOutputFormat.setOutputPath(job, new Path(args[1])); job.waitForCompletion(true); } } public class TextFileGenMapper extends Mapper<Text, NullWritable, Text, NullWritable> { private final static NullWritable nullWritable = NullWritable.get(); public void map(Text key, NullWritable value, Context context) throws IOException, InterruptedException { context.write(key, nullWritable); } } 
+1

도움 주셔서 감사합니다. –