Skip to content

Commit d14b6cd

Browse files
committed
Update readTable example
1 parent de864ed commit d14b6cd

File tree

3 files changed

+61
-21
lines changed

3 files changed

+61
-21
lines changed

ydb-cookbook/pom.xml

+15
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,19 @@
5151
<scope>test</scope>
5252
</dependency>
5353
</dependencies>
54+
55+
<build>
56+
<plugins>
57+
<plugin>
58+
<groupId>org.apache.maven.plugins</groupId>
59+
<artifactId>maven-surefire-plugin</artifactId>
60+
<configuration>
61+
<environmentVariables>
62+
<TESTCONTAINERS_REUSE_ENABLE>true</TESTCONTAINERS_REUSE_ENABLE>
63+
<YDB_ANONYMOUS_CREDENTIALS>1</YDB_ANONYMOUS_CREDENTIALS>
64+
</environmentVariables>
65+
</configuration>
66+
</plugin>
67+
</plugins>
68+
</build>
5469
</project>

ydb-cookbook/src/main/java/tech/ydb/examples/simple/ReadTableExample.java

+32-21
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package tech.ydb.examples.simple;
22

33

4-
import java.time.Duration;
4+
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
57

68
import tech.ydb.core.grpc.GrpcTransport;
79
import tech.ydb.examples.SimpleExample;
8-
import tech.ydb.table.Session;
10+
import tech.ydb.table.SessionRetryContext;
911
import tech.ydb.table.TableClient;
1012
import tech.ydb.table.result.ResultSetReader;
1113
import tech.ydb.table.settings.ReadTableSettings;
@@ -17,28 +19,31 @@
1719
* @author Sergey Polovko
1820
*/
1921
public class ReadTableExample extends SimpleExample {
22+
private static final String TABLE_NAME = "read_table_example";
23+
private static final Logger logger = LoggerFactory.getLogger(ReadTableExample.class);
24+
2025

2126
@Override
2227
protected void run(GrpcTransport transport, String pathPrefix) {
23-
try (
24-
TableClient tableClient = TableClient.newClient(transport).build();
25-
Session session = tableClient.createSession(Duration.ofSeconds(5)).join().getValue()
26-
) {
27-
28-
String tablePath = pathPrefix + getClass().getSimpleName();
29-
createAndFillTable(session, tablePath);
30-
readTable(session, tablePath);
28+
try (TableClient tableClient = TableClient.newClient(transport).build()) {
29+
SessionRetryContext retryCtx = SessionRetryContext.create(tableClient).build();
30+
31+
createAndFillTable(retryCtx);
32+
readTable(retryCtx, transport.getDatabase());
33+
dropTable(retryCtx);
3134
}
3235
}
3336

34-
private void readTable(Session session, String tablePath) {
37+
private void readTable(SessionRetryContext retryCtx, String database) {
38+
3539
ReadTableSettings settings = ReadTableSettings.newBuilder()
3640
.orderedRead(true)
3741
.fromKeyInclusive(PrimitiveValue.newUint32(10))
3842
.toKeyExclusive(PrimitiveValue.newUint32(25))
3943
.build();
4044

41-
session.executeReadTable(tablePath, settings).start(part -> {
45+
String tablePath = database + "/" + TABLE_NAME;
46+
retryCtx.supplyStatus(session -> session.executeReadTable(tablePath, settings).start(part -> {
4247
ResultSetReader resultSet = part.getResultSetReader();
4348

4449
// 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) {
4954
while (resultSet.next()) {
5055
long key = resultSet.getColumn(keyIdx).getUint32();
5156
String value = resultSet.getColumn(valueIdx).getText();
52-
System.out.printf("key=%d, value=%s\n", key, value);
57+
logger.info("key={}, value={}", key, value);
5358
}
54-
}).join().expectSuccess("readTable failed");
59+
})).join().expectSuccess("readTable failed");
5560
}
5661

57-
private void createAndFillTable(Session session, String tablePath) {
62+
private void createAndFillTable(SessionRetryContext retryCtx) {
5863
String createTable =
59-
"CREATE TABLE [" + tablePath + "] (" +
64+
"CREATE TABLE " + TABLE_NAME + " (" +
6065
" key Uint32," +
6166
" value Utf8," +
6267
" PRIMARY KEY(key)" +
6368
");";
6469

65-
session.executeSchemeQuery(createTable)
70+
retryCtx.supplyStatus(session -> session.executeSchemeQuery(createTable))
6671
.join()
6772
.expectSuccess("cannot create table");
6873

6974
for (int i = 0; i < 100; i++) {
70-
String query = "REPLACE INTO [" + tablePath + "](key, value) VALUES (" + i + ", \"<" + i + ">\");";
71-
session.executeDataQuery(query, TxControl.serializableRw().setCommitTx(true))
72-
.join()
73-
.getStatus().expectSuccess("cannot execute insert");
75+
String query = "UPSERT INTO " + TABLE_NAME + "(key, value) VALUES (" + i + ", \"<" + i + ">\");";
76+
retryCtx.supplyResult(session -> session.executeDataQuery(query, TxControl.serializableRw()))
77+
.join().getStatus().expectSuccess("cannot execute insert");
7478
}
7579
}
7680

81+
private void dropTable(SessionRetryContext retryCtx) {
82+
String dropSQL = "DROP TABLE " + TABLE_NAME + ";";
83+
retryCtx.supplyStatus(session -> session.executeSchemeQuery(dropSQL))
84+
.join()
85+
.expectSuccess("cannot create table");
86+
}
87+
7788
public static void main(String[] args) {
7889
new ReadTableExample().doMain(args);
7990
}

ydb-cookbook/src/test/java/tech/ydb/examples/ExamplesTest.java

+14
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import tech.ydb.examples.batch_upload.BatchUpload;
88
import tech.ydb.examples.bulk_upsert.BulkUpsert;
99
import tech.ydb.examples.pagination.PaginationApp;
10+
import tech.ydb.examples.simple.ReadTableExample;
1011
import tech.ydb.test.junit5.YdbHelperExtension;
1112

1213
/**
@@ -24,6 +25,14 @@ private String[] args() {
2425
};
2526
}
2627

28+
private String[] connectionString() {
29+
StringBuilder sb = new StringBuilder();
30+
sb.append(ydb.useTls() ? "grpcs://" : "grpc://" );
31+
sb.append(ydb.endpoint());
32+
sb.append(ydb.database());
33+
return new String [] { sb.toString() };
34+
}
35+
2736
@Test
2837
public void testBatchUpload() {
2938
Assertions.assertEquals(0, BatchUpload.test(args()), "Batch upload test");
@@ -34,6 +43,11 @@ public void testBulkUpsert() {
3443
Assertions.assertEquals(0, BulkUpsert.test(args()), "Bulk upsert test");
3544
}
3645

46+
@Test
47+
public void testReadTable() {
48+
ReadTableExample.main(connectionString());
49+
}
50+
3751
@Test
3852
public void testPagination() {
3953
Assertions.assertEquals(0, PaginationApp.test(args(), "Pagination test"));

0 commit comments

Comments
 (0)