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;
  2. import com.webank.wedatasphere.linkis.common.utils.Utils;
  3. import com.webank.wedatasphere.linkis.httpclient.dws.authentication.StaticAuthenticationStrategy;
  4. import com.webank.wedatasphere.linkis.httpclient.dws.authentication.TokenAuthenticationStrategy;
  5. import com.webank.wedatasphere.linkis.httpclient.dws.config.DWSClientConfig;
  6. import com.webank.wedatasphere.linkis.httpclient.dws.config.DWSClientConfigBuilder;
  7. import com.webank.wedatasphere.linkis.ujes.client.UJESClient;
  8. import com.webank.wedatasphere.linkis.ujes.client.UJESClientImpl;
  9. import com.webank.wedatasphere.linkis.ujes.client.request.JobExecuteAction;
  10. import com.webank.wedatasphere.linkis.ujes.client.request.ResultSetAction;
  11. import com.webank.wedatasphere.linkis.ujes.client.response.JobExecuteResult;
  12. import com.webank.wedatasphere.linkis.ujes.client.response.JobInfoResult;
  13. import com.webank.wedatasphere.linkis.ujes.client.response.JobProgressResult;
  14. import org.apache.commons.io.IOUtils;
  15. import java.util.HashMap;
  16. import java.util.Map;
  17. import java.util.concurrent.TimeUnit;
  18. public class UJESClientTest {
  19. public static void main(String[] args){
  20. String user = "hadoop";
  21. String executeCode = "show databases;";
  22. // 1. Configure DWSClientBuilder, get a DWSClientConfig through DWSClientBuilder
  23. DWSClientConfig clientConfig = ((DWSClientConfigBuilder) (DWSClientConfigBuilder.newBuilder()
  24. .addUJESServerUrl("http://${ip}:${port}") //Specify ServerUrl, the address of the linkis server-side gateway, such as http://{ip}:{port}
  25. .connectionTimeout(30000) //connectionTimeOut client connection timeout
  26. .discoveryEnabled(false).discoveryFrequency(1, TimeUnit.MINUTES) //Whether to enable registration discovery, if enabled, the newly launched Gateway will be automatically discovered
  27. .loadbalancerEnabled(true) // Whether to enable load balancing, if registration discovery is not enabled, load balancing is meaningless
  28. .maxConnectionSize(5) //Specify the maximum number of connections, that is, the maximum number of concurrent
  29. .retryEnabled(false).readTimeout(30000) //execution failed, whether to allow retry
  30. .setAuthenticationStrategy(new StaticAuthenticationStrategy()) //AuthenticationStrategy Linkis authentication method
  31. .setAuthTokenKey("${username}").setAuthTokenValue("${password}"))) //Authentication key, generally the user name; authentication value, generally the password corresponding to the user name
  32. .setDWSVersion("v1").build(); //Linkis background protocol version, the current version is v1
  33. // 2. Get a UJESClient through DWSClientConfig
  34. UJESClient client = new UJESClientImpl(clientConfig);
  35. try {
  36. // 3. Start code execution
  37. System.out.println("user: "+ user + ", code: [" + executeCode + "]");
  38. Map<String, Object> startupMap = new HashMap<String, Object>();
  39. startupMap.put("wds.linkis.yarnqueue", "default"); // Various startup parameters can be stored in startupMap, see linkis management console configuration
  40. JobExecuteResult jobExecuteResult = client.execute(JobExecuteAction.builder()
  41. .setCreator("linkisClient-Test") //creator, the system name of the client requesting linkis, used for system-level isolation
  42. .addExecuteCode(executeCode) //ExecutionCode The code to be executed
  43. .setEngineType((JobExecuteAction.EngineType) JobExecuteAction.EngineType$.MODULE$.HIVE()) // The execution engine type of the linkis that you want to request, such as Spark hive, etc.
  44. .setUser(user) //User, request user; used for user-level multi-tenant isolation
  45. .setStartupParams(startupMap)
  46. .build());
  47. System.out.println("execId: "+ jobExecuteResult.getExecID() + ", taskId:" + jobExecuteResult.taskID());
  48. // 4. Get the execution status of the script
  49. JobInfoResult jobInfoResult = client.getJobInfo(jobExecuteResult);
  50. int sleepTimeMills = 1000;
  51. while(!jobInfoResult.isCompleted()) {
  52. // 5. Get the execution progress of the script
  53. JobProgressResult progress = client.progress(jobExecuteResult);
  54. Utils.sleepQuietly(sleepTimeMills);
  55. jobInfoResult = client.getJobInfo(jobExecuteResult);
  56. }
  57. // 6. Get the job information of the script
  58. JobInfoResult jobInfo = client.getJobInfo(jobExecuteResult);
  59. // 7. Get the list of result sets (if the user submits multiple SQL at a time, multiple result sets will be generated)
  60. String resultSet = jobInfo.getResultSetList(client)[0];
  61. // 8. Get a specific result set through a result set information
  62. Object fileContents = client.resultSet(ResultSetAction.builder().setPath(resultSet).setUser(jobExecuteResult.getUser()).build()).getFileContent();
  63. System.out.println("fileContents: "+ fileContents);
  64. } catch (Exception e) {
  65. e.printStackTrace();
  66. IOUtils.closeQuietly(client);
  67. }
  68. IOUtils.closeQuietly(client);
  69. }
  70. }

Run the above code to interact with Linkis

3 Scala test code

  1. package com.wedatasphere.linkis.ujes.test
  2. import java.util.concurrent.TimeUnit
  3. import com.webank.wedatasphere.linkis.common.utils.Utils
  4. import com.webank.wedatasphere.linkis.httpclient.dws.authentication.StaticAuthenticationStrategy
  5. import com.webank.wedatasphere.linkis.httpclient.dws.config.DWSClientConfigBuilder
  6. import com.webank.wedatasphere.linkis.ujes.client.UJESClient
  7. import com.webank.wedatasphere.linkis.ujes.client.request.JobExecuteAction.EngineType
  8. import com.webank.wedatasphere.linkis.ujes.client.request.{JobExecuteAction, ResultSetAction}
  9. import org.apache.commons.io.IOUtils
  10. object UJESClientImplTest extends App {
  11. var executeCode = "show databases;"
  12. var user = "hadoop"
  13. // 1. Configure DWSClientBuilder, get a DWSClientConfig through DWSClientBuilder
  14. val clientConfig = DWSClientConfigBuilder.newBuilder()
  15. .addUJESServerUrl("http://${ip}:${port}") //Specify ServerUrl, the address of the Linkis server-side gateway, such as http://{ip}:{port}
  16. .connectionTimeout(30000) //connectionTimeOut client connection timeout
  17. .discoveryEnabled(false).discoveryFrequency(1, TimeUnit.MINUTES) //Whether to enable registration discovery, if enabled, the newly launched Gateway will be automatically discovered
  18. .loadbalancerEnabled(true) // Whether to enable load balancing, if registration discovery is not enabled, load balancing is meaningless
  19. .maxConnectionSize(5) //Specify the maximum number of connections, that is, the maximum number of concurrent
  20. .retryEnabled(false).readTimeout(30000) //execution failed, whether to allow retry
  21. .setAuthenticationStrategy(new StaticAuthenticationStrategy()) //AuthenticationStrategy Linkis authentication method
  22. .setAuthTokenKey("${username}").setAuthTokenValue("${password}") //Authentication key, generally the user name; authentication value, generally the password corresponding to the user name
  23. .setDWSVersion("v1").build() //Linkis backend protocol version, the current version is v1
  24. // 2. Get a UJESClient through DWSClientConfig
  25. val client = UJESClient(clientConfig)
  26. try {
  27. // 3. Start code execution
  28. println("user: "+ user + ", code: [" + executeCode + "]")
  29. val startupMap = new java.util.HashMap[String, Any]()
  30. startupMap.put("wds.linkis.yarnqueue", "default") //Startup parameter configuration
  31. val jobExecuteResult = client.execute(JobExecuteAction.builder()
  32. .setCreator("LinkisClient-Test") //creator, requesting the system name of the Linkis client, used for system-level isolation
  33. .addExecuteCode(executeCode) //ExecutionCode The code to be executed
  34. .setEngineType(EngineType.SPARK) // The execution engine type of Linkis that you want to request, such as Spark hive, etc.
  35. .setStartupParams(startupMap)
  36. .setUser(user).build()) //User, request user; used for user-level multi-tenant isolation
  37. println("execId: "+ jobExecuteResult.getExecID + ", taskId:" + jobExecuteResult.taskID)
  38. // 4. Get the execution status of the script
  39. var jobInfoResult = client.getJobInfo(jobExecuteResult)
  40. val sleepTimeMills: Int = 1000
  41. while (!jobInfoResult.isCompleted) {
  42. // 5. Get the execution progress of the script
  43. val progress = client.progress(jobExecuteResult)
  44. val progressInfo = if (progress.getProgressInfo != null) progress.getProgressInfo.toList else List.empty
  45. println("progress: "+ progress.getProgress + ", progressInfo:" + progressInfo)
  46. Utils.sleepQuietly(sleepTimeMills)
  47. jobInfoResult = client.getJobInfo(jobExecuteResult)
  48. }
  49. if (!jobInfoResult.isSucceed) {
  50. println("Failed to execute job: "+ jobInfoResult.getMessage)
  51. throw new Exception(jobInfoResult.getMessage)
  52. }
  53. // 6. Get the job information of the script
  54. val jobInfo = client.getJobInfo(jobExecuteResult)
  55. // 7. Get the list of result sets (if the user submits multiple SQL at a time, multiple result sets will be generated)
  56. val resultSetList = jobInfoResult.getResultSetList(client)
  57. println("All result set list:")
  58. resultSetList.foreach(println)
  59. val oneResultSet = jobInfo.getResultSetList(client).head
  60. // 8. Get a specific result set through a result set information
  61. val fileContents = client.resultSet(ResultSetAction.builder().setPath(oneResultSet).setUser(jobExecuteResult.getUser).build()).getFileContent
  62. println("First fileContents: ")
  63. println(fileContents)
  64. } catch {
  65. case e: Exception => {
  66. e.printStackTrace()
  67. }
  68. }
  69. IOUtils.closeQuietly(client)
  70. }