0

I'm trying to read from a txt file and write to 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);
        }
    }

But I'm unsucessful in doing same.

I'm getting following error java.lang.ClassCastException: org.apache.hadoop.io.LongWritable cannot be cast to org.apache.hadoop.io.Text

4

2 に答える 2

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());
    }
于 2012-05-03T13:38:35.153 に答える
0

どうやら MR はデフォルトで MapOutputKeyClass として LongWritable に設定されているようです。この場合、これは Text であるため、エラーになります。

job.setMapOutputKeyClass(Text.class)を設定し、job.setMapOutputValueClass も適切に設定してみてください。

于 2012-04-19T17:33:23.223 に答える