Crunch 学习(一)
来源:互联网 发布:开源电商平台php 编辑:程序博客网 时间:2024/06/03 15:18
Crunch 提供一种Mapreduce顶层抽象,简化Mapreduce的创建,降低入门成本。Crunch的亮点在于:允许在不使用Mapreduce结构的情况下,使用java对Mapreduce管道进行建模;可以不必像使用Pig和Hive那样在编写UDF时强制使用自带的数据类型,而且Crunch不强迫程序员使用自带的类型系统。
简单例子
public class MaxTemperatureCrunch extends Configured implements Tool,Serializable { public static void main(String[] args) throws Exception { int result = ToolRunner.run(new Configuration(), new MaxTemperatureCrunch(), args); System.exit(result); } static DoFn<String,Pair<String,Integer>> toYearTempPairsFn(){ return new DoFn<String, Pair<String, Integer>>() { NcdcRecordParser parser = new NcdcRecordParser(); @Override public void process(String input, Emitter<Pair<String, Integer>> emitter) { parser.parse(input); if (parser.isValidTemperature()){ emitter.emit(Pair.of(parser.getYear(), parser.getAirTemperature())); } } }; } @Override public int run(String[] args) throws Exception { Pipeline pipeline = new MRPipeline(MaxTemperatureCrunch.class,"max temperature"); PCollection<String> records = pipeline.readTextFile("hdfs://hadoop:9000/user/hadoop/temp/"); PTable<String, Integer> yearTemperatures = records .parallelDo(toYearTempPairsFn(), tableOf(strings(), ints())); PTable<String, Integer> maxTemps = yearTemperatures .groupByKey() .combineValues(Aggregators.MAX_INTS()); maxTemps.write(To.textFile("hdfs://hadoop:9000/user/hadoop/temp-out")); PipelineResult result = pipeline.done(); return result.succeeded() ? 0 : 1; }}
在Crunch中,每个Job都是开始与一个pipeline实例,它管理这数据管道的生命周期。pipeline主要有3种,MRPipeline 在本地或者Hadoop集群上运行mapreduce任务;MemPipeline在内存中运行一个pipeline,主要用于测试;SparkPipeline在本地或者hadoop集群上运行spark job。
PCollection<String> lines = pipeline.readTextFile(inputPath);
Pcollection是Crunch API中的核心数据抽象概念,是一个分布式的,无序的集合。pipeline接口上的readTextFile()方法可以很方便的将一个文本文件转换成一个String泛型的Pcollection对象,当然也可以创建各种Hadoop InputFormat类型Pcollection。Source是一个接口,定义包装 InputFormat的配置,并且将InputFormat格式的数据读入pipeline。
读取数据后,需要对Pcollection每一个行记录进行处理。
PTable<String, Integer> yearTemperatures = records .parallelDo(toYearTempPairsFn(), tableOf(strings(), ints()));
toYearTempPairsFn()方法返回一个DoFn
0 0
- Crunch 学习(一)
- crunch学习一
- Crunch学习(二)
- Hello Crunch
- crunch使用说明
- select Crunch gym locations
- Unknown Command 'crunch 解决办法
- ERROR: Unknown command 'crunch'
- Failed to crunch file
- fail to crunch file
- FREEBSD中使用crunch编译程序
- ERROR: UNKNOWN COMMAND 'CRUNCH' 解决方法
- ERROR: Unknown command 'crunch' 解决方法
- ERROR: Unknown command 'crunch' 解决方法
- Apache Crunch设计:基础数据处理
- crunch - 根据字符集生成字典
- Crunch字典生成器的使用
- kali渗透测试工具:Crunch
- Android 原生开发、H5、React-Native使用利弊和场景技术分享
- iOS设计模式(四) 单例模式
- 单例模式学习
- 互联网时代的社会语言学:基于SNS的文本数据挖掘
- Vim多行缩进及高级命令
- Crunch 学习(一)
- h264 流、帧结构
- 选择排序
- django相关
- Android自定义控件-不同形状的ImageView
- Format(const wchar_t *,...)”: 不能将参数 1 从“const char [3]”转换为“const wchar_t *”.
- Android面试经验总结
- java LinkedList源码解读
- 仿美团实现城市定位。