Use of 0.X SDK

Linkis provides a convenient interface for calling JAVA and SCALA, which can be used only by introducing the ujes-client module

1 Introduce dependent modules

1. <dependency>
2. <groupId>com.webank.wedatasphere.linkis</groupId>
3. <artifactId>linkis-ujes-client</artifactId>
4. <version>0.9.3</version>
5. </dependency>

2 Java test code

Establish the Java test class UJESClientImplTestJ, the specific interface meaning can be seen in the notes:

1. package com.wedatasphere.linkis.ujes.test;

4. import com.webank.wedatasphere.linkis.common.utils.Utils;
5. import com.webank.wedatasphere.linkis.httpclient.dws.authentication.StaticAuthenticationStrategy;
6. import com.webank.wedatasphere.linkis.httpclient.dws.authentication.TokenAuthenticationStrategy;
7. import com.webank.wedatasphere.linkis.httpclient.dws.config.DWSClientConfig;
8. import com.webank.wedatasphere.linkis.httpclient.dws.config.DWSClientConfigBuilder;
9. import com.webank.wedatasphere.linkis.ujes.client.UJESClient;
10. import com.webank.wedatasphere.linkis.ujes.client.UJESClientImpl;
11. import com.webank.wedatasphere.linkis.ujes.client.request.JobExecuteAction;
12. import com.webank.wedatasphere.linkis.ujes.client.request.ResultSetAction;
13. import com.webank.wedatasphere.linkis.ujes.client.response.JobExecuteResult;
14. import com.webank.wedatasphere.linkis.ujes.client.response.JobInfoResult;
15. import com.webank.wedatasphere.linkis.ujes.client.response.JobProgressResult;
16. import org.apache.commons.io.IOUtils;

19. import java.util.HashMap;
20. import java.util.Map;
21. import java.util.concurrent.TimeUnit;

24. public class UJESClientTest {

27. public static void main(String[] args){

30. String user = "hadoop";
31. String executeCode = "show databases;";

34. // 1. Configure DWSClientBuilder, get a DWSClientConfig through DWSClientBuilder
35. DWSClientConfig clientConfig = ((DWSClientConfigBuilder) (DWSClientConfigBuilder.newBuilder()
36. .addUJESServerUrl("http://${ip}:${port}") //Specify ServerUrl, the address of the linkis server-side gateway, such as http://{ip}:{port}
37. .connectionTimeout(30000) //connectionTimeOut client connection timeout
38. .discoveryEnabled(false).discoveryFrequency(1, TimeUnit.MINUTES) //Whether to enable registration discovery, if enabled, the newly launched Gateway will be automatically discovered
39. .loadbalancerEnabled(true) // Whether to enable load balancing, if registration discovery is not enabled, load balancing is meaningless
40. .maxConnectionSize(5) //Specify the maximum number of connections, that is, the maximum number of concurrent
41. .retryEnabled(false).readTimeout(30000) //execution failed, whether to allow retry
42. .setAuthenticationStrategy(new StaticAuthenticationStrategy()) //AuthenticationStrategy Linkis authentication method
43. .setAuthTokenKey("${username}").setAuthTokenValue("${password}"))) //Authentication key, generally the user name; authentication value, generally the password corresponding to the user name
44. .setDWSVersion("v1").build(); //Linkis background protocol version, the current version is v1

47. // 2. Get a UJESClient through DWSClientConfig
48. UJESClient client = new UJESClientImpl(clientConfig);

51. try {
52. // 3. Start code execution
53. System.out.println("user: "+ user + ", code: [" + executeCode + "]");
54. Map<String, Object> startupMap = new HashMap<String, Object>();
55. startupMap.put("wds.linkis.yarnqueue", "default"); // Various startup parameters can be stored in startupMap, see linkis management console configuration
56. JobExecuteResult jobExecuteResult = client.execute(JobExecuteAction.builder()
57. .setCreator("linkisClient-Test") //creator, the system name of the client requesting linkis, used for system-level isolation
58. .addExecuteCode(executeCode) //ExecutionCode The code to be executed
59. .setEngineType((JobExecuteAction.EngineType) JobExecuteAction.EngineType$.MODULE$.HIVE()) // The execution engine type of the linkis that you want to request, such as Spark hive, etc.
60. .setUser(user) //User, request user; used for user-level multi-tenant isolation
61. .setStartupParams(startupMap)
62. .build());
63. System.out.println("execId: "+ jobExecuteResult.getExecID() + ", taskId:" + jobExecuteResult.taskID());

66. // 4. Get the execution status of the script
67. JobInfoResult jobInfoResult = client.getJobInfo(jobExecuteResult);
68. int sleepTimeMills = 1000;
69. while(!jobInfoResult.isCompleted()) {
70. // 5. Get the execution progress of the script
71. JobProgressResult progress = client.progress(jobExecuteResult);
72. Utils.sleepQuietly(sleepTimeMills);
73. jobInfoResult = client.getJobInfo(jobExecuteResult);
74. }

77. // 6. Get the job information of the script
78. JobInfoResult jobInfo = client.getJobInfo(jobExecuteResult);
79. // 7. Get the list of result sets (if the user submits multiple SQL at a time, multiple result sets will be generated)
80. String resultSet = jobInfo.getResultSetList(client)[0];
81. // 8. Get a specific result set through a result set information
82. Object fileContents = client.resultSet(ResultSetAction.builder().setPath(resultSet).setUser(jobExecuteResult.getUser()).build()).getFileContent();
83. System.out.println("fileContents: "+ fileContents);

86. } catch (Exception e) {
87. e.printStackTrace();
88. IOUtils.closeQuietly(client);
89. }
90. IOUtils.closeQuietly(client);
91. }
92. }

Run the above code to interact with Linkis

3 Scala test code

1. package com.wedatasphere.linkis.ujes.test

4. import java.util.concurrent.TimeUnit

7. import com.webank.wedatasphere.linkis.common.utils.Utils
8. import com.webank.wedatasphere.linkis.httpclient.dws.authentication.StaticAuthenticationStrategy
9. import com.webank.wedatasphere.linkis.httpclient.dws.config.DWSClientConfigBuilder
10. import com.webank.wedatasphere.linkis.ujes.client.UJESClient
11. import com.webank.wedatasphere.linkis.ujes.client.request.JobExecuteAction.EngineType
12. import com.webank.wedatasphere.linkis.ujes.client.request.{JobExecuteAction, ResultSetAction}
13. import org.apache.commons.io.IOUtils

16. object UJESClientImplTest extends App {

19. var executeCode = "show databases;"
20. var user = "hadoop"

23. // 1. Configure DWSClientBuilder, get a DWSClientConfig through DWSClientBuilder
24. val clientConfig = DWSClientConfigBuilder.newBuilder()
25. .addUJESServerUrl("http://${ip}:${port}") //Specify ServerUrl, the address of the Linkis server-side gateway, such as http://{ip}:{port}
26. .connectionTimeout(30000) //connectionTimeOut client connection timeout
27. .discoveryEnabled(false).discoveryFrequency(1, TimeUnit.MINUTES) //Whether to enable registration discovery, if enabled, the newly launched Gateway will be automatically discovered
28. .loadbalancerEnabled(true) // Whether to enable load balancing, if registration discovery is not enabled, load balancing is meaningless
29. .maxConnectionSize(5) //Specify the maximum number of connections, that is, the maximum number of concurrent
30. .retryEnabled(false).readTimeout(30000) //execution failed, whether to allow retry
31. .setAuthenticationStrategy(new StaticAuthenticationStrategy()) //AuthenticationStrategy Linkis authentication method
32. .setAuthTokenKey("${username}").setAuthTokenValue("${password}") //Authentication key, generally the user name; authentication value, generally the password corresponding to the user name
33. .setDWSVersion("v1").build() //Linkis backend protocol version, the current version is v1

36. // 2. Get a UJESClient through DWSClientConfig
37. val client = UJESClient(clientConfig)

39. try {
40. // 3. Start code execution
41. println("user: "+ user + ", code: [" + executeCode + "]")
42. val startupMap = new java.util.HashMap[String, Any]()
43. startupMap.put("wds.linkis.yarnqueue", "default") //Startup parameter configuration
44. val jobExecuteResult = client.execute(JobExecuteAction.builder()
45. .setCreator("LinkisClient-Test") //creator, requesting the system name of the Linkis client, used for system-level isolation
46. .addExecuteCode(executeCode) //ExecutionCode The code to be executed
47. .setEngineType(EngineType.SPARK) // The execution engine type of Linkis that you want to request, such as Spark hive, etc.
48. .setStartupParams(startupMap)
49. .setUser(user).build()) //User, request user; used for user-level multi-tenant isolation
50. println("execId: "+ jobExecuteResult.getExecID + ", taskId:" + jobExecuteResult.taskID)

52. // 4. Get the execution status of the script
53. var jobInfoResult = client.getJobInfo(jobExecuteResult)
54. val sleepTimeMills: Int = 1000
55. while (!jobInfoResult.isCompleted) {
56. // 5. Get the execution progress of the script
57. val progress = client.progress(jobExecuteResult)
58. val progressInfo = if (progress.getProgressInfo != null) progress.getProgressInfo.toList else List.empty
59. println("progress: "+ progress.getProgress + ", progressInfo:" + progressInfo)
60. Utils.sleepQuietly(sleepTimeMills)
61. jobInfoResult = client.getJobInfo(jobExecuteResult)
62. }
63. if (!jobInfoResult.isSucceed) {
64. println("Failed to execute job: "+ jobInfoResult.getMessage)
65. throw new Exception(jobInfoResult.getMessage)
66. }

69. // 6. Get the job information of the script
70. val jobInfo = client.getJobInfo(jobExecuteResult)
71. // 7. Get the list of result sets (if the user submits multiple SQL at a time, multiple result sets will be generated)
72. val resultSetList = jobInfoResult.getResultSetList(client)
73. println("All result set list:")
74. resultSetList.foreach(println)
75. val oneResultSet = jobInfo.getResultSetList(client).head
76. // 8. Get a specific result set through a result set information
77. val fileContents = client.resultSet(ResultSetAction.builder().setPath(oneResultSet).setUser(jobExecuteResult.getUser).build()).getFileContent
78. println("First fileContents: ")
79. println(fileContents)
80. } catch {
81. case e: Exception => {
82. e.printStackTrace()
83. }
84. }
85. IOUtils.closeQuietly(client)
86. }