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

feat: project course lesson 1 #47

Merged
merged 2 commits into from
Jan 23, 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
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<module>ydb-cookbook</module>
<module>url-shortener-demo</module>
<module>jdbc</module>
<module>project-course</module>
</modules>

<dependencyManagement>
Expand Down
40 changes: 40 additions & 0 deletions project-course/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>tech.ydb.examples</groupId>
<artifactId>ydb-sdk-examples</artifactId>
<version>1.1.0-SNAPSHOT</version>
</parent>

<groupId>tech.ydb.app</groupId>
<artifactId>project-course</artifactId>
<packaging>jar</packaging>

<name>project-course</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>tech.ydb</groupId>
<artifactId>ydb-sdk-query</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.5.0</version>
<configuration>
<mainClass>tech.ydb.app.App</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
13 changes: 13 additions & 0 deletions project-course/src/main/java/tech/ydb/app/App.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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!");
}
}
}
Loading