This topic uses the Single Source Shortest Path (SSSP) algorithm as an example to show how to write a Graph job.
Prerequisites
-
You have installed and configured the MaxCompute client and connected to a MaxCompute instance. For more information, see MaxCompute client.
-
You have installed and configured IntelliJ IDEA 2024 and a compatible version of MaxCompute Studio updated in 2024. For more information, see Install MaxCompute Studio and Configure MaxCompute Studio.
-
You have configured Apache Maven version
apache-maven-3.5.0. -
You have installed JDK 1.8 or later.
-
You have prepared a data file. This topic uses the sssp.txt file as an example.
1 2:2,3:1,4:4 2 1:2,3:2,4:1 3 1:1,2:2,5:1 4 1:4,2:1,5:1 5 3:1,4:1
Procedure
-
Start the MaxCompute client and run the following commands to create the input table sssp_in and the output table sssp_out.
CREATE TABLE sssp_in (v bigint, es string); CREATE TABLE sssp_out (vertex bigint, value bigint); -
Run the Tunnel command to upload the data from the sssp.txt file to the sssp_in table, using a space as the column delimiter.
tunnel u -fd " " sssp.txt sssp_in;NoteIn this example, the sssp.txt file is saved in the
bindirectory of the MaxCompute client. Make sure to specify the correct path to your sssp.txt file. -
Write the SSSP example.
-
In IntelliJ IDEA, create a MaxCompute Java Module and name it odps-graph-example-sssp.
NoteFor more information, see Create a MaxCompute Java Module.
-
In the new odps-graph-example-sssp module, create the
BaseLoadingVertexResolverclass and theSSSPclass. For the complete Java code, see the directed graph example in Single Source Shortest Path. Add the following dependencies to the pom.xml file.<dependency> <groupId>com.aliyun.odps</groupId> <artifactId>odps-sdk-core</artifactId> <version>0.48.0-public</version> </dependency> <dependency> <groupId>com.aliyun.odps</groupId> <artifactId>odps-sdk-graph</artifactId> <version>0.48.0-public</version> </dependency> <!-- For local testing --> <dependency> <groupId>com.aliyun.odps</groupId> <artifactId>odps-graph-local</artifactId> <version>0.48.0-public</version> </dependency> -
In IntelliJ IDEA, use MaxCompute Studio to package the project. For detailed instructions, see Package, upload, and register.
NoteIn this example, the JAR package deployed to the MaxCompute project is named odps-graph-example-sssp.jar.
-
-
In the MaxCompute client, run the following command to run the SSSP job.
jar -libjars odps-graph-example-sssp.jar -classpath <LOCAL_JAR_PATH>/odps-graph-example-sssp.jar SSSP 1 sssp_in sssp_out;LOCAL_JAR_PATH: The local path to the odps-graph-example-sssp.jar file.
After the job is complete, run
select * from sssp_out;to query the sssp_out table and verify the result.vertex value 1 0 2 2 3 1 4 3 5 2-
vertex: The ID of a vertex.
-
value: The shortest distance from the corresponding vertex to the source vertex (1).
NoteTo use the Graph feature, simply submit a Graph job.
-