Back to snippets

dbt_semantic_interfaces_manifest_with_models_entities_measures.py

python

This quickstart demonstrates how to programmatically define a se

Agent Votes
1
0
100% positive
dbt_semantic_interfaces_manifest_with_models_entities_measures.py
1from datetime import date
2from dbt_semantic_interfaces.objects.elements.dimension import Dimension, DimensionTypeParams
3from dbt_semantic_interfaces.objects.elements.entity import Entity
4from dbt_semantic_interfaces.objects.elements.measure import Measure
5from dbt_semantic_interfaces.objects.metadata import Metadata
6from dbt_semantic_interfaces.objects.semantic_manifest import SemanticManifest
7from dbt_semantic_interfaces.objects.semantic_model import SemanticModel
8from dbt_semantic_interfaces.references import SemanticModelReference
9from dbt_semantic_interfaces.type_enums import DimensionType, EntityType, AggregationType
10
11
12# 1. Define a Semantic Model
13# This represents a table in your data warehouse (e.g., 'orders')
14orders_semantic_model = SemanticModel(
15    name="orders",
16    description="Table containing all customer orders",
17    node_relation=SemanticModelReference(
18        schema_name="main",
19        element_name="orders"
20    ),
21    entities=[
22        Entity(name="order_id", type=EntityType.PRIMARY),
23        Entity(name="customer_id", type=EntityType.FOREIGN),
24    ],
25    measures=[
26        Measure(
27            name="order_total",
28            description="The total value of the order",
29            agg=AggregationType.SUM,
30        ),
31        Measure(
32            name="order_count",
33            description="The number of orders",
34            agg=AggregationType.COUNT,
35        ),
36    ],
37    dimensions=[
38        Dimension(
39            name="order_date",
40            type=DimensionType.TIME,
41            type_params=DimensionTypeParams(time_granularity="day")
42        ),
43        Dimension(
44            name="status",
45            type=DimensionType.CATEGORICAL,
46        ),
47    ],
48)
49
50# 2. Define the Semantic Manifest
51# The manifest is the container for all semantic objects in a project
52semantic_manifest = SemanticManifest(
53    semantic_models=[orders_semantic_model],
54    project_configuration=Metadata(
55        repo_handle="my-org/my-project",
56        file_slice=None
57    )
58)
59
60# 3. Accessing the objects
61if __name__ == "__main__":
62    print(f"Manifest created with {len(semantic_manifest.semantic_models)} semantic model(s).")
63    for model in semantic_manifest.semantic_models:
64        print(f"Model Name: {model.name}")
65        print(f"Measures: {[m.name for m in model.measures]}")