WordCount面试

WordCount面试
#Mapper端代码
package Linux;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

public class WordCountMappper extends Mapper<LongWritable, Text, Text, IntWritable> {

@Override


protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {


    String[] words = value.toString().split(" ");
    for(String word:words)
    {

        context.write(new Text(word),new IntWritable(1));
    }
}

}

#Reduce端代码
package Linux;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class WordCountReduce extends Reducer <Text, IntWritable, Text, IntWritable>{
@Override
protected void reduce(Text key, Iterable values, Context context) throws IOException, InterruptedException {
int sum=0;
for(IntWritable value:values)
{
sum+=value.get();
}
context.write(key,new IntWritable(sum));
}
}

#WordCount端代码
package Linux;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class WordCountJob {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
// Configuration conf=new Configuration();
// Job job=Job.getInstance(conf);
// job.setJarByClass(WordCountJob.class);
// job.setMapperClass(WordCountMappper.class);
// job.setReducerClass(WordCountReduce.class);
// job.setMapOutputKeyClass(Text.class);
// job.setMapOutputValueClass(IntWritable.class);
// job.setOutputKeyClass(Text.class);
// job.setOutputValueClass(IntWritable.class);
// FileInputFormat.setInputPaths(job,new Path(“D:\abc\wordcount.txt”));
// FileOutputFormat.setOutputPath(job,new Path(“D:\abc\wordcount1”));
// job.submit();
Configuration conf = new Configuration();
Job myjob = Job.getInstance(conf);
myjob.setJarByClass(WordCountJob.class);
myjob.setMapperClass(WordCountMappper.class);
myjob.setReducerClass(WordCountReduce.class);

    myjob.setMapOutputKeyClass(Text.class);
    myjob.setMapOutputValueClass(IntWritable.class);

    myjob.setOutputKeyClass(Text.class);
    myjob.setOutputValueClass(IntWritable.class);
    //FileInputFormat.setInputPaths(myjob,new Path("D:\\abc\\wordcount.txt"));
    //FileOutputFormat.setOutputPath(myjob, new Path("D:\\abc\\wordcount1"));
     FileInputFormat.setInputPaths(myjob, new Path(args[0]));
     FileOutputFormat.setOutputPath(myjob, new Path(args[1]));

    myjob.submit();
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Flink WordCount 是 Flink 的一个经典示例,用于演示 Flink 流处理的基本功能。它是一个简单的单词计数程序,可以从输入文本中读取单词,并计算每个单词在文本中出现的次数。 以下是一个 Flink WordCount 的示例代码: ```java import org.apache.flink.api.java.utils.ParameterTool; import org.apache.flink.streaming.api.datastream.DataStream; import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; import org.apache.flink.streaming.api.functions.source.FileSource; import org.apache.flink.streaming.api.functions.source.SourceFunction; import org.apache.flink.streaming.api.windowing.time.Time; public class WordCount { public static void main(String[] args) throws Exception { final ParameterTool params = ParameterTool.fromArgs(args); // 设置执行环境 final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); // 设置数据源 DataStream<String> text; if (params.has("input")) { text = env.readTextFile(params.get("input")); } else { System.out.println("Executing WordCount example with default input data set."); System.out.println("Use --input to specify file input."); text = env.fromElements(WordCountData.WORDS); } // 转换数据流 DataStream<WordWithCount> counts = text.flatMap(new Tokenizer()) .keyBy("word") .timeWindow(Time.seconds(5)) .sum("count"); // 输出结果 if (params.has("output")) { counts.writeAsText(params.get("output")); } else { System.out.println("Printing result to stdout. Use --output to specify output path."); counts.print(); } // 执行任务 env.execute("Streaming WordCount"); } // 单词拆分函数 public static final class Tokenizer implements FlatMapFunction<String, WordWithCount> { @Override public void flatMap(String value, Collector<WordWithCount> out) { String[] tokens = value.toLowerCase().split("\\W+"); for (String token : tokens) { if (token.length() > 0) { out.collect(new WordWithCount(token, 1L)); } } } } // 单词计数类 public static final class WordWithCount { public String word; public long count; public WordWithCount() {} public WordWithCount(String word, long count) { this.word = word; this.count = count; } @Override public String toString() { return word + " : " + count; } } } ``` 该程序使用 Flink 流处理 API 来读取输入文本、拆分单词、计数单词并输出结果。程序的具体执行流程如下: 1. 读取命令行参数或默认数据源。 2. 创建 Flink 执行环境。 3. 读取数据源。 4. 转换数据流,拆分单词并计数。 5. 输出结果到文件或标准输出。 6. 执行任务。 如果你想要运行 Flink WordCount 示例程序,可以按照以下步骤进行: 1. 下载 Flink 并解压。 2. 打开终端并进入 Flink 的安装目录。 3. 运行 `./bin/start-cluster.sh` 启动 Flink 集群。 4. 运行 `./bin/flink run examples/streaming/WordCount.jar --input /path/to/input/file --output /path/to/output/file`。 5. 等待程序执行完成,查看输出结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值