Skip to content

Commit e6c2726

Browse files
authored
Better Java examples (#200)
## Changes This PR makes several changes to examples and documentation for the Java SDK. 1. Add documentation comments for the Wait class and methods, describing when they are returned and how to use them to retrieve API responses. 2. Check in examples from https://docs.databricks.com/en/dev-tools/sdk-java.html as a reference for @PaulCornellDB for maintaining the docs page. This way, examples are updated as necessary to account for any changes in the SDK. 3. Refreshed all existing examples to work with the current version of the Java SDK. 4. Refactored all non-Spring Boot examples into a single maven project in `examples/docs` so that users can find all relevant examples in a single directory (examples/docs/src/main/java/com/databricks/sdk/examples). This also means that there are fewer `pom.xml` files that need to be maintained. 5. Bump the version of the Java SDK used in the example maven projects as part of the release flow. Later, as part of the release process, we may need to set the `SNAPSHOT` version to make it possible to test local changes to the SDK more easily. ## Tests - [x] Compilation of the maven projects in the `examples/` directory passes. - [ ] Unable to test the release workflow (as that forcibly resets to the last commit on main).
1 parent 2e49467 commit e6c2726

File tree

18 files changed

+213
-151
lines changed

18 files changed

+213
-151
lines changed

.codegen.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
"version": {
1919
"pom.xml": "<artifactId>databricks-sdk-parent</artifactId>\n <version>$VERSION</version>",
2020
"databricks-sdk-java/pom.xml": "<artifactId>databricks-sdk-parent</artifactId>\n <version>$VERSION</version>",
21-
"databricks-sdk-java/src/main/java/com/databricks/sdk/core/UserAgent.java": "private static final String version = \"$VERSION\";"
21+
"databricks-sdk-java/src/main/java/com/databricks/sdk/core/UserAgent.java": "private static final String version = \"$VERSION\";",
22+
"examples/docs/pom.xml": "<artifactId>databricks-sdk-java</artifactId>\n <version>$VERSION</version>",
23+
"examples/spring-boot-oauth-u2m-demo/pom.xml": "<artifactId>databricks-sdk-java</artifactId>\n <version>$VERSION</version>"
2224
},
2325
"toolchain": {
2426
"require": ["mvn", "java"],

databricks-sdk-java/src/main/java/com/databricks/sdk/support/Wait.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,29 @@
44
import java.util.concurrent.TimeoutException;
55
import java.util.function.Consumer;
66

7+
/**
8+
* A Wait object is returned by long-running operations. It can be used to wait for the operation to
9+
* complete.
10+
*
11+
* <p>Several operations, such as starting a cluster or submitting a job, are long-running
12+
* operations. After being initiated by a call to the API, these operations continue to run in the
13+
* background until they complete. A separate API call can be made to check the status of the
14+
* operation. {@code Wait} objects encapsulate this process, polling for the status of the operation
15+
* until it completes.
16+
*
17+
* <p>{@code Wait} objects expose both the response from the triggering API call and the awaited
18+
* resource. For example, the {@link
19+
* com.databricks.sdk.service.compute.ClustersAPI#create(com.databricks.sdk.service.compute.CreateCluster)}
20+
* method returns a {@code Wait<ClusterDetails, CreateClusterResponse>} object. The response from
21+
* the API call to create the cluster is available via the {@link #getResponse()} method. The
22+
* awaited resource, the cluster details, is available via the {@link #get()} method.
23+
*
24+
* <p>The {@link #get()} method waits for the operation to complete for up to 20 minutes. To wait
25+
* for a custom duration, use the {@link #get(Duration)} method.
26+
*
27+
* @param <T>
28+
* @param <R>
29+
*/
730
public class Wait<T, R> {
831
private final WaitStarter<T> impl;
932
private final R response;
@@ -23,14 +46,32 @@ public Wait<T, R> onProgress(Consumer<T> progress) {
2346
return this;
2447
}
2548

49+
/**
50+
* Wait for the operation to complete for up to 20 minutes.
51+
*
52+
* @return the awaited resource
53+
* @throws TimeoutException if the operation does not complete within 20 minutes
54+
*/
2655
public T get() throws TimeoutException {
2756
return get(Duration.ofMinutes(20));
2857
}
2958

59+
/**
60+
* Wait for the operation to complete for up to the specified duration.
61+
*
62+
* @param timeout the maximum duration to wait
63+
* @return the awaited resource
64+
* @throws TimeoutException if the operation does not complete within the specified duration
65+
*/
3066
public T get(Duration timeout) throws TimeoutException {
3167
return impl.apply(timeout, progress);
3268
}
3369

70+
/**
71+
* Get the response for the initial API call made to start the operation.
72+
*
73+
* @return the response object
74+
*/
3475
public R getResponse() {
3576
return response;
3677
}

examples/cli-auth-demo/pom.xml

Lines changed: 0 additions & 36 deletions
This file was deleted.

examples/cli-oauth-u2m-demo/pom.xml

Lines changed: 0 additions & 76 deletions
This file was deleted.

examples/docs/pom.xml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
<modelVersion>4.0.0</modelVersion>
6+
<groupId>com.databricks</groupId>
7+
<artifactId>docs</artifactId>
8+
<version>0.0.1</version>
9+
10+
<name>docs</name>
11+
<url>https://github.com/databricks/databricks-sdk-java</url>
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
<maven.compiler.source>1.8</maven.compiler.source>
16+
<maven.compiler.target>1.8</maven.compiler.target>
17+
</properties>
18+
<dependencies>
19+
<dependency>
20+
<groupId>org.slf4j</groupId>
21+
<artifactId>slf4j-reload4j</artifactId>
22+
<version>2.0.7</version>
23+
</dependency>
24+
<dependency>
25+
<groupId>com.databricks</groupId>
26+
<artifactId>databricks-sdk-java</artifactId>
27+
<version>0.14.0</version>
28+
</dependency>
29+
</dependencies>
30+
</project>

examples/cli-oauth-u2m-demo/src/main/java/com/databricks/example/App.java renamed to examples/docs/src/main/java/com/databricks/example/CliOAuthU2MExample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
* <p>Before running this example, make sure to configure the host and client ID in the {@code DatabricksConfig} object.
1212
*/
13-
public class App {
13+
public class CliOAuthU2MExample {
1414
public static void main(String[] args) {
1515
DatabricksConfig config = new DatabricksConfig()
1616
.setAuthType("external-browser")
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.databricks.sdk.examples;
2+
3+
import com.databricks.sdk.WorkspaceClient;
4+
import com.databricks.sdk.service.compute.ClusterDetails;
5+
import com.databricks.sdk.service.compute.CreateCluster;
6+
import com.databricks.sdk.service.compute.CreateClusterResponse;
7+
import com.databricks.sdk.support.Wait;
8+
import java.time.Duration;
9+
import java.util.concurrent.TimeoutException;
10+
11+
public class CreateClusterExample {
12+
public static void main(String[] args) throws TimeoutException {
13+
WorkspaceClient w = new WorkspaceClient();
14+
15+
// Creating a cluster returns immediately.
16+
Wait<ClusterDetails, CreateClusterResponse> wait =
17+
w.clusters()
18+
.create(
19+
new CreateCluster()
20+
.setClusterName("my-cluster")
21+
.setSparkVersion("12.2.x-scala2.12")
22+
.setNodeTypeId("i3.xlarge")
23+
.setAutoterminationMinutes(15L)
24+
.setNumWorkers(1L));
25+
26+
// Creating a cluster is a long-running operation. Wait for the cluster to finish
27+
// starting by calling the Wait.get() method. This waits for 20 minutes by default.
28+
ClusterDetails c = wait.get();
29+
30+
// Wait for a custom duration with the Wait.get(Duration timeout) method.
31+
c = wait.get(Duration.ofMinutes(10));
32+
33+
System.out.println(
34+
"View the cluster at "
35+
+ w.config().getHost()
36+
+ "#setting/clusters/"
37+
+ c.getClusterId()
38+
+ "/configuration\n");
39+
}
40+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.databricks.sdk.examples;
2+
3+
import com.databricks.sdk.WorkspaceClient;
4+
import com.databricks.sdk.service.jobs.*;
5+
import java.util.*;
6+
7+
public class CreateJobExample {
8+
public static void main(String[] args) {
9+
System.out.println("Some short name for the job (for example, my-job):");
10+
Scanner in = new Scanner(System.in);
11+
String jobName = in.nextLine();
12+
13+
System.out.println("Some short description for the job (for example, My job):");
14+
String description = in.nextLine();
15+
16+
System.out.println(
17+
"ID of the existing cluster in the workspace to run the job on (for example, 1234-567890-ab123cd4):");
18+
String existingClusterId = in.nextLine();
19+
20+
System.out.println(
21+
"Workspace path of the notebook to run (for example, /Users/[email protected]/my-notebook):");
22+
String notebookPath = in.nextLine();
23+
24+
System.out.println("Some key to apply to the job's tasks (for example, my-key): ");
25+
String taskKey = in.nextLine();
26+
27+
System.out.println("Attempting to create the job. Please wait...");
28+
29+
WorkspaceClient w = new WorkspaceClient();
30+
31+
Map<String, String> map = new HashMap<>();
32+
33+
Collection<Task> tasks =
34+
Arrays.asList(
35+
new Task()
36+
.setDescription(description)
37+
.setExistingClusterId(existingClusterId)
38+
.setNotebookTask(
39+
new NotebookTask()
40+
.setBaseParameters(map)
41+
.setNotebookPath(notebookPath)
42+
.setSource(Source.WORKSPACE))
43+
.setTaskKey(taskKey));
44+
45+
CreateResponse j = w.jobs().create(new CreateJob().setName(jobName).setTasks(tasks));
46+
47+
System.out.println("View the job at " + w.config().getHost() + "/#job/" + j.getJobId());
48+
}
49+
}

0 commit comments

Comments
 (0)