-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: add docs examples for PreviewStorageClient #384
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
0aa92e7
chore: add docs examples for PreviewStorageClient
cprice404 4279929
fix: example readme linting and cleanups
cprice404 2db90a2
fix: readme linting
cprice404 34a8090
fix: readme linting
cprice404 41f420e
fix: examples CI
cprice404 748f284
fix: debugging gradle docs ci tasks
cprice404 db57306
fix: debugging gradle docs ci tasks
cprice404 a110eea
fix: debugging gradle docs ci tasks
cprice404 bfbcda0
fix: debugging gradle docs ci tasks
cprice404 58e0fa1
fix: debugging gradle docs ci tasks
cprice404 11db5bd
fix: debugging gradle docs ci tasks
cprice404 a37f3d8
fix: debugging gradle docs ci tasks
cprice404 d1924cf
fix: debugging gradle docs ci tasks
cprice404 d4e773e
fix: debugging gradle docs ci tasks
cprice404 7b97287
fix: linting
cprice404 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,4 +46,3 @@ examples/lib/.classpath | |
examples/lib/.project | ||
examples/lib/bin | ||
.vscode/ | ||
*logback.xml |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{{ ossHeader }} | ||
|
||
# Momento Java SDK Examples | ||
|
||
## Examples | ||
|
||
All examples are in different subdirectories for different use-cases, and minimizes the dependencies required to run them. | ||
|
||
- [Examples to get started with Momento Cache](https://github.com/momentohq/client-sdk-java/tree/main/examples/cache) | ||
- [Examples that uses Momento Cache with one or more AWS integrations, such as AWS Secrets Manager to store your Momento auth token](https://github.com/momentohq/client-sdk-java/tree/main/examples/cache-with-aws) | ||
- [Examples to get started with Momento Storage](https://github.com/momentohq/client-sdk-java/tree/main/examples/storage) | ||
|
||
{{ ossFooter }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,3 +25,4 @@ include("cache-with-aws") | |
include("lambda:docker") | ||
include("token") | ||
include("topic") | ||
include("storage") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{{ ossHeader }} | ||
|
||
# Momento Java SDK - Storage Client Examples | ||
|
||
## Running the Examples | ||
|
||
- You do not need gradle to be installed | ||
- JDK 14 or above is required to run the example | ||
- To get started with Momento you will need a Momento API key. You can get one from the | ||
[Momento Console](https://console.gomomento.com). | ||
|
||
### Basic | ||
|
||
```bash | ||
MOMENTO_API_KEY=<YOUR API KEY> ./gradlew basic | ||
``` | ||
|
||
Example Code: [BasicExample.java](src/main/java/momento/client/example/BasicExample.java) | ||
|
||
## Using the Java SDK in your project | ||
|
||
### Gradle Configuration | ||
|
||
Update your Gradle build to include the components | ||
|
||
#### build.gradle.kts | ||
|
||
```kotlin | ||
dependencies { | ||
implementation("software.momento.java:sdk:1.x.x") | ||
} | ||
``` | ||
|
||
{{ ossFooter }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* This file was generated by the Gradle 'init' task. | ||
* | ||
* This generated file contains a sample Java library project to get you started. | ||
* For more details take a look at the 'Building Java & JVM projects' chapter in the Gradle | ||
* User Manual available at https://docs.gradle.org/7.2/userguide/building_java_projects.html | ||
*/ | ||
|
||
plugins { | ||
application | ||
id("com.diffplug.spotless") version "5.15.1" | ||
} | ||
|
||
repositories { | ||
// Use Maven Central for resolving dependencies. | ||
mavenCentral() | ||
} | ||
|
||
dependencies { | ||
implementation("software.momento.java:sdk:1.14.1") | ||
|
||
// Logging framework to log and enable logging in the Momento client. | ||
implementation("ch.qos.logback:logback-classic:1.4.7") | ||
|
||
// Use JUnit Jupiter for testing. | ||
testImplementation("org.junit.jupiter:junit-jupiter:5.9.2") | ||
} | ||
|
||
spotless { | ||
java { | ||
removeUnusedImports() | ||
googleJavaFormat("1.11.0") | ||
} | ||
} | ||
|
||
tasks.test { | ||
// Use JUnit Platform for unit tests. | ||
useJUnitPlatform() | ||
} | ||
|
||
task("basic", JavaExec::class) { | ||
description = "Run the basic example" | ||
classpath = sourceSets.main.get().runtimeClasspath | ||
mainClass.set("momento.client.example.BasicExample") | ||
} | ||
|
||
task("docExamples", JavaExec::class) { | ||
description = "Validate that the API doc examples run" | ||
classpath = sourceSets.main.get().runtimeClasspath | ||
mainClass.set("momento.client.example.doc_examples.DocExamplesJavaAPIs") | ||
} | ||
|
||
task("docCheatSheet", JavaExec::class) { | ||
description = "Validate that the doc cheat sheet runs" | ||
classpath = sourceSets.main.get().runtimeClasspath | ||
mainClass.set("momento.client.example.doc_examples.CheatSheet") | ||
} | ||
|
||
task("docsTasks") { | ||
dependsOn("docCheatSheet") | ||
dependsOn("docExamples") | ||
} | ||
|
||
task("prepareKotlinBuildScriptModel") {} |
117 changes: 117 additions & 0 deletions
117
examples/storage/src/main/java/momento/client/example/BasicExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package momento.client.example; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import momento.sdk.PreviewStorageClient; | ||
import momento.sdk.auth.CredentialProvider; | ||
import momento.sdk.config.StorageConfigurations; | ||
import momento.sdk.exceptions.StoreAlreadyExistsException; | ||
import momento.sdk.responses.storage.CreateStoreResponse; | ||
import momento.sdk.responses.storage.GetResponse; | ||
import momento.sdk.responses.storage.ListStoresResponse; | ||
import momento.sdk.responses.storage.StoreInfo; | ||
|
||
public class BasicExample { | ||
|
||
private static final String API_KEY_ENV_VAR = "MOMENTO_API_KEY"; | ||
|
||
private static final String STORE_NAME = "store"; | ||
private static final String KEY = "key"; | ||
private static final String VALUE = "value"; | ||
|
||
public static void main(String[] args) { | ||
printStartBanner(); | ||
|
||
final CredentialProvider credentialProvider = CredentialProvider.fromEnvVar(API_KEY_ENV_VAR); | ||
|
||
try (final var client = | ||
new PreviewStorageClient(credentialProvider, StorageConfigurations.Laptop.latest())) { | ||
|
||
createStore(client, STORE_NAME); | ||
|
||
listStores(client); | ||
|
||
System.out.printf("Putting key '%s', value '%s'%n", KEY, VALUE); | ||
// store a string value | ||
client.put(STORE_NAME, KEY, VALUE).join(); | ||
|
||
// you can also store values of type byte[], long, or double: | ||
byte[] bytesValue = "value".getBytes(StandardCharsets.UTF_8); | ||
client.put(STORE_NAME, "bytes-key", bytesValue).join(); | ||
client.put(STORE_NAME, "long-key", 42L).join(); | ||
client.put(STORE_NAME, "double-key", 3.14).join(); | ||
|
||
System.out.printf("Getting value for key '%s'%n", KEY); | ||
|
||
final GetResponse getResponse = client.get(STORE_NAME, KEY).join(); | ||
|
||
// simplified style, if you are confident the value exists and is a String: | ||
final String value = getResponse.valueWhenFound().get().getString().get(); | ||
|
||
// pattern-matching style, more suitable for production code: | ||
if (getResponse instanceof GetResponse.Found hit) { | ||
// if you're confident it's a String: | ||
System.out.printf("Found value for key '%s': '%s'%n", KEY, hit.value().getString().get()); | ||
|
||
// you're not sure of the type: | ||
switch (hit.value().getType()) { | ||
case BYTE_ARRAY -> { | ||
System.out.println("Got a byte array: " + hit.value().getByteArray().get()); | ||
} | ||
case STRING -> { | ||
System.out.println("Got a string: " + hit.value().getString().get()); | ||
} | ||
case LONG -> { | ||
System.out.println("Got a long: " + hit.value().getLong().get()); | ||
} | ||
case DOUBLE -> { | ||
System.out.println("Got a double: " + hit.value().getDouble().get()); | ||
} | ||
} | ||
} else if (getResponse instanceof GetResponse.NotFound) { | ||
System.out.println("Found no value for key " + KEY); | ||
} else if (getResponse instanceof GetResponse.Error error) { | ||
System.out.printf( | ||
"Unable to look up value for key '%s' with error %s\n", KEY, error.getErrorCode()); | ||
System.out.println(error.getMessage()); | ||
} | ||
} | ||
printEndBanner(); | ||
} | ||
|
||
private static void createStore(PreviewStorageClient storageClient, String storeName) { | ||
final CreateStoreResponse createResponse = storageClient.createStore(storeName).join(); | ||
if (createResponse instanceof CreateStoreResponse.Error error) { | ||
if (error.getCause() instanceof StoreAlreadyExistsException) { | ||
System.out.println("Store with name '" + storeName + "' already exists."); | ||
} else { | ||
System.out.println("Unable to create store with error " + error.getErrorCode()); | ||
System.out.println(error.getMessage()); | ||
} | ||
} | ||
} | ||
|
||
private static void listStores(PreviewStorageClient storageClient) { | ||
System.out.println("Listing caches:"); | ||
final ListStoresResponse listResponse = storageClient.listStores().join(); | ||
if (listResponse instanceof ListStoresResponse.Success success) { | ||
for (StoreInfo storeInfo : success.getStores()) { | ||
System.out.println(storeInfo.getName()); | ||
} | ||
} else if (listResponse instanceof ListStoresResponse.Error error) { | ||
System.out.println("Unable to list caches with error " + error.getErrorCode()); | ||
System.out.println(error.getMessage()); | ||
} | ||
} | ||
|
||
private static void printStartBanner() { | ||
System.out.println("******************************************************************"); | ||
System.out.println("Basic Example Start"); | ||
System.out.println("******************************************************************"); | ||
} | ||
|
||
private static void printEndBanner() { | ||
System.out.println("******************************************************************"); | ||
System.out.println("Basic Example End"); | ||
System.out.println("******************************************************************"); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
examples/storage/src/main/java/momento/client/example/doc_examples/CheatSheet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package momento.client.example.doc_examples; | ||
|
||
import momento.sdk.PreviewStorageClient; | ||
import momento.sdk.auth.CredentialProvider; | ||
import momento.sdk.config.StorageConfigurations; | ||
|
||
public class CheatSheet { | ||
public static void main(String[] args) { | ||
try (final var storageClient = | ||
new PreviewStorageClient( | ||
CredentialProvider.fromEnvVar("MOMENTO_API_KEY"), | ||
StorageConfigurations.Laptop.latest())) { | ||
// ... | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This SDK doesn't have the this as an alternative for the create cache response unfortunately.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, i noticed that. I kind of want to fix that right now, do you have any opinion on it?