Java SDK

Last Updated: May 08, 2018

Maven users can search ‘odps-sdk-core’ from Maven Warehouse to get the required Java SDK (available in different versions). The package is described in the following table:

Package Name Description
odps-sdk-core Core interface of MaxCompute basic functions, the keyword used to search is “odps-sdk-core”.
odps-sdk-commons Some Util packages, the keyword used to search is “odps-sdk-commons”.
odps-sdk-udf Main interface of UDF, the keyword used to search is “odps-sdk-udf”.
odps-sdk-mapred MapReduce Java SDK, the keyword used to search is “odps-sdk-mapred”.
odps-sdk-graph Graph Java SDK, the keyword used to search is “odps-sdk-graph”.

AlibabaCloudAccount

The primary account created in Alibaba Cloud. It generally has an AccessKey that is comprised of an AccessKeyId and an AccessKeySecret and which is used to initialize MaxCompute.

MaxCompute

It is the entry of MaxCompute SDK. User can get set of all objects under the project shell by such endpoint, including Projects, Tables, Resources, Functions, and Instances. The user can construct MaxCompute object by introducing AliyunAccount instance. An example is as follows:

Note:

MaxCompute was formerly called ODPS, so the portal class is still named ODPS in the current SDK version.

User can construct MaxCompute object by inputting the AliyunAccount instance. An example is as follows:

  1. Account account = new AliyunAccount("my_access_id", "my_access_key");
  2. Odps odps = new Odps(account);
  3. String odpsUrl = "<your odps endpoint>";
  4. odps.setEndpoint(odpsUrl);
  5. odps.setDefaultProject("my_project");
  6. for (Table t : odps.tables()) {
  7. ....
  8. }

Projects

It is the set of all projects in MaxCompute. The element of this set is Project. An example is as follows:

  1. Account account = new AliyunAccount("my_access_id", "my_access_key");
  2. Odps odps = new Odps(account);
  3. String odpsUrl = "<your odps endpoint>";
  4. odps.setEndpoint(odpsUrl);
  5. Project p = odps.projects().get("my_exists");
  6. p.reload();
  7. Map<String, String> properties = prj.getProperties();
  8. ...

Project

It refers to the description of project and corresponding project, and can be acquired from Projects.

SQLTask

It refers to an interface to run and process SQL task. SQL can run directly through the interface ‘run’. The interface ‘run’ returns Instance.

The running status and result of SQL can be acquired through ‘Instance’. An example is as follows:

  1. Account account = new AliyunAccount("my_access_id", "my_access_key");
  2. Odps odps = new Odps(account);
  3. String odpsUrl = "<your odps endpoint>";
  4. odps.setEndpoint(odpsUrl);
  5. Instance instance = SQLTask.run(odps, "my_project", "select ...");
  6. String id = instance.getId();
  7. instance.waitforsuccess();
  8. Set<String> taskNames = instance.getTaskNames();
  9. for (String name : taskNames) {
  10. TaskSummary summary = instance.getTaskSummary(name);
  11. String s = summary.getSummaryText();
  12. }
  13. Map<String, String> results = instance.getTaskResults();
  14. Map<String, TaskStatus> taskStatus = instance.getTaskStatus();
  15. for (Entry<String, TaskStatus> status : taskStatus.entrySet()) {
  16. String result = results.get(status.getKey());
  17. }

Notes:

To create a table, you can use SQLTask interface instead of the interface Table. You must introduce the statement of CREATE TABLE into SQLTask.

Instances

This class refers to the set of all instances in MaxCompute and the element of this set is Instance. An example is as follows:

  1. Account account = new AliyunAccount("my_access_id", "my_access_key");
  2. Odps odps = new Odps(account);
  3. String odpsUrl = "<your odps endpoint>";
  4. odps.setEndpoint(odpsUrl);
  5. odps.setDefaultProject("my_project");
  6. for (Instance i : odps.instances()) {
  7. ....
  8. }

Instance

It refers to the description of instance and corresponding instance, and can be acquired from Instances. An example is as follows:

  1. Account account = new AliyunAccount("my_access_id", "my_access_key");
  2. Odps odps = new Odps(account);
  3. String odpsUrl = "<your odps endpoint>";
  4. odps.setEndpoint(odpsUrl);
  5. Instance ins = odps.instances().get("instance id");
  6. Date startTime = instance.getStartTime();
  7. Date endTime = instance.getEndTime();
  8. ...
  9. Status instanceStatus = instance.getStatus();
  10. String instanceStatusStr = null;
  11. if (instanceStatus == Status.TERMINATED) {
  12. instanceStatusStr = TaskStatus.Status.SUCCESS.toString();
  13. Map<String, TaskStatus> taskStatus = instance.getTaskStatus();
  14. for (Entry<String, TaskStatus> status : taskStatus.entrySet()) {
  15. if (status.getValue().getStatus() != TaskStatus.Status.SUCCESS) {
  16. instanceStatusStr = status.getValue().getStatus().toString();
  17. break;
  18. }
  19. }
  20. } else {
  21. instanceStatusStr = instanceStatus.toString();
  22. }
  23. ...
  24. TaskSummary summary = instance.getTaskSummary("instance name");
  25. String s = summary.getSummaryText();

Tables

This class refers to the set of all tables in MaxCompute. The element of this set is Table. An example is as follows:

  1. Account account = new AliyunAccount("my_access_id", "my_access_key");
  2. Odps odps = new Odps(account);
  3. String odpsUrl = "<your odps endpoint>";
  4. odps.setEndpoint(odpsUrl);
  5. odps.setDefaultProject("my_project");
  6. for (Table t : odps.tables()) {
  7. ....
  8. }

Table

It refers to the description of table and corresponding table, and can be acquired through Tables. An example is as follows:

  1. Account account = new AliyunAccount("my_access_id", "my_access_key");
  2. Odps odps = new Odps(account);
  3. String odpsUrl = "<your odps endpoint>";
  4. odps.setEndpoint(odpsUrl);
  5. Table t = odps.tables().get("table name");
  6. t.reload();
  7. Partition part = t.getPartition(new PartitionSpec(tableSpec[1]));
  8. part.reload();
  9. ...

Resources

The class refers to the set of all resources in MaxCompute. The element of this set is Resource. An example is as follows:

  1. Account account = new AliyunAccount("my_access_id", "my_access_key");
  2. Odps odps = new Odps(account);
  3. String odpsUrl = "<your odps endpoint>";
  4. odps.setEndpoint(odpsUrl);
  5. odps.setDefaultProject("my_project");
  6. for (Resource r : odps.resources()) {
  7. ....
  8. }

Resource

It refers to the description of resource and corresponding resource, and can be acquired through Resources. An example is as follows:

  1. Account account = new AliyunAccount("my_access_id", "my_access_key");
  2. Odps odps = new Odps(account);
  3. String odpsUrl = "<your odps endpoint>";
  4. odps.setEndpoint(odpsUrl);
  5. Resource r = odps.resources().get("resource name");
  6. r.reload();
  7. if (r.getType() == Resource.Type.TABLE) {
  8. TableResource tr = new TableResource(r);
  9. String tableSource = tr.getSourceTable().getProject() + "."
  10. + tr.getSourceTable().getName();
  11. if (tr.getSourceTablePartition() != null) {
  12. tableSource += " partition(" + tr.getSourceTablePartition().toString()
  13. + ")";
  14. }
  15. ....
  16. }

File resource creation example:

  1. String projectName = "my_porject";
  2. String source = "my_local_file.txt";
  3. File file = new File(source);
  4. InputStream is = new FileInputStream(file);
  5. FileResource resource = new FileResource();
  6. String name = file.getName();
  7. resource.setName(name);
  8. odps.resources().create(projectName, resource, is);

Table resource creation example:

  1. TableResource resource = new TableResource(tableName, tablePrj, partitionSpec);
  2. //resource.setName(INVALID_USER_TABLE);
  3. resource.setName("table_resource_name");
  4. odps.resources().update(projectName, resource);

Functions

This class refers to the set of all functions in MaxCompute. The element of this set is Function. An example is as follows:

  1. Account account = new AliyunAccount("my_access_id", "my_access_key");
  2. Odps odps = new Odps(account);
  3. String odpsUrl = "<your odps endpoint>";
  4. odps.setEndpoint(odpsUrl);
  5. odps.setDefaultProject("my_project");
  6. for (Function f : odps.functions()) {
  7. ....
  8. }

Function

It refers to the description of function and corresponding function, and can be acquired through Functions. The code example is shown as follows:

  1. Account account = new AliyunAccount("my_access_id", "my_access_key");
  2. Odps odps = new Odps(account);
  3. String odpsUrl = "<your odps endpoint>";
  4. odps.setEndpoint(odpsUrl);
  5. Function f = odps.functions().get("function name");
  6. List<Resource> resources = f.getResources();

Function creation example:

  1. String resources = "xxx:xxx";
  2. String classType = "com.aliyun.odps.mapred.open.example.WordCount";
  3. ArrayList<String> resourceList = new ArrayList<String>();
  4. for (String r : resources.split(":")) {
  5. resourceList.add(r);
  6. }
  7. Function func = new Function();
  8. func.setName(name);
  9. func.setClassType(classType);
  10. func.setResources(resourceList);
  11. odps.functions().create(projectName, func);
Thank you! We've received your feedback.