The metadata.py module in flowerpower-io provides utility functions for extracting and managing metadata from various data structures. This metadata includes schema information, data dimensions, file paths, and timestamps, which are crucial for understanding data characteristics and lineage.
Functions
get_serializable_schema
get_serializable_schema
Converts the schema (data types) of a given DataFrame or data structure into a serializable dictionary format. This is useful for storing schema information in a persistent, human-readable way.
data: The input data structure (Pandas DataFrame, Polars DataFrame, PyArrow Table, DuckDB Relation, etc.) from which to extract the schema.
Returns:
dict[str, str]: A dictionary where keys are column names and values are their corresponding data types as strings.
Example:
Show the code
import pandas as pdimport polars as plimport pyarrow as pafrom flowerpower_io.metadata import get_serializable_schema# Example with Pandas DataFramedf_pandas = pd.DataFrame({'col1': [1, 2], 'col2': ['A', 'B']})pandas_schema = get_serializable_schema(df_pandas)print("Pandas Schema:", pandas_schema)# Example with Polars DataFramedf_polars = pl.DataFrame({'col3': [True, False], 'col4': [1.1, 2.2]})polars_schema = get_serializable_schema(df_polars)print("Polars Schema:", polars_schema)# Example with PyArrow Tabletable_arrow = pa.table({'col5': [10, 20], 'col6': ['X', 'Y']})pyarrow_schema = get_serializable_schema(table_arrow)print("PyArrow Schema:", pyarrow_schema)
get_dataframe_metadata
get_dataframe_metadata
Generates a comprehensive metadata dictionary for a DataFrame, including information such as path, format, timestamp, schema, number of columns, rows, and files.
**kwargs: Additional key-value pairs to include in the metadata.
Returns:
dict: A dictionary containing the extracted DeltaTable metadata.
Example:
Show the code
# This example requires a Delta Lake table to exist.# For demonstration, we'll show the function call.from deltalake import DeltaTablefrom flowerpower_io.metadata import get_delta_metadataimport osimport shutil# Create a dummy Delta Lake table (requires deltalake package and rust toolchain)# from delta import DeltaTable, configure_spark_with_delta_pip# builder = configure_spark_with_delta_pip()# spark = builder.getOrCreate()# data = spark.range(0, 5).toDF("id")# delta_path = "temp_delta_table"# data.write.format("delta").save(delta_path)# Assuming a DeltaTable exists at 'path_to_delta_table'# delta_table = DeltaTable("path_to_delta_table")# delta_meta = get_delta_metadata(delta_table, path="path_to_delta_table")# print("DeltaTable Metadata:", delta_meta)# Placeholder for demonstration without actual DeltaTable setupprint("DeltaTable example skipped as it requires Delta Lake setup.")
get_mqtt_metadata
get_mqtt_metadata
Generates metadata for MQTT payloads, including topic, format, timestamp, schema, and basic data dimensions. This function requires the orjson library.