From c71ea4a8c02d0f7d41a4772eab6cd13372f56685 Mon Sep 17 00:00:00 2001 From: Andreas Motl Date: Fri, 24 Nov 2023 23:07:05 +0100 Subject: [PATCH] Examples: Use `refresh_table` to synchronize CrateDB write operations --- examples/tracking_merlion.py | 17 +++++++++++++++++ examples/tracking_pycaret.py | 10 +++++++--- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/examples/tracking_merlion.py b/examples/tracking_merlion.py index 59cb060..7f31545 100644 --- a/examples/tracking_merlion.py +++ b/examples/tracking_merlion.py @@ -112,6 +112,20 @@ def import_data(data_table_name: str, anomalies_table_name: str): ) +def refresh_table(table_name: str): + """ + Flush/Synchronize CrateDB write operations. + Refresh the table, to make sure the data is up-to-date. + + https://cratedb.com/docs/crate/reference/en/latest/sql/statements/refresh.html + """ + + with connect_database() as conn: + cursor = conn.cursor() + cursor.execute(f"REFRESH TABLE {table_name}") + cursor.close() + + def read_data(table_name: str) -> pd.DataFrame: """ Read data from database into pandas DataFrame. @@ -249,6 +263,9 @@ def main(): if not table_exists(data_table): import_data(data_table, anomalies_table) + # Flush/Synchronize CrateDB write operations. + refresh_table(data_table) + # Read data into pandas DataFrame. data = read_data(data_table) diff --git a/examples/tracking_pycaret.py b/examples/tracking_pycaret.py index 660c836..fda19d2 100644 --- a/examples/tracking_pycaret.py +++ b/examples/tracking_pycaret.py @@ -122,8 +122,12 @@ def import_data(data_table_name: str): def refresh_table(table_name: str): - """Refresh the table, to make sure the data is up to date. - Required due to crate being eventually consistent.""" + """ + Flush/Synchronize CrateDB write operations. + Refresh the table, to make sure the data is up-to-date. + + https://cratedb.com/docs/crate/reference/en/latest/sql/statements/refresh.html + """ with connect_database() as conn: cursor = conn.cursor() @@ -204,7 +208,7 @@ def main(): if not table_exists(data_table): import_data(data_table) - # Refresh the table - due to crate's eventual consistency. + # Flush/Synchronize CrateDB write operations. refresh_table(data_table) # Read data into pandas DataFrame.