Skip to content

Commit dce803d

Browse files
author
Rafa de la Torre
authored
Merge pull request #518 from CartoDB/copy-doc-improvements
Copy doc improvements
2 parents a8ef19d + c86f49e commit dce803d

File tree

1 file changed

+45
-15
lines changed

1 file changed

+45
-15
lines changed

doc/copy_queries.md

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ Creating a new CARTO table with all the right triggers and columns can be tricky
4848
-- adds the 'cartodb_id' and 'the_geom_webmercator'
4949
-- adds the required triggers and indexes
5050
SELECT CDB_CartodbfyTable('upload_example');
51+
52+
-- Note that CDB_CartodbfyTable is called differently if you have an organization user
53+
-- SELECT CDB_CartodbfyTable('your_org_username', 'upload_example');
5154

5255
Now you are ready to upload your file. Suppose you have a CSV file like this:
5356

@@ -112,6 +115,41 @@ with open(upload_file, 'rb') as f:
112115

113116
A slightly more sophisticated script could read the headers from the CSV and compose the `COPY` command on the fly.
114117

118+
### CSV files and column ordering
119+
120+
When using the **CSV format, please note that [PostgreSQL ignores the header](https://www.postgresql.org/docs/10/static/sql-copy.html)**.
121+
122+
> HEADER
123+
>
124+
> Specifies that the file contains a header line with the names of each column in the file. On output, the first line contains the column names from the table, and **on input, the first line is ignored**. This option is allowed only when using CSV format.
125+
126+
If the ordering of the columns does not match the table definition, you must specify it as part of the query.
127+
128+
For example, if your table is defined as:
129+
130+
```sql
131+
CREATE TABLE upload_example (
132+
the_geom geometry,
133+
name text,
134+
age integer
135+
);
136+
```
137+
138+
but your CSV file has the following structure (note `name` and `age` columns are swapped):
139+
140+
```csv
141+
#the_geom,age,name
142+
SRID=4326;POINT(-126 54),89,North West
143+
SRID=4326;POINT(-96 34),99,South East
144+
SRID=4326;POINT(-6 -25),124,Souther Easter
145+
```
146+
147+
your query has to specify the correct ordering, regardless of the header in the CSV:
148+
149+
```sql
150+
COPY upload_example (the_geom, age, name) FROM stdin WITH (FORMAT csv, HEADER true);
151+
```
152+
115153
### Response Format
116154

117155
A successful upload will return with status code 200, and a small JSON with information about the upload.
@@ -166,28 +204,20 @@ The Python to "copy to" is very simple, because the HTTP call is a simple get. T
166204

167205
```python
168206
import requests
169-
import re
170207

171208
api_key = {api_key}
172209
username = {api_key}
173-
download_file = 'upload_example_dl.csv'
210+
download_filename = 'download_example.csv'
174211
q = "COPY upload_example (the_geom, name, age) TO stdout WITH (FORMAT csv, HEADER true)"
175212

176-
# request the download, specifying desired file name
213+
# request the download
177214
url = "http://%s.carto.com/api/v2/sql/copyto" % username
178-
r = requests.get(url, params={'api_key': api_key, 'q': q, 'filename': download_file}, stream=True)
215+
r = requests.get(url, params={'api_key': api_key, 'q': q}, stream=True)
179216
r.raise_for_status()
180217

181-
# read save file name from response headers
182-
d = r.headers['content-disposition']
183-
savefilename = re.findall("filename=(.+)", d)
184-
185-
if len(savefilename) > 0:
186-
with open(savefilename[0], 'wb') as handle:
187-
for block in r.iter_content(1024):
188-
handle.write(block)
189-
print("Downloaded to: %s" % savefilename)
190-
else:
191-
print("Error: could not find read file name from headers")
218+
with open(download_filename, 'wb') as handle:
219+
for block in r.iter_content(1024):
220+
handle.write(block)
221+
print("Downloaded to: %s" % savefilename)
192222
```
193223

0 commit comments

Comments
 (0)