本文介绍的项目都是完整的可编译可运行的项目,包括MapReduce、Pig、Hive和Spark。
示例项目
示例名称如下所示,详情代码示例请参见集群运行。
- MapReduce
WordCount:单词统计
- Hive
sample.hive:表的简单查询
- Pig
sample.pig:Pig处理OSS数据实例
- Spark
- SparkPi:计算Pi
- SparkWordCount:单词统计
- LinearRegression:线性回归
- OSSSample:OSS使用示例
- MaxComputeSample:MaxCompute使用示例
- MNSSample:MNS使用示例
- LoghubSample:Loghub使用示例
依赖资源
- 测试数据(data目录下):
- The_Sorrows_of_Young_Werther.txt:可作为WordCount(MapReduce或Spark)的输入数据。
- patterns.txt:WordCount(MapReduce)作业的过滤字符。
- u.data:sample.hive脚本的测试表数据。
- abalone:线性回归算法测试数据。
- 依赖JAR包(lib目录下):
tutorial.jar:sample.pig作业需要的依赖JAR包。
准备工作
-
创建LogService,请参见快速入门。
-
创建MaxCompute项目和表,请参见创建MaxCompute项目和创建表。
- 创建ONS,请参见快速入门概述。
- 创建MNS,请参见快速入门概述。
基本概念
-
OSSURI:oss://<accessKeyId>:<accessKeySecret>@<bucket>.<endpoint>/a/b/c.txt,用户在作业中指定输入输出数据源时使用,类似hdfs://。
-
阿里云AccessKeyId和AccessKeySecret是您访问阿里云API的密钥。
集群运行
开源项目运行示例如下:
-
Spark
-
SparkWordCount:
spark-submit --class SparkWordCount examples-1.0-SNAPSHOT-shaded.jar <inputPath> <outputPath> <numPartition>
参数说明如下:
参数 描述 inputPath 输入数据路径。 outputPath 输出路径。 numPartition 输入数据RDD分片数目。 -
SparkPi:
spark-submit --class SparkPi examples-1.0-SNAPSHOT-shaded.jar
-
OSSSample:
spark-submit --class OSSSample examples-1.0-SNAPSHOT-shaded.jar <inputPath> <numPartition>
参数说明如下:
参数 描述 inputPath 输入数据路径。 numPartition 输入数据RDD分片数目。 -
ONSSample:
spark-submit --class ONSSample examples-1.0-SNAPSHOT-shaded.jar <accessKeyId> <accessKeySecret> <consumerId> <topic> <subExpression> <parallelism>
参数说明如下:
参数 描述 accessKeyId 阿里云AccessKeyId。 accessKeySecret 阿里云AccessKeySecret。 consumerId 消费者ID。 topic 每个消息队列都有一个 topic。 subExpression 消息子主题。 parallelism 指定多少个接收器来消费队列消息。 -
MaxComputeSample:
spark-submit --class MaxComputeSample examples-1.0-SNAPSHOT-shaded.jar <accessKeyId> <accessKeySecret> <envType> <project> <table> <numPartitions>
参数说明如下:
参数 描述 accessKeyId 阿里云AccessKeyId。 accessKeySecret 阿里云AccessKeySecret。 envType 0表示公网环境,1表示内网环境。
如果是本地调试选择 0,如果是在 E-MapReduce上执行请选择 1。
project 请参见术语表。 table 请参见术语表。 numPartition 输入数据RDD分片数目。 -
MNSSample:
spark-submit --class MNSSample examples-1.0-SNAPSHOT-shaded.jar <queueName> <accessKeyId> <accessKeySecret> <endpoint>
参数说明如下:
参数 描述 queueName Queue的名称。 accessKeyId 阿里云AccessKeyId。 accessKeySecret 阿里云AccessKeySecret。 endpoint 队列数据访问地址。 -
LoghubSample:
spark-submit --class LoghubSample examples-1.0-SNAPSHOT-shaded.jar <sls project> <sls logstore> <loghub group name> <sls endpoint> <access key id> <access key secret> <batch interval seconds>
参数说明如下:
参数 描述 sls project: LogService 项目名。 sls logstore 日志库名。 loghub group name 作业中消费日志数据的组名。sls project和sls store相同时,相同组名的作业会协同消费sls store中的数据;不同组名的作业会相互隔离地消费sls store中的数据。 sls endpoint 请参见日志服务入口。 accessKeyId 阿里云AccessKeyId。 accessKeySecret 阿里云AccessKeySecret。 -
LinearRegression:
spark-submit --class LinearRegression examples-1.0-SNAPSHOT-shaded.jar <inputPath> <numPartitions>
参数说明如下:
参数 描述 inputPath 输入数据路径。 numPartition 输入数据RDD分片数目。
-
-
Mapreduce
WordCount:
hadoop jar examples-1.0-SNAPSHOT-shaded.jar WordCount -Dwordcount.case.sensitive=true <inputPath> <outputPath> -skip <patternPath>
参数说明如下。
参数 描述 inputPath 输入数据路径。 outputPath 输出路径。 patternPath 过滤字符文件,可以使用data或patterns.txt。 -
Hive
hive -f sample.hive -hiveconf inputPath=<inputPath>
参数说明如下。
参数 描述 inputPath 输入数据路径。 -
Pig
pig -x mapreduce -f sample.pig -param tutorial=<tutorialJarPath> -param input=<inputPath> -param result=<resultPath>
参数说明如下:
参数 描述 tutorialJarPath 依赖Jar包,可使用lib或tutorial.jar。 inputPath 输入数据路径。 resultPath 输出路径。
- 在E-MapReduce上使用时,请将测试数据和依赖jar包上传到OSS中,路径规则遵循OSSURI定义。
- 如果集群中使用,可以放在机器本地。
本地运行
- IntelliJ IDEA
- 准备工作
安装IntelliJ IDEA、Maven、 IntelliJ IDEA Maven插件、Scala和IntelliJ IDEA Scala插件。
- 开发流程
-
双击进入SparkWordCount.scala。
-
进入作业配置界面。
-
选择 SparkWordCount,在作业参数框中按照所需传入作业参数。
- 单击 OK。
- 单击运行,执行作业。
-
查看作业执行日志。
-
- 准备工作
- Scala IDE for Eclipse
- 准备工作
安装Scala IDE for Eclipse、Maven、Eclipse Maven插件。
- 开发流程
-
请根据以下图示导入项目。
- Run As Maven build,快捷键是Alt + Shilft + X, M,也可以在项目名上右键选择 。
- 等待编译完后,在需要运行的作业上右键,选择Run Configuration,进入配置页。
-
在配置页中,选择 Scala Application,并配置作业的Main Class和参数等等。如下图所示。
- 单击Run。
-
查看控制台输出日志,如下图所示。
-
- 准备工作