SDK

Last Updated: Oct 22, 2017

The user who uses Maven can search ‘odps-sdk’ from Maven Warehouse to get different versions of Java SDK:

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”.

AliyunAccount

It is Aliyun authentication account and corresponding input parameters are accessId and accessKey which are identifier and authentication key of Aliyun user. This class is used for initializing 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. The program example is as follows:

It is the entry of MaxCompute SDK. User can get the set of all objects in the project through this class, including Projects, Tables, Instances, Resources, Functions.

User can construct MaxCompute object by inputting AliyunAccount instance. The program 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. 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. The program 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. 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 can be gotten from Projects.

Tables

This class refers to the set of all tables in MaxCompute. The element of this set is Table. 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. odps.setDefaultProject("my_project");
  6. for (Table t : odps.tables()) {
  7. ....
  8. }

Table

It refers to the description of table and corresponding table can be gotten through Tables. 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. Table t = odps.tables().get("table name");
  6. t.reload();
  7. Partition part = t.getPartition(new PartitionSpec(tableSpec[1]));
  8. part.reload();
  9. ...

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 gotten through ‘Instance’.The program example is shown as follows (just for reference):

  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:

  • If you need to create a table, you should use SQLTask interface instead of the interface Table. You need to 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. The program 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. odps.setDefaultProject("my_project");
  6. for (Instance i : odps.instances()) {
  7. ....
  8. }

Instance

It refers to the description of instance and corresponding instance can be gotten from Instances. 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. 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();

Resources

The class refers to the set of all resources in MaxCompute. The element of this set is Resource. 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. odps.setDefaultProject("my_project");
  6. for (Resource r : odps.resources()) {
  7. ....
  8. }

Resource

It refer to the description of resource and corresponding resource can be gotten through Resources. 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. 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. }

An example to create a file resource:

  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);

An example to create a table resource:

  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. 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. odps.setDefaultProject("my_project");
  6. for (Function f : odps.functions()) {
  7. ....
  8. }

Function

It refers to the description of function and corresponding function can be gotten 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();

An example to create a function:

  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.