|
| 1 | +import tempfile |
| 2 | +import uuid |
| 3 | +from datetime import datetime |
| 4 | +from os import path |
| 5 | +from urllib.parse import urlparse |
| 6 | + |
| 7 | +import numpy as np |
| 8 | +import pandas as pd |
| 9 | +import pytest |
| 10 | +from google.cloud import storage |
| 11 | +from pytz import utc |
| 12 | + |
| 13 | +from feast.pyspark.abc import RetrievalJobParameters |
| 14 | + |
| 15 | + |
| 16 | +@pytest.fixture(scope="module") |
| 17 | +def customer_entity() -> pd.DataFrame: |
| 18 | + return pd.DataFrame( |
| 19 | + np.array([[1001, datetime(year=2020, month=9, day=1, tzinfo=utc)]]), |
| 20 | + columns=["customer_id", "event_timestamp"], |
| 21 | + ) |
| 22 | + |
| 23 | + |
| 24 | +@pytest.fixture(scope="module") |
| 25 | +def customer_feature() -> pd.DataFrame: |
| 26 | + return pd.DataFrame( |
| 27 | + np.array( |
| 28 | + [ |
| 29 | + [ |
| 30 | + 1001, |
| 31 | + 100.0, |
| 32 | + datetime(year=2020, month=9, day=1, tzinfo=utc), |
| 33 | + datetime(year=2020, month=9, day=1, tzinfo=utc), |
| 34 | + ], |
| 35 | + ] |
| 36 | + ), |
| 37 | + columns=[ |
| 38 | + "customer_id", |
| 39 | + "total_transactions", |
| 40 | + "event_timestamp", |
| 41 | + "created_timestamp", |
| 42 | + ], |
| 43 | + ) |
| 44 | + |
| 45 | + |
| 46 | +def upload_dataframe_to_gcs_as_parquet(df: pd.DataFrame, staging_location: str): |
| 47 | + gcs_client = storage.Client() |
| 48 | + staging_location_uri = urlparse(staging_location) |
| 49 | + staging_bucket = staging_location_uri.netloc |
| 50 | + remote_path = staging_location_uri.path.lstrip("/") |
| 51 | + gcs_bucket = gcs_client.get_bucket(staging_bucket) |
| 52 | + temp_dir = str(uuid.uuid4()) |
| 53 | + df_remote_path = path.join(remote_path, temp_dir) |
| 54 | + blob = gcs_bucket.blob(df_remote_path) |
| 55 | + with tempfile.NamedTemporaryFile() as df_local_path: |
| 56 | + df.to_parquet(df_local_path.name) |
| 57 | + blob.upload_from_filename(df_local_path.name) |
| 58 | + return path.join(staging_location, df_remote_path) |
| 59 | + |
| 60 | + |
| 61 | +def new_retrieval_job_params( |
| 62 | + entity_source_uri: str, feature_source_uri: str, destination_uri: str |
| 63 | +) -> RetrievalJobParameters: |
| 64 | + entity_source = { |
| 65 | + "file": { |
| 66 | + "format": "parquet", |
| 67 | + "path": entity_source_uri, |
| 68 | + "event_timestamp_column": "event_timestamp", |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + feature_tables_sources = [ |
| 73 | + { |
| 74 | + "file": { |
| 75 | + "format": "parquet", |
| 76 | + "path": feature_source_uri, |
| 77 | + "event_timestamp_column": "event_timestamp", |
| 78 | + "created_timestamp_column": "created_timestamp", |
| 79 | + } |
| 80 | + } |
| 81 | + ] |
| 82 | + |
| 83 | + feature_tables = [ |
| 84 | + { |
| 85 | + "name": "customer_transactions", |
| 86 | + "entities": [{"name": "customer", "type": "int32"}], |
| 87 | + } |
| 88 | + ] |
| 89 | + |
| 90 | + destination = {"format": "parquet", "path": destination_uri} |
| 91 | + |
| 92 | + return RetrievalJobParameters( |
| 93 | + feature_tables=feature_tables, |
| 94 | + feature_tables_sources=feature_tables_sources, |
| 95 | + entity_source=entity_source, |
| 96 | + destination=destination, |
| 97 | + ) |
| 98 | + |
| 99 | + |
| 100 | +@pytest.fixture(scope="module") |
| 101 | +def dataproc_retrieval_job_params( |
| 102 | + pytestconfig, customer_entity, customer_feature |
| 103 | +) -> RetrievalJobParameters: |
| 104 | + staging_location = pytestconfig.getoption("--dataproc-staging-location") |
| 105 | + entity_source_uri = upload_dataframe_to_gcs_as_parquet( |
| 106 | + customer_entity, staging_location |
| 107 | + ) |
| 108 | + feature_source_uri = upload_dataframe_to_gcs_as_parquet( |
| 109 | + customer_feature, staging_location |
| 110 | + ) |
| 111 | + destination_uri = path.join(staging_location, str(uuid.uuid4())) |
| 112 | + |
| 113 | + return new_retrieval_job_params( |
| 114 | + entity_source_uri, feature_source_uri, destination_uri |
| 115 | + ) |
0 commit comments