CartoDB / carto-python

CARTO Python client
https://carto.com
BSD 3-Clause "New" or "Revised" License
154 stars 62 forks source link

Example of CopySQLClient usage (low-level iterable interface) #91

Closed rafatower closed 6 years ago

rafatower commented 6 years ago

This does not adds code to the library but an example (hopefully a better one) of how to use the CopySQLClient to actually stream data.

I tried to make it both instructive and useful.

Example usage:

$ python duplicate_table.py 
 01:10:24 PM - ERROR - secret.conf file does not exist. Generating...
 01:10:24 PM - ERROR - Generated secret.conf. Please fill in the details of your source and destination CARTO accounts and re-run the script

then after filling in the secret.conf:

$ python duplicate_table.py 
usage: duplicate_table.py [-h] -t TABLE_NAME
duplicate_table.py: error: the following arguments are required: -t/--table

$ python duplicate_table.py -h
usage: duplicate_table.py [-h] -t TABLE_NAME

Example of CopySQLClient usage (low-level iterable interface). It uses the
CopySQLClient to duplicate a table from a source CARTO account to a
destination CARTO account. It works even across clouds and on-premise
installations, as long as there's connectivity and the API keys have suffient
privileges.

optional arguments:
  -h, --help            show this help message and exit
  -t TABLE_NAME, --table TABLE_NAME
                        Name of the table to COPY from source to destination
                        account

and the real execution of it:

$ python duplicate_table.py -t busstops_losangeles                                                                                                                                                               
 01:17:48 PM - INFO - Creating function generate_create_table_statement...
 01:17:49 PM - INFO - Response: {'total_rows': 0, 'fields': {}, 'time': 0.015, 'rows': []}
 01:17:49 PM - INFO - Getting the CREATE TABLE statement for busstops_losangeles
 01:17:51 PM - INFO - CREATE TABLE IF NOT EXISTS busstops_losangeles (
    cartodb_id integer   NOT NULL,
    the_geom geometry(Geometry,4326)  NULL,
    the_geom_webmercator geometry(Geometry,3857)  NULL,
    along text  NULL,
    at text  NULL,
    long double precision  NULL,
    lat double precision  NULL,
    linedir01 text  NULL,
    linedir02 text  NULL,
    linedir03 text  NULL,
    linedir04 text  NULL,
    linedir05 text  NULL,
    linedir06 text  NULL,
    linedir07 text  NULL,
    linedir08 text  NULL,
    linedir09 text  NULL,
    stopnum integer  NULL,
    linedir11 text  NULL,
    linedir12 text  NULL,
    linedir13 text  NULL,
    linedir14 text  NULL,
    linedir15 text  NULL,
    linedir16 text  NULL,
    linedir17 text  NULL,
    linedir18 text  NULL,
    linedir19 text  NULL,
    linedir20 text  NULL,
    created_at date  NULL,
    updated_at date  NULL,
    linedir10 text  NULL);
 01:17:51 PM - INFO - Creating the table in the destination account...
 01:17:52 PM - INFO - Response: {'total_rows': 0, 'fields': {}, 'time': 0.09, 'rows': []}
 01:17:52 PM - INFO - Cartodbfy'ing the destination table...
 01:17:52 PM - INFO - Response: {'total_rows': 1, 'fields': {'cdb_cartodbfytable': {'type': 'regclass'}}, 'time': 0.091, 'rows': [{'cdb_cartodbfytable': 'busstops_losangeles'}], 'notices': ['trigger "track_updates" for relation "busstops_losangeles" does not exist, skipping', 'trigger "update_the_geom_webmercator_trigger" for relation "busstops_losangeles" does not exist, skipping', 'trigger "test_quota" for relation "busstops_losangeles" does not exist, skipping', 'trigger "test_quota_per_row" for relation "busstops_losangeles" does not exist, skipping']}
 01:17:52 PM - INFO - Streaming the data from source to destination...
 01:17:57 PM - INFO - Result: {'total_rows': 14777, 'time': 4.1}
rafatower commented 6 years ago

Tests are meant to be fixed with this: https://github.com/CartoDB/carto-python/pull/92