1
1
package tech .ydb .examples .simple ;
2
2
3
3
4
- import java .time .Duration ;
4
+
5
+ import org .slf4j .Logger ;
6
+ import org .slf4j .LoggerFactory ;
5
7
6
8
import tech .ydb .core .grpc .GrpcTransport ;
7
9
import tech .ydb .examples .SimpleExample ;
8
- import tech .ydb .table .Session ;
10
+ import tech .ydb .table .SessionRetryContext ;
9
11
import tech .ydb .table .TableClient ;
10
12
import tech .ydb .table .result .ResultSetReader ;
11
13
import tech .ydb .table .settings .ReadTableSettings ;
17
19
* @author Sergey Polovko
18
20
*/
19
21
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
+
20
25
21
26
@ Override
22
27
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 );
31
34
}
32
35
}
33
36
34
- private void readTable (Session session , String tablePath ) {
37
+ private void readTable (SessionRetryContext retryCtx , String database ) {
38
+
35
39
ReadTableSettings settings = ReadTableSettings .newBuilder ()
36
40
.orderedRead (true )
37
41
.fromKeyInclusive (PrimitiveValue .newUint32 (10 ))
38
42
.toKeyExclusive (PrimitiveValue .newUint32 (25 ))
39
43
.build ();
40
44
41
- session .executeReadTable (tablePath , settings ).start (part -> {
45
+ String tablePath = database + "/" + TABLE_NAME ;
46
+ retryCtx .supplyStatus (session -> session .executeReadTable (tablePath , settings ).start (part -> {
42
47
ResultSetReader resultSet = part .getResultSetReader ();
43
48
44
49
// 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) {
49
54
while (resultSet .next ()) {
50
55
long key = resultSet .getColumn (keyIdx ).getUint32 ();
51
56
String value = resultSet .getColumn (valueIdx ).getText ();
52
- System . out . printf ("key=%d , value=%s \n " , key , value );
57
+ logger . info ("key={} , value={} " , key , value );
53
58
}
54
- }).join ().expectSuccess ("readTable failed" );
59
+ })) .join ().expectSuccess ("readTable failed" );
55
60
}
56
61
57
- private void createAndFillTable (Session session , String tablePath ) {
62
+ private void createAndFillTable (SessionRetryContext retryCtx ) {
58
63
String createTable =
59
- "CREATE TABLE [ " + tablePath + "] (" +
64
+ "CREATE TABLE " + TABLE_NAME + " (" +
60
65
" key Uint32," +
61
66
" value Utf8," +
62
67
" PRIMARY KEY(key)" +
63
68
");" ;
64
69
65
- session .executeSchemeQuery (createTable )
70
+ retryCtx . supplyStatus ( session -> session .executeSchemeQuery (createTable ) )
66
71
.join ()
67
72
.expectSuccess ("cannot create table" );
68
73
69
74
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" );
74
78
}
75
79
}
76
80
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
+
77
88
public static void main (String [] args ) {
78
89
new ReadTableExample ().doMain (args );
79
90
}
0 commit comments