Skip to content

Commit 4d7f051

Browse files
thomasnieblerThomas Nieblerjaidisido
authored
enable list timestream databases and tables (#1345)
* extended timestream.py with two functions to list databases and list tables * added tests for timestream list db and tbl functions * blackened * more extensive tests less assumptions (removed assumed count of dbs of 1) * timestream.py: avoided None error Co-authored-by: Thomas Niebler <[email protected]> Co-authored-by: jaidisido <[email protected]>
1 parent 8a9048c commit 4d7f051

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed

awswrangler/timestream.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,3 +482,75 @@ def delete_table(
482482
"""
483483
client: boto3.client = _utils.client(service_name="timestream-write", session=boto3_session)
484484
client.delete_table(DatabaseName=database, TableName=table)
485+
486+
487+
def list_databases(
488+
boto3_session: Optional[boto3.Session] = None,
489+
) -> List[str]:
490+
"""
491+
List all databases in timestream.
492+
493+
Parameters
494+
----------
495+
boto3_session : boto3.Session(), optional
496+
Boto3 Session. The default boto3 Session will be used if boto3_session receive None.
497+
498+
Returns
499+
-------
500+
List[str]
501+
a list of available timestream databases.
502+
503+
Examples
504+
--------
505+
Querying the list of all available databases
506+
507+
>>> import awswrangler as wr
508+
>>> wr.timestream.list_databases()
509+
... ["database1", "database2"]
510+
511+
512+
"""
513+
client: boto3.client = _utils.client(service_name="timestream-write", session=boto3_session)
514+
dbs = client.list_databases()
515+
return [db["DatabaseName"] for db in dbs["Databases"]]
516+
517+
518+
def list_tables(database: Optional[str] = None, boto3_session: Optional[boto3.Session] = None) -> List[str]:
519+
"""
520+
List tables in timestream.
521+
522+
Parameters
523+
----------
524+
database: str
525+
Database name. If None, all tables in Timestream will be returned. Otherwise, only the tables inside the
526+
given database are returned.
527+
boto3_session : boto3.Session(), optional
528+
Boto3 Session. The default boto3 Session will be used if boto3_session receive None.
529+
530+
Returns
531+
-------
532+
List[str]
533+
A list of table names.
534+
535+
Examples
536+
--------
537+
Listing all tables in timestream across databases
538+
539+
>>> import awswrangler as wr
540+
>>> wr.timestream.list_tables()
541+
... ["table1", "table2"]
542+
543+
Listing all tables in timestream in a specific database
544+
545+
>>> import awswrangler as wr
546+
>>> wr.timestream.list_tables(DatabaseName="database1")
547+
... ["table1"]
548+
549+
"""
550+
client: boto3.client = _utils.client(service_name="timestream-write", session=boto3_session)
551+
if database:
552+
tables = client.list_tables(DatabaseName=database)
553+
else:
554+
tables = client.list_tables()
555+
556+
return [tbl["TableName"] for tbl in tables["Tables"]]

tests/test_timestream.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,3 +211,50 @@ def test_multimeasure_scenario(timestream_database_and_table):
211211
""",
212212
)
213213
assert df.shape == (3, 6)
214+
215+
216+
def test_list_databases(timestream_database_and_table):
217+
dbs = wr.timestream.list_databases()
218+
219+
assert timestream_database_and_table in dbs
220+
dummy_db_name = f"{timestream_database_and_table}_2"
221+
222+
wr.timestream.create_database(dummy_db_name)
223+
dbs_tmp = wr.timestream.list_databases()
224+
225+
assert timestream_database_and_table in dbs_tmp
226+
assert dummy_db_name in dbs_tmp
227+
assert len(dbs_tmp) == len(dbs) + 1
228+
229+
wr.timestream.delete_database(dummy_db_name)
230+
231+
dbs_tmp = wr.timestream.list_databases()
232+
assert timestream_database_and_table in dbs_tmp
233+
assert dummy_db_name not in dbs_tmp
234+
assert len(dbs_tmp) == len(dbs)
235+
assert dbs_tmp == dbs
236+
237+
238+
def test_list_tables(timestream_database_and_table):
239+
all_tables = wr.timestream.list_tables()
240+
241+
assert timestream_database_and_table in all_tables
242+
243+
tables_in_db = wr.timestream.list_tables(database=timestream_database_and_table)
244+
assert timestream_database_and_table in tables_in_db
245+
assert len(tables_in_db) <= len(all_tables)
246+
247+
wr.timestream.create_table(
248+
database=timestream_database_and_table,
249+
table=f"{timestream_database_and_table}_2",
250+
memory_retention_hours=1,
251+
magnetic_retention_days=1,
252+
)
253+
254+
tables_in_db = wr.timestream.list_tables(database=timestream_database_and_table)
255+
assert f"{timestream_database_and_table}_2" in tables_in_db
256+
257+
wr.timestream.delete_table(database=timestream_database_and_table, table=f"{timestream_database_and_table}_2")
258+
259+
tables_in_db = wr.timestream.list_tables(database=timestream_database_and_table)
260+
assert f"{timestream_database_and_table}_2" not in tables_in_db

0 commit comments

Comments
 (0)