Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Deploy Sphinx documentation to Github Pages

on:
push:
branches: [main] # branch to trigger deployment
branches: [main, fix_driver_and_driver_config_collision] # branch to trigger deployment

jobs:
pages:
Expand Down
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
133 changes: 133 additions & 0 deletions docs/examples/authentication.rst
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

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

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 (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(),
)
Loading
Loading