1. First feature¶
Reference
The reference for this topic is First feature.
Goal: turn a SQL query on the events table into registered features you can re-run, version and share. Five steps, one screen of code.
import dataclasses
import getpass
import os
import tdfs4ds # importing it first also loads a .env file, if you have one
import teradataml as tdml
# 1. Open the Teradata session. Host and user come from the environment (TDFS4DS_TD_HOST,
# TDFS4DS_TD_USERNAME), the password from TDFS4DS_TD_PASSWORD or a prompt: nothing secret
# is written on this page.
host = os.environ["TDFS4DS_TD_HOST"]
user = os.environ["TDFS4DS_TD_USERNAME"]
password = os.environ.get("TDFS4DS_TD_PASSWORD") or getpass.getpass("Teradata password: ")
tdml.create_context(host=host, username=user, password=password)
# 2. Pick your industry. This is the ONLY line to change: retail, banking, healthcare, pharma,
# manufacturing, assembly, energy or transportation. Every name below comes from S.
S = tdfs4ds.tutorial_scenario("retail")
# 3. Connect to the feature store (created when missing) and choose a data domain. disposable=True
# records that this domain is an experiment, so the last page removes it in one call.
DATABASE = os.environ.get("TDFS4DS_TD_DATABASE") or user
tdfs4ds.connect(database=DATABASE, create_if_missing=True)
tdfs4ds.create_data_domain(f"TUTORIAL_{S.name.upper()}", disposable=True) # an experiment: removable in one call
# 4. The tutorial data: three tables written once; a second run keeps them.
try:
S = tdfs4ds.create_tutorial_data(S.name, DATABASE)
except ValueError:
S = dataclasses.replace(S, database=DATABASE)
def register_entity_features():
"""Page 1 in one call: (re)register the entity features and return the process id."""
sql = f"""
SELECT {S.entity}, COUNT(*) AS nb_events, SUM({S.amount}) AS total_amount,
AVG({S.amount}) AS avg_amount, COUNT(DISTINCT {S.partner}) AS nb_partners
FROM {S.table('events')}
GROUP BY 1"""
return tdfs4ds.upload_features(
df=None, sql_query=sql, target_view_name=f"{S.database}.{S.name}_entity_features",
entity_id=[S.entity], feature_names=["nb_events", "total_amount", "avg_amount", "nb_partners"],
replace=True, document=False, optimize_query=False, return_process_id=True)
You should see the connection, the data domain TUTORIAL_<FLAVOR> and the three
tutorial tables written (a second run keeps them). Change "retail" in the block to pick
another industry.
The shape of the workflow¶
- Connect with
tdfs4ds.connect(...)(done above). The first call also creates the catalog tables. - Pick a data domain with
create_data_domain(...)(done above). Every feature, process and dataset is tagged with it, which keeps unrelated projects apart in one database. - Write a SQL query with the entity key and one column per feature.
- Call
upload_featureswith the entity key and the feature names. It registers the view as a process, stores its metadata in the catalogs, runs it once and merges the values into the feature tables. - Re-run later with
run(process_id); the view is first-class, no Python needs to be kept.
Steps 3 and 4¶
sql = f"""
SELECT {S.entity}
, COUNT(*) AS nb_events
, SUM({S.amount}) AS total_amount
, AVG({S.amount}) AS avg_amount
, COUNT(DISTINCT {S.partner}) AS nb_partners
FROM {S.table('events')}
GROUP BY 1"""
process_id = tdfs4ds.upload_features(
df=None,
sql_query=sql,
target_view_name=f"{S.database}.{S.name}_entity_features",
entity_id=[S.entity],
feature_names=["nb_events", "total_amount", "avg_amount", "nb_partners"],
replace=True, # re-registers the same process when run twice
document=False, optimize_query=False, # modules 2 and 3 switch these on
return_process_id=True,
)
print(process_id)
Output: a process id (a UUID). The log lists the view created, the four features registered and the run completed.
Step 5: the process is a handle¶
tdfs4ds.run(process_id=process_id, force_compute=True)
print(tdfs4ds.get_process_info(process_id)["VIEW_NAME"])
Output: the run log again, then the name of the process view:
Why the data domain matters¶
A single Teradata database can host the feature stores of several teams or modelling exercises. The data domain is the namespace that keeps their catalogs apart. Set it before any registration call: otherwise features land in the previous domain and become hard to find later.
Clean up¶
Keep the process: the next modules reuse it. At the very end, module 13 lists everything to remove.
Learn more¶
- Installation:
pip install, optional extras, and which dependencies are lazy. - Connect and configure:
connect()vssetup(),.envand JSON config files. - First feature: the same workflow with more detail.
- Recipes › One-line bootstrap: a minimal
.env-driven launch. - Data domains: isolation semantics.