From e991f9a6e1c8fff22ea7c28d8c694bae1e0f196c Mon Sep 17 00:00:00 2001 From: Alexandr Gorshenin Date: Wed, 8 Nov 2023 13:41:28 +0000 Subject: [PATCH 1/3] Update auth examples --- .../src/main/java/tech/ydb/example/Main.java | 24 ++++++++---------- .../src/main/java/tech/ydb/example/Main.java | 25 ++++++++----------- .../src/main/java/tech/ydb/example/Main.java | 25 ++++++++----------- .../src/main/java/tech/ydb/example/Main.java | 25 ++++++++----------- .../src/main/java/tech/ydb/example/Main.java | 25 ++++++++----------- .../src/main/java/tech/ydb/example/Main.java | 25 ++++++++----------- 6 files changed, 60 insertions(+), 89 deletions(-) diff --git a/auth/access_token_credentials/src/main/java/tech/ydb/example/Main.java b/auth/access_token_credentials/src/main/java/tech/ydb/example/Main.java index 0721050..fd33e59 100644 --- a/auth/access_token_credentials/src/main/java/tech/ydb/example/Main.java +++ b/auth/access_token_credentials/src/main/java/tech/ydb/example/Main.java @@ -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; @@ -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(); + } } } } diff --git a/auth/anonymous_credentials/src/main/java/tech/ydb/example/Main.java b/auth/anonymous_credentials/src/main/java/tech/ydb/example/Main.java index 243ef26..3d54e33 100644 --- a/auth/anonymous_credentials/src/main/java/tech/ydb/example/Main.java +++ b/auth/anonymous_credentials/src/main/java/tech/ydb/example/Main.java @@ -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; @@ -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(); + } } } } diff --git a/auth/environ/src/main/java/tech/ydb/example/Main.java b/auth/environ/src/main/java/tech/ydb/example/Main.java index 21bfec8..3042d3e 100644 --- a/auth/environ/src/main/java/tech/ydb/example/Main.java +++ b/auth/environ/src/main/java/tech/ydb/example/Main.java @@ -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; @@ -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(); + } } } } diff --git a/auth/metadata_credentials/src/main/java/tech/ydb/example/Main.java b/auth/metadata_credentials/src/main/java/tech/ydb/example/Main.java index cdbc64b..67462de 100644 --- a/auth/metadata_credentials/src/main/java/tech/ydb/example/Main.java +++ b/auth/metadata_credentials/src/main/java/tech/ydb/example/Main.java @@ -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; @@ -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(); + } } } } diff --git a/auth/service_account_credentials/src/main/java/tech/ydb/example/Main.java b/auth/service_account_credentials/src/main/java/tech/ydb/example/Main.java index 57d88a5..8edf12e 100644 --- a/auth/service_account_credentials/src/main/java/tech/ydb/example/Main.java +++ b/auth/service_account_credentials/src/main/java/tech/ydb/example/Main.java @@ -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; @@ -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(); + } } } } diff --git a/auth/static_credentials/src/main/java/tech/ydb/example/Main.java b/auth/static_credentials/src/main/java/tech/ydb/example/Main.java index 876292d..b5272fd 100644 --- a/auth/static_credentials/src/main/java/tech/ydb/example/Main.java +++ b/auth/static_credentials/src/main/java/tech/ydb/example/Main.java @@ -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; @@ -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(); + } } } } From de864ed2cc63f0e674343aa2aa0211ebf2562d4e Mon Sep 17 00:00:00 2001 From: Alexandr Gorshenin Date: Wed, 8 Nov 2023 15:08:34 +0000 Subject: [PATCH 2/3] Added integration test to basic example --- basic_example/pom.xml | 19 +++++++++++- .../src/main/java/tech/ydb/example/App.java | 2 +- basic_example/src/main/resources/log4j2.xml | 27 ++++++----------- .../tech/ydb/example/BasicExampleTest.java | 30 +++++++++++++++++++ 4 files changed, 58 insertions(+), 20 deletions(-) create mode 100644 basic_example/src/test/java/tech/ydb/example/BasicExampleTest.java diff --git a/basic_example/pom.xml b/basic_example/pom.xml index c47233a..d299527 100644 --- a/basic_example/pom.xml +++ b/basic_example/pom.xml @@ -30,6 +30,12 @@ org.apache.logging.log4j log4j-slf4j-impl + + + tech.ydb.test + ydb-junit5-support + test + @@ -52,7 +58,18 @@ tech.ydb.example.App - + + + + + org.apache.maven.plugins + maven-surefire-plugin + + + true + 1 + + diff --git a/basic_example/src/main/java/tech/ydb/example/App.java b/basic_example/src/main/java/tech/ydb/example/App.java index 72cefcc..f515841 100644 --- a/basic_example/src/main/java/tech/ydb/example/App.java +++ b/basic_example/src/main/java/tech/ydb/example/App.java @@ -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(); diff --git a/basic_example/src/main/resources/log4j2.xml b/basic_example/src/main/resources/log4j2.xml index 7ca65b6..cdcbde9 100644 --- a/basic_example/src/main/resources/log4j2.xml +++ b/basic_example/src/main/resources/log4j2.xml @@ -2,30 +2,21 @@ - + - - - - - - - - - - - - - - - - + + + + + + - + + diff --git a/basic_example/src/test/java/tech/ydb/example/BasicExampleTest.java b/basic_example/src/test/java/tech/ydb/example/BasicExampleTest.java new file mode 100644 index 0000000..8d162a1 --- /dev/null +++ b/basic_example/src/test/java/tech/ydb/example/BasicExampleTest.java @@ -0,0 +1,30 @@ +package tech.ydb.example; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import tech.ydb.test.junit5.YdbHelperExtension; + +/** + * + * @author Aleksandr Gorshenin + */ +public class BasicExampleTest { + @RegisterExtension + private static final YdbHelperExtension ydb = new YdbHelperExtension(); + + private static String connectionString() { + StringBuilder sb = new StringBuilder(); + sb.append(ydb.useTls() ? "grpcs://" : "grpc://" ); + sb.append(ydb.endpoint()); + sb.append(ydb.database()); + return sb.toString(); + } + + @Test + public void testBasicApp() { + App app = new App(connectionString()); + app.run(); + app.close(); + } +} From d14b6cdfa87a88865992c6cd9f9a4f2bb6dc81aa Mon Sep 17 00:00:00 2001 From: Alexandr Gorshenin Date: Wed, 8 Nov 2023 15:23:03 +0000 Subject: [PATCH 3/3] Update readTable example --- ydb-cookbook/pom.xml | 15 ++++++ .../ydb/examples/simple/ReadTableExample.java | 53 +++++++++++-------- .../java/tech/ydb/examples/ExamplesTest.java | 14 +++++ 3 files changed, 61 insertions(+), 21 deletions(-) diff --git a/ydb-cookbook/pom.xml b/ydb-cookbook/pom.xml index 799d38e..69bdb9d 100644 --- a/ydb-cookbook/pom.xml +++ b/ydb-cookbook/pom.xml @@ -51,4 +51,19 @@ test + + + + + org.apache.maven.plugins + maven-surefire-plugin + + + true + 1 + + + + + diff --git a/ydb-cookbook/src/main/java/tech/ydb/examples/simple/ReadTableExample.java b/ydb-cookbook/src/main/java/tech/ydb/examples/simple/ReadTableExample.java index f53a456..e6ab31b 100644 --- a/ydb-cookbook/src/main/java/tech/ydb/examples/simple/ReadTableExample.java +++ b/ydb-cookbook/src/main/java/tech/ydb/examples/simple/ReadTableExample.java @@ -1,11 +1,13 @@ package tech.ydb.examples.simple; -import java.time.Duration; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import tech.ydb.core.grpc.GrpcTransport; import tech.ydb.examples.SimpleExample; -import tech.ydb.table.Session; +import tech.ydb.table.SessionRetryContext; import tech.ydb.table.TableClient; import tech.ydb.table.result.ResultSetReader; import tech.ydb.table.settings.ReadTableSettings; @@ -17,28 +19,31 @@ * @author Sergey Polovko */ public class ReadTableExample extends SimpleExample { + private static final String TABLE_NAME = "read_table_example"; + private static final Logger logger = LoggerFactory.getLogger(ReadTableExample.class); + @Override protected void run(GrpcTransport transport, String pathPrefix) { - try ( - TableClient tableClient = TableClient.newClient(transport).build(); - Session session = tableClient.createSession(Duration.ofSeconds(5)).join().getValue() - ) { - - String tablePath = pathPrefix + getClass().getSimpleName(); - createAndFillTable(session, tablePath); - readTable(session, tablePath); + try (TableClient tableClient = TableClient.newClient(transport).build()) { + SessionRetryContext retryCtx = SessionRetryContext.create(tableClient).build(); + + createAndFillTable(retryCtx); + readTable(retryCtx, transport.getDatabase()); + dropTable(retryCtx); } } - private void readTable(Session session, String tablePath) { + private void readTable(SessionRetryContext retryCtx, String database) { + ReadTableSettings settings = ReadTableSettings.newBuilder() .orderedRead(true) .fromKeyInclusive(PrimitiveValue.newUint32(10)) .toKeyExclusive(PrimitiveValue.newUint32(25)) .build(); - session.executeReadTable(tablePath, settings).start(part -> { + String tablePath = database + "/" + TABLE_NAME; + retryCtx.supplyStatus(session -> session.executeReadTable(tablePath, settings).start(part -> { ResultSetReader resultSet = part.getResultSetReader(); // we are going to read a lot of data, so map column names to indexes @@ -49,31 +54,37 @@ private void readTable(Session session, String tablePath) { while (resultSet.next()) { long key = resultSet.getColumn(keyIdx).getUint32(); String value = resultSet.getColumn(valueIdx).getText(); - System.out.printf("key=%d, value=%s\n", key, value); + logger.info("key={}, value={}", key, value); } - }).join().expectSuccess("readTable failed"); + })).join().expectSuccess("readTable failed"); } - private void createAndFillTable(Session session, String tablePath) { + private void createAndFillTable(SessionRetryContext retryCtx) { String createTable = - "CREATE TABLE [" + tablePath + "] (" + + "CREATE TABLE " + TABLE_NAME + " (" + " key Uint32," + " value Utf8," + " PRIMARY KEY(key)" + ");"; - session.executeSchemeQuery(createTable) + retryCtx.supplyStatus(session -> session.executeSchemeQuery(createTable)) .join() .expectSuccess("cannot create table"); for (int i = 0; i < 100; i++) { - String query = "REPLACE INTO [" + tablePath + "](key, value) VALUES (" + i + ", \"<" + i + ">\");"; - session.executeDataQuery(query, TxControl.serializableRw().setCommitTx(true)) - .join() - .getStatus().expectSuccess("cannot execute insert"); + String query = "UPSERT INTO " + TABLE_NAME + "(key, value) VALUES (" + i + ", \"<" + i + ">\");"; + retryCtx.supplyResult(session -> session.executeDataQuery(query, TxControl.serializableRw())) + .join().getStatus().expectSuccess("cannot execute insert"); } } + private void dropTable(SessionRetryContext retryCtx) { + String dropSQL = "DROP TABLE " + TABLE_NAME + ";"; + retryCtx.supplyStatus(session -> session.executeSchemeQuery(dropSQL)) + .join() + .expectSuccess("cannot create table"); + } + public static void main(String[] args) { new ReadTableExample().doMain(args); } diff --git a/ydb-cookbook/src/test/java/tech/ydb/examples/ExamplesTest.java b/ydb-cookbook/src/test/java/tech/ydb/examples/ExamplesTest.java index 0d4884d..38fc4c8 100644 --- a/ydb-cookbook/src/test/java/tech/ydb/examples/ExamplesTest.java +++ b/ydb-cookbook/src/test/java/tech/ydb/examples/ExamplesTest.java @@ -7,6 +7,7 @@ import tech.ydb.examples.batch_upload.BatchUpload; import tech.ydb.examples.bulk_upsert.BulkUpsert; import tech.ydb.examples.pagination.PaginationApp; +import tech.ydb.examples.simple.ReadTableExample; import tech.ydb.test.junit5.YdbHelperExtension; /** @@ -24,6 +25,14 @@ private String[] args() { }; } + private String[] connectionString() { + StringBuilder sb = new StringBuilder(); + sb.append(ydb.useTls() ? "grpcs://" : "grpc://" ); + sb.append(ydb.endpoint()); + sb.append(ydb.database()); + return new String [] { sb.toString() }; + } + @Test public void testBatchUpload() { Assertions.assertEquals(0, BatchUpload.test(args()), "Batch upload test"); @@ -34,6 +43,11 @@ public void testBulkUpsert() { Assertions.assertEquals(0, BulkUpsert.test(args()), "Bulk upsert test"); } + @Test + public void testReadTable() { + ReadTableExample.main(connectionString()); + } + @Test public void testPagination() { Assertions.assertEquals(0, PaginationApp.test(args(), "Pagination test"));