![]() |
VOOZH | about |
Singer.io is a free, open-source ETL framework that connects data sources to destinations using standardized JSON-based extractors called Taps and loaders called Targets. It extracts data from a source (like a CRM or database) through a Tap and loads it into a Target system (like Snowflake) — all while maintaining data integrity and structure.
CData Python Connectors enhance this process by offering real-time, no-code tools to build custom Taps and Targets for hundreds of data sources.
In this guide, we'll build a Tap for Snapchat Ads using the CData Python Connector and load the extracted data into a CSV Target (target-csv). We'll use the Free Community License of the CData Python Connector for Snapchat Ads along with VSCode, leveraging the sys_tablecolumns system tables to fetch schema and metadata efficiently.
Let's get started!
Here's a quick overview of the steps we'll follow:
Tip: You're going to be interacting with a lot of files and folders. It's a good idea to create a dedicated folder for this project — for example: Singer-Tap — and place all the project files in it.
Dependencies Note: The Python connector supports Python versions 3.8, 3.9, 3.10, 3.11, and 3.12. If you're using a version outside this range, you may need to create a virtual environment with virtualenv.
This is an optional step. A Free Community Edition license for your machine should already be installed when you install the connector using the .whl file.
However, if you don't see the license or you're using a trial version, install your Free Community Edition license using the key sent to your email.
If you didn't receive it, request one for your Connector here.
.\license-installer.exe [YOUR LICENSE KEY HERE]
cd ~/Downloads/CData-Python-SnapchatAds
./license-installer [YOUR LICENSE KEY HERE]
pip install singer-python
python -m venv %USERPROFILE%\target-csv %USERPROFILE%\target-csv\Scripts\activate pip install target-csv deactivate
python3 -m venv ~/.virtualenvs/target-csv source ~/.virtualenvs/target-csv/bin/activate pip install target-csv deactivate
import collectionswith:
from collections.abc import MutableMapping
isinstance(v, collections.MutableMapping)to:
isinstance(v, MutableMapping)
Now that everything is set up, let's build the actual Singer Tap for Snapchat Ads. This Tap will connect to your Snapchat Ads instance, retrieve metadata using CData's system tables, and prepare the data for transfer.
You can copy the complete working code at the end of this article, but we'll go through it step by step below.
import singer import cdata.snapchatads as mod from datetime import date, datetime
def create_connection(): return mod.connect( "InitiateOAuth=GETANDREFRESH;" )
With the connection working, let's define the logic for querying the metadata, mapping data types into Singer-compatible formats, and writing the records to the standard output stdout.
def query(conn, sql): cur = conn.cursor() cur.execute(sql) return cur.description, cur.fetchall()
TABLE_NAME = 'Opportunity' # Change to your desired object/table RECORD_LIMIT = 50 # Use None or 0 to fetch all rows
def mapping_types(sf_type): t = sf_type.lower() if 'int' in t: return ['integer', 'null'] if 'float' in t or 'decimal' in t or 'double' in t: return ['number', 'null'] if 'bool' in t: return ['boolean', 'null'] if 'date' in t or 'time' in t: return ['string', 'null'] return ['string', 'null']
def write_records():
conn = create_connection()
# Get columns and types for schema
_, cols = query(conn, f"SELECT ColumnName, DataTypeName FROM sys_tablecolumns WHERE TableName = '{TABLE_NAME}'")
schema = {
'properties': {col: {'type': mapping_types(dtype)} for col, dtype in cols}
}
# Build query dynamically based on RECORD_LIMIT
limit_clause = f"LIMIT {RECORD_LIMIT}" if RECORD_LIMIT and RECORD_LIMIT > 0 else ""
query_cmd = f"SELECT * FROM {TABLE_NAME} {limit_clause}"
columns, records = query(conn, query_cmd)
stream = TABLE_NAME.lower()
singer.write_schema(stream, schema, [columns[0][0]] if columns else [])
for row in records:
record = {
columns[i][0]: (val.isoformat() if isinstance(val, (date, datetime)) else val)
for i, val in enumerate(row)
}
singer.write_record(stream, record)
conn.close()
if __name__ == '__main__': write_records()
With your Tap ready, it's time to move the data from your Snapchat Ads instance into a usable format. We'll use target-csv to output the data as tab-delimited CSV files.
You'll need a my-config.json file to tell target-csv how to format and where to save the output. It defines configuration settings such as the delimiter, quote character, output path, logging level, and metadata settings.
{
"delimiter": " ",
"quotechar": "'",
"destination_path": "YOUR/PATH/FOR/CSV/OUTPUTS",
"disable_collection": true,
"verbosity": 5
}
Building a custom Singer Tap for Snapchat Ads (and for hundreds of other data sources) is faster and easier with the CData Python Connector. You get built-in schema discovery, live data access, and plug-and-play connectivity — all with minimal code.
Start your free trial today and simplify your ETL pipelines.
import singer
import cdata.snapchatads as mod
from datetime import date, datetime
def create_connection():
return mod.connect(
"InitiateOAuth=GETANDREFRESH;"
)
def query(conn, sql):
cur = conn.cursor()
cur.execute(sql)
return cur.description, cur.fetchall()
TABLE_NAME = 'Opportunity' # Change to your desired object/table
RECORD_LIMIT = 50 # Change accordingly
def mapping_types(sf_type):
t = sf_type.lower()
if 'int' in t:
return ['integer', 'null']
if 'float' in t or 'decimal' in t or 'double' in t:
return ['number', 'null']
if 'bool' in t:
return ['boolean', 'null']
if 'date' in t or 'time' in t:
return ['string', 'null']
return ['string', 'null']
def write_records():
conn = create_connection()
# Get columns and types for schema
_, cols = query(conn, f"SELECT ColumnName, DataTypeName FROM sys_tablecolumns WHERE TableName = '{TABLE_NAME}'")
schema = {
'properties': {col: {'type': mapping_types(dtype)} for col, dtype in cols}
}
# Fetch actual data rows
columns, records = query(conn, f"SELECT * FROM {TABLE_NAME} LIMIT {RECORD_LIMIT}")
stream = TABLE_NAME.lower()
singer.write_schema(stream, schema, [columns[0][0]] if columns else [])
for row in records:
record = {
columns[i][0]: (val.isoformat() if isinstance(val, (date, datetime)) else val)
for i, val in enumerate(row)
}
singer.write_record(stream, record)
conn.close()
if __name__ == '__main__':
write_records()
Download a Community License of the Snapchat Ads Connector to get started:
Download NowLearn more:
👁 Snapchat Ads IconPython Connector Libraries for Snapchat Ads Data Connectivity. Integrate Snapchat Ads with popular Python tools like Pandas, SQLAlchemy, Dash & petl.