Run security-related commands using the Java SDK
This topic describes how to run security-related commands using Java SDK operations in MaxCompute Studio.
Background information
Security-related commands can be run in either of the following ways:
Run the commands on the MaxCompute client. For more information, see List of security parameters.
Commands that start with the following keywords are security-related MaxCompute commands.
GRANT/REVOKE ... SHOW GRANTS/ACL/PACKAGE/LABEL/ROLE/PRINCIPALS SHOW PRIV/PRIVILEGES LIST/ADD/REMOVE USERS/ROLES/TRUSTEDPROJECTS DROP/CREATE ROLE CLEAR EXPIRED GRANTS DESC/DESCRIBE ROLE/PACKAGE CREATE/DELETE/DROP PACKAGE ADD ... TO PACKAGE REMOVE ... FROM PACKAGE ALLOW/DISALLOW PROJECT INSTALL/UNINSTALL PACKAGE LIST/ADD/REMOVE ACCOUNTPROVIDERS SET LABEL ...Run the commands by using the
SecurityManager.runQuery()operation of the Java SDK. For operation details, see MaxCompute SDK Java Doc.NoteMaxCompute security-related commands are not SQL commands and cannot be run through SQLTask.
Prerequisites
Complete the following operations first:
Install IntelliJ IDEA. For more information, see Install IntelliJ IDEA.
Install MaxCompute Studio and connect it to a MaxCompute project. For more information, see Install MaxCompute Studio and Create a MaxCompute project connection.
Create a MaxCompute Java module, and add project dependencies in the pom.xml file.
The SecurityManager class is included in the odps-sdk-core package. Add the following dependency configuration.
<dependency> <groupId>com.aliyun.odps</groupId> <artifactId>odps-sdk-core</artifactId> <version>0.29.11-oversea-public</version> </dependency>To get the latest SDK version, search for odps-sdk-core on search.maven.org.
Procedure
Create a test table named test_label.
CREATE TABLE IF NOT EXISTS test_label( key STRING, value BIGINT );Create a class in the MaxCompute Java module and develop the Java code.
import com.aliyun.odps.Column; import com.aliyun.odps.Odps; import com.aliyun.odps.OdpsException; import com.aliyun.odps.OdpsType; import com.aliyun.odps.TableSchema; import com.aliyun.odps.account.Account; import com.aliyun.odps.account.AliyunAccount; import com.aliyun.odps.security.SecurityManager; public class test { public static void main(String [] args) throws OdpsException { try { // init odps Account account = new AliyunAccount("<your_accessid>", "<your_accesskey>"); Odps odps = new Odps(account); odps.setEndpoint("http://service.odps.aliyun.com/api"); odps.setDefaultProject("<your_project>"); // set label 2 to table columns SecurityManager securityManager = odps.projects().get().getSecurityManager(); String res = securityManager.runQuery("SET LABEL 2 TO TABLE test_label(key, value);", false); System.out.println(res); } catch (OdpsException e) { e.printStackTrace(); } } }Run the preceding code and view the result in the console.
"D:\Program Files\Java\jdk1.8.0_181\bin\java.exe" ... OK Process finished with exit code 0Verify the result.
After the program finishes running, run the
desc test_labelcommand on the MaxCompute client. The set label command has taken effect.------+ ! Native Columns: ! +------+ ! Field ! Type ! Label ! Comment ! +------+ ! key ! string ! 2 ! ! ! value ! bigint ! 2 ! ! +------+NoteFor more security commands, see MaxCompute permissions, Label-based access control, List of security parameters, and Cross-project resource access based on packages.