Blackjaguar
Good afternoon!
I am working on a project where users have the possibility to export data from different Psql tables as CSV.
Inspired by a great article (https://joaquimadraz.com/csv-export-with-postgres-ecto-and-phoenix) I managed to successfully setup a Stream to query and send the csv data as chunks to the user, where they are made available as a download.
This works great for one table at a time, but today I attempted to expand this as such that the user is able to request for multiple tables and receive the data in csv format zipped (compressed) into one file. However, I cannot get my head around how to achieve this.
Zipping data in a lazy manner seems counterintuitive to me, is this possible in Elixir/Erlang? It appears to me I need a structure that lazily reads rows from a Psql copy command, while zipping and also keep track of where one file stops and another starts.
How can I compress the data and send it as one package using a Ecto.Adapters.SQL stream and send it chunked to the user?
I hope I phrased the question clear enough, please let me know if more explanation is needed.
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #ai
- #genstage
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










Showing Posts 1 to 6- Show Best Posts
- Show All Posts (oldest first)
- Show All Posts (newest first)
dimitarvp
It would be great if you give a step-by-step example, giving several table names, input parameters and expected output – because I can’t quite follow.
I’ve done streaming and in-place CSV reports in Elixir before and it can be confusing when you first do it but rest assured, it’s very doable.
Blackjaguar
Thank you for your response, right now the flow is as following:
The data model up for export so far contains of a user table (name, email etc.) and a table of achievements within the application (title, expiration date, description).
A user request to download their data, which result into a GET request to the server.
The input for now is only the user id, and the result is queried from one table resulting into one CSV file using a COPY statement, i.e.:
COPY (
query here
) to STDOUT WITH CSV DELIMITER ‘,’;
“”"
This query is used in:
defp stream(query, header, batch_size \ 500) do
Repo
|> SQL.stream(query, , max_rows: batch_size)
|> Stream.map(& &1.rows)
|> (fn stream → Stream.concat(header, stream) end).()
end
In a controller, I reduce this stream and send the data in chunks to the user back using chunk/2.
The desired situation would be:
I hope this makes things clearer, if not please let me know!
dimitarvp
Off the top of my head:
For more options on processing the results of many asynchronous tasks (as used in this very crude example), please see Task.yield_many docs.
Also check Erlang’s zip module.
Again, this is very rough but it should give you a general idea. I opted to spawn each report in a separate process which should be beneficial on multi-core machines; however, if each report is quite heavy and needs a lot of CPU, then you might want to just iterate over each report in-place (without spawning processes) – that depends on your priorities.
Blackjaguar
This looks very promising, I will try this out today!
Thank you very much for your effort, I will report on whether I managed to solve this or not.
Blackjaguar
This worked out perfectly. Thank you very much for the assist @dimitarvp, this topic can be closed!
dimitarvp
FYI you can mark mine as the accepted answer and it’s considered closed.
Glad I could help! Hope this made you understand Elixir better.