|
| 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 | +} |
0 commit comments