@@ -26,7 +26,7 @@ App code snippet for driver initialization:
26
26
- Synchronous
27
27
28
28
``` python
29
- def run (endpoint , database , path ):
29
+ def run (endpoint , database ):
30
30
driver_config = ydb.DriverConfig(
31
31
endpoint, database, credentials = ydb.credentials_from_env_variables(),
32
32
root_certificates = ydb.load_ydb_root_certificate(),
@@ -44,7 +44,7 @@ App code snippet for driver initialization:
44
44
- Asynchronous
45
45
46
46
``` python
47
- async def run (endpoint , database , path ):
47
+ async def run (endpoint , database ):
48
48
driver_config = ydb.DriverConfig(
49
49
endpoint, database, credentials = ydb.credentials_from_env_variables(),
50
50
root_certificates = ydb.load_ydb_root_certificate(),
@@ -70,109 +70,97 @@ To execute YQL queries, use the `pool.execute_with_retries()` method. For exampl
70
70
- Synchronous
71
71
72
72
``` python
73
- def create_tables (pool : ydb.QuerySessionPool, path : str ):
73
+ def create_tables (pool : ydb.QuerySessionPool):
74
74
print (" \n Creating table series..." )
75
75
pool.execute_with_retries(
76
- f """
77
- PRAGMA TablePathPrefix(" { path} ");
78
- CREATE table `series` (
79
- `series_id` Uint64,
80
- `title` Utf8,
81
- `series_info` Utf8,
82
- `release_date` Uint64,
83
- PRIMARY KEY (`series_id`)
84
- )
85
- """
76
+ """
77
+ CREATE table `series` (
78
+ `series_id` Int64,
79
+ `title` Utf8,
80
+ `series_info` Utf8,
81
+ `release_date` Date,
82
+ PRIMARY KEY (`series_id`)
83
+ )
84
+ """
86
85
)
87
86
88
87
print (" \n Creating table seasons..." )
89
88
pool.execute_with_retries(
90
- f """
91
- PRAGMA TablePathPrefix(" { path} ");
92
- CREATE table `seasons` (
93
- `series_id` Uint64,
94
- `season_id` Uint64,
95
- `title` Utf8,
96
- `first_aired` Uint64,
97
- `last_aired` Uint64,
98
- PRIMARY KEY (`series_id`, `season_id`)
99
- )
100
- """
89
+ """
90
+ CREATE table `seasons` (
91
+ `series_id` Int64,
92
+ `season_id` Int64,
93
+ `title` Utf8,
94
+ `first_aired` Date,
95
+ `last_aired` Date,
96
+ PRIMARY KEY (`series_id`, `season_id`)
97
+ )
98
+ """
101
99
)
102
100
103
101
print (" \n Creating table episodes..." )
104
102
pool.execute_with_retries(
105
- f """
106
- PRAGMA TablePathPrefix(" { path} ");
107
- CREATE table `episodes` (
108
- `series_id` Uint64,
109
- `season_id` Uint64,
110
- `episode_id` Uint64,
111
- `title` Utf8,
112
- `air_date` Uint64,
113
- PRIMARY KEY (`series_id`, `season_id`, `episode_id`)
114
- )
115
- """
103
+ """
104
+ CREATE table `episodes` (
105
+ `series_id` Int64,
106
+ `season_id` Int64,
107
+ `episode_id` Int64,
108
+ `title` Utf8,
109
+ `air_date` Date,
110
+ PRIMARY KEY (`series_id`, `season_id`, `episode_id`)
111
+ )
112
+ """
116
113
)
117
114
```
118
115
119
116
- Asynchronous
120
117
121
118
``` python
122
- async def create_tables (pool : ydb.aio.QuerySessionPoolAsync, path : str ):
119
+ async def create_tables (pool : ydb.aio.QuerySessionPoolAsync):
123
120
print (" \n Creating table series..." )
124
121
await pool.execute_with_retries(
125
- f """
126
- PRAGMA TablePathPrefix(" { path} ");
127
- CREATE table `series` (
128
- `series_id` Uint64,
129
- `title` Utf8,
130
- `series_info` Utf8,
131
- `release_date` Uint64,
132
- PRIMARY KEY (`series_id`)
133
- )
134
- """
122
+ """
123
+ CREATE table `series` (
124
+ `series_id` Int64,
125
+ `title` Utf8,
126
+ `series_info` Utf8,
127
+ `release_date` Date,
128
+ PRIMARY KEY (`series_id`)
129
+ )
130
+ """
135
131
)
136
132
137
133
print (" \n Creating table seasons..." )
138
134
await pool.execute_with_retries(
139
- f """
140
- PRAGMA TablePathPrefix(" { path} ");
141
- CREATE table `seasons` (
142
- `series_id` Uint64,
143
- `season_id` Uint64,
144
- `title` Utf8,
145
- `first_aired` Uint64,
146
- `last_aired` Uint64,
147
- PRIMARY KEY (`series_id`, `season_id`)
148
- )
149
- """
135
+ """
136
+ CREATE table `seasons` (
137
+ `series_id` Int64,
138
+ `season_id` Int64,
139
+ `title` Utf8,
140
+ `first_aired` Date,
141
+ `last_aired` Date,
142
+ PRIMARY KEY (`series_id`, `season_id`)
143
+ )
144
+ """
150
145
)
151
146
152
147
print (" \n Creating table episodes..." )
153
148
await pool.execute_with_retries(
154
- f """
155
- PRAGMA TablePathPrefix(" { path} ");
156
- CREATE table `episodes` (
157
- `series_id` Uint64,
158
- `season_id` Uint64,
159
- `episode_id` Uint64,
160
- `title` Utf8,
161
- `air_date` Uint64,
162
- PRIMARY KEY (`series_id`, `season_id`, `episode_id`)
163
- )
164
- """
149
+ """
150
+ CREATE table `episodes` (
151
+ `series_id` Int64,
152
+ `season_id` Int64,
153
+ `episode_id` Int64,
154
+ `title` Utf8,
155
+ `air_date` Date,
156
+ PRIMARY KEY (`series_id`, `season_id`, `episode_id`)
157
+ )
158
+ """
165
159
)
166
160
```
167
161
168
162
{% endlist %}
169
163
170
- The path parameter accepts the absolute path starting from the root:
171
-
172
- ``` python
173
- full_path = os.path.join(database, path)
174
- ```
175
-
176
164
The function ` pool.execute_with_retries(query) ` , unlike ` tx.execute() ` , loads the result of the query into memory before returning it to the client. This eliminates the need to use special constructs to control the iterator, but it is necessary to use this method with caution for large ` SELECT ` queries. More information about streams will be discussed below.
177
165
178
166
{% include [ steps/03_write_queries.md] ( ../_includes/steps/03_write_queries.md ) %}
@@ -184,11 +172,10 @@ Code snippet for data insert/update:
184
172
- Synchronous
185
173
186
174
``` python
187
- def upsert_simple (pool , path ):
175
+ def upsert_simple (pool : ydb.QuerySessionPool ):
188
176
print (" \n Performing UPSERT into episodes..." )
189
177
pool.execute_with_retries(
190
- f """
191
- PRAGMA TablePathPrefix(" { path} ");
178
+ """
192
179
UPSERT INTO episodes (series_id, season_id, episode_id, title) VALUES (2, 6, 1, "TBD");
193
180
"""
194
181
)
@@ -197,20 +184,17 @@ Code snippet for data insert/update:
197
184
- Asynchronous
198
185
199
186
``` python
200
- async def upsert_simple (pool : ydb.aio.QuerySessionPoolAsync, path : str ):
187
+ async def upsert_simple (pool : ydb.aio.QuerySessionPoolAsync):
201
188
print (" \n Performing UPSERT into episodes..." )
202
189
await pool.execute_with_retries(
203
- f """
204
- PRAGMA TablePathPrefix(" { path} ");
190
+ """
205
191
UPSERT INTO episodes (series_id, season_id, episode_id, title) VALUES (2, 6, 1, "TBD");
206
192
"""
207
193
)
208
194
```
209
195
210
196
{% endlist %}
211
197
212
- {% include [ pragmatablepathprefix.md] ( ../_includes/auxilary/pragmatablepathprefix.md ) %}
213
-
214
198
{% include [ steps/04_query_processing.md] ( ../_includes/steps/04_query_processing.md ) %}
215
199
216
200
To execute YQL queries, it is often enough to use the already familiar ` pool.execute_with_retries() ` method.
@@ -220,11 +204,10 @@ To execute YQL queries, it is often enough to use the already familiar `pool.exe
220
204
- Synchronous
221
205
222
206
``` python
223
- def select_simple (pool : ydb.QuerySessionPool, path : str ):
207
+ def select_simple (pool : ydb.QuerySessionPool):
224
208
print (" \n Check series table..." )
225
209
result_sets = pool.execute_with_retries(
226
- f """
227
- PRAGMA TablePathPrefix(" { path} ");
210
+ """
228
211
SELECT
229
212
series_id,
230
213
title,
@@ -249,11 +232,10 @@ To execute YQL queries, it is often enough to use the already familiar `pool.exe
249
232
- Asynchronous
250
233
251
234
``` python
252
- async def select_simple (pool : ydb.aio.QuerySessionPoolAsync, path : str ):
235
+ async def select_simple (pool : ydb.aio.QuerySessionPoolAsync):
253
236
print (" \n Check series table..." )
254
237
result_sets = await pool.execute_with_retries(
255
- f """
256
- PRAGMA TablePathPrefix(" { path} ");
238
+ """
257
239
SELECT
258
240
series_id,
259
241
title,
@@ -309,10 +291,13 @@ A code snippet demonstrating the possibility of using parameterized queries:
309
291
- Synchronous
310
292
311
293
``` python
312
- def select_with_parameters (pool : ydb.QuerySessionPool, path : str , series_id , season_id , episode_id ):
294
+ def select_with_parameters (pool : ydb.QuerySessionPool, series_id , season_id , episode_id ):
313
295
result_sets = pool.execute_with_retries(
314
- f """
315
- PRAGMA TablePathPrefix(" { path} ");
296
+ """
297
+ DECLARE $seriesId AS Int64;
298
+ DECLARE $seasonId AS Int64;
299
+ DECLARE $episodeId AS Int64;
300
+
316
301
SELECT
317
302
title,
318
303
air_date
@@ -337,10 +322,13 @@ A code snippet demonstrating the possibility of using parameterized queries:
337
322
- Asynchronous
338
323
339
324
``` python
340
- async def select_with_parameters (pool : ydb.aio.QuerySessionPoolAsync, path : str , series_id , season_id , episode_id ):
325
+ async def select_with_parameters (pool : ydb.aio.QuerySessionPoolAsync, series_id , season_id , episode_id ):
341
326
result_sets = await pool.execute_with_retries(
342
- f """
343
- PRAGMA TablePathPrefix(" { path} ");
327
+ """
328
+ DECLARE $seriesId AS Int64;
329
+ DECLARE $seasonId AS Int64;
330
+ DECLARE $episodeId AS Int64;
331
+
344
332
SELECT
345
333
title,
346
334
air_date
@@ -416,10 +404,13 @@ A code snippet demonstrating the explicit use of `transaction().begin()` and `tx
416
404
- Synchronous
417
405
418
406
``` python
419
- def explicit_transaction_control (pool : ydb.QuerySessionPool, path : str , series_id , season_id , episode_id ):
407
+ def explicit_transaction_control (pool : ydb.QuerySessionPool, series_id , season_id , episode_id ):
420
408
def callee (session : ydb.QuerySessionSync):
421
- query = f """
422
- PRAGMA TablePathPrefix(" { path} ");
409
+ query = """
410
+ DECLARE $seriesId AS Int64;
411
+ DECLARE $seasonId AS Int64;
412
+ DECLARE $episodeId AS Int64;
413
+
423
414
UPDATE episodes
424
415
SET air_date = CurrentUtcDate()
425
416
WHERE series_id = $seriesId AND season_id = $seasonId AND episode_id = $episodeId;
@@ -452,11 +443,14 @@ A code snippet demonstrating the explicit use of `transaction().begin()` and `tx
452
443
453
444
``` python
454
445
async def explicit_transaction_control (
455
- pool : ydb.aio.QuerySessionPoolAsync, path : str , series_id , season_id , episode_id
446
+ pool : ydb.aio.QuerySessionPoolAsync, series_id , season_id , episode_id
456
447
):
457
448
async def callee (session : ydb.aio.QuerySessionAsync):
458
- query = f """
459
- PRAGMA TablePathPrefix(" { path} ");
449
+ query = """
450
+ DECLARE $seriesId AS Int64;
451
+ DECLARE $seasonId AS Int64;
452
+ DECLARE $episodeId AS Int64;
453
+
460
454
UPDATE episodes
461
455
SET air_date = CurrentUtcDate()
462
456
WHERE series_id = $seriesId AND season_id = $seasonId AND episode_id = $episodeId;
@@ -501,12 +495,9 @@ Example of a `SELECT` with unlimited data and implicit transaction control:
501
495
- Synchronous
502
496
503
497
``` python
504
- def huge_select (pool : ydb.QuerySessionPool, path : str ):
498
+ def huge_select (pool : ydb.QuerySessionPool):
505
499
def callee (session : ydb.QuerySessionSync):
506
- query = f """
507
- PRAGMA TablePathPrefix(" { path} ");
508
- SELECT * from episodes;
509
- """
500
+ query = """ SELECT * from episodes;"""
510
501
511
502
with session.transaction().execute(
512
503
query,
@@ -523,12 +514,9 @@ Example of a `SELECT` with unlimited data and implicit transaction control:
523
514
- Asynchronous
524
515
525
516
``` python
526
- async def huge_select (pool : ydb.aio.QuerySessionPoolAsync, path : str ):
517
+ async def huge_select (pool : ydb.aio.QuerySessionPoolAsync):
527
518
async def callee (session : ydb.aio.QuerySessionAsync):
528
- query = f """
529
- PRAGMA TablePathPrefix(" { path} ");
530
- SELECT * from episodes;
531
- """
519
+ query = """ SELECT * from episodes;"""
532
520
533
521
async with await session.transaction().execute(
534
522
query,
0 commit comments