-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: Export from InfluxDB and import into RDBMS
Using SQLAlchemy/pandas/Dask.
- Loading branch information
Showing
21 changed files
with
791 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,6 @@ | |
.DS_Store | ||
__pycache__ | ||
*.egg-info | ||
.coverage | ||
.coverage* | ||
coverage.xml | ||
/example_*.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
""" | ||
Miniature data pipeline shuffling data from InfluxDB to PostgreSQL/CrateDB. | ||
- Load a synthetic pandas DataFrame into InfluxDB. | ||
- Export data to InfluxDB line protocol format (ILP). | ||
- Read back a few samples worth of data from the ILP file. | ||
https://docs.influxdata.com/influxdb/latest/reference/syntax/line-protocol/ | ||
""" | ||
import gzip | ||
import logging | ||
from pathlib import Path | ||
|
||
from influxio.io import dataframe_from_lineprotocol | ||
from influxio.model import InfluxDbAdapter | ||
from influxio.testdata import DataFrameFactory | ||
from influxio.util.common import setup_logging | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
LINEPROTOCOL_FILE = Path("./var/export/demo.lp.gz") | ||
DATASET_SIZE = 15_000 | ||
|
||
|
||
def main(): | ||
logger.info("Connecting to InfluxDB") | ||
influx = InfluxDbAdapter( | ||
url="http://localhost:8086", | ||
org="example", | ||
token="token", # noqa: S106 | ||
bucket="testdrive", | ||
measurement="demo", | ||
) | ||
|
||
# Provision data to source database. | ||
logger.info("Provisioning InfluxDB") | ||
dff = DataFrameFactory(rows=DATASET_SIZE) | ||
df = dff.make("dateindex") | ||
influx.write_df(df) | ||
|
||
# Export data into file using lineprotocol format. | ||
logger.info("Exporting data to lineprotocol file (ILP)") | ||
LINEPROTOCOL_FILE.parent.mkdir(parents=True, exist_ok=True) | ||
influx.to_lineprotocol(engine_path="./var/lib/influxdb2/engine", output_path=LINEPROTOCOL_FILE) | ||
|
||
logger.info("Reading back data from lineprotocol file") | ||
with gzip.open(LINEPROTOCOL_FILE) as buffer: | ||
df = dataframe_from_lineprotocol(buffer) | ||
print(df) # noqa: T201 | ||
|
||
logger.info("Ready.") | ||
|
||
|
||
if __name__ == "__main__": | ||
setup_logging() | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
""" | ||
Miniature data pipeline shuffling data from InfluxDB to PostgreSQL/CrateDB. | ||
- Load a synthetic pandas DataFrame into InfluxDB. | ||
- Transfer data from InfluxDB to RDBMS database using SQLAlchemy/pandas/Dask. | ||
- Read back a few samples worth of data from the RDBMS database. | ||
""" | ||
import logging | ||
|
||
import sqlalchemy as sa | ||
|
||
from influxio.io import dataframe_to_sql | ||
from influxio.model import InfluxDbAdapter | ||
from influxio.testdata import DataFrameFactory | ||
from influxio.util.common import jd, setup_logging | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
DBURI = "crate://localhost:4200" | ||
DATASET_SIZE = 15_000 | ||
|
||
|
||
def main(): | ||
logger.info("Connecting to InfluxDB") | ||
influx = InfluxDbAdapter( | ||
url="http://localhost:8086", | ||
org="example", | ||
token="token", # noqa: S106 | ||
bucket="testdrive", | ||
measurement="demo", | ||
) | ||
|
||
# Provision data to source database. | ||
logger.info("Provisioning InfluxDB") | ||
dff = DataFrameFactory(rows=DATASET_SIZE) | ||
df = dff.make("dateindex") | ||
influx.write_df(df) | ||
|
||
# Transfer data. | ||
logger.info("Transferring data") | ||
for df in influx.read_df(): | ||
logger.info("Loading data frame into RDBMS/SQL database using pandas/Dask") | ||
dataframe_to_sql(df, dburi=DBURI, tablename="demo", progress=True) | ||
|
||
# Read back data from target database. | ||
logger.info("Reading back data from the target database") | ||
engine = sa.create_engine(DBURI) | ||
with engine.connect() as connection: | ||
connection.execute(sa.text("REFRESH TABLE demo;")) | ||
result = connection.execute(sa.text("SELECT * FROM demo LIMIT 3;")) | ||
records = [dict(item) for item in result.mappings().fetchall()] | ||
jd(records) | ||
logger.info("Ready.") | ||
|
||
|
||
if __name__ == "__main__": | ||
setup_logging() | ||
main() |
Oops, something went wrong.