Skip to content

Commit

Permalink
Merge pull request #498 from ydb-platform/fix_driver_and_driver_confi…
Browse files Browse the repository at this point in the history
…g_collision

Fix StaticCredentials and DriverConfig collision
  • Loading branch information
vgvoleg authored Oct 4, 2024
2 parents ede35b0 + eaf6807 commit 3000ae3
Show file tree
Hide file tree
Showing 9 changed files with 410 additions and 161 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ jobs:
id-token: write
steps:
- id: deployment
uses: sphinx-notes/pages@v3
uses: sphinx-notes/pages@v3
155 changes: 4 additions & 151 deletions docs/examples.rst
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
104 changes: 104 additions & 0 deletions docs/examples/authentication.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
Authentication
==============

Check warning on line 2 in docs/examples/authentication.rst

View workflow job for this annotation

GitHub Actions / pages

Duplicate explicit target name: "here".

Check warning on line 2 in docs/examples/authentication.rst

View workflow job for this annotation

GitHub Actions / pages

Duplicate explicit target name: "here".

Check warning on line 2 in docs/examples/authentication.rst

View workflow job for this annotation

GitHub Actions / pages

Duplicate explicit target name: "here".

Check warning on line 2 in docs/examples/authentication.rst

View workflow job for this annotation

GitHub Actions / pages

Duplicate explicit target name: "here".

Check warning on line 2 in docs/examples/authentication.rst

View workflow job for this annotation

GitHub Actions / pages

Duplicate explicit target name: "here".

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
---------------------------

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.StaticCredentials.from_user_password(user, password),
)
driver = ydb.Driver(driver_config=driver_config)
Service Account 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(),
)
Loading

0 comments on commit 3000ae3

Please sign in to comment.