Skip to content

Commit

Permalink
Examples: Use refresh_table to synchronize CrateDB write operations
Browse files Browse the repository at this point in the history
  • Loading branch information
amotl committed Nov 24, 2023
1 parent e965e3e commit c71ea4a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
17 changes: 17 additions & 0 deletions examples/tracking_merlion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)

Expand Down
10 changes: 7 additions & 3 deletions examples/tracking_pycaret.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit c71ea4a

Please sign in to comment.