配置 windows 10 mapreduce 开发环境
MapReduce
是什么
- 优点:
MR将所有的计算抽象为Map(映射) 与Reduce(聚合) 两个阶段
只需要继承并实现Mapper和Reducer类,就可以完成高性能的分布式程序
与HDFS类似,HDFS是通过将多台机器的存储能力整合到集群中,提供更大的存储能力,MR是通过将多台机器的计算能力(cpu、内存)综合起来,提供海量数据的计算
高并发(多线程)的分布式程序运行过程中,一些线程出现错误或者某些机器出现故障时,MR框架可以自动启动错误重试机制,或将任务转移到其他机器运行,可以保证任务最终正确执行
MR不适合处理小数据量级,而随着数据量级增大,HDFS可以存储的数据量级,MR都可以使用相同的应用程序完成计算
- 缺点:
编程模型
1、用java代码统计文本中每个单词出现的次数
import org.apache.commons.io.FileUtils; import java.io.File; import java.util.*; public class JavaWordCount { public static void main(String[] args) throws Exception { // 0.创建容器存储结果 HashMap<String, Integer> map = new HashMap<String, Integer>(); // 1.读取文件 File file = new File("C:\\projects\\idea\\bigdata2107\\amos\\amos-hadoop\\src\\main\\resources\\Harry.txt"); String encoding = "utf8"; List<String> lines = FileUtils.readLines(file, encoding); // 2.遍历每一行 for (String line : lines) { // 3.切分出每个单词 String[] words = line.split("\\s+"); for (String w : words) { // 4.每出现一个单词进行数量+1 // if (map.containsKey(word)) { // map.put(word, map.get(word) + 1); // } else { // map.put(word, 0 + 1); // } // map.put(word, map.containsKey(word) ? map.get(word) + 1 : 1); String word = w.toLowerCase() .replaceAll("\\W", ""); if (!word.isEmpty()) { map.put(word, map.getOrDefault(word, 0) + 1); } } } // 5.打印结果 System.out.println(map); // 6.将处理结果进行排序 ArrayList<Map.Entry<String, Integer>> entries = new ArrayList<>(map.entrySet()); // entries.sort(new Comparator<Map.Entry<String, Integer>>() { // @Override // public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) { // return o2.getValue() - o1.getValue(); // } // }); entries.sort((o1, o2) -> o2.getValue() - o1.getValue()); for (Map.Entry<String, Integer> entry : entries) { String word = entry.getKey(); Integer count = entry.getValue(); System.out.printf("单词:%s 出现的数量%d\n", word, count); } } }2、用MapperReduce思想
- 通常一个典型的MR程序 需要实现三个类
- Mapper
setup(context) 在map任务执行前 执行一次
map(KEYIN k,VALUEIN v,context) 每次获取一组输入的kv对,进行处理,并将处理完的结果交给context进行写出
cleanup(context) 在map任务执行后 执行一次
run() 将上面三个方法组织起来执行Mapper的逻辑
- Reducer
reduce(KEYIN k, Iterable<VALUEIN> values)方法每次接收一个key和相同Key对应的所有Value
在reduce方法中对数据进行聚合
并将处理完的结果交给context进行写出
- Driver
是一个包含main方法的MR任务的入口类
main中 获取job对象实例并添加各种配置
提交job到集群运行
Windows 10 搭建Python开发环境(PyCharm )pycharm是python的集成开发环境
Java AOT: Achieve Lightning-Fast Startup and Reduced Memory for Optimized Applications
Optimize Circuit Timing with RC Tree: Essential Techniques to Reduce Delays and Boost Performance
解决 nll_loss_forward_reduce_cuda_kernel_2d_index not implemented for 'float' 错误的有效方法
PotatoFieldImageToolkit: Effortless Potato Crop Monitoring for Higher Yields and Reduced Pests
Redis Java Client Comparison: Jedis vs Lettuce vs Redisson for Optimal Performance