This topic describes the packages that MaxCompute SDK for Java provides and the core classes of the SDK, such as Instances, Resources, Tables, and Functions.

Note When you use MaxCompute SDK for Java to access the compute and storage services provided by MaxCompute, you are charged in the same way as you access the services by using other methods.

For more information, see Storage pricing (pay-as-you-go), Computing pricing, and Download pricing(Pay-As-You-Go).

Background information

This topic describes the core classes of MaxCompute SDK for Java. For more information about the SDK, see the MaxCompute SDK for Java documentation.

You can use Maven to configure the version of the SDK that you want to use. For example, to specify the version of odps-sdk-core to use, add the following Maven dependency:
<dependency>
  <groupId>com.aliyun.odps</groupId>
  <artifactId>odps-sdk-core</artifactId>
  <version>xxxx-public</version>
</dependency>
Note
The following table describes the packages that MaxCompute SDK for Java provides.
Package Description
odps-sdk-core Provides basic features of MaxCompute, for example, the features that allow you to manage tables and projects. You can also use this package to access the Tunnel service.
odps-sdk-commons Provides common utilities.
odps-sdk-udf Provides user defined function (UDF) features.
odps-sdk-mapred Provides MapReduce features.
odps-sdk-graph Provides Graph SDK for Java. You can obtain the SDK by searching for odps-sdk-graph in the Maven central repository.

AliyunAccount

The AliyunAccount class defines an Alibaba Cloud account. The constructor of the AliyunAccount class takes an AccessKey ID and an AccessKey secret as input parameters. An AccessKey ID and an AccessKey secret constitute an AccessKey pair that is used to authenticate an Alibaba Cloud user. An AliyunAccount object is used to initialize an Odps object.

Odps

The Odps class is the entry point of MaxCompute SDK for Java. The Odps class allows you to access all types of objects in MaxCompute, including Projects, Tables, Resources, Functions, and Instances.
Note MaxCompute was formerly called ODPS. Therefore, the entry class of MaxCompute is still named Odps.
You can pass an AliyunAccount object to the constructor of the Odps class to create an Odps object. The following sample code shows how to create an Odps object:
Account account = new AliyunAccount("my_access_id", "my_access_key");
Odps odps = new Odps(account);
String odpsUrl = "<your odps endpoint>";
odps.setEndpoint(odpsUrl);
odps.setDefaultProject("my_project");
for (Table t : odps.tables()) {
    ....
}              

MaxCompute Tunnel

MaxCompute Tunnel is developed based on Tunnel SDK. You can use MaxCompute Tunnel to upload data to and download data from MaxCompute. For more information, see MaxCompute Tunnel overview MaxCompute Tunnel allows you to upload and download only tables, but not views.

MapReduce

For more information about MapReduce SDK, see Overview.

Projects

The Projects class defines the collection of all projects in MaxCompute. Each MaxCompute project is an element in the collection. The following sample code shows how to use the Projects class:
Account account = new AliyunAccount("my_access_id", "my_access_key");
Odps odps = new Odps(account);
String odpsUrl = "<your odps endpoint>";
odps.setEndpoint(odpsUrl);
Project p = odps.projects().get("my_exists");
p.reload();
Map<String, String> properties = prj.getProperties();
...

Project

The Project class defines the details of a project. You can obtain a specified project by calling the get method of the Projects class.

SQLTask

The SQLTask class allows you to run and process SQL statements in MaxCompute. You can call the run method of the SQLTask class to run SQL statements. You can submit only one SQL statement each time.

The run method returns an Instance object. You can obtain the running status and result of the SQL statement from the Instance object. The following sample code shows how to use the SQLTask class:
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 {
        private static final String accessId = "";
    private static final String accessKey = "";       
        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();
        }
    }
}
Note To create a table by using MaxCompute SDK for Java, you must use the SQLTask class instead of the Table class. Use an SQLTask object to execute the CREATE TABLE statement. For more information about the CREATE TABLE statement, see Table operations.

Instances

The Instances class defines the collection of all instances in MaxCompute. Each instance is an element in the collection. The following sample code shows how to use the Instances class:
Account account = new AliyunAccount("my_access_id", "my_access_key");
Odps odps = new Odps(account);
String odpsUrl = "<your odps endpoint>";
odps.setEndpoint(odpsUrl);
odps.setDefaultProject("my_project");
for (Instance i : odps.instances()) {
    ....
}

Instance

The Instance class defines the details of an instance. You can obtain a specified instance by calling the get method of the Instances class. The following sample code shows how to use the Instance class:
Account account = new AliyunAccount("my_access_id", "my_access_key");
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

The Tables class defines the collection of all tables in MaxCompute. Each table is an element in the collection. The following sample code shows how to use the Tables class:
Account account = new AliyunAccount("my_access_id", "my_access_key");
Odps odps = new Odps(account);
String odpsUrl = "<your odps endpoint>";
odps.setEndpoint(odpsUrl);
odps.setDefaultProject("my_project");
for (Table t : odps.tables()) {
    ....
}

Table

The Table class defines the details of a table. You can obtain a specified table by calling the get method of the Tables class. The following sample code shows how to use the Table class:
Account account = new AliyunAccount("my_access_id", "my_access_key");
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(tableSpec[1]));
part.reload();
...
The following sample code shows how to obtain the schema of a table:
Account account = new AliyunAccount("my_access_id", "my_access_key");
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();
 }

Resources

The Resources class defines the collection all resources in MaxCompute. Each resource is an element in the collection. The following sample code shows how to use the Resources class:
Account account = new AliyunAccount("my_access_id", "my_access_key");
Odps odps = new Odps(account);
String odpsUrl = "<your odps endpoint>";
odps.setEndpoint(odpsUrl);
odps.setDefaultProject("my_project");
for (Resource r : odps.resources()) {
    ....
}

Resource

The Resource class defines the details of a resource. You can obtain a specified resource by calling the get method of the Resources class. The following sample code shows how to use the Resource class:
Account account = new AliyunAccount("my_access_id", "my_access_key");
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 sample code 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 sample code 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

The Functions class defines the collection of all functions in MaxCompute. Each function is an element in the collection. The following sample code shows how to use the Functions class:
Account account = new AliyunAccount("my_access_id", "my_access_key");
Odps odps = new Odps(account);
String odpsUrl = "<your odps endpoint>";
odps.setEndpoint(odpsUrl);
odps.setDefaultProject("my_project");
for (Function f : odps.functions()) {
    ....
}                

Function

The Function class defines the details of a function. You can obtain a specified function by calling the get method of the Functions class. The following sample code shows how to use the Function class:
Account account = new AliyunAccount("my_access_id", "my_access_key");
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 sample code 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);