gpt4 book ai didi

hadoop_mapreduce_wordcount 字符串到文本或文本到字符串

转载 作者:可可西里 更新时间:2023-11-01 17:01:37 25 4
gpt4 key购买 nike

选择计数(*)从 F其中 A = '李'

我想使用 wordcount 示例将此查询转化为代码。

public class WordCountDriver {
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();

Job job = new Job(conf, "word count");
job.setJarByClass(WordCountDriver.class);
job.setMapperClass(WordCountMapper.class);
job.setInputFormatClass(TextInputFormat.class);
FileInputFormat.addInputPath(job, new Path(args[1]));

job.setReducerClass(WordCountReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
FileOutputFormat.setOutputPath(job, new Path(args[2]));

job.waitForCompletion(true);
}
}

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

public void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
StringTokenizer itr = new StringTokenizer(value.toString());
while (itr.hasMoreTokens()) {
context.write(new Text(itr.nextToken()), one);
}
}
}

public class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
private IntWritable result = new IntWritable();

public void reduce(Text key, Iterable<IntWritable> values, Context context)
throws IOException, InterruptedException {
int sum = 0;

Text a;
String convertkey;
convertkey = "Lee";
a = new Text(convertkey);

if( key == a){

for (IntWritable val : values) {
sum += val.get();
}
result.set(sum);
context.write(key, result);
}
}
}

但是如果循环不起作用。 :(它不能工作 'if(k==a)'如果我使用“if(k!=a)”,它会起作用。为什么它知道 k 不等于 a??

最佳答案

您不想使用 ==。请改用 equals() 方法。但是请注意,该键是文本而不是像“Lee”这样的字符串。您需要对 Text 使用 toString() 方法来获取要比较的 String。

关于hadoop_mapreduce_wordcount 字符串到文本或文本到字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23419111/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com