The Java software development kit (SDK) provides a set of Java programming interfaces for MaxCompute. You can use the SDK to manage projects, tables, data transfers, and functions using Java code. This topic outlines the Java SDK, including instances, resources, tables, and functions.
The computing and storage fees for using the SDK to call MaxCompute are the same as the fees for using MaxCompute directly. For more information, see Billing.
Background information
For more information about the common core interfaces of MaxCompute, see SDK Java Doc.
You can use Maven to manage and configure the SDK version. The following code shows a sample Maven configuration.
<dependency>
<groupId>com.aliyun.odps</groupId>
<artifactId>odps-sdk-core</artifactId>
<version>X.X.X-public</version>
</dependency>Only version 0.27.2-public and later support the new data types of MaxCompute V2.0.
You can search for odps-sdk-core on search.maven.org to obtain the latest SDK version.
The following table describes the SDK packages that MaxCompute provides.
Package name | Description |
odps-sdk-core | Contains the basic features of MaxCompute, such as operations on tables and projects, and the tunnel. |
odps-sdk-commons | Contains Util encapsulations. |
odps-sdk-udf | The main interfaces for the UDF feature. |
odps-sdk-mapred | The MapReduce feature. |
odps-sdk-graph | The Graph Java SDK. Search for the keyword odps-sdk-graph. |
AliyunAccount
Represents an Alibaba Cloud account. The input parameters are an AccessKey ID and an AccessKey secret. They serve as the identity and authentication key for an Alibaba Cloud account. You can use this class to initialize MaxCompute.
ODPS
The entry point for the MaxCompute SDK. You can use this class to obtain all object collections in a project, such as Projects, Tables, Resources, Functions, and Instances.
You can construct a MaxCompute object by passing an AliyunAccount instance. The following code provides an example.
// An AccessKey for an Alibaba Cloud account has all API access permissions, which poses a high security threat. Create and use a Resource Access Management (RAM) user for API calls or routine O&M. Log on to the RAM console to create a RAM user.
// This example shows how to store the AccessKey and AccessKey secret in environment variables. You can also store them in a configuration file as needed.
// Do not save the AccessKey and AccessKey secret in your code. This prevents key leaks.
Account account = new AliyunAccount(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
Odps odps = new Odps(account);
String odpsUrl = "<your odps endpoint>";
odps.setEndpoint(odpsUrl);
odps.setDefaultProject("my_project");
for (Table t : odps.tables()) {
....
} Batch data tunnel
The MaxCompute Tunnel data tunnel is based on the Tunnel SDK. You can use the Tunnel SDK to upload or download data for MaxCompute tables. Views are not supported.
MapReduce
For more information about the SDKs that MapReduce supports, see Native SDK overview.
Projects
Represents a collection of all projects in MaxCompute. The elements in the collection are Project objects. The following code provides an example.
// An AccessKey for an Alibaba Cloud account has all API access permissions, which poses a high security threat. Create and use a Resource Access Management (RAM) user for API calls or routine O&M. Log on to the RAM console to create a RAM user.
// This example shows how to store the AccessKey and AccessKey secret in environment variables. You can also store them in a configuration file as needed.
// Do not save the AccessKey and AccessKey secret in your code. This prevents key leaks.
Account account = new AliyunAccount(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
Odps odps = new Odps(account);
String odpsUrl = "<your odps endpoint>";
odps.setEndpoint(odpsUrl);
Project p = odps.projects().get("my_exists");
p.reload();
...Project
Represents a project and its information. You can obtain the corresponding project from the Projects collection.
SQLTask
SQLTask is an interface for running and processing SQL tasks. You can run SQL statements directly using the run interface.
The run interface returns an Instance, which you can use to retrieve the running status and result of the SQL statement. The following code provides an example.
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 TestSql {
// An AccessKey for an Alibaba Cloud account has all API access permissions, which poses a high security threat. Create and use a Resource Access Management (RAM) user for API calls or routine O&M. Log on to the RAM console to create a RAM user.
// This example shows how to store the AccessKey and AccessKey secret in environment variables. You can also store them in a configuration file as needed.
// Do not save the AccessKey and AccessKey secret in your code. This prevents key leaks.
private static String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
private static String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
private static final String endPoint = "http://service.odps.aliyun.com/api";
private static final String project = "";
private static final String sql = "select category from iris;";
public static void
main(String[] args) {
Account account = new AliyunAccount(accessId, accessKey);
Odps odps = new Odps(account);
odps.setEndpoint(endPoint);
odps.setDefaultProject(project);
Instance i;
try {
i = SQLTask.run(odps, sql);
i.waitForSuccess();
List<Record> records = SQLTask.getResult(i);
for(Record r:records){
System.out.println(r.get(0).toString());
}
} catch (OdpsException e) {
e.printStackTrace();
}
}
}You can submit and run only one SQL statement at a time.
To create a table, use the SQLTask interface instead of the Table interface. Pass the table operation statement to the SQLTask interface.
Instances
Represents a collection of all instances in MaxCompute. The elements in the collection are Instance objects. The following code provides an example.
// An AccessKey for an Alibaba Cloud account has all API access permissions, which poses a high security threat. Create and use a Resource Access Management (RAM) user for API calls or routine O&M. Log on to the RAM console to create a RAM user.
// This example shows how to store the AccessKey and AccessKey secret in environment variables. You can also store them in a configuration file as needed.
// Do not save the AccessKey and AccessKey secret in your code. This prevents key leaks.
Account account = new AliyunAccount(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
Odps odps = new Odps(account);
String odpsUrl = "<your odps endpoint>";
odps.setEndpoint(odpsUrl);
odps.setDefaultProject("my_project");
for (Instance i : odps.instances()) {
....
}Instance
Represents an instance and its information. You can obtain the corresponding instance from the Instances collection. The following code provides an example.
// An AccessKey for an Alibaba Cloud account has all API access permissions, which poses a high security threat. Create and use a Resource Access Management (RAM) user for API calls or routine O&M. Log on to the RAM console to create a RAM user.
// This example shows how to store the AccessKey and AccessKey secret in environment variables. You can also store them in a configuration file as needed.
// Do not save the AccessKey and AccessKey secret in your code. This prevents key leaks.
Account account = new AliyunAccount(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
Odps odps = new Odps(account);
String odpsUrl = "<your odps endpoint>";
odps.setEndpoint(odpsUrl);
Instance instance= odps.instances().get("instance id");
Date startTime = instance.getStartTime();
Date endTime = instance.getEndTime();
...
Status instanceStatus = instance.getStatus();
String instanceStatusStr = null;
if (instanceStatus == Status.TERMINATED) {
instanceStatusStr = TaskStatus.Status.SUCCESS.toString();
Map<String, TaskStatus> taskStatus = instance.getTaskStatus();
for (Entry<String, TaskStatus> status : taskStatus.entrySet()) {
if (status.getValue().getStatus() != TaskStatus.Status.SUCCESS) {
instanceStatusStr = status.getValue().getStatus().toString();
break;
}
}
} else {
instanceStatusStr = instanceStatus.toString();
}
...
TaskSummary summary = instance.getTaskSummary("task name");
String s = summary.getSummaryText();Tables
Represents a collection of all tables in MaxCompute. The elements in the collection are Table objects. The following code provides an example.
// An AccessKey for an Alibaba Cloud account has all API access permissions, which poses a high security threat. Create and use a Resource Access Management (RAM) user for API calls or routine O&M. Log on to the RAM console to create a RAM user.
// This example shows how to store the AccessKey and AccessKey secret in environment variables. You can also store them in a configuration file as needed.
// Do not save the AccessKey and AccessKey secret in your code. This prevents key leaks.
Account account = new AliyunAccount(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
Odps odps = new Odps(account);
String odpsUrl = "<your odps endpoint>";
odps.setEndpoint(odpsUrl);
odps.setDefaultProject("my_project");
for (Table t : odps.tables()) {
....
}Table
Represents a table and its information. You can obtain the corresponding table from the Tables collection. The following code provides an example.
// An AccessKey for an Alibaba Cloud account has all API access permissions, which poses a high security threat. Create and use a Resource Access Management (RAM) user for API calls or routine O&M. Log on to the RAM console to create a RAM user.
// This example shows how to store the AccessKey and AccessKey secret in environment variables. You can also store them in a configuration file as needed.
// Do not save the AccessKey and AccessKey secret in your code. This prevents key leaks.
Account account = new AliyunAccount(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
Odps odps = new Odps(account);
String odpsUrl = "<your odps endpoint>";
odps.setEndpoint(odpsUrl);
odps.setDefaultProject("my_project");
Table table = odps.tables().get("tablename");
for(Column c : table.getSchema().getColumns())
{
String name = c.getName();
TypeInfo type = c.getTypeInfo();
}Use the following code to obtain table partition data.
// An AccessKey for an Alibaba Cloud account has all API access permissions, which poses a high security threat. Create and use a Resource Access Management (RAM) user for API calls or routine O&M. Log on to the RAM console to create a RAM user.
// This example shows how to store the AccessKey and AccessKey secret in environment variables. You can also store them in a configuration file as needed.
// Do not save the AccessKey and AccessKey secret in your code. This prevents key leaks.
Account account = new AliyunAccount(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
Odps odps = new Odps(account);
String odpsUrl = "<your odps endpoint>";
odps.setEndpoint(odpsUrl);
Table t = odps.tables().get("table name");
t.reload();
Partition part = t.getPartition(new PartitionSpec("partition_col=partition_col_value"));
part.reload();
...Resources
Represents a collection of all resources in MaxCompute. The elements in the collection are Resource objects. The following code provides an example.
// An AccessKey for an Alibaba Cloud account has all API access permissions, which poses a high security threat. Create and use a Resource Access Management (RAM) user for API calls or routine O&M. Log on to the RAM console to create a RAM user.
// This example shows how to store the AccessKey and AccessKey secret in environment variables. You can also store them in a configuration file as needed.
// Do not save the AccessKey and AccessKey secret in your code. This prevents key leaks.
Account account = new AliyunAccount(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
Odps odps = new Odps(account);
String odpsUrl = "<your odps endpoint>";
odps.setEndpoint(odpsUrl);
odps.setDefaultProject("my_project");
for (Resource r : odps.resources()) {
....
}Resource
Represents a resource and its information. You can obtain the corresponding resource from the Resources collection. The following code provides an example.
// An AccessKey for an Alibaba Cloud account has all API access permissions, which poses a high security threat. Create and use a Resource Access Management (RAM) user for API calls or routine O&M. Log on to the RAM console to create a RAM user.
// This example shows how to store the AccessKey and AccessKey secret in environment variables. You can also store them in a configuration file as needed.
// Do not save the AccessKey and AccessKey secret in your code. This prevents key leaks.
Account account = new AliyunAccount(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
Odps odps = new Odps(account);
String odpsUrl = "<your odps endpoint>";
odps.setEndpoint(odpsUrl);
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()
+ ")";
}
....
}The following example shows how to create a file resource.
String projectName = "my_porject";
String source = "my_local_file.txt";
File file = new File(source);
InputStream is = new FileInputStream(file);
FileResource resource = new FileResource();
String name = file.getName();
resource.setName(name);
odps.resources().create(projectName, resource, is);The following example shows how to create a table resource.
TableResource resource = new TableResource(tableName, tablePrj, partitionSpec);
//resource.setName(INVALID_USER_TABLE);
resource.setName("table_resource_name");
odps.resources().update(projectName, resource);Functions
Represents a collection of all functions in MaxCompute. The elements in the collection are Function objects. The following code provides an example.
// An AccessKey for an Alibaba Cloud account has all API access permissions, which poses a high security threat. Create and use a Resource Access Management (RAM) user for API calls or routine O&M. Log on to the RAM console to create a RAM user.
// This example shows how to store the AccessKey and AccessKey secret in environment variables. You can also store them in a configuration file as needed.
// Do not save the AccessKey and AccessKey secret in your code. This prevents key leaks.
Account account = new AliyunAccount(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
Odps odps = new Odps(account);
String odpsUrl = "<your odps endpoint>";
odps.setEndpoint(odpsUrl);
odps.setDefaultProject("my_project");
for (Function f : odps.functions()) {
....
} Function
Represents a function and its information. You can obtain the corresponding function from the Functions collection. The following code provides an example.
// An AccessKey for an Alibaba Cloud account has all API access permissions, which poses a high security threat. Create and use a Resource Access Management (RAM) user for API calls or routine O&M. Log on to the RAM console to create a RAM user.
// This example shows how to store the AccessKey and AccessKey secret in environment variables. You can also store them in a configuration file as needed.
// Do not save the AccessKey and AccessKey secret in your code. This prevents key leaks.
Account account = new AliyunAccount(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
Odps odps = new Odps(account);
String odpsUrl = "<your odps endpoint>";
odps.setEndpoint(odpsUrl);
Function f = odps.functions().get("function name");
List<Resource> resources = f.getResources(); The following example shows how to create a function.
String resources = "xxx:xxx";
String classType = "com.aliyun.odps.mapred.open.example.WordCount";
ArrayList<String> resourceList = new ArrayList<String>();
for (String r : resources.split(":")) {
resourceList.add(r);
}
Function func = new Function();
func.setName(name);
func.setClassType(classType);
func.setResources(resourceList);
odps.functions().create(projectName, func); References
If you want to use Python to interact with MaxCompute and process data, see Python SDK overview.