MaxCompute Java SDK介绍

MaxCompute Java SDK介绍

MaxCompute提供了Java SDK,可以对实例、资源、表、函数等几个方面进行操作。使用SDK调用MaxCompute产生的计算、存储等费用与直接使用MaxCompute产生的费用一致。较为常用的MaxCompute核心接口详情请参见SDK Java Doc

使用Maven管理配置新SDK的版本,Maven的配置示例如下:

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

MaxCompute提供的SDK包整体信息,如下表所示:

 
包名 描述
odps-sdk-core MaxCompute的基础功能,例如对表、Project的操作,以及Tunnel均在此包中。
odps-sdk-commons 一些Util封装。
odps-sdk-udf UDF功能的主体接口。
odps-sdk-mapred MapReduce功能。
odps-sdk-graph Graph Java SDK,搜索关键词odps-sdk-graph。

MaxCompute Java SDK的一些使用示例为:Java SDK使用示例。 

AliyunAccount

阿里云认证账号。输入参数为AccessKey ID及AccessKey Secret,是阿里云用户的身份标识和认证密钥。此类用来初始化MaxCompute。

MaxCompute

MaxCompute SDK的入口,通过此类来获取项目空间下的所有对象集合,包括:

  • projects
  • tables
  • resources
  • functions
  • instances

通过传入AliyunAccount实例来构造MaxCompute对象:

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()) {
    ....
}              

Projects

Projects是MaxCompute中所有项目空间的集合。集合中的元素为Project。Project是对项目空间信息的描述。您可以通过Projects获取相应的项目空间。程序示例如下。

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

SQLTask

SQLTask是用于运行、处理SQL任务的接口。可以通过运行接口直接运行SQL(注意:每次只能提交运行一个SQL语句)。运行接口返回Instance实例,通过Instance获取SQL的运行状态及运行结果。程序示例如下。

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

说明 创建表,需要通过SQLTask接口,而不是Table接口,将建表的语句传入SQLTask。

Instances

Instances是MaxCompute中所有实例(Instance)的集合。集合中的元素为Instance。程序示例如下。

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是对实例信息的描述。可以通过Instances获取相应的实例。程序示例如下。

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

Tables是MaxCompute中所有表的集合。集合中的元素为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");
for (Table t : odps.tables()) {
    ....
}

Table是对表信息的描述。可以通过Tables获取相应的表。程序示例如下。

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

Resources

Resources是MaxCompute中所有资源的集合。集合中的元素为Resource。程序示例如下。

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是对资源信息的描述。可以通过Resources获取相应的资源。程序示例如下。

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()
            + ")";
    }
    ....
}

创建文件资源的示例,如下所示:

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

创建表资源的示例,如下所示:

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

Functions

Functions是MaxCompute中所有函数的集合。集合中的元素为Function。程序示例如下。

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是对函数信息的描述。您可以通过Functions获取相应的函数。程序示例如下。

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

创建函数的示例,如下所示:

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

批量数据通道

MaxCompute Tunnel数据通道是基于Tunnel SDK编写的。可以通过Tunnel向MaxCompute中上传或者下载数据,详细内容请参见批量数据通道。目前Tunnel仅支持表(不包括视图View)和数据的上传和下载。

MapReduce

MapReduce支持的MapReduce SDK请参见原生SDK概述

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章