|
1 | 1 | Examples
|
2 | 2 | ===============
|
3 | 3 |
|
4 |
| -Basic example |
5 |
| -^^^^^^^^^^^^^ |
6 |
| - |
7 |
| -All examples in this section are parts of `basic example <https://github.com/ydb-platform/ydb-python-sdk/tree/main/examples/basic_example_v2>`_. |
8 |
| - |
9 |
| -For deeper upderstanding it is better to read the whole example. |
10 |
| - |
11 |
| -Create table |
12 |
| ------------- |
13 |
| - |
14 |
| -.. code-block:: python |
15 |
| -
|
16 |
| - def create_tables(pool: ydb.QuerySessionPool): |
17 |
| - print("\nCreating table series...") |
18 |
| - pool.execute_with_retries( |
19 |
| - """ |
20 |
| - CREATE table `series` ( |
21 |
| - `series_id` Int64, |
22 |
| - `title` Utf8, |
23 |
| - `series_info` Utf8, |
24 |
| - `release_date` Date, |
25 |
| - PRIMARY KEY (`series_id`) |
26 |
| - ) |
27 |
| - """ |
28 |
| - ) |
29 |
| -
|
30 |
| - print("\nCreating table seasons...") |
31 |
| - pool.execute_with_retries( |
32 |
| - """ |
33 |
| - CREATE table `seasons` ( |
34 |
| - `series_id` Int64, |
35 |
| - `season_id` Int64, |
36 |
| - `title` Utf8, |
37 |
| - `first_aired` Date, |
38 |
| - `last_aired` Date, |
39 |
| - PRIMARY KEY (`series_id`, `season_id`) |
40 |
| - ) |
41 |
| - """ |
42 |
| - ) |
43 |
| -
|
44 |
| - print("\nCreating table episodes...") |
45 |
| - pool.execute_with_retries( |
46 |
| - """ |
47 |
| - CREATE table `episodes` ( |
48 |
| - `series_id` Int64, |
49 |
| - `season_id` Int64, |
50 |
| - `episode_id` Int64, |
51 |
| - `title` Utf8, |
52 |
| - `air_date` Date, |
53 |
| - PRIMARY KEY (`series_id`, `season_id`, `episode_id`) |
54 |
| - ) |
55 |
| - """ |
56 |
| - ) |
57 |
| -
|
58 |
| -
|
59 |
| -Upsert Simple |
60 |
| -------------- |
61 |
| - |
62 |
| -.. code-block:: python |
63 |
| -
|
64 |
| - def upsert_simple(pool: ydb.QuerySessionPool): |
65 |
| - print("\nPerforming UPSERT into episodes...") |
66 |
| -
|
67 |
| - pool.execute_with_retries( |
68 |
| - """ |
69 |
| - UPSERT INTO episodes (series_id, season_id, episode_id, title) VALUES (2, 6, 1, "TBD"); |
70 |
| - """ |
71 |
| - ) |
72 |
| -
|
73 |
| -
|
74 |
| -Simple Select |
75 |
| ----------- |
76 |
| - |
77 |
| -.. code-block:: python |
78 |
| -
|
79 |
| - def select_simple(pool: ydb.QuerySessionPool): |
80 |
| - print("\nCheck series table...") |
81 |
| - result_sets = pool.execute_with_retries( |
82 |
| - """ |
83 |
| - SELECT |
84 |
| - series_id, |
85 |
| - title, |
86 |
| - release_date |
87 |
| - FROM series |
88 |
| - WHERE series_id = 1; |
89 |
| - """, |
90 |
| - ) |
91 |
| - first_set = result_sets[0] |
92 |
| - for row in first_set.rows: |
93 |
| - print( |
94 |
| - "series, id: ", |
95 |
| - row.series_id, |
96 |
| - ", title: ", |
97 |
| - row.title, |
98 |
| - ", release date: ", |
99 |
| - row.release_date, |
100 |
| - ) |
101 |
| -
|
102 |
| - return first_set |
103 |
| -
|
104 |
| -Select With Parameters |
105 |
| ----------------------- |
106 |
| - |
107 |
| -.. code-block:: python |
108 |
| -
|
109 |
| - def select_with_parameters(pool: ydb.QuerySessionPool, series_id, season_id, episode_id): |
110 |
| - result_sets = pool.execute_with_retries( |
111 |
| - """ |
112 |
| - DECLARE $seriesId AS Int64; |
113 |
| - DECLARE $seasonId AS Int64; |
114 |
| - DECLARE $episodeId AS Int64; |
115 |
| -
|
116 |
| - SELECT |
117 |
| - title, |
118 |
| - air_date |
119 |
| - FROM episodes |
120 |
| - WHERE series_id = $seriesId AND season_id = $seasonId AND episode_id = $episodeId; |
121 |
| - """, |
122 |
| - { |
123 |
| - "$seriesId": series_id, # could be defined implicit |
124 |
| - "$seasonId": (season_id, ydb.PrimitiveType.Int64), # could be defined via tuple |
125 |
| - "$episodeId": ydb.TypedValue(episode_id, ydb.PrimitiveType.Int64), # could be defined via special class |
126 |
| - }, |
127 |
| - ) |
128 |
| -
|
129 |
| - print("\n> select_with_parameters:") |
130 |
| - first_set = result_sets[0] |
131 |
| - for row in first_set.rows: |
132 |
| - print("episode title:", row.title, ", air date:", row.air_date) |
133 |
| -
|
134 |
| - return first_set |
135 |
| -
|
136 |
| -Huge Select |
137 |
| ------------ |
138 |
| - |
139 |
| -.. code-block:: python |
140 |
| -
|
141 |
| - def huge_select(pool: ydb.QuerySessionPool): |
142 |
| - def callee(session: ydb.QuerySessionSync): |
143 |
| - query = """SELECT * from episodes;""" |
144 |
| -
|
145 |
| - with session.transaction().execute( |
146 |
| - query, |
147 |
| - commit_tx=True, |
148 |
| - ) as result_sets: |
149 |
| - print("\n> Huge SELECT call") |
150 |
| - for result_set in result_sets: |
151 |
| - for row in result_set.rows: |
152 |
| - print("episode title:", row.title, ", air date:", row.air_date) |
153 |
| -
|
154 |
| - return pool.retry_operation_sync(callee) |
| 4 | +.. toctree:: |
| 5 | + :maxdepth: 3 |
155 | 6 |
|
| 7 | + examples/basic_example |
| 8 | + examples/authentication |
0 commit comments