Skip to content

Commit

Permalink
Merge pull request ydb-platform#18 from alex268/update_examples
Browse files Browse the repository at this point in the history
Update examples
  • Loading branch information
alex268 authored Nov 9, 2023
2 parents 58b76eb + d14b6cd commit 22dee7c
Show file tree
Hide file tree
Showing 13 changed files with 179 additions and 130 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package tech.ydb.example;

import java.util.concurrent.CompletableFuture;

import tech.ydb.auth.AuthProvider;
import tech.ydb.auth.TokenAuthProvider;
import tech.ydb.core.Result;
import tech.ydb.core.grpc.GrpcTransport;
import tech.ydb.table.SessionRetryContext;
import tech.ydb.table.TableClient;
import tech.ydb.table.query.DataQueryResult;
import tech.ydb.table.result.ResultSetReader;
import tech.ydb.table.transaction.TxControl;

Expand All @@ -25,23 +24,20 @@ public static void main(String[] args) {
// Access token credentials
AuthProvider authProvider = new TokenAuthProvider(accessToken);

try ( GrpcTransport transport = GrpcTransport.forConnectionString(connectionString)
.withAuthProvider(authProvider) // Or this method could not be called at all
try (GrpcTransport transport = GrpcTransport.forConnectionString(connectionString)
.withAuthProvider(authProvider)
.build()) {
try ( TableClient tableClient = TableClient
.newClient(transport)
.build()) {
try (TableClient tableClient = TableClient.newClient(transport).build()) {
SessionRetryContext retryCtx = SessionRetryContext.create(tableClient).build();

retryCtx.supplyResult(session -> {
ResultSetReader rsReader = session.executeDataQuery("SELECT 1;", TxControl.serializableRw())
.join().getValue().getResultSet(0);
DataQueryResult dataQueryResult = retryCtx.supplyResult(
session -> session.executeDataQuery("SELECT 1;", TxControl.serializableRw())
).join().getValue();

rsReader.next();
ResultSetReader rsReader = dataQueryResult.getResultSet(0);
while (rsReader.next()) {
System.out.println(rsReader.getColumn(0).getInt32());

return CompletableFuture.completedFuture(Result.success(Boolean.TRUE));
}).join();
}
}
}
}
Expand Down
25 changes: 10 additions & 15 deletions auth/anonymous_credentials/src/main/java/tech/ydb/example/Main.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package tech.ydb.example;

import java.util.concurrent.CompletableFuture;

import tech.ydb.auth.AuthProvider;
import tech.ydb.auth.NopAuthProvider;
import tech.ydb.core.Result;
import tech.ydb.core.grpc.GrpcTransport;
import tech.ydb.table.SessionRetryContext;
import tech.ydb.table.TableClient;
import tech.ydb.table.query.DataQueryResult;
import tech.ydb.table.result.ResultSetReader;
import tech.ydb.table.transaction.TxControl;

Expand All @@ -22,24 +21,20 @@ public static void main(String[] args) {
// Anonymous credentials
AuthProvider authProvider = NopAuthProvider.INSTANCE;

try ( GrpcTransport transport = GrpcTransport.forConnectionString(connectionString)
.withAuthProvider(authProvider) // Or this method could not be called at all
try (GrpcTransport transport = GrpcTransport.forConnectionString(connectionString)
.withAuthProvider(authProvider)
.build()) {
try ( TableClient tableClient = TableClient
.newClient(transport)
.build()) {

try (TableClient tableClient = TableClient.newClient(transport).build()) {
SessionRetryContext retryCtx = SessionRetryContext.create(tableClient).build();

retryCtx.supplyResult(session -> {
ResultSetReader rsReader = session.executeDataQuery("SELECT 1;", TxControl.serializableRw())
.join().getValue().getResultSet(0);
DataQueryResult dataQueryResult = retryCtx.supplyResult(
session -> session.executeDataQuery("SELECT 1;", TxControl.serializableRw())
).join().getValue();

rsReader.next();
ResultSetReader rsReader = dataQueryResult.getResultSet(0);
while (rsReader.next()) {
System.out.println(rsReader.getColumn(0).getInt32());

return CompletableFuture.completedFuture(Result.success(Boolean.TRUE));
}).join();
}
}
}
}
Expand Down
25 changes: 10 additions & 15 deletions auth/environ/src/main/java/tech/ydb/example/Main.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package tech.ydb.example;

import java.util.concurrent.CompletableFuture;

import tech.ydb.auth.AuthProvider;
import tech.ydb.auth.iam.CloudAuthHelper;
import tech.ydb.core.Result;
import tech.ydb.core.grpc.GrpcTransport;
import tech.ydb.table.SessionRetryContext;
import tech.ydb.table.TableClient;
import tech.ydb.table.query.DataQueryResult;
import tech.ydb.table.result.ResultSetReader;
import tech.ydb.table.transaction.TxControl;

Expand All @@ -22,24 +21,20 @@ public static void main(String[] args) {
// Construct authProvider from environment variables
AuthProvider authProvider = CloudAuthHelper.getAuthProviderFromEnviron();

try ( GrpcTransport transport = GrpcTransport.forConnectionString(connectionString)
.withAuthProvider(authProvider) // Or this method could not be called at all
try (GrpcTransport transport = GrpcTransport.forConnectionString(connectionString)
.withAuthProvider(authProvider)
.build()) {
try ( TableClient tableClient = TableClient
.newClient(transport)
.build()) {

try (TableClient tableClient = TableClient.newClient(transport).build()) {
SessionRetryContext retryCtx = SessionRetryContext.create(tableClient).build();

retryCtx.supplyResult(session -> {
ResultSetReader rsReader = session.executeDataQuery("SELECT 1;", TxControl.serializableRw())
.join().getValue().getResultSet(0);
DataQueryResult dataQueryResult = retryCtx.supplyResult(
session -> session.executeDataQuery("SELECT 1;", TxControl.serializableRw())
).join().getValue();

rsReader.next();
ResultSetReader rsReader = dataQueryResult.getResultSet(0);
while (rsReader.next()) {
System.out.println(rsReader.getColumn(0).getInt32());

return CompletableFuture.completedFuture(Result.success(Boolean.TRUE));
}).join();
}
}
}
}
Expand Down
25 changes: 10 additions & 15 deletions auth/metadata_credentials/src/main/java/tech/ydb/example/Main.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package tech.ydb.example;

import java.util.concurrent.CompletableFuture;

import tech.ydb.auth.AuthProvider;
import tech.ydb.auth.iam.CloudAuthHelper;
import tech.ydb.core.Result;
import tech.ydb.core.grpc.GrpcTransport;
import tech.ydb.table.SessionRetryContext;
import tech.ydb.table.TableClient;
import tech.ydb.table.query.DataQueryResult;
import tech.ydb.table.result.ResultSetReader;
import tech.ydb.table.transaction.TxControl;

Expand All @@ -23,24 +22,20 @@ public static void main(String[] args) {
// Use metadata credentials
AuthProvider authProvider = CloudAuthHelper.getMetadataAuthProvider();

try ( GrpcTransport transport = GrpcTransport.forConnectionString(connectionString)
.withAuthProvider(authProvider) // Or this method could not be called at all
try (GrpcTransport transport = GrpcTransport.forConnectionString(connectionString)
.withAuthProvider(authProvider)
.build()) {
try ( TableClient tableClient = TableClient
.newClient(transport)
.build()) {

try (TableClient tableClient = TableClient.newClient(transport).build()) {
SessionRetryContext retryCtx = SessionRetryContext.create(tableClient).build();

retryCtx.supplyResult(session -> {
ResultSetReader rsReader = session.executeDataQuery("SELECT 1;", TxControl.serializableRw())
.join().getValue().getResultSet(0);
DataQueryResult dataQueryResult = retryCtx.supplyResult(
session -> session.executeDataQuery("SELECT 1;", TxControl.serializableRw())
).join().getValue();

rsReader.next();
ResultSetReader rsReader = dataQueryResult.getResultSet(0);
while (rsReader.next()) {
System.out.println(rsReader.getColumn(0).getInt32());

return CompletableFuture.completedFuture(Result.success(Boolean.TRUE));
}).join();
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package tech.ydb.example;

import java.util.concurrent.CompletableFuture;

import tech.ydb.auth.AuthProvider;
import tech.ydb.auth.iam.CloudAuthHelper;
import tech.ydb.core.Result;
import tech.ydb.core.grpc.GrpcTransport;
import tech.ydb.table.SessionRetryContext;
import tech.ydb.table.TableClient;
import tech.ydb.table.query.DataQueryResult;
import tech.ydb.table.result.ResultSetReader;
import tech.ydb.table.transaction.TxControl;

Expand All @@ -23,24 +22,20 @@ public static void main(String[] args) {

AuthProvider authProvider = CloudAuthHelper.getServiceAccountFileAuthProvider(saKeyFile);

try ( GrpcTransport transport = GrpcTransport.forConnectionString(connectionString)
.withAuthProvider(authProvider) // Or this method could not be called at all
try (GrpcTransport transport = GrpcTransport.forConnectionString(connectionString)
.withAuthProvider(authProvider)
.build()) {
try ( TableClient tableClient = TableClient
.newClient(transport)
.build()) {

try (TableClient tableClient = TableClient.newClient(transport).build()) {
SessionRetryContext retryCtx = SessionRetryContext.create(tableClient).build();

retryCtx.supplyResult(session -> {
ResultSetReader rsReader = session.executeDataQuery("SELECT 1;", TxControl.serializableRw())
.join().getValue().getResultSet(0);
DataQueryResult dataQueryResult = retryCtx.supplyResult(
session -> session.executeDataQuery("SELECT 1;", TxControl.serializableRw())
).join().getValue();

rsReader.next();
ResultSetReader rsReader = dataQueryResult.getResultSet(0);
while (rsReader.next()) {
System.out.println(rsReader.getColumn(0).getInt32());

return CompletableFuture.completedFuture(Result.success(Boolean.TRUE));
}).join();
}
}
}
}
Expand Down
25 changes: 10 additions & 15 deletions auth/static_credentials/src/main/java/tech/ydb/example/Main.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@

package tech.ydb.example;

import java.util.concurrent.CompletableFuture;

import tech.ydb.core.Result;
import tech.ydb.core.auth.StaticCredentials;
import tech.ydb.core.grpc.GrpcTransport;
import tech.ydb.table.SessionRetryContext;
import tech.ydb.table.TableClient;
import tech.ydb.table.query.DataQueryResult;
import tech.ydb.table.result.ResultSetReader;
import tech.ydb.table.transaction.TxControl;

Expand All @@ -29,23 +27,20 @@ public static void main(String[] args) {
// Use credentials auth provider with username and password
StaticCredentials authProvider = new StaticCredentials(username, password);

try ( GrpcTransport transport = GrpcTransport.forConnectionString(connectionString)
.withAuthProvider(authProvider) // Or this method could not be called at all
try (GrpcTransport transport = GrpcTransport.forConnectionString(connectionString)
.withAuthProvider(authProvider)
.build()) {
try ( TableClient tableClient = TableClient
.newClient(transport)
.build()) {
try (TableClient tableClient = TableClient.newClient(transport).build()) {
SessionRetryContext retryCtx = SessionRetryContext.create(tableClient).build();

retryCtx.supplyResult(session -> {
ResultSetReader rsReader = session.executeDataQuery("SELECT 1;", TxControl.serializableRw())
.join().getValue().getResultSet(0);
DataQueryResult dataQueryResult = retryCtx.supplyResult(
session -> session.executeDataQuery("SELECT 1;", TxControl.serializableRw())
).join().getValue();

rsReader.next();
ResultSetReader rsReader = dataQueryResult.getResultSet(0);
while (rsReader.next()) {
System.out.println(rsReader.getColumn(0).getInt32());

return CompletableFuture.completedFuture(Result.success(Boolean.TRUE));
}).join();
}
}
}
}
Expand Down
19 changes: 18 additions & 1 deletion basic_example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
</dependency>

<dependency>
<groupId>tech.ydb.test</groupId>
<artifactId>ydb-junit5-support</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand All @@ -52,7 +58,18 @@
<mainClass>tech.ydb.example.App</mainClass>
</manifest>
</archive>
</configuration>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<environmentVariables>
<TESTCONTAINERS_REUSE_ENABLE>true</TESTCONTAINERS_REUSE_ENABLE>
<YDB_ANONYMOUS_CREDENTIALS>1</YDB_ANONYMOUS_CREDENTIALS>
</environmentVariables>
</configuration>
</plugin>
</plugins>
</build>
Expand Down
2 changes: 1 addition & 1 deletion basic_example/src/main/java/tech/ydb/example/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public final class App implements Runnable, AutoCloseable {
private final String database;
private final SessionRetryContext retryCtx;

private App(String connectionString) {
App(String connectionString) {
this.transport = GrpcTransport.forConnectionString(connectionString)
.withAuthProvider(CloudAuthHelper.getAuthProviderFromEnviron())
.build();
Expand Down
27 changes: 9 additions & 18 deletions basic_example/src/main/resources/log4j2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,21 @@
<Configuration status="WARN" shutdownHook="disable">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %logger{1} - %msg%n"/>
<PatternLayout pattern="[%level] %d{HH:mm:ss.SSS} %logger - %msg%n"/>
</Console>
</Appenders>

<Loggers>
<Logger name="io.netty" level="warn" additivity="false">
<AppenderRef ref="Console"/>
</Logger>
<Logger name="io.grpc.netty" level="warn" additivity="false">
<AppenderRef ref="Console"/>
</Logger>
<Logger name="tech.ydb.core.grpc" level="info" additivity="false">
<AppenderRef ref="Console"/>
</Logger>
<Logger name="tech.ydb.table" level="info" additivity="false">
<AppenderRef ref="Console"/>
</Logger>
<Logger name="tech.ydb.table.SessionRetryContext" level="info" additivity="false">
<AppenderRef ref="Console"/>
</Logger>
<Logger name="tech.ydb.table.Session" level="info" additivity="false">
<!-- https://www.testcontainers.org/supported_docker_environment/logging_config/ -->
<Logger name="org.testcontainers" level="warn" />
<Logger name="com.github.dockerjava" level="warn"/>
<Logger name="com.github.dockerjava.zerodep.shaded.org.apache.hc.client5.http.wire" level="off"/>

<Logger name="io.grpc" level="warn" additivity="false">
<AppenderRef ref="Console"/>
</Logger>
<Logger name="tech.ydb.topic" level="info" additivity="false">

<Logger name="tech.ydb" level="info" additivity="false">
<AppenderRef ref="Console"/>
</Logger>

Expand Down
Loading

0 comments on commit 22dee7c

Please sign in to comment.