|
| 1 | +# Migration Guide |
| 2 | + |
| 3 | +The Couchbase Analytics Java SDK is designed to work specifically with Couchbase Enterprise Analytics. |
| 4 | +It is a successor to the analytics API from the general Couchbase Java SDK, which we'll refer to here as the "operational" SDK. |
| 5 | + |
| 6 | +This section offers advice on how to migrate code from the operational SDK ("before") to the analytics SDK ("after"). |
| 7 | + |
| 8 | +## Class names |
| 9 | + |
| 10 | +The analytics SDK omits the "Analytics" prefix from several class names in favor of the more general "Query" prefix. |
| 11 | + |
| 12 | +| Before | After | |
| 13 | +|---------------------|-------------------------------| |
| 14 | +| `AnalyticsResult` | `QueryResult` | |
| 15 | +| `AnalyticsOptions` | `QueryOptions` | |
| 16 | +| `AnalyticsMetaData` | `QueryMetadata` (lowercase d) | |
| 17 | +| `AnalyticsWarning` | `QueryWarning` | |
| 18 | + |
| 19 | +The operational SDK's reactive API does not have a direct analogue in the base analytics SDK. |
| 20 | +An API for integrating with Project Reactor is available as an extension library. |
| 21 | +Note the prefix "Reactive" changes to "Reactor". |
| 22 | + |
| 23 | +| Before | After (with extension library) | |
| 24 | +|-----------------------|--------------------------------| |
| 25 | +| `ReactiveQueryResult` | `ReactorQueryResult` | |
| 26 | + |
| 27 | +## Method names |
| 28 | + |
| 29 | +| Before | After | |
| 30 | +|------------------|------------------------------------------| |
| 31 | +| `analyticsQuery` | `executeQuery` / `executeStreamingQuery` | |
| 32 | + |
| 33 | + |
| 34 | +## Query options |
| 35 | + |
| 36 | +With the operational SDK, the caller creates an instance of `AnalayticsOptions` and configures it. |
| 37 | +The operational SDK expected you to pass positional parameters as a `JsonArray`, and named parameters as a `JsonObject`. |
| 38 | + |
| 39 | +With the analytics SDK, options are specified via a callback that modifies an instance of `QueryOptions` created by the SDK. |
| 40 | +The analytics SDK takes positional parameters as a `List`, and named parameters as a `Map`. |
| 41 | + |
| 42 | +_Before:_ |
| 43 | + |
| 44 | +```java |
| 45 | +AnalyticsResult result = operationalCluster |
| 46 | + .analyticsQuery( |
| 47 | + "SELECT ? AS greeting", |
| 48 | + AnalyticsOptions.analyticsOptions() |
| 49 | + .readonly(true) |
| 50 | + .parameters(JsonArray.from("hello world")) |
| 51 | + ); |
| 52 | +``` |
| 53 | + |
| 54 | +_After:_ |
| 55 | + |
| 56 | +```java |
| 57 | +QueryResult result = analyticsCluster |
| 58 | + .executeQuery( |
| 59 | + "SELECT ? AS greeting", |
| 60 | + options -> options |
| 61 | + .readOnly(true) // uppercase "O" |
| 62 | + .parameters(List.of("hello world")) |
| 63 | + ); |
| 64 | +``` |
| 65 | + |
| 66 | +## JsonObject and JsonArray |
| 67 | + |
| 68 | +These classes are present in both the operational and analytics SDKs, but in different packages. |
| 69 | +The two versions have similar methods, but are not interchangeable. |
| 70 | + |
| 71 | +The version to use with the analytics SDK is in package `com.couchbase.analytics.client.java.json`. |
| 72 | + |
| 73 | +If you need to pass JSON between the analytics and operational SDKs, first convert the JSON to a byte array using the `toBytes()` method. |
| 74 | +Then use the other SDK's version to parse the JSON using the `fromJson(byte[])` method. |
| 75 | + |
| 76 | + |
| 77 | +## Converting row values |
| 78 | + |
| 79 | +With the operational SDK, query result rows are accessed by calling `AnalyticsResult.rowsAs(<type>)`. |
| 80 | +This method returns a new list where each result row is mapped to an instance of the specified type. |
| 81 | + |
| 82 | +The analytics SDK represents result rows differently. |
| 83 | +It introduces a new `Row` class that represents a single result row. |
| 84 | +The `queryResult.rows()` method returns a `List<Row>`. |
| 85 | +To convert a row to an instance of some type, call `row.as(<type>)`. |
| 86 | +If null is a valid value, call `row.asNullable(<type>)` instead. |
| 87 | + |
| 88 | +Unlike the operational SDK, the analytics SDK does not have a dedicated method for converting a row to a `JsonObject`. |
| 89 | +Instead, use `row.as(JsonObject.class)`. |
| 90 | +(Make sure to use the version of `JsonObject` from the analytics SDK instead of the operational SDK, otherwise the conversion will fail.) |
| 91 | + |
| 92 | +_Before:_ |
| 93 | + |
| 94 | +```java |
| 95 | +import com.couchbase.client.java.json.JsonObject; |
| 96 | +``` |
| 97 | + |
| 98 | +```java |
| 99 | +AnalyticsResult result = operationalCluster |
| 100 | + .analyticsQuery("SELECT 'hello world' AS greeting"); |
| 101 | + |
| 102 | +JsonObject obj = result.rowsAsObject().getFirst(); |
| 103 | +System.out.println(obj.getString("greeting")); |
| 104 | +``` |
| 105 | + |
| 106 | +_After:_ |
| 107 | + |
| 108 | +```java |
| 109 | +import com.couchbase.analytics.client.java.json.JsonObject; |
| 110 | +``` |
| 111 | + |
| 112 | +```java |
| 113 | +QueryResult result = analyticsCluster |
| 114 | + .executeQuery("SELECT 'hello world' AS greeting"); |
| 115 | + |
| 116 | +JsonObject obj = result.rows().getFirst().as(JsonObject.class); |
| 117 | +System.out.println(obj.getString("greeting")); |
| 118 | +``` |
| 119 | + |
| 120 | +## Streaming result rows |
| 121 | + |
| 122 | +In the operational SDK, the only way to stream result rows from the server is to use the Reactive API. |
| 123 | +The analytics SDK adds a safe and convenient way to stream results rows without the complexity of reactive programming. |
| 124 | + |
| 125 | +_Before:_ |
| 126 | + |
| 127 | +```java |
| 128 | +Mono<ReactiveAnalyticsResult> resultMono = operationalCluster.reactive() |
| 129 | + .analyticsQuery("SELECT RAW i FROM ARRAY_RANGE(0, 10) as i"); |
| 130 | + |
| 131 | +resultMono.flatMapMany(result -> result.rowsAs(Integer.class)) |
| 132 | + .doOnNext(System.out::println) |
| 133 | + .blockLast(); |
| 134 | +``` |
| 135 | + |
| 136 | +_After:_ |
| 137 | + |
| 138 | +```java |
| 139 | +analyticsCluster.executeStreamingQuery( |
| 140 | + "SELECT RAW i FROM ARRAY_RANGE(0, 10) as i", |
| 141 | + row -> System.out.println(row.as(Integer.class)) |
| 142 | +); |
| 143 | +``` |
| 144 | + |
| 145 | +To aid migration of existing reactive codebases, and to support integrations with other reactive components, the analytics SDK has an optional extension library that adds support for Project Reactor. |
| 146 | + |
| 147 | +```xml |
| 148 | +<dependency> |
| 149 | + <groupId>com.couchbase.client</groupId> |
| 150 | + <artifactId>couchbase-analytics-java-client-reactor</artifactId> |
| 151 | + <version>x.y.z</version> |
| 152 | +</dependency> |
| 153 | +``` |
| 154 | + |
| 155 | +_After (with Reactor extension library):_ |
| 156 | + |
| 157 | +```java |
| 158 | +var reactor = ReactorQueryable.from(analyticsClusterOrScope); |
| 159 | + |
| 160 | +Mono<ReactorQueryResult> resultMono = reactor |
| 161 | + .executeQuery("SELECT RAW i FROM ARRAY_RANGE(0, 10) as i"); |
| 162 | + |
| 163 | +resultMono.flatMapMany(ReactorQueryResult::rows) |
| 164 | + .map(row -> row.as(Integer.class)) |
| 165 | + .doOnNext(System.out::println) |
| 166 | + .blockLast(); |
| 167 | +``` |
0 commit comments