-
-
Save xia-stan/c804d4479de360283577bebba2ce6487 to your computer and use it in GitHub Desktop.
Code snippit to export Atrex table metadata
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
import datetime | |
import logging | |
import sys | |
import pyodbc | |
import pandas as pd | |
logging.basicConfig(stream=sys.stdout, level=logging.INFO, datefmt="%Y-%m-%dT%H:%M:%S", | |
format='%(asctime)s.%(msecs)03d - %(levelname)s - %(message)s') | |
DSN = "<update to the correct DSN>" | |
QUERY = "select * from information.Tables" | |
try: | |
logging.info("Initializing Connection") | |
connection = pyodbc.connect(f"DSN={DSN}") | |
logging.info("Getting cursor") | |
cursor = connection.cursor() | |
logging.info(f"Executing {QUERY}") | |
cursor.execute(QUERY) | |
columns = [x[0] for x in cursor.description] | |
results = list() | |
logging.info("Fetching records") | |
records = cursor.fetchall() | |
logging.info(f"Result set contains {len(records)} records.") | |
logging.info("Processing results of cursor.fetchall()") | |
for row in records: | |
results.append(dict(zip(columns, row))) | |
cursor.close() | |
logging.info("Outputting to json") | |
pd.DataFrame(results).to_json(f'data/raw/{datetime.date.today()}-table-meta.json', 'records') | |
except pyodbc.Error as err: | |
logging.error(repr(err)) | |
finally: | |
if connection: | |
connection.close() | |
logging.info("Execution complete") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment