-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CrateDB connector #3
base: cratedb
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
--- | ||
title: "CrateDB" | ||
sidebarTitle: "CrateDB" | ||
--- | ||
|
||
## Credentials | ||
|
||
Open the file named `io_config.yaml` at the root of your Mage project and enter cratedb required fields: | ||
|
||
```yaml | ||
version: 0.1.1 | ||
default: | ||
CRATEDB_COLLECTION: collection_name | ||
CRATEDB_PATH: path of the cratedb persisitant storage | ||
``` | ||
|
||
## Dependencies | ||
|
||
The dependency libraries are not installed in the docker image by default. You'll need to add the libraries to | ||
project `requirements.txt` file manually and install them. | ||
|
||
``` | ||
cratedb-client==1.6.9 | ||
sentence-transformers==2.2.2 | ||
``` | ||
|
||
## Using Python block | ||
|
||
1. Create a new pipeline or open an existing pipeline. | ||
2. Add a data loader, transformer, or data exporter block (the code snippet | ||
below is for a data loader). | ||
3. Select `Generic (no template)`. | ||
4. Enter this code snippet (note: change the `config_profile` from `default` if | ||
you have a different profile): | ||
|
||
```python | ||
from mage_ai.settings.repo import get_repo_path | ||
from mage_ai.io.config import ConfigFileLoader | ||
from mage_ai.io.postgres import Postgres | ||
from os import path | ||
from pandas import DataFrame | ||
|
||
if 'data_loader' not in globals(): | ||
from mage_ai.data_preparation.decorators import data_loader | ||
|
||
|
||
@data_loader | ||
def load_data_from_postgres(**kwargs) -> DataFrame: | ||
query = 'SELECT 1' | ||
config_path = path.join(get_repo_path(), 'io_config.yaml') | ||
config_profile = 'default' | ||
|
||
with Postgres.with_config(ConfigFileLoader(config_path, config_profile)) as loader: | ||
return loader.load(query) | ||
``` | ||
|
||
5. Run the block. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -206,6 +206,13 @@ | |
name='Chroma', | ||
path='data_loaders/chroma.py', | ||
), | ||
dict( | ||
block_type=BlockType.DATA_LOADER, | ||
groups=[GROUP_DATABASES], | ||
language=BlockLanguage.PYTHON, | ||
name='CrateDB', | ||
path='data_loaders/cratedb.py', | ||
), | ||
Comment on lines
+209
to
+215
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah wow. The framework allows database adapters to be written in other languages than Python? |
||
dict( | ||
block_type=BlockType.DATA_LOADER, | ||
groups=[GROUP_DATABASES], | ||
|
@@ -594,6 +601,13 @@ | |
name='Chroma', | ||
path='data_exporters/chroma.py', | ||
), | ||
dict( | ||
block_type=BlockType.DATA_EXPORTER, | ||
groups=[GROUP_DATABASES], | ||
language=BlockLanguage.PYTHON, | ||
name='CrateDB', | ||
path='data_exporters/cratedb.py', | ||
), | ||
dict( | ||
block_type=BlockType.DATA_EXPORTER, | ||
groups=[GROUP_DATABASES], | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from mage_ai.settings.repo import get_repo_path | ||
from mage_ai.io.config import ConfigFileLoader | ||
from mage_ai.io.cratedb import CrateDB | ||
from pandas import DataFrame | ||
from os import path | ||
|
||
if 'data_exporter' not in globals(): | ||
from mage_ai.data_preparation.decorators import data_exporter | ||
|
||
|
||
@data_exporter | ||
def export_data_to_cratedb(df: DataFrame, **kwargs) -> None: | ||
""" | ||
Template for exporting data to a PostgreSQL database. | ||
Specify your configuration settings in 'io_config.yaml'. | ||
|
||
Docs: https://docs.mage.ai/design/data-loading#postgresql | ||
""" | ||
Comment on lines
+13
to
+18
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Docstrings also need a few more adjustments, but sure all that can happen after merging to |
||
schema_name = 'your_schema_name' # Specify the name of the schema to export data to | ||
table_name = 'your_table_name' # Specify the name of the table to export data to | ||
config_path = path.join(get_repo_path(), 'io_config.yaml') | ||
config_profile = 'default' | ||
|
||
with CrateDB.with_config(ConfigFileLoader(config_path, config_profile)) as loader: | ||
loader.export( | ||
df, | ||
schema_name, | ||
table_name, | ||
index=False, # Specifies whether to include index in exported table | ||
if_exists='replace', # Specify resolution policy if table name already exists | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{% extends "data_loaders/default.jinja" %} | ||
{% block imports %} | ||
from mage_ai.settings.repo import get_repo_path | ||
from mage_ai.io.config import ConfigFileLoader | ||
from mage_ai.io.cratedb import CrateDB | ||
from os import path | ||
{{ super() -}} | ||
{% endblock %} | ||
|
||
|
||
{% block content %} | ||
@data_loader | ||
def load_data_from_cratedb(*args, **kwargs): | ||
""" | ||
Template for loading data from a PostgreSQL database. | ||
Specify your configuration settings in 'io_config.yaml'. | ||
|
||
Docs: https://docs.mage.ai/design/data-loading#postgresql | ||
""" | ||
query = 'your CrateDB query' # Specify your SQL query here | ||
config_path = path.join(get_repo_path(), 'io_config.yaml') | ||
config_profile = 'default' | ||
|
||
with CrateDB.with_config(ConfigFileLoader(config_path, config_profile)) as loader: | ||
return loader.load(query) | ||
{% endblock %} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe those are not needed at all?