Skip to content

Commit 881516e

Browse files
committed
chore(example): Add odps client setup sample
1 parent 98954b2 commit 881516e

File tree

3 files changed

+131
-0
lines changed

3 files changed

+131
-0
lines changed

odps-examples/basic-examples/pom.xml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<groupId>com.aliyun.odps</groupId>
6+
<artifactId>basic-examples</artifactId>
7+
<version>1.0</version>
8+
<modelVersion>4.0.0</modelVersion>
9+
10+
<dependencies>
11+
<dependency>
12+
<groupId>com.aliyun.odps</groupId>
13+
<artifactId>odps-sdk-commons</artifactId>
14+
<version>0.48.8-public</version>
15+
</dependency>
16+
<dependency>
17+
<groupId>com.aliyun.odps</groupId>
18+
<artifactId>odps-sdk-core</artifactId>
19+
<version>0.48.8-public</version>
20+
</dependency>
21+
</dependencies>
22+
</project>
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/**
2+
* OdpsClientSetupSample.java
3+
*
4+
* This example class demonstrates how to construct Alibaba Cloud MaxCompute (formerly ODPS) client instances based on various authentication methods.
5+
* MaxCompute is a large-scale data processing and analytics service; this SDK facilitates rapid integration and usage of the service.
6+
*/
7+
8+
import com.aliyun.auth.credentials.provider.ICredentialProvider;
9+
import com.aliyun.odps.Odps;
10+
import com.aliyun.odps.account.Account;
11+
import com.aliyun.odps.account.AklessAccount;
12+
import com.aliyun.odps.account.AliyunAccount;
13+
import com.aliyun.odps.account.AppAccount;
14+
import com.aliyun.odps.account.BearerTokenAccount;
15+
import com.aliyun.odps.account.StsAccount;
16+
17+
/**
18+
* OdpsClientSetupSample provides static methods for constructing Odps clients,
19+
* supporting authentication via AccessKey, STS Token, Aliyun Credential Provider, Dual-Signature Authentication, and Bearer Token.
20+
*/
21+
public class OdpsClientSetupSample {
22+
// Replace this Endpoint with your actual MaxCompute service address
23+
private static final String SAMPLE_ENDPOINT = "<your odps endpoint>";
24+
25+
/**
26+
* Constructs an Odps client using AccessKey.
27+
* Requires users to provide an AccessId and AccessKey.
28+
* <a href="https://help.aliyun.com/zh/ram/user-guide/create-an-accesskey-pair">How to create and obtain AccessKey</a>
29+
*
30+
* @param accessId User's AccessId.
31+
* @param accessKey User's AccessKey.
32+
* @return An initialized Odps instance.
33+
*/
34+
public static Odps buildWithAccessKey(String accessId, String accessKey) {
35+
Account account = new AliyunAccount(accessId, accessKey);
36+
Odps odps = new Odps(account);
37+
odps.setEndpoint(SAMPLE_ENDPOINT);
38+
return odps;
39+
}
40+
41+
/**
42+
* Constructs an Odps client using STS Token.
43+
* Suitable for temporary authorization scenarios.
44+
*
45+
* @param accessId User's AccessId.
46+
* @param accessKey User's AccessKey.
47+
* @param stsToken The STS Token.
48+
* @return An initialized Odps instance.
49+
*/
50+
public static Odps buildWithStsToken(String accessId, String accessKey, String stsToken) {
51+
Account account = new StsAccount(accessId, accessKey, stsToken);
52+
Odps odps = new Odps(account);
53+
odps.setEndpoint(SAMPLE_ENDPOINT);
54+
return odps;
55+
}
56+
57+
/**
58+
* Constructs an Odps client using an Aliyun Credential Provider.
59+
* Suitable for scenarios such as RAM role authorization on ECS instances.
60+
* ICredentialProvider is an AK-less authentication method provided by Alibaba Cloud (it actually provides a mechanism for generating and automatically rotating STS Tokens based on RamRole, and for ODPS, it still falls under STS Token-based authentication).
61+
* The aliyun-java-auth package provides many implementations of ICredentialProvider, such as DefaultCredentialProvider and RamRoleArnCredentialProvider; users can choose different implementations based on their needs.
62+
*
63+
* @param credentialProvider An instance of Aliyun Credential Provider.
64+
* @return An initialized Odps instance.
65+
*/
66+
public static Odps buildWithCredentialProvider(ICredentialProvider credentialProvider) {
67+
Account account = new AklessAccount(credentialProvider);
68+
Odps odps = new Odps(account);
69+
odps.setEndpoint(SAMPLE_ENDPOINT);
70+
return odps;
71+
}
72+
73+
/**
74+
* Constructs an Odps client using Dual-Signature Authentication.
75+
* Some applications require dual-signature authentication (which essentially uses one set of AKs for application identification and another for user identification).
76+
*
77+
* @param accessId User's AccessId.
78+
* @param accessKey User's AccessKey.
79+
* @param appAccessId Application's AccessId.
80+
* @param appAccessKey Application's AccessKey.
81+
* @return An initialized Odps instance.
82+
*/
83+
public static Odps buildWithDualSignature(String accessId, String accessKey, String appAccessId, String appAccessKey) {
84+
AppAccount appAccount = new AppAccount(new AliyunAccount(appAccessId, appAccessKey));
85+
Account account = new AliyunAccount(accessId, accessKey);
86+
Odps odps = new Odps(account, appAccount);
87+
odps.setEndpoint(SAMPLE_ENDPOINT);
88+
return odps;
89+
}
90+
91+
/**
92+
* Constructs an Odps client using Bearer Token.
93+
* Bearer Tokens are typically used for short-term access authorization.
94+
* How to generate Bearer Token: {@link com.aliyun.odps.security.SecurityManager#generateAuthorizationToken(String, String)}
95+
* Example: sm.generateAuthorizationToken(policy, "BEARER");
96+
*
97+
* @param bearerToken The generated Bearer Token string.
98+
* @return An initialized Odps instance.
99+
*/
100+
public static Odps buildWithBearerToken(String bearerToken) {
101+
Account account = new BearerTokenAccount(bearerToken);
102+
Odps odps = new Odps(account);
103+
odps.setEndpoint(SAMPLE_ENDPOINT);
104+
return odps;
105+
}
106+
}

odps-sdk/odps-sdk-core/src/main/java/com/aliyun/odps/security/SecurityManager.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -844,6 +844,9 @@ public String runQuery(String query, Boolean jsonOutput, String supervisionToken
844844
return run(query, jsonOutput, supervisionToken, settings).waitForSuccess();
845845
}
846846

847+
/**
848+
* @param policy 参考文档: <a href="https://help.aliyun.com/zh/maxcompute/user-guide/policy-based-access-control-1">Policy权限控制</a>
849+
*/
847850
public String generateAuthorizationToken(String policy, String type)
848851
throws OdpsException {
849852
if ("Bearer".equalsIgnoreCase(type)) {

0 commit comments

Comments
 (0)