All Products
Search
Document Center

MaxCompute:Java SDK

Last Updated:Mar 26, 2026

MaxCompute SDK for Java provides a set of Java interfaces for managing and interacting with MaxCompute programmatically. Use the SDK to manage projects, run SQL statements, manipulate tables, transfer data through Tunnel, and manage resources and functions.

Accessing MaxCompute computing and storage services through the SDK incurs the same charges as accessing them through other methods. For pricing details, see Storage pricing (pay-as-you-go), Computing pricing (pay-as-you-go), and Download pricing (pay-as-you-go).

SDK packages

MaxCompute SDK for Java is distributed as a set of Maven packages.

This topic describes the common core interfaces of MaxCompute. For more information, see SDK Java Doc.

For more information, see Storage fees (pay-as-you-go), Computing fees (pay-as-you-go), and Download fees (pay-as-you-go).

PackageDescription
odps-sdk-coreCore features: manage tables and projects, access the Tunnel service
odps-sdk-commonsCommon utilities
odps-sdk-udfUser-defined function (UDF) features
odps-sdk-mapredMapReduce features
odps-sdk-graphGraph SDK for Java

For the full API reference, see ODPS SDK Core 0.45.1-public API.

Add the Maven dependency

Add odps-sdk-core to your pom.xml:

<dependency>
  <groupId>com.aliyun.odps</groupId>
  <artifactId>odps-sdk-core</artifactId>
  <version>X.X.X-public</version>
</dependency>

To find the latest version, search for odps-sdk-core at search.maven.org.

SDK version 0.27.2-public or later is required to use the new data types introduced in MaxCompute V2.0.

Initialize the client

The Odps object is the entry point for all SDK operations. It provides access to all MaxCompute resources: projects, tables, instances, resources, and functions.

Note

MaxCompute was formerly known as ODPS. Therefore, the entry class is still named ODPS in the current SDK version.

Create the Odps object by passing an AliyunAccount to its constructor. AliyunAccount takes an AccessKey ID and AccessKey secret, which together form an AccessKey pair used for authentication.

// Store your AccessKey credentials as environment variables rather than
// hardcoding them. Using an Alibaba Cloud account's AccessKey pair grants
// full API access — create and use a RAM user for routine operations.
// To create a RAM user, see the Resource Access Management (RAM) console.
Account account = new AliyunAccount(
    System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"),
    System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
);
Odps odps = new Odps(account);
odps.setEndpoint("<your-odps-endpoint>");
odps.setDefaultProject("<your-project>");

Replace the following placeholders with actual values:

PlaceholderDescription
<your-odps-endpoint>The MaxCompute endpoint for your region, for example, http://service.odps.aliyun.com/api
<your-project>The name of your MaxCompute project

Quick reference

Each collection class on the Odps object maps to a set of SDK operations:

OperationSDK call
List all tablesodps.tables()
Get a specific tableodps.tables().get("table_name")
List all instancesodps.instances()
Get a specific instanceodps.instances().get("instance_id")
List all resourcesodps.resources()
Get a specific resourceodps.resources().get("resource_name")
List all functionsodps.functions()
Get a specific functionodps.functions().get("function_name")
Run a SQL statementSQLTask.run(odps, sql)
Get project detailsodps.projects().get("project_name")

Core classes

SQLTask

SQLTask runs SQL statements in MaxCompute. Call SQLTask.run() to submit a statement — it returns an Instance object you use to poll status and retrieve results.

import java.util.List;
import com.aliyun.odps.Instance;
import com.aliyun.odps.Odps;
import com.aliyun.odps.OdpsException;
import com.aliyun.odps.account.Account;
import com.aliyun.odps.account.AliyunAccount;
import com.aliyun.odps.data.Record;
import com.aliyun.odps.task.SQLTask;

public class RunSqlExample {
    private static final String ACCESS_ID  = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
    private static final String ACCESS_KEY = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
    private static final String ENDPOINT   = "http://service.odps.aliyun.com/api";
    private static final String PROJECT    = "<your-project>";

    public static void main(String[] args) {
        Account account = new AliyunAccount(ACCESS_ID, ACCESS_KEY);
        Odps odps = new Odps(account);
        odps.setEndpoint(ENDPOINT);
        odps.setDefaultProject(PROJECT);

        String sql = "select category from iris;";
        try {
            // Submit the SQL statement and wait for it to complete
            Instance instance = SQLTask.run(odps, sql);
            instance.waitForSuccess();

            // Retrieve and print results
            List<Record> records = SQLTask.getResult(instance);
            for (Record r : records) {
                System.out.println(r.get(0).toString());
            }
        } catch (OdpsException e) {
            e.printStackTrace();
        }
    }
}

Usage notes:

  • Submit only one SQL statement per SQLTask.run() call.

  • To create a table programmatically, pass a CREATE TABLE statement to SQLTask.run(). The Table class does not support table creation. For the CREATE TABLE syntax, see Table operations.

Instances and Instance

Instances is the collection of all instances in the project. Instance represents a single instance and exposes status, timing, and task summary information.

// Iterate over all instances
for (Instance i : odps.instances()) {
    // process each instance
}

// Get a specific instance and inspect its status
Instance instance = odps.instances().get("instance_id");
Date startTime = instance.getStartTime();
Date endTime   = instance.getEndTime();

Status instanceStatus = instance.getStatus();
String statusStr;
if (instanceStatus == Status.TERMINATED) {
    statusStr = TaskStatus.Status.SUCCESS.toString();
    Map<String, TaskStatus> taskStatus = instance.getTaskStatus();
    for (Entry<String, TaskStatus> entry : taskStatus.entrySet()) {
        if (entry.getValue().getStatus() != TaskStatus.Status.SUCCESS) {
            statusStr = entry.getValue().getStatus().toString();
            break;
        }
    }
} else {
    statusStr = instanceStatus.toString();
}

// Get the task summary
TaskSummary summary = instance.getTaskSummary("task_name");
String summaryText  = summary.getSummaryText();

Projects and Project

Projects is the collection of all MaxCompute projects accessible with your credentials. Project holds the metadata for a single project.

// Get a specific project
Project p = odps.projects().get("project_name");
p.reload();

Tables and Table

Tables is the collection of all tables in the default project. Use Table to inspect a table's schema and partitions.

// Iterate over all tables
for (Table t : odps.tables()) {
    // process each table
}

// Get a table and iterate over its columns
Table table = odps.tables().get("table_name");
for (Column c : table.getSchema().getColumns()) {
    String   name = c.getName();
    TypeInfo type = c.getTypeInfo();
}

// Access a specific partition
Table t = odps.tables().get("table_name");
t.reload();
Partition part = t.getPartition(new PartitionSpec("partition_col=partition_col_value"));
part.reload();

Resources and Resource

Resources is the collection of all resources in the project. Resources include files and table references used by UDFs and MapReduce jobs.

// Iterate over all resources
for (Resource r : odps.resources()) {
    // process each resource
}

// Get a resource and check its type
Resource r = odps.resources().get("resource_name");
r.reload();
if (r.getType() == Resource.Type.TABLE) {
    TableResource tr = new TableResource(r);
    String tableSource = tr.getSourceTable().getProject() + "."
        + tr.getSourceTable().getName();
    if (tr.getSourceTablePartition() != null) {
        tableSource += " partition(" + tr.getSourceTablePartition().toString() + ")";
    }
}

Create a file resource:

String projectName = "my_project";
String source      = "my_local_file.txt";
File file          = new File(source);
InputStream is     = new FileInputStream(file);

FileResource resource = new FileResource();
resource.setName(file.getName());
odps.resources().create(projectName, resource, is);

Create a table resource:

TableResource resource = new TableResource(tableName, tablePrj, partitionSpec);
resource.setName("table_resource_name");
odps.resources().update(projectName, resource);

Functions and Function

Functions is the collection of all functions in the project. Use Function to read or create UDFs registered in MaxCompute.

// Iterate over all functions
for (Function f : odps.functions()) {
    // process each function
}

// Get a function and list its associated resources
Function f = odps.functions().get("function_name");
List<Resource> resources = f.getResources();

Create a function:

String classType = "com.aliyun.odps.mapred.open.example.WordCount";
ArrayList<String> resourceList = new ArrayList<>();
for (String r : "resource1:resource2".split(":")) {
    resourceList.add(r);
}

Function func = new Function();
func.setName("function_name");
func.setClassType(classType);
func.setResources(resourceList);
odps.functions().create(projectName, func);

MaxCompute Tunnel

Tunnel is built on the Tunnel SDK and supports bulk data upload and download from MaxCompute tables. Views are not supported.

MapReduce

For the SDK classes and methods supported by MaxCompute MapReduce, see Overview.

What's next

References