# export hdb to csv day by day

**URL:** <https://forum.kx.com/t/export-hdb-to-csv-day-by-day/8467>\
**Category:** Community Support\
**Tags:** kdb-and-q\
**Created:** [November 5, 2013, 2:05pm UTC](https://forum.kx.com/t/export-hdb-to-csv-day-by-day/8467 "2013-11-05T14:05:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![michael2](https://avatars.discourse-cdn.com/v4/letter/m/3e96dc/32.png) [@michael2](https://forum.kx.com/u/michael2)\
**Post date:** [November 5, 2013, 2:05pm UTC](https://forum.kx.com/t/export-hdb-to-csv-day-by-day/8467/1 "2013-11-05T14:05:00Z")

</div>

Hi,

I have a hdb (parted and splayed), so it looks like:

- 2013.10.01

&nbsp; &nbsp;- table\_1

&nbsp; &nbsp; &nbsp; - col\_a

&nbsp; &nbsp; &nbsp; - col\_b

&nbsp; &nbsp;- table\_2

&nbsp; &nbsp; &nbsp; - col\_1

&nbsp; &nbsp; &nbsp; - col\_2

- 2013.10.02

&nbsp; &nbsp;- table\_1

&nbsp; &nbsp; &nbsp; - col\_a

&nbsp; &nbsp; &nbsp; - col\_b

&nbsp; &nbsp;- table\_2

&nbsp; &nbsp; &nbsp; - col\_1

&nbsp; &nbsp; &nbsp; - col\_2

…

I would like to export each table for each day to csv file.

Currently I am loading the table with the data of one day into memory and than save it to disk like this:

h “`table\_1csv&nbsp;set select from table\_1 where date=2013.10.01; save `table\_1csv.csv; delete&nbsp;table\_1csv&nbsp;from `.”

the problem is, that not all tables fit into memory. Is there a way to do this?

Regards,

Michael

---

<div class="post-metadata">

**Author:** ![manni\_patel](https://avatars.discourse-cdn.com/v4/letter/m/958977/32.png) [@manni\_patel](https://forum.kx.com/u/manni_patel)\
**Post date:** [November 5, 2013, 2:45pm UTC](https://forum.kx.com/t/export-hdb-to-csv-day-by-day/8467/2 "2013-11-05T14:45:00Z")

</div>

If it doesn’t fit into memory, you can chunk, say, 3 hours at a time (whatever fits into memory for you) and append a text file:?http://code.kx.com/wiki/JB:QforMortals2/i\_o#Text\_Files

---

<div class="post-metadata">

**Author:** ![pawel\_tryfon](https://avatars.discourse-cdn.com/v4/letter/p/d78d45/32.png) [@pawel\_tryfon](https://forum.kx.com/u/pawel_tryfon)\
**Post date:** [November 5, 2013, 3:15pm UTC](https://forum.kx.com/t/export-hdb-to-csv-day-by-day/8467/3 "2013-11-05T15:15:00Z")

</div>

Michael,

You could try writing to .csv file data chunks that fit into memory. Example function below:

batchedCsvSave:{[file;table;batchSize]  
&nbsp; fileHandle: hopen file;  
&nbsp; (neg fileHandle) first csv 0: 1#table; // First output column names  
&nbsp; i:0;  
&nbsp; while[i\<count table; (neg fileHandle) each 1 \_ csv 0: table i+til batchSize; i+:batchSize];  
&nbsp; hclose fileHandle;  
&nbsp; }

Usage: t:(n:til 1000;f:1000?10f); batchedCsvSave[`:t.csv;t;1000]

HTH,

Pawel

2013/11/5 Michael Wittig \<michael@widdix.de\>

> Hi,
> 
> I have a hdb (parted and splayed), so it looks like:
> 
> - 2013.10.01
> 
> &nbsp; &nbsp;- table\_1
> 
> &nbsp; &nbsp; &nbsp; - col\_a
> 
> &nbsp; &nbsp; &nbsp; - col\_b
> 
> &nbsp; &nbsp;- table\_2
> 
> &nbsp; &nbsp; &nbsp; - col\_1
> 
> &nbsp; &nbsp; &nbsp; - col\_2
> 
> - 2013.10.02
> 
> &nbsp; &nbsp;- table\_1
> 
> &nbsp; &nbsp; &nbsp; - col\_a
> 
> &nbsp; &nbsp; &nbsp; - col\_b
> 
> &nbsp; &nbsp;- table\_2
> 
> &nbsp; &nbsp; &nbsp; - col\_1
> 
> &nbsp; &nbsp; &nbsp; - col\_2
> 
> …
> 
> I would like to export each table for each day to csv file.
> 
> Currently I am loading the table with the data of one day into memory and than save it to disk like this:
> 
> h “`table\_1csv&nbsp;set select from table\_1 where date=2013.10.01; save `table\_1csv.csv; delete&nbsp;table\_1csv&nbsp;from `.”
> 
> the problem is, that not all tables fit into memory. Is there a way to do this?
> 
> Regards,
> 
> Michael
> 
> –   
> 
> Submitted via Google Groups

---

<div class="post-metadata">

**Author:** ![michael2](https://avatars.discourse-cdn.com/v4/letter/m/3e96dc/32.png) [@michael2](https://forum.kx.com/u/michael2)\
**Post date:** [November 6, 2013, 12:38pm UTC](https://forum.kx.com/t/export-hdb-to-csv-day-by-day/8467/4 "2013-11-06T12:38:00Z")

</div>

thx!

Am Dienstag, 5. November 2013 16:15:28 UTC+1 schrieb pawel.tryfon:

> Michael,
> 
> You could try writing to .csv file data chunks that fit into memory. Example function below:
> 
> batchedCsvSave:{[file;table;batchSize]  
> fileHandle: hopen file;  
> (neg fileHandle) first csv 0: 1#table; // First output column names  
> i:0;  
> while[i\<count table; (neg fileHandle) each 1 \_ csv 0: table i+til batchSize; i+:batchSize];  
> hclose fileHandle;  
> }
> 
> Usage: t:(n:til 1000;f:1000?10f); batchedCsvSave[`:t.csv;t;1000]
> 
> HTH,
> 
> Pawel
> 
> 2013/11/5 Michael Wittig \<mic…@widdix.de\>
> 
> > Hi,
> > 
> > I have a hdb (parted and splayed), so it looks like:
> > 
> > - 2013.10.01
> > 
> > &nbsp; &nbsp;- table\_1
> > 
> > &nbsp; &nbsp; &nbsp; - col\_a
> > 
> > &nbsp; &nbsp; &nbsp; - col\_b
> > 
> > &nbsp; &nbsp;- table\_2
> > 
> > &nbsp; &nbsp; &nbsp; - col\_1
> > 
> > &nbsp; &nbsp; &nbsp; - col\_2
> > 
> > - 2013.10.02
> > 
> > &nbsp; &nbsp;- table\_1
> > 
> > &nbsp; &nbsp; &nbsp; - col\_a
> > 
> > &nbsp; &nbsp; &nbsp; - col\_b
> > 
> > &nbsp; &nbsp;- table\_2
> > 
> > &nbsp; &nbsp; &nbsp; - col\_1
> > 
> > &nbsp; &nbsp; &nbsp; - col\_2
> > 
> > …
> > 
> > I would like to export each table for each day to csv file.
> > 
> > Currently I am loading the table with the data of one day into memory and than save it to disk like this:
> > 
> > h “`table\_1csv&nbsp;set select from table\_1 where date=2013.10.01; save `table\_1csv.csv; delete&nbsp;table\_1csv&nbsp;from `.”
> > 
> > the problem is, that not all tables fit into memory. Is there a way to do this?
> > 
> > Regards,
> > 
> > Michael
> > 
> > –   
> > 
> > Submitted via Google Groups
