Skip to content
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 leaderboard examples #411

Merged
merged 1 commit into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/cache-with-aws/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ repositories {
}

dependencies {
implementation("software.momento.java:sdk:1.18.0")
implementation("software.momento.java:sdk:1.19.0")

// For examples to store secrets in AWS Secrets Manager
implementation("software.amazon.awssdk:secretsmanager:2.20.93")
Expand Down
2 changes: 1 addition & 1 deletion examples/cache/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ repositories {
}

dependencies {
implementation("software.momento.java:sdk:1.18.0")
implementation("software.momento.java:sdk:1.19.0")

implementation("com.google.guava:guava:31.1-android")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,14 @@ public static void example_API_CredentialProviderFromString() {
CredentialProvider.fromString(authToken);
}

@SuppressWarnings("ResultOfMethodCallIgnored")
public static void example_API_ConfigurationLaptop() {
Configurations.Laptop.v1();
}

@SuppressWarnings("ResultOfMethodCallIgnored")
public static void example_API_ConfigurationInRegionLatest() {
Configurations.InRegion.latest();
}

@SuppressWarnings("ResultOfMethodCallIgnored")
public static void example_API_ConfigurationLowLatency() {
Configurations.LowLatency.latest();
}
Expand Down
2 changes: 1 addition & 1 deletion examples/lambda/docker/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ repositories {

dependencies {
implementation("com.amazonaws:aws-lambda-java-core:1.2.1")
implementation("software.momento.java:sdk:1.18.0")
implementation("software.momento.java:sdk:1.19.0")
}

tasks.jar {
Expand Down
39 changes: 39 additions & 0 deletions examples/leaderboard/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<img src="https://docs.momentohq.com/img/momento-logo-forest.svg" alt="logo" width="400"/>

[![project status](https://momentohq.github.io/standards-and-practices/badges/project-status-official.svg)](https://github.com/momentohq/standards-and-practices/blob/main/docs/momento-on-github.md)
[![project stability](https://momentohq.github.io/standards-and-practices/badges/project-stability-stable.svg)](https://github.com/momentohq/standards-and-practices/blob/main/docs/momento-on-github.md)


# Momento Java SDK - Leaderboard 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")
}
```

----------------------------------------------------------------------------------------
For more info, visit our website at [https://gomomento.com](https://gomomento.com)!
34 changes: 34 additions & 0 deletions examples/leaderboard/README.template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{{ ossHeader }}

# Momento Java SDK - Leaderboard 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 }}
64 changes: 64 additions & 0 deletions examples/leaderboard/build.gradle.kts
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.19.0")

// 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") {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package momento.client.example;

import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
import momento.sdk.CacheClient;
import momento.sdk.ILeaderboard;
import momento.sdk.LeaderboardClient;
import momento.sdk.auth.CredentialProvider;
import momento.sdk.auth.EnvVarCredentialProvider;
import momento.sdk.config.Configurations;
import momento.sdk.config.LeaderboardConfigurations;
import momento.sdk.responses.SortOrder;
import momento.sdk.responses.leaderboard.DeleteResponse;
import momento.sdk.responses.leaderboard.FetchResponse;
import momento.sdk.responses.leaderboard.LeaderboardElement;
import momento.sdk.responses.leaderboard.UpsertResponse;

public class BasicExample {
private static final String API_KEY_ENV_VAR = "MOMENTO_API_KEY";
private static final Duration DEFAULT_ITEM_TTL = Duration.ofSeconds(60);

private static final String CACHE_NAME = "cache";
private static final String LEADERBOARD_NAME = "leaderboard";

public static void main(String[] args) {
final CredentialProvider credentialProvider = new EnvVarCredentialProvider(API_KEY_ENV_VAR);

try (final CacheClient cacheClient =
CacheClient.create(
credentialProvider, Configurations.Laptop.latest(), DEFAULT_ITEM_TTL);
final LeaderboardClient leaderboardClient =
LeaderboardClient.builder(credentialProvider, LeaderboardConfigurations.Laptop.latest())
.build()) {

cacheClient.createCache(CACHE_NAME).join();

final ILeaderboard leaderboard = leaderboardClient.leaderboard(CACHE_NAME, LEADERBOARD_NAME);

final Map<Integer, Double> elements = new HashMap<>();
elements.put(1, 1.0);
elements.put(2, 2.0);
elements.put(3, 3.0);

UpsertResponse upsertResponse = leaderboard.upsert(elements).join();
if (upsertResponse instanceof UpsertResponse.Error error) {
System.out.println("Error during upsert: " + error.getMessage());
}

System.out.println("First two elements by rank ascending:");
final FetchResponse byRankResponse =
leaderboard.fetchByRank(0, 2, SortOrder.ASCENDING).join();
if (byRankResponse instanceof FetchResponse.Success success) {
for (LeaderboardElement element : success.elementsList()) {
System.out.printf(
"Rank: %d, ID: %d, Score: %.2f%n",
element.getRank(), element.getId(), element.getScore());
}
} else if (byRankResponse instanceof FetchResponse.Error error) {
System.out.println("Error during fetch: " + error.getMessage());
}

upsertResponse = leaderboard.upsert(Map.of(3, 5.0, 4, 6.0)).join();
if (upsertResponse instanceof UpsertResponse.Error error) {
System.out.println("Error during upsert: " + error.getMessage());
}

System.out.println("Elements by score descending:");
final FetchResponse byScoreResponse =
leaderboard.fetchByScore(1.0, 5.1, SortOrder.DESCENDING).join();
if (byScoreResponse instanceof FetchResponse.Success success) {
for (LeaderboardElement element : success.elementsList()) {
System.out.printf(
"Rank: %d, ID: %d, Score: %.2f%n",
element.getRank(), element.getId(), element.getScore());
}
} else if (byScoreResponse instanceof FetchResponse.Error error) {
System.out.println("Error during fetch: " + error.getMessage());
}

final DeleteResponse deleteResponse = leaderboard.delete().join();
if (deleteResponse instanceof DeleteResponse.Error error) {
System.out.println("Error during delete: " + error.getMessage());
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package momento.client.example.doc_examples;

import momento.sdk.LeaderboardClient;
import momento.sdk.auth.CredentialProvider;
import momento.sdk.config.LeaderboardConfigurations;

public class CheatSheet {
public static void main(String[] args) {
try (final LeaderboardClient leaderboardClient =
LeaderboardClient.builder(
CredentialProvider.fromEnvVar("MOMENTO_API_KEY"),
LeaderboardConfigurations.Laptop.latest())
.build()) {
// ...
}
}
}
Loading
Loading