1
1
# -*- coding: utf-8 -*-
2
- import posixpath
3
2
import ydb
4
3
import basic_example_data
5
4
6
- # Table path prefix allows to put the working tables into the specific directory
7
- # inside the YDB database. Putting `PRAGMA TablePathPrefix("some/path")`
8
- # at the beginning of the query allows to reference the tables through
9
- # their names "under" the specified directory.
10
- #
11
- # TablePathPrefix has to be defined as an absolute path, which has to be started
12
- # with the current database location.
13
- #
14
- # https://ydb.tech/ru/docs/yql/reference/syntax/pragma#table-path-prefix
15
-
16
- DropTablesQuery = """PRAGMA TablePathPrefix("{}");
5
+
6
+ DropTablesQuery = """
17
7
DROP TABLE IF EXISTS series;
18
8
DROP TABLE IF EXISTS seasons;
19
9
DROP TABLE IF EXISTS episodes;
20
10
"""
21
11
22
- FillDataQuery = """PRAGMA TablePathPrefix("{}");
23
-
12
+ FillDataQuery = """
24
13
DECLARE $seriesData AS List<Struct<
25
14
series_id: Int64,
26
15
title: Utf8,
69
58
"""
70
59
71
60
72
- def fill_tables_with_data (pool : ydb .QuerySessionPool , path : str ):
61
+ def fill_tables_with_data (pool : ydb .QuerySessionPool ):
73
62
print ("\n Filling tables with data..." )
74
63
75
- query = FillDataQuery .format (path )
76
-
77
64
pool .execute_with_retries (
78
- query ,
65
+ FillDataQuery ,
79
66
{
80
67
"$seriesData" : (basic_example_data .get_series_data (), basic_example_data .get_series_data_type ()),
81
68
"$seasonsData" : (basic_example_data .get_seasons_data (), basic_example_data .get_seasons_data_type ()),
@@ -84,11 +71,10 @@ def fill_tables_with_data(pool: ydb.QuerySessionPool, path: str):
84
71
)
85
72
86
73
87
- def select_simple (pool : ydb .QuerySessionPool , path : str ):
74
+ def select_simple (pool : ydb .QuerySessionPool ):
88
75
print ("\n Check series table..." )
89
76
result_sets = pool .execute_with_retries (
90
- f"""
91
- PRAGMA TablePathPrefix("{ path } ");
77
+ """
92
78
SELECT
93
79
series_id,
94
80
title,
@@ -111,21 +97,23 @@ def select_simple(pool: ydb.QuerySessionPool, path: str):
111
97
return first_set
112
98
113
99
114
- def upsert_simple (pool : ydb .QuerySessionPool , path : str ):
100
+ def upsert_simple (pool : ydb .QuerySessionPool ):
115
101
print ("\n Performing UPSERT into episodes..." )
116
102
117
103
pool .execute_with_retries (
118
- f"""
119
- PRAGMA TablePathPrefix("{ path } ");
104
+ """
120
105
UPSERT INTO episodes (series_id, season_id, episode_id, title) VALUES (2, 6, 1, "TBD");
121
106
"""
122
107
)
123
108
124
109
125
- def select_with_parameters (pool : ydb .QuerySessionPool , path : str , series_id , season_id , episode_id ):
110
+ def select_with_parameters (pool : ydb .QuerySessionPool , series_id , season_id , episode_id ):
126
111
result_sets = pool .execute_with_retries (
127
- f"""
128
- PRAGMA TablePathPrefix("{ path } ");
112
+ """
113
+ DECLARE $seriesId AS Int64;
114
+ DECLARE $seasonId AS Int64;
115
+ DECLARE $episodeId AS Int64;
116
+
129
117
SELECT
130
118
title,
131
119
air_date
@@ -151,10 +139,13 @@ def select_with_parameters(pool: ydb.QuerySessionPool, path: str, series_id, sea
151
139
# In most cases it's better to use transaction control settings in session.transaction
152
140
# calls instead to avoid additional hops to YDB cluster and allow more efficient
153
141
# execution of queries.
154
- def explicit_transaction_control (pool : ydb .QuerySessionPool , path : str , series_id , season_id , episode_id ):
142
+ def explicit_transaction_control (pool : ydb .QuerySessionPool , series_id , season_id , episode_id ):
155
143
def callee (session : ydb .QuerySessionSync ):
156
- query = f"""
157
- PRAGMA TablePathPrefix("{ path } ");
144
+ query = """
145
+ DECLARE $seriesId AS Int64;
146
+ DECLARE $seasonId AS Int64;
147
+ DECLARE $episodeId AS Int64;
148
+
158
149
UPDATE episodes
159
150
SET air_date = CurrentUtcDate()
160
151
WHERE series_id = $seriesId AND season_id = $seasonId AND episode_id = $episodeId;
@@ -183,12 +174,9 @@ def callee(session: ydb.QuerySessionSync):
183
174
return pool .retry_operation_sync (callee )
184
175
185
176
186
- def huge_select (pool : ydb .QuerySessionPool , path : str ):
177
+ def huge_select (pool : ydb .QuerySessionPool ):
187
178
def callee (session : ydb .QuerySessionSync ):
188
- query = f"""
189
- PRAGMA TablePathPrefix("{ path } ");
190
- SELECT * from episodes;
191
- """
179
+ query = """SELECT * from episodes;"""
192
180
193
181
with session .transaction ().execute (
194
182
query ,
@@ -202,16 +190,15 @@ def callee(session: ydb.QuerySessionSync):
202
190
return pool .retry_operation_sync (callee )
203
191
204
192
205
- def drop_tables (pool : ydb .QuerySessionPool , path : str ):
193
+ def drop_tables (pool : ydb .QuerySessionPool ):
206
194
print ("\n Cleaning up existing tables..." )
207
- pool .execute_with_retries (DropTablesQuery . format ( path ) )
195
+ pool .execute_with_retries (DropTablesQuery )
208
196
209
197
210
- def create_tables (pool : ydb .QuerySessionPool , path : str ):
198
+ def create_tables (pool : ydb .QuerySessionPool ):
211
199
print ("\n Creating table series..." )
212
200
pool .execute_with_retries (
213
- f"""
214
- PRAGMA TablePathPrefix("{ path } ");
201
+ """
215
202
CREATE table `series` (
216
203
`series_id` Int64,
217
204
`title` Utf8,
@@ -224,8 +211,7 @@ def create_tables(pool: ydb.QuerySessionPool, path: str):
224
211
225
212
print ("\n Creating table seasons..." )
226
213
pool .execute_with_retries (
227
- f"""
228
- PRAGMA TablePathPrefix("{ path } ");
214
+ """
229
215
CREATE table `seasons` (
230
216
`series_id` Int64,
231
217
`season_id` Int64,
@@ -239,8 +225,7 @@ def create_tables(pool: ydb.QuerySessionPool, path: str):
239
225
240
226
print ("\n Creating table episodes..." )
241
227
pool .execute_with_retries (
242
- f"""
243
- PRAGMA TablePathPrefix("{ path } ");
228
+ """
244
229
CREATE table `episodes` (
245
230
`series_id` Int64,
246
231
`season_id` Int64,
@@ -253,29 +238,7 @@ def create_tables(pool: ydb.QuerySessionPool, path: str):
253
238
)
254
239
255
240
256
- def is_directory_exists (driver : ydb .Driver , path : str ):
257
- try :
258
- return driver .scheme_client .describe_path (path ).is_directory ()
259
- except ydb .SchemeError :
260
- return False
261
-
262
-
263
- def ensure_path_exists (driver , database , path ):
264
- paths_to_create = list ()
265
- path = path .rstrip ("/" )
266
- while path not in ("" , database ):
267
- full_path = posixpath .join (database , path )
268
- if is_directory_exists (driver , full_path ):
269
- break
270
- paths_to_create .append (full_path )
271
- path = posixpath .dirname (path ).rstrip ("/" )
272
-
273
- while len (paths_to_create ) > 0 :
274
- full_path = paths_to_create .pop (- 1 )
275
- driver .scheme_client .make_directory (full_path )
276
-
277
-
278
- def run (endpoint , database , path ):
241
+ def run (endpoint , database ):
279
242
with ydb .Driver (
280
243
endpoint = endpoint ,
281
244
database = database ,
@@ -284,26 +247,19 @@ def run(endpoint, database, path):
284
247
driver .wait (timeout = 5 , fail_fast = True )
285
248
286
249
with ydb .QuerySessionPool (driver ) as pool :
250
+ drop_tables (pool )
287
251
288
- ensure_path_exists (driver , database , path )
289
-
290
- # absolute path - prefix to the table's names,
291
- # including the database location
292
- full_path = posixpath .join (database , path )
293
-
294
- drop_tables (pool , full_path )
295
-
296
- create_tables (pool , full_path )
252
+ create_tables (pool )
297
253
298
- fill_tables_with_data (pool , full_path )
254
+ fill_tables_with_data (pool )
299
255
300
- select_simple (pool , full_path )
256
+ select_simple (pool )
301
257
302
- upsert_simple (pool , full_path )
258
+ upsert_simple (pool )
303
259
304
- select_with_parameters (pool , full_path , 2 , 3 , 7 )
305
- select_with_parameters (pool , full_path , 2 , 3 , 8 )
260
+ select_with_parameters (pool , 2 , 3 , 7 )
261
+ select_with_parameters (pool , 2 , 3 , 8 )
306
262
307
- explicit_transaction_control (pool , full_path , 2 , 6 , 1 )
308
- select_with_parameters (pool , full_path , 2 , 6 , 1 )
309
- huge_select (pool , full_path )
263
+ explicit_transaction_control (pool , 2 , 6 , 1 )
264
+ select_with_parameters (pool , 2 , 6 , 1 )
265
+ huge_select (pool )
0 commit comments