Develop a WordCount MapReduce program in MaxCompute Studio, package it as a JAR file, and run the job on the MaxCompute client.
Prerequisites
Before you begin, make sure the following requirements are met:
-
The MaxCompute client is installed and configured. For more information, see Install and configure the MaxCompute client.
-
MaxCompute Studio is installed and connected to a MaxCompute project. For more information, see Install MaxCompute Studio and Manage project connections.
-
Ensure that your subscription is not the pay-as-you-go Developer Edition.
MapReduce jobs are not supported by the pay-as-you-go Developer Edition.
-
You have a source data file saved on your local machine.
This topic uses a sample file named data.txt that contains
hello,odps. The file is saved in thebindirectory of the MaxCompute client.
Usage notes
If you use Maven to develop a MapReduce program, search for odps-sdk-mapred, odps-sdk-commons, and odps-sdk-core in the Maven Central Repository to obtain the required Java SDK version. This topic uses version 0.36.4-public as an example. Add the following dependencies to the pom.xml file.
<dependency>
<groupId>com.aliyun.odps</groupId>
<artifactId>odps-sdk-mapred</artifactId>
<version>0.36.4-public</version>
</dependency>
<dependency>
<groupId>com.aliyun.odps</groupId>
<artifactId>odps-sdk-commons</artifactId>
<version>0.36.4-public</version>
</dependency>
<dependency>
<groupId>com.aliyun.odps</groupId>
<artifactId>odps-sdk-core</artifactId>
<version>0.36.4-public</version>
</dependency>
Procedure
-
Step 1: Develop a MapReduce program
Write, run, and debug a MapReduce program in MaxCompute Studio.
-
Step 2: Generate and upload a MapReduce JAR file
Package the compiled WordCount.java script into a JAR file and upload it to the MaxCompute project.
-
Run the MapReduce job using the
jarcommand with the JAR file uploaded to the MaxCompute project.
Step 1: Develop a MapReduce program
-
Create a MaxCompute Java module.
-
Start IntelliJ IDEA. In the menu bar, select .
-
In the left-side navigation pane of the New Module dialog box, select MaxCompute Java.
-
Configure the Module SDK and click Next.
-
Enter a Module name, such as mapreduce, and click Finish.
-
-
Write, run, and debug the WordCount MapReduce program.
-
In the Project pane, right-click the source code directory of the module , and select .
-
In the Create new MaxCompute java class dialog box, click Driver, enter a Name such as WordCount, and press Enter.
-
In the newly created WordCount.java file, write the WordCount MapReduce program.
For the complete WordCount sample code, see Code example.
-
In the left-side navigation pane, right-click the WordCount.java script and select Run.
-
In the Run/Debug Configurations dialog box, configure the MaxCompute project.
For the MaxCompute project setting, select an Endpoint such as
http://service.cn-hangzhou.maxcompute.aliyun.com/api, and then choose your target project, such asdoc_test_dev. -
Click OK. Run and debug the WordCount.java script to verify that it compiles successfully.
-
Step 2: Generate and upload a MapReduce JAR file
-
In the left-side navigation pane of IntelliJ IDEA, right-click the WordCount.java script and select Deploy to server....
-
In the Package a jar and submit resource dialog box, configure the parameters and click OK.
Select the target MaxCompute project. The Resource file field displays the path of the packaged JAR file. Enter a Resource name, such as
mapreduce-1.0-SNAPSHOT.jar. Optionally, add a comment in the Resource comment field. Select Force update if already exists to overwrite any existing resource with the same name.For more information about the parameters, see Procedure.
NoteIf you use Maven to develop the MapReduce program, after packaging it into a JAR file, you must manually upload it to the MaxCompute project by using the MaxCompute client. For more information, see Add a resource. Example:
add jar mapreduce-1.0-SNAPSHOT.jar;
Step 3: Run the MapReduce job
-
Log on to the MaxCompute client or open the MaxCompute client in MaxCompute Studio.
The MaxCompute client is integrated into MaxCompute Studio, so you can run it directly from the IDE. For more information, see Integrate the MaxCompute client.
-
Create an input table and an output table.
The MapReduce job reads data from the input table and writes results to the output table. Example:
-- Create the input table wc_in. create table wc_in (key STRING, value STRING); -- Create the output table wc_out. create table wc_out (key STRING, cnt BIGINT);For more information about the syntax for creating tables, see Create a table.
-
Use the Tunnel Upload command to insert data into the wc_in table.
-
Use the
jarcommand to run the MapReduce job with the generated JAR file.Example:
jar -resources mapreduce-1.0-SNAPSHOT.jar -classpath mapreduce-1.0-SNAPSHOT.jar com.aliyun.odps.mapred.open.example.WordCount wc_in wc_out;-
-resources mapreduce-1.0-SNAPSHOT.jar: Specifies the resource used by the MapReduce job. In this example, the resource is the mapreduce-1.0-SNAPSHOT.jar file uploaded in Step 2. -
-classpath mapreduce-1.0-SNAPSHOT.jar: Specifies the local path to the JAR file that contains the MainClass. -
com.aliyun.odps.mapred.open.example.WordCount: The MainClass of the MapReduce program. -
wc_in wc_out: The input table and output table.
For more information about the
jarcommand, see Syntax. -
-
Run the following command to view the data in the wc_out table.
select * from wc_out;Expected output:
+------------+------------+ | key | cnt | +------------+------------+ | hello | 1 | | odps | 1 | +------------+------------+