Creating a Semantic Model using Snowflake's Semantic Model Generator
Rany ElHousieny, PhD???
Generative AI ENGINEERING MANAGER | ex-Microsoft | AI Solutions Architect | Generative AI & NLP Expert | Proven Leader in AI-Driven Innovation | Former Microsoft Research & Azure AI | Software Engineering Manager
A semantic model is a structured representation of data that defines the meaning, relationships, and rules within a specific domain. It is used to create a shared understanding of data across different systems and users, enabling consistent interpretation and analysis. Semantic models are commonly used in data warehousing, business intelligence, and analytics to ensure that data is accurately and consistently represented and queried.
Creating a Semantic Model using Snowflake's Semantic Model Generator
The Semantic Model Generator by Snowflake Labs is a tool that helps you create semantic models in Snowflake. Here’s a step-by-step guide to creating a semantic model using this tool:
1. Install the Tool:
First, you need to clone the repository and install the necessary dependencies. You can do this using the following commands:
git clone https://github.com/Snowflake-Labs/semantic-model-generator.git
cd semantic-model-generator
Create Environment
pyenv virtualenv 3.11.6 SnowFlakeAI
Restart the terminal or quit and start a new one
Activate the Env
pyenv activate SnowFlakeAI
Start VSCode
Create a temp (or any name) directory on the root
Inside temp, create two files:
领英推荐
Setting Environment Variables
Set the necessary environment variables for your Snowflake connection using the export commands:
creds.sh the has the following
export SNOWFLAKE_ROLE="<your-snowflake-role>"
export SNOWFLAKE_WAREHOUSE="<your-snowflake-warehouse>"
export SNOWFLAKE_USER="<your-snowflake-user>"
export SNOWFLAKE_PASSWORD="<your-snowflake-password>"
export SNOWFLAKE_ACCOUNT_LOCATOR="<your-snowflake-account-locator>"
export SNOWFLAKE_HOST="<your-snowflake-host>"
If you are using SSO, you will have a file as follows:
export SNOWFLAKE_ROLE="<your-snowflake-role>"
export SNOWFLAKE_WAREHOUSE="<your-snowflake-warehouse>"
export SNOWFLAKE_USER="<your-snowflake-user>"
export SNOWFLAKE_ACCOUNT_LOCATOR="<your-snowflake-account-locator>"
export SNOWFLAKE_HOST="<your-snowflake-host>"
export SNOWFLAKE_AUTHENTICATOR="externalbrowser"
Make sure to replace <your-snowflake-account-locator> and <your-snowflake-host> with your actual Snowflake details.
Using Python to Generate the Model with OKTA SSO
pip install dist/*.whl
Generate the Semantic Model:
Run the following Python code to generate the semantic model:
generate_semantic_model.py
from semantic_model_generator.generate_model import generate_base_semantic_model_from_snowflake
BASE_TABLES = ['<your-database-name-1>.<your-schema-name-1>.<your-base-table-or-view-name-1>','<your-database-name-2>.<your-schema-name-2>.<your-base-table-or-view-name-2>']
SNOWFLAKE_ACCOUNT = "<your-snowflake-account>"
SEMANTIC_MODEL_NAME = "<a-meaningful-semantic-model-name>"
generate_base_semantic_model_from_snowflake(
base_tables=BASE_TABLES,
snowflake_account=SNOWFLAKE_ACCOUNT,
semantic_model_name=SEMANTIC_MODEL_NAME,
)
cd temp
python generate_semantic_model.py
The generated YAML model will be under the directory temp where you executed generate_semantic_model.py