|
| 1 | +--- |
| 2 | +sidebar_position: 5 |
| 3 | +--- |
| 4 | + |
| 5 | +# MySQL |
| 6 | + |
| 7 | +`intugle` integrates with MySQL, allowing you to read data from your tables and views. |
| 8 | + |
| 9 | +## Installation |
| 10 | + |
| 11 | +To use `intugle` with MySQL, you must install the optional dependencies: |
| 12 | + |
| 13 | +```bash |
| 14 | +pip install "intugle[mysql]" |
| 15 | +``` |
| 16 | + |
| 17 | +This installs the `PyMySQL` and `sqlglot` libraries. |
| 18 | + |
| 19 | +## Configuration |
| 20 | + |
| 21 | +To connect to your MySQL database, you must provide connection credentials in a `profiles.yml` file at the root of your project. The adapter looks for a top-level `mysql:` key. |
| 22 | + |
| 23 | +**Example `profiles.yml`:** |
| 24 | + |
| 25 | +```yaml |
| 26 | +mysql: |
| 27 | + host: <your_mysql_host> |
| 28 | + port: 3306 # Default MySQL port |
| 29 | + user: <your_username> |
| 30 | + password: <your_password> |
| 31 | + database: <your_database_name> |
| 32 | +``` |
| 33 | +
|
| 34 | +## Usage |
| 35 | +
|
| 36 | +### Reading Data from MySQL |
| 37 | +
|
| 38 | +To include a MySQL table or view in your `SemanticModel`, define it in your input dictionary with `type: "mysql"` and use the `identifier` key to specify the object name. |
| 39 | + |
| 40 | +:::caution Important |
| 41 | +The dictionary key for your dataset (e.g., `"CUSTOMERS"`) must exactly match the table or view name specified in the `identifier`. |
| 42 | +::: |
| 43 | + |
| 44 | +```python |
| 45 | +from intugle import SemanticModel |
| 46 | +
|
| 47 | +datasets = { |
| 48 | + "CUSTOMERS": { |
| 49 | + "identifier": "CUSTOMERS", # Must match the key above |
| 50 | + "type": "mysql" |
| 51 | + }, |
| 52 | + "ORDERS_VIEW": { |
| 53 | + "identifier": "ORDERS_VIEW", # Can be a view |
| 54 | + "type": "mysql" |
| 55 | + } |
| 56 | +} |
| 57 | +
|
| 58 | +# Initialize the semantic model |
| 59 | +sm = SemanticModel(datasets, domain="E-commerce") |
| 60 | +
|
| 61 | +# Build the model as usual |
| 62 | +sm.build() |
| 63 | +``` |
| 64 | + |
| 65 | +### Materializing Data Products |
| 66 | + |
| 67 | +When you use the `DataProduct` class with a MySQL connection, the resulting data product can be materialized as a new **table** or **view** directly within your target database. |
| 68 | + |
| 69 | +```python |
| 70 | +from intugle import DataProduct |
| 71 | +
|
| 72 | +etl_model = { |
| 73 | + "name": "top_customers", |
| 74 | + "fields": [ |
| 75 | + {"id": "CUSTOMERS.customer_id", "name": "customer_id"}, |
| 76 | + {"id": "CUSTOMERS.name", "name": "customer_name"}, |
| 77 | + ] |
| 78 | +} |
| 79 | +
|
| 80 | +dp = DataProduct() |
| 81 | +
|
| 82 | +# Materialize as a view (default) |
| 83 | +dp.build(etl_model, materialize="view") |
| 84 | +
|
| 85 | +# Materialize as a table |
| 86 | +dp.build(etl_model, materialize="table") |
| 87 | +``` |
0 commit comments