Skip to content

Commit 156b7db

Browse files
Merge pull request #9 from ydb-platform/feat-lesson-10
feat: lesson 10
2 parents bc4e087 + 21cac9e commit 156b7db

File tree

11 files changed

+823
-0
lines changed

11 files changed

+823
-0
lines changed

java/lesson-10/pom.xml

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>tech.ydb.app</groupId>
5+
<artifactId>lesson-10</artifactId>
6+
<version>1.0-SNAPSHOT</version>
7+
<name>Lesson-10</name>
8+
9+
<properties>
10+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
11+
<maven.compiler.source>21</maven.compiler.source>
12+
<maven.compiler.target>21</maven.compiler.target>
13+
</properties>
14+
15+
<dependencyManagement>
16+
<dependencies>
17+
<dependency>
18+
<groupId>tech.ydb</groupId>
19+
<artifactId>ydb-sdk-bom</artifactId>
20+
<version>2.3.8</version>
21+
<type>pom</type>
22+
<scope>import</scope>
23+
</dependency>
24+
</dependencies>
25+
</dependencyManagement>
26+
27+
<dependencies>
28+
<dependency>
29+
<groupId>tech.ydb</groupId>
30+
<artifactId>ydb-sdk-query</artifactId>
31+
</dependency>
32+
<dependency>
33+
<groupId>tech.ydb</groupId>
34+
<artifactId>ydb-sdk-topic</artifactId>
35+
</dependency>
36+
<dependency>
37+
<groupId>com.opencsv</groupId>
38+
<artifactId>opencsv</artifactId>
39+
<version>5.8</version>
40+
</dependency>
41+
</dependencies>
42+
43+
<build>
44+
<pluginManagement>
45+
<plugins>
46+
<plugin>
47+
<groupId>org.apache.maven.plugins</groupId>
48+
<artifactId>maven-compiler-plugin</artifactId>
49+
<version>3.12.1</version>
50+
<configuration>
51+
<source>21</source>
52+
</configuration>
53+
<executions>
54+
<execution>
55+
<id>attach-javadocs</id>
56+
<goals>
57+
<goal>jar</goal>
58+
</goals>
59+
</execution>
60+
</executions>
61+
</plugin>
62+
</plugins>
63+
</pluginManagement>
64+
</build>
65+
</project>
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package tech.ydb.app;
2+
3+
import com.opencsv.CSVReader;
4+
import com.opencsv.exceptions.CsvException;
5+
import java.io.IOException;
6+
import java.nio.file.Files;
7+
import java.nio.file.Path;
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
import tech.ydb.core.grpc.GrpcTransport;
11+
import tech.ydb.query.QueryClient;
12+
import tech.ydb.query.tools.SessionRetryContext;
13+
import tech.ydb.table.TableClient;
14+
15+
/**
16+
* @author Kirill Kurdyukov
17+
*/
18+
public class Application {
19+
20+
private static final String PATH = "/java/lesson-10/title_author.csv";
21+
private static final String CONNECTION_STRING = "grpc://localhost:2136/local";
22+
23+
public static void main(String[] args) {
24+
try (GrpcTransport grpcTransport = GrpcTransport.forConnectionString(CONNECTION_STRING).build();
25+
TableClient tableClient = TableClient.newClient(grpcTransport).build();
26+
QueryClient queryClient = QueryClient.newClient(grpcTransport).build()) {
27+
var retryCtx = SessionRetryContext.create(queryClient).build();
28+
var retryTableCtx = tech.ydb.table.SessionRetryContext.create(tableClient).build();
29+
30+
var schemaYdbRepository = new SchemaYdbRepository(retryCtx);
31+
var issueYdbRepository = new IssueYdbRepository(retryCtx);
32+
var nativeApiYdbRepository = new KeyValueApiYdbRepository(retryTableCtx);
33+
34+
schemaYdbRepository.createSchema();
35+
36+
var titleAuthorList = new ArrayList<TitleAuthor>();
37+
try (CSVReader reader = new CSVReader(Files.newBufferedReader(
38+
Path.of(System.getProperty("user.dir"), PATH)))
39+
) {
40+
List<String[]> allRows = reader.readAll();
41+
42+
for (int i = 1; i < allRows.size(); i++) {
43+
var title = allRows.get(i)[0];
44+
var author = allRows.get(i)[1];
45+
46+
titleAuthorList.add(new TitleAuthor(title, author));
47+
}
48+
} catch (IOException | CsvException e) {
49+
System.err.println(e.getMessage());
50+
51+
throw new RuntimeException(e);
52+
}
53+
54+
nativeApiYdbRepository.bulkUpsert("/local/issues", titleAuthorList);
55+
56+
Issue lastIssue = null;
57+
System.out.println("Print all issues: ");
58+
for (var issue : issueYdbRepository.findAll()) {
59+
printIssue(issue);
60+
61+
lastIssue = issue;
62+
}
63+
64+
System.out.println("ReadTable: ");
65+
for (var issue : nativeApiYdbRepository.readTable("/local/issues")) {
66+
printIssue(issue);
67+
}
68+
69+
System.out.println("ReadRows: ");
70+
assert lastIssue != null;
71+
for (var issue : nativeApiYdbRepository.readRows("/local/issues", lastIssue.id())) {
72+
printIssue(issue);
73+
}
74+
75+
schemaYdbRepository.dropSchema();
76+
}
77+
}
78+
79+
private static void printIssue(Issue issue) {
80+
System.out.println("Ticket: {id: " + issue.id() + ", title: " + issue.title() + ", timestamp: "
81+
+ issue.now() + ", author: " + issue.author() + ", link_count: "
82+
+ issue.linkCounts() + ", status: " + issue.status() + "}");
83+
}
84+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package tech.ydb.app;
2+
3+
import java.time.Instant;
4+
import java.util.UUID;
5+
6+
/**
7+
* @author Kirill Kurdyukov
8+
*/
9+
public record Issue(UUID id, String title, Instant now, String author, long linkCounts, String status) {
10+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package tech.ydb.app;
2+
3+
import java.util.UUID;
4+
5+
/**
6+
* @author Kirill Kurdyukov
7+
*/
8+
public record IssueLinkCount(UUID id, long linkCount) {
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package tech.ydb.app;
2+
3+
import java.util.UUID;
4+
5+
/**
6+
* @author Kirill Kurdyukov
7+
*/
8+
public record IssueTitle(UUID id, String title) {
9+
}

0 commit comments

Comments
 (0)