-
Notifications
You must be signed in to change notification settings - Fork 58
Fix StaticCredentials and DriverConfig collision #498
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 8 commits
95e04f9
598776e
78c3a5c
aaf60a8
c41c7ae
30fbf40
d36e407
f24ae46
e3a2f98
dff7303
257f5c4
ae3f8f9
ff72d74
4675d57
df79b17
eaf6807
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,155 +1,8 @@ | ||
Examples | ||
=============== | ||
|
||
Basic example | ||
^^^^^^^^^^^^^ | ||
|
||
All examples in this section are parts of `basic example <https://github.com/ydb-platform/ydb-python-sdk/tree/main/examples/basic_example_v2>`_. | ||
|
||
For deeper upderstanding it is better to read the whole example. | ||
|
||
Create table | ||
------------ | ||
|
||
.. code-block:: python | ||
|
||
def create_tables(pool: ydb.QuerySessionPool): | ||
print("\nCreating table series...") | ||
pool.execute_with_retries( | ||
""" | ||
CREATE table `series` ( | ||
`series_id` Int64, | ||
`title` Utf8, | ||
`series_info` Utf8, | ||
`release_date` Date, | ||
PRIMARY KEY (`series_id`) | ||
) | ||
""" | ||
) | ||
|
||
print("\nCreating table seasons...") | ||
pool.execute_with_retries( | ||
""" | ||
CREATE table `seasons` ( | ||
`series_id` Int64, | ||
`season_id` Int64, | ||
`title` Utf8, | ||
`first_aired` Date, | ||
`last_aired` Date, | ||
PRIMARY KEY (`series_id`, `season_id`) | ||
) | ||
""" | ||
) | ||
|
||
print("\nCreating table episodes...") | ||
pool.execute_with_retries( | ||
""" | ||
CREATE table `episodes` ( | ||
`series_id` Int64, | ||
`season_id` Int64, | ||
`episode_id` Int64, | ||
`title` Utf8, | ||
`air_date` Date, | ||
PRIMARY KEY (`series_id`, `season_id`, `episode_id`) | ||
) | ||
""" | ||
) | ||
|
||
|
||
Upsert Simple | ||
------------- | ||
|
||
.. code-block:: python | ||
|
||
def upsert_simple(pool: ydb.QuerySessionPool): | ||
print("\nPerforming UPSERT into episodes...") | ||
|
||
pool.execute_with_retries( | ||
""" | ||
UPSERT INTO episodes (series_id, season_id, episode_id, title) VALUES (2, 6, 1, "TBD"); | ||
""" | ||
) | ||
|
||
|
||
Simple Select | ||
---------- | ||
|
||
.. code-block:: python | ||
|
||
def select_simple(pool: ydb.QuerySessionPool): | ||
print("\nCheck series table...") | ||
result_sets = pool.execute_with_retries( | ||
""" | ||
SELECT | ||
series_id, | ||
title, | ||
release_date | ||
FROM series | ||
WHERE series_id = 1; | ||
""", | ||
) | ||
first_set = result_sets[0] | ||
for row in first_set.rows: | ||
print( | ||
"series, id: ", | ||
row.series_id, | ||
", title: ", | ||
row.title, | ||
", release date: ", | ||
row.release_date, | ||
) | ||
|
||
return first_set | ||
|
||
Select With Parameters | ||
---------------------- | ||
|
||
.. code-block:: python | ||
|
||
def select_with_parameters(pool: ydb.QuerySessionPool, series_id, season_id, episode_id): | ||
result_sets = pool.execute_with_retries( | ||
""" | ||
DECLARE $seriesId AS Int64; | ||
DECLARE $seasonId AS Int64; | ||
DECLARE $episodeId AS Int64; | ||
|
||
SELECT | ||
title, | ||
air_date | ||
FROM episodes | ||
WHERE series_id = $seriesId AND season_id = $seasonId AND episode_id = $episodeId; | ||
""", | ||
{ | ||
"$seriesId": series_id, # could be defined implicit | ||
"$seasonId": (season_id, ydb.PrimitiveType.Int64), # could be defined via tuple | ||
"$episodeId": ydb.TypedValue(episode_id, ydb.PrimitiveType.Int64), # could be defined via special class | ||
}, | ||
) | ||
|
||
print("\n> select_with_parameters:") | ||
first_set = result_sets[0] | ||
for row in first_set.rows: | ||
print("episode title:", row.title, ", air date:", row.air_date) | ||
|
||
return first_set | ||
|
||
Huge Select | ||
----------- | ||
|
||
.. code-block:: python | ||
|
||
def huge_select(pool: ydb.QuerySessionPool): | ||
def callee(session: ydb.QuerySessionSync): | ||
query = """SELECT * from episodes;""" | ||
|
||
with session.transaction().execute( | ||
query, | ||
commit_tx=True, | ||
) as result_sets: | ||
print("\n> Huge SELECT call") | ||
for result_set in result_sets: | ||
for row in result_set.rows: | ||
print("episode title:", row.title, ", air date:", row.air_date) | ||
|
||
return pool.retry_operation_sync(callee) | ||
.. toctree:: | ||
:maxdepth: 3 | ||
|
||
examples/basic_example | ||
examples/authentication |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
Authentication | ||
============== | ||
Check warning on line 2 in docs/examples/authentication.rst
|
||
|
||
There are several ways to authenticate through YDB Python SDK. | ||
|
||
Anonymous Credentials | ||
--------------------- | ||
|
||
Full executable example `here <https://github.com/ydb-platform/ydb-python-sdk/tree/main/examples/anonymous-credentials>`_. | ||
|
||
.. code-block:: python | ||
|
||
driver = ydb.Driver( | ||
endpoint=os.getenv("YDB_ENDPOINT"), | ||
database=os.getenv("YDB_DATABASE"), | ||
credentials=ydb.AnonymousCredentials(), | ||
) | ||
|
||
|
||
Access Token Credentials | ||
------------------------ | ||
|
||
Full executable example `here <https://github.com/ydb-platform/ydb-python-sdk/tree/main/examples/access-token-credentials>`_. | ||
|
||
.. code-block:: python | ||
|
||
driver = ydb.Driver( | ||
endpoint=os.getenv("YDB_ENDPOINT"), | ||
database=os.getenv("YDB_DATABASE"), | ||
credentials=ydb.AccessTokenCredentials(os.getenv("YDB_ACCESS_TOKEN_CREDENTIALS")), | ||
) | ||
|
||
|
||
Static Credentials (Legacy) | ||
--------------------------- | ||
This method is legacy, use UserPasswordCredentials instead. | ||
|
||
Full executable example `here <https://github.com/ydb-platform/ydb-python-sdk/tree/main/examples/static-credentials>`_. | ||
|
||
|
||
.. code-block:: python | ||
|
||
driver_config = ydb.DriverConfig( | ||
endpoint=endpoint, | ||
database=database, | ||
) | ||
creds = ydb.StaticCredentials( | ||
driver_config=driver_config, | ||
user=user, | ||
password=password, | ||
) | ||
|
||
driver = ydb.Driver( | ||
driver_config=driver_config, | ||
credentials=creds, | ||
) | ||
|
||
|
||
User Password Credentials | ||
------------------------- | ||
|
||
Full executable example `here <https://github.com/ydb-platform/ydb-python-sdk/tree/main/examples/static-credentials>`_. | ||
|
||
.. code-block:: python | ||
|
||
driver_config = ydb.DriverConfig( | ||
endpoint=endpoint, | ||
database=database, | ||
credentials=ydb.UserPasswordCredentials( | ||
user=user, | ||
password=password, | ||
endpoint=endpoint, | ||
database=database, | ||
), | ||
) | ||
|
||
driver = ydb.Driver(driver_config=driver_config) | ||
|
||
|
||
Service Accaount Credentials | ||
---------------------------- | ||
|
||
Full executable example `here <https://github.com/ydb-platform/ydb-python-sdk/tree/main/examples/service-account-credentials>`_. | ||
|
||
.. code-block:: python | ||
|
||
driver = ydb.Driver( | ||
endpoint=os.getenv("YDB_ENDPOINT"), | ||
database=os.getenv("YDB_DATABASE"), | ||
credentials=ydb.iam.ServiceAccountCredentials.from_file( | ||
os.getenv("SA_KEY_FILE"), | ||
), | ||
) | ||
|
||
|
||
OAuth 2.0 Token Exchange Credentials | ||
------------------------------------ | ||
|
||
Full executable example `here <https://github.com/ydb-platform/ydb-python-sdk/tree/main/examples/oauth2-token-exchange-credentials>`_. | ||
|
||
.. code-block:: python | ||
|
||
driver = ydb.Driver( | ||
endpoint=args.endpoint, | ||
database=args.database, | ||
root_certificates=ydb.load_ydb_root_certificate(), | ||
credentials=ydb.oauth2_token_exchange.Oauth2TokenExchangeCredentials( | ||
token_endpoint=args.token_endpoint, | ||
audience=args.audience, | ||
subject_token_source=ydb.oauth2_token_exchange.JwtTokenSource( | ||
signing_method="RS256", | ||
private_key_file=args.private_key_file, | ||
key_id=args.key_id, | ||
issuer=args.issuer, | ||
subject=args.subject, | ||
audience=args.audience, | ||
), | ||
), | ||
) | ||
|
||
|
||
Metadata Credentials | ||
-------------------- | ||
|
||
Full executable example `here <https://github.com/ydb-platform/ydb-python-sdk/tree/main/examples/metadata-credentials>`_. | ||
|
||
.. code-block:: python | ||
|
||
driver = ydb.Driver( | ||
endpoint=os.getenv("YDB_ENDPOINT"), | ||
database=os.getenv("YDB_DATABASE"), | ||
credentials=ydb.iam.MetadataUrlCredentials(), | ||
) |
Uh oh!
There was an error while loading. Please reload this page.