在hadoop map reduce中更改默认分隔符

63lcw9qa  于 2021-06-03  发布在  Hadoop
关注(0)|答案(5)|浏览(361)

我对hadoop的map reduce概念完全陌生
我有一些文件是用“::”分隔的,而不是用空格(“”)分隔的。
hadoop map reduce是否默认使用空格作为分隔符。如果是这样,如何将其更改为接受用户定义的分隔符。
谢谢
谢谢praveen,100gods和eric给我的指导。再次出现了一个问题。我将编写我的代码和错误
我想我可能做错了。所以请澄清我的问题
再次感谢 enter code here ```
package com;

import java.io.IOException;
import java.util.Iterator;
import java.util.StringTokenizer;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.FileInputFormat;
import org.apache.hadoop.mapred.FileOutputFormat;
import org.apache.hadoop.mapred.JobClient;
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapred.KeyValueTextInputFormat;
import org.apache.hadoop.mapred.MapReduceBase;
import org.apache.hadoop.mapred.Mapper;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reducer;
import org.apache.hadoop.mapred.Reporter;
import org.apache.hadoop.mapred.TextInputFormat;
import org.apache.hadoop.mapred.TextOutputFormat;

public class WordCount {

public static class Map extends MapReduceBase implements
        Mapper<LongWritable, Text, Text, IntWritable> {
    private final static IntWritable one = new IntWritable(1);
    private Text word = new Text();

    public void map(LongWritable key, Text value,
            OutputCollector<Text, IntWritable> output, Reporter reporter)
            throws IOException {
        String line = value.toString();

        StringTokenizer tokenizer = new StringTokenizer(line);
        while (tokenizer.hasMoreTokens()) {
            word.set(tokenizer.nextToken());
            output.collect(word, one);
        }
    }
}

public static class Reduce extends MapReduceBase implements
        Reducer<Text, IntWritable, Text, IntWritable> {
    public void reduce(Text key, Iterator<IntWritable> values,
            OutputCollector<Text, IntWritable> output, Reporter reporter)
            throws IOException {
        int sum = 0;
        while (values.hasNext()) {
            sum += values.next().get();
        }
        output.collect(key, new IntWritable(sum));
    }
}

public static void main(String[] args) throws Exception {
    /*
     * JobConf conf = new JobConf(WordCount.class);
     * conf.setJobName("wordcount");
     */
    Configuration configuration = new Configuration();
    JobConf conf = new JobConf(configuration);

    conf.setOutputKeyClass(Text.class);
    conf.setOutputValueClass(IntWritable.class);

    conf.setMapperClass(Map.class);
    conf.setCombinerClass(Reduce.class);
    conf.setReducerClass(Reduce.class);

    conf.setInputFormat(KeyValueTextInputFormat.class);
    conf.set("key.value.separator.in.input.line", ":::");

    // conf.setInputFormat(TextInputFormat.class);
    conf.setOutputFormat(TextOutputFormat.class);
    // conf.set("mapreduce.input.keyvaluelinerecordreader.key.value.separator",
    // ":::");

    FileInputFormat.setInputPaths(conf, "/home/vishal/note.txt");
    FileOutputFormat.setOutputPath(conf, new Path("/home/vishal/output"));

    JobClient.runJob(conf);
}

}

下面是错误

12/10/30 12:23:04 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
12/10/30 12:23:04 WARN mapred.JobClient: Use GenericOptionsParser for parsing the arguments. Applications should implement Tool for the same.
12/10/30 12:23:04 WARN mapred.JobClient: No job jar file set. User classes may not be found. See JobConf(Class) or JobConf#setJar(String).
12/10/30 12:23:04 WARN snappy.LoadSnappy: Snappy native library not loaded
12/10/30 12:23:04 INFO mapred.FileInputFormat: Total input paths to process : 1
12/10/30 12:23:04 INFO mapred.JobClient: Running job: job_local_0001
12/10/30 12:23:04 INFO util.ProcessTree: setsid exited with exit code 0
12/10/30 12:23:04 INFO mapred.Task: Using ResourceCalculatorPlugin : org.apache.hadoop.util.LinuxResourceCalculatorPlugin@193722c
12/10/30 12:23:04 INFO mapred.MapTask: numReduceTasks: 1
12/10/30 12:23:04 INFO mapred.MapTask: io.sort.mb = 100
12/10/30 12:23:05 INFO mapred.MapTask: data buffer = 79691776/99614720
12/10/30 12:23:05 INFO mapred.MapTask: record buffer = 262144/327680
12/10/30 12:23:05 WARN mapred.LocalJobRunner: job_local_0001

`java.lang.ClassCastException: org.apache.hadoop.io.Text cannot be cast to org.apache.hadoop.io.LongWritable
at com.WordCount$Map.map(WordCount.java:1)
at org.apache.hadoop.mapred.MapRunner.run(MapRunner.java:50)
at org.apache.hadoop.mapred.MapTask.runOldMapper(MapTask.java:436)
at org.apache.hadoop.mapred.MapTask.run(MapTask.java:372)
at org.apache.hadoop.mapred.LocalJobRunner$Job.run(LocalJobRunner.java:212)
12/10/30 12:23:05 INFO mapred.JobClient: map 0% reduce 0%
12/10/30 12:23:05 INFO mapred.JobClient: Job complete: job_local_0001
12/10/30 12:23:05 INFO mapred.JobClient: Counters: 0
12/10/30 12:23:05 INFO mapred.JobClient: Job Failed: NA
Exception in thread "main" java.io.IOException: Job failed!
at org.apache.hadoop.mapred.JobClient.runJob(JobClient.java:1327)
at com.WordCount.main(WordCount.java:84)

23c0lvtd

23c0lvtd1#

它应该是文本,而不是你正在使用的长写 KeyValuetextInputFormat.class ```
public static class Map extends MapReduceBase implements
Mapper<Text, Text, Text, IntWritable> {
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();

    public void map(Text key, Text value,
            OutputCollector<Text, IntWritable> output, Reporter reporter)
            throws IOException {
采用新api

public class Map extends Mapper<Text, Text, Text, IntWritable> {
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();

public void map(Text key, Text value, Context context)
        throws IOException, InterruptedException {
utugiqy6

utugiqy62#

您必须使用旧的hadoopapi来使用keyvaluetextinputformat。下面的代码将完成这项工作。

Configuration configuration=new Configuration();
JobConf conf=new JobConf(configuration);
conf.setInputFormat(KeyValueTextInputFormat.class);
conf.set("key.value.separator.in.input.line", ":::");

note:keyvaluetextinputformat has 已从新的hadoop api中删除。

7cwmlq89

7cwmlq893#

您使用的是keyvaluetextinputformat,并且指定了分隔符。发送到Map作业的键=>值对是文本类型。
尝试将Map器类定义更改为:

public static class Map extends MapReduceBase implements
        Mapper<Text, Text, Text, IntWritable> {

           .....

    public void map(Text key, Text value,
            OutputCollector<Text, IntWritable> output, Reporter reporter)
            throws IOException {
           .....
    }
}

注意:您的应用程序正在使用旧的api。尝试切换到新的,它简化了很多事情(例如:outputcollector和reporter已经融合到上下文中)。

ia2d9nvy

ia2d9nvy4#

使用 KeyValueTextInputFormat 因为它允许您选择分隔符。这将允许您使用 Configuration 对象来设置分隔符: conf.set("mapreduce.input.keyvaluelinerecordreader.key.value.separator", ":::");

8zzbczxx

8zzbczxx5#

hadoop中用户定义的map函数以key和value作为输入。对于fileinputformat,键是文件中的行偏移量(通常被忽略),值是输入文件中的一行。由Map程序使用任何分隔符分割输入行(aka值)。或者keyvaluetextinputformat可以在其他查询中使用。

相关问题