|
| 1 | +## File Transfer |
| 2 | +MonetDB supports the non-standard `COPY INTO` statement to load a CSV-like |
| 3 | +text file into a table or to dump a table into a text file. This statement has an |
| 4 | +optional modifier `ON CLIENT` to indicate that the server should not |
| 5 | +try to open the file on the server side but instead ask the client to open it |
| 6 | +on its behalf. |
| 7 | + |
| 8 | +For example:: |
| 9 | +```sql |
| 10 | + COPY INTO mytable FROM 'data.csv' ON CLIENT |
| 11 | + USING DELIMITERS ',', E'\n', '"'; |
| 12 | +``` |
| 13 | +For security reason `monetdb-nodejs` enforces files to be realtive to the current |
| 14 | +working directory of the Node.js process. |
| 15 | +## Skip rows and early cancellation |
| 16 | +MonetDB's `COPY INTO` statement allows you to skip, for example, the first |
| 17 | +line in a file using the modifier `OFFSET 2`, and load `n` records from the file using |
| 18 | +`RECORDS` modifier. |
| 19 | +```sql |
| 20 | + COPY 100 RECORDS OFFSET 2 INTO mytable FROM 'data.csv' ON CLIENT |
| 21 | +``` |
| 22 | +, for detailed documentation on `COPY INTO` statement please vist [MonetDB documentation](https://www.monetdb.org/documentation-Jun2023/user-guide/sql-manual/data-loading/) |
| 23 | +## Example |
| 24 | +Assume `data.csv` with the following content |
| 25 | +```bash |
| 26 | +cat<<EOF>data.csv |
| 27 | +1|one |
| 28 | +2|two |
| 29 | +3|three |
| 30 | +EOF |
| 31 | +``` |
| 32 | +, then load that into MonetDB |
| 33 | +```ts |
| 34 | +import {Connection} from 'monetdb'; |
| 35 | +
|
| 36 | +const conn = new Connection({database: 'test'}); |
| 37 | +const ready = await conn.connect(); |
| 38 | +await conn.execute('create table foo(i int, a varchar(10))'); |
| 39 | +let res = await conn.execute(`copy into foo from \'data.csv\' on client`); |
| 40 | +res = await conn.execute('select * from foo order by i'); |
| 41 | +console.log(res.data); |
| 42 | +// [[1, 'one'], [2, 'two'], [3, 'three']] |
| 43 | +``` |
0 commit comments