|
| 1 | +--- |
| 2 | +sidebarTitle: Java |
| 3 | +--- |
| 4 | + |
| 5 | +import {Tabs} from 'nextra/components' |
| 6 | + |
| 7 | +# Java Client SDK |
| 8 | + |
| 9 | + |
| 10 | +## Installing Java client SDK |
| 11 | + |
| 12 | +<Tabs items={['Maven', 'Gradle']}> |
| 13 | + <Tabs.Tab> |
| 14 | + ```xml |
| 15 | + <dependency> |
| 16 | + <groupId>io.github.oxia-db</groupId> |
| 17 | + <artifactId>oxia-client</artifactId> |
| 18 | + <version>${VERSION}</version> |
| 19 | + </dependency> |
| 20 | + ``` |
| 21 | + </Tabs.Tab> |
| 22 | + <Tabs.Tab> |
| 23 | + ```java |
| 24 | + implementation("io.github.oxia-db:oxia-client:${VERSION}") |
| 25 | + ``` |
| 26 | + </Tabs.Tab> |
| 27 | +</Tabs> |
| 28 | + |
| 29 | +Javadoc documentation is available at https://oxia-db.github.io/oxia-client-java/apidocs/latest |
| 30 | + |
| 31 | +## Sync and Async APIs |
| 32 | + |
| 33 | +Oxia provides two API flavors in the client: |
| 34 | + * ***Sync***: Easier and convenient to use. Methods are blocking and return the result of the operation. |
| 35 | + * ***Async***: Returns futures to track completion of the operations. Allows having more operations outstanding |
| 36 | +and achieving higher throughput. |
| 37 | + |
| 38 | +## Client API |
| 39 | + |
| 40 | +### Initializing the client |
| 41 | + |
| 42 | +To use the Oxia client, you need to create a client instance. Once created, a client instance will be valid until |
| 43 | +it's explicitly closed, and it can be used from different threads. |
| 44 | + |
| 45 | +<Tabs items={['Sync', 'Async']}> |
| 46 | + <Tabs.Tab> |
| 47 | + ```java |
| 48 | + SyncOxiaClient client = OxiaClientBuilder.create(oxia.getServiceAddress()) |
| 49 | + .namespace("my-namespace") |
| 50 | + .syncClient(); |
| 51 | + ``` |
| 52 | + </Tabs.Tab> |
| 53 | + <Tabs.Tab> |
| 54 | + ```java |
| 55 | + AsyncOxiaClient client = OxiaClientBuilder.create(oxia.getServiceAddress()) |
| 56 | + .namespace("my-namespace") |
| 57 | + .asyncClient() |
| 58 | + .get(); |
| 59 | + ``` |
| 60 | + </Tabs.Tab> |
| 61 | +</Tabs> |
| 62 | + |
| 63 | +For the full list of options when creating an Oxia clint, please refer to the [OxiaClientBuilder Javadocs](https://oxia-db.github.io/oxia-client-java/apidocs/latest/io/oxia/client/api/OxiaClientBuilder.html). |
| 64 | + |
| 65 | +### Writing records |
| 66 | + |
| 67 | +<Tabs items={['Sync', 'Async']}> |
| 68 | + <Tabs.Tab> |
| 69 | + ```java |
| 70 | + // Write a record to Oxia with the specified key and value, and with the expectation |
| 71 | + // that the record does not already exist. |
| 72 | + PutResult res = client.put("my-key", "my-value".getBytes(), Set.of(IfRecordDoesNotExist)); |
| 73 | + |
| 74 | + // Write a record with the expectation that it has not changed since the previous write. |
| 75 | + // If there was any change, the operation will fail |
| 76 | + client.put("my-key", "my-value-2".getBytes(), |
| 77 | + Set.of(IfVersionIdEquals(res.version().versionId()))); |
| 78 | + ``` |
| 79 | + </Tabs.Tab> |
| 80 | + <Tabs.Tab> |
| 81 | + ```java |
| 82 | + // Write a record to Oxia with the specified key and value, and with the expectation |
| 83 | + // that the record does not already exist. |
| 84 | + client.put("my-string", "my-value".getBytes(), Set.of(IfRecordDoesNotExist)) |
| 85 | + .thenAccept(res -> { |
| 86 | + log.info("Put result: {}", res); |
| 87 | + }).exceptionally(ex -> { |
| 88 | + log.warn("Put failed", ex); |
| 89 | + return null; |
| 90 | + }); |
| 91 | + |
| 92 | + // Write a record with the expectation that it has not changed since the previous write. |
| 93 | + // If there was any change, the operation will fail |
| 94 | + client.put("my-key", "my-value-2".getBytes(), |
| 95 | + Set.of(IfVersionIdEquals(res.version().versionId()))) |
| 96 | + .thenAccept(res -> { |
| 97 | + log.info("Put result: {}", res); |
| 98 | + }).exceptionally(ex -> { |
| 99 | + log.warn("Put failed", ex); |
| 100 | + return null; |
| 101 | + }); |
| 102 | + ``` |
| 103 | + </Tabs.Tab> |
| 104 | +</Tabs> |
| 105 | + |
| 106 | +All the options for the put operation are available in the |
| 107 | +[Javadocs](https://oxia-db.github.io/oxia-client-java/apidocs/latest/io/oxia/client/api/PutOption.html). |
| 108 | + |
| 109 | +### Reading records |
| 110 | + |
| 111 | +Reading the value of a record |
| 112 | + |
| 113 | +<Tabs items={['Sync', 'Async']}> |
| 114 | + <Tabs.Tab> |
| 115 | + ```java |
| 116 | + GetResult res = client.get("my-key"); |
| 117 | + log.info("Got key: {} - value: {} - version: {}", |
| 118 | + res.getKey(), res.getValue(), res.getVersion()); |
| 119 | + ``` |
| 120 | + </Tabs.Tab> |
| 121 | + <Tabs.Tab> |
| 122 | + ```java |
| 123 | + aclient.get("my-key") |
| 124 | + .thenAccept(res -> { |
| 125 | + log.info("Got key: {} - value: {} - version: {}", |
| 126 | + res.getKey(), res.getValue(), res.getVersion()); |
| 127 | + }).exceptionally(ex -> { |
| 128 | + log.warn("Get failed", ex); |
| 129 | + return null; |
| 130 | + }); |
| 131 | + ``` |
| 132 | + </Tabs.Tab> |
| 133 | +</Tabs> |
| 134 | + |
| 135 | +All the options for the get operation are available in the |
| 136 | +[Javadocs](https://oxia-db.github.io/oxia-client-java/apidocs/latest/io/oxia/client/api/GetOption.html). |
| 137 | + |
0 commit comments