Skip to content

Commit

Permalink
[*] improve Download chain sample
Browse files Browse the repository at this point in the history
  • Loading branch information
pashagolub committed May 23, 2023
1 parent cf4c120 commit 66372b6
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions samples/Download.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
-- Prepare the destination table 'location'
CREATE TABLE IF NOT EXISTS city(
city text,
lat numeric,
lng numeric,
country text,
iso2 text,
admin_name text,
capital text,
population bigint,
population_proper bigint);

-- An enhanced example consisting of three tasks:
-- 1. Download text file from internet using BUILT-IN command
-- 2. Remove accents (diacritic signs) from letters using PROGRAM command (can be done with `unaccent` PostgreSQL extension)
Expand All @@ -24,7 +36,7 @@ BEGIN
VALUES (v_task_id, 1,
'{
"workersnum": 1,
"fileurls": ["https://www.cybertec-postgresql.com/secret/orte.txt"],
"fileurls": ["https://simplemaps.com/static/data/country-cities/mt/mt.csv"],
"destpath": "."
}'::jsonb);

Expand All @@ -40,7 +52,7 @@ BEGIN
-- Under Windows we should call PowerShell instead of "uconv" with command:
-- Set-content "orte_ansi.txt" ((Get-content "orte.txt").Normalize("FormD") -replace '\p{M}', '')
INSERT INTO timetable.parameter (task_id, order_id, value)
VALUES (v_task_id, 1, '["-x", "Latin-ASCII", "-o", "orte_ansi.txt", "orte.txt"]'::jsonb);
VALUES (v_task_id, 1, '["-x", "Latin-ASCII", "-o", "mt_ansi.csv", "mt.csv"]'::jsonb);

RAISE NOTICE 'Step 2 completed. Unacent task added with ID: %', v_task_id;

Expand All @@ -49,13 +61,17 @@ BEGIN
VALUES (v_chain_id, 3, 'BUILTIN', 'CopyFromFile')
RETURNING task_id INTO v_task_id;

-- Prepare the destination table 'location'
CREATE TABLE IF NOT EXISTS location(name text);

-- Add the parameters for the download task. Execute client side COPY to 'location' from 'orte_ansi.txt'
INSERT INTO timetable.parameter (task_id, order_id, value)
VALUES (v_task_id, 1, '{"sql": "COPY location FROM STDIN", "filename": "orte_ansi.txt" }'::jsonb);
VALUES (v_task_id, 1, '{"sql": "COPY city FROM STDIN (FORMAT csv, HEADER true)", "filename": "mt_ansi.csv" }'::jsonb);

RAISE NOTICE 'Step 3 completed. Import task added with ID: %', v_task_id;

INSERT INTO timetable.task (chain_id, task_order, kind, command, ignore_error, task_name)
VALUES (v_chain_id, 4, 'PROGRAM', 'bash', TRUE, 'remove .csv')
RETURNING task_id INTO v_task_id;

INSERT INTO timetable.parameter (task_id, order_id, value)
VALUES (v_task_id, 1, '["-c", "rm *.csv"]'::jsonb);
END;
$$ LANGUAGE PLPGSQL;
$$ LANGUAGE PLPGSQL;

0 comments on commit 66372b6

Please sign in to comment.