2012-04-19 3 views
0

나는 txt 파일에서 읽고 HBase에 쓰려고합니다.txt 파일에서 읽기 및 HBase에 쓰기

Job Class 
    Job job = new Job(conf, "HWriterJob"); 
     job.setJarByClass(HWriterJob.class); 
     FileInputFormat.setInputPaths(job, new Path(otherArgs[0])); 

     job.setMapperClass(TokenizerMapper.class); 
     job.setOutputKeyClass(ImmutableBytesWritable.class); 
     job.setOutputValueClass(Put.class); 

     TableMapReduceUtil.initTableReducerJob(table,null,job); 

Mapper Class 
@Override 
    public void map(Text key, Text value, Context context) 
      throws IOException, InterruptedException { 

     String line = value.toString(); 

     StringTokenizer st = new StringTokenizer(line, "|"); 
     String result[] = new String[st.countTokens()]; 
     int i = 0; 
     while (st.hasMoreTokens()) { 
      result[i] = st.nextToken(); 
      i++; 
     } 

     Map<ImmutableBytesWritable,Put> resultSet = writeToHBase(result); 

     for (Map.Entry<ImmutableBytesWritable,Put> entry : resultSet.entrySet()) { 
      context.write(new Text(entry.getValue().getRow()), entry.getValue()); 
     } 
    } 

Reducer Class 

public void reduce(Text key, Iterable<Put> values, Context context) 
      throws IOException, InterruptedException { 

     for (Put val : values) { 
      context.write(key, val); 
     } 
    } 

하지만 나는 똑같이하는 데에있어 불평합니다.

나는 다음과 같은 오류 java.lang.ClassCastException가납니다 : org.apache.hadoop.io.LongWritable는 MapOutputKeyClass로 LongWritable에 org.apache.hadoop.io.Text에

답변

0

분명히 MR 기본값을 캐스팅 할 수 없습니다 귀하의 경우 텍스트 및 따라서 오류가 있어야합니다.

job.setMapOutputKeyClass (Text.class)를 설정하십시오 및 도 적절하게 job.setMapOutputValueClass을 설정합니다.

0
  @Job Class 
      Job job = new Job(conf, "HWriterJob"); 
    job.setJarByClass(HWriterJob.class); 
    FileInputFormat.setInputPaths(job, new Path(otherArgs[0])); 

    job.setMapperClass(TokenizerMapper.class); 
    TextInputFormat.setInputPaths(job, new Path(args[0])); 
    job.setInputFormatClass(TextInputFormat.class); 
    job.setOutputKeyClass(ImmutableBytesWritable.class); 
     job.setOutputValueClass(Put.class); 
    job.setOutputFormatClass(TableOutputFormat.class); 

    job.setNumReduceTasks(0); 
    System.exit(job.waitForCompletion(true) ? 0 : 1); 
      @Mapper 
      Map<ImmutableBytesWritable,Put> resultSet = writeToHBase(result); 

    for (Map.Entry<ImmutableBytesWritable,Put> entry : resultSet.entrySet()) { 
     context.write(entry.getKey(), entry.getValue()); 
    }