diff --git a/pom.xml b/pom.xml index f81f8ca..6bd086e 100644 --- a/pom.xml +++ b/pom.xml @@ -29,6 +29,7 @@ ydb-cookbook url-shortener-demo jdbc + project-course diff --git a/project-course/pom.xml b/project-course/pom.xml new file mode 100644 index 0000000..ebd2358 --- /dev/null +++ b/project-course/pom.xml @@ -0,0 +1,40 @@ + + 4.0.0 + + tech.ydb.examples + ydb-sdk-examples + 1.1.0-SNAPSHOT + + + tech.ydb.app + project-course + jar + + project-course + http://maven.apache.org + + + UTF-8 + + + + + tech.ydb + ydb-sdk-query + + + + + + + org.codehaus.mojo + exec-maven-plugin + 3.5.0 + + tech.ydb.app.App + + + + + diff --git a/project-course/src/main/java/tech/ydb/app/App.java b/project-course/src/main/java/tech/ydb/app/App.java new file mode 100644 index 0000000..6f16c24 --- /dev/null +++ b/project-course/src/main/java/tech/ydb/app/App.java @@ -0,0 +1,13 @@ +package tech.ydb.app; + +import tech.ydb.app.repository.YdbRepository; + +public class App { + public static void main(String[] args) { + if (args.length != 1) { + throw new UnsupportedOperationException("Expected 1 parameter connectionString"); + } + + new YdbRepository(args[0]).TestDatabase(); + } +} diff --git a/project-course/src/main/java/tech/ydb/app/repository/YdbRepository.java b/project-course/src/main/java/tech/ydb/app/repository/YdbRepository.java new file mode 100644 index 0000000..384811e --- /dev/null +++ b/project-course/src/main/java/tech/ydb/app/repository/YdbRepository.java @@ -0,0 +1,33 @@ +package tech.ydb.app.repository; + +import tech.ydb.common.transaction.TxMode; +import tech.ydb.core.grpc.GrpcTransport; +import tech.ydb.query.QueryClient; +import tech.ydb.query.tools.QueryReader; +import tech.ydb.query.tools.SessionRetryContext; +import tech.ydb.table.result.ResultSetReader; + +/** + * @author Kirill Kurdyukov + */ +public class YdbRepository { + private final SessionRetryContext retryCtx; + + public YdbRepository(String connectionString) { + GrpcTransport transport = GrpcTransport.forConnectionString(connectionString).build(); + QueryClient queryClient = QueryClient.newClient(transport).build(); + this.retryCtx = SessionRetryContext.create(queryClient).build(); + } + + public void TestDatabase() { + QueryReader resultSet = retryCtx.supplyResult(session -> + QueryReader.readFrom(session.createQuery("SELECT 1;", TxMode.NONE)) + ).join().getValue(); + + ResultSetReader resultSetReader = resultSet.getResultSet(0); + + if (resultSetReader.next()) { + System.out.println("Database is available!"); + } + } +}