This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-10-26
Channels
- # announcements (17)
- # babashka (68)
- # beginners (8)
- # biff (14)
- # calva (25)
- # cherry (10)
- # clj-kondo (1)
- # clj-on-windows (12)
- # cljsrn (6)
- # clojure (134)
- # clojure-berlin (1)
- # clojure-europe (33)
- # clojure-nl (4)
- # clojure-norway (6)
- # clojure-uk (10)
- # clojurescript (9)
- # datalevin (8)
- # datomic (34)
- # docker (1)
- # emacs (31)
- # fulcro (6)
- # honeysql (8)
- # java (7)
- # joyride (14)
- # kaocha (7)
- # malli (11)
- # nbb (4)
- # off-topic (11)
- # pedestal (14)
- # rdf (53)
- # re-frame (6)
- # reagent (39)
- # reitit (2)
- # releases (9)
- # rewrite-clj (14)
- # shadow-cljs (97)
- # specter (1)
- # testing (5)
- # tools-deps (12)
- # vim (4)
- # xtdb (9)
hello everyone, I have a potentially large lazy dataset returned by jdbc-next, and I want to stream the response as a csv file whats the best way of doing this? returning an input steram? a function? I took a look at the docs but its not clear to me how to connect the dots
Something like this
(defn get-huge-data
[req]
(let [c (async/chan)]
(async/thread
(doseq [v (jdbc/get-values)]
(async/>!! c v)))
{:body c
:status 200}))
maybe instead of returning a channel I could return an input stream, and write to that input using a thread
instead of core.async?
note that the key is "return a channel" How you will pipe the data into this channel may be tricky
given it returns a lazy list as a result I was under the impression that I could doseq
without having to load the entire collection in memory
In this specific case, of a lazy seq from jdbc into the response, may be easier to write as you read, like this
(require '[io.pedestal.http :as http]
'[clojure.core.async :as async]
'[io.pedestal.test :refer [response-for]])
(let [index (fn [req]
(let [lazy-jdbc-result (#_jdbc/execute.. do
[1 2 3])]
{:body (fn [out]
(with-open [w (io/writer out)]
(doseq [v lazy-jdbc-result]
(json/write v w)
(.write w (int \newline)))))
:status 200}))
service-fn (-> {::http/routes `#{["/" :get ~index
:route-name ::index]}}
http/default-interceptors
http/create-servlet
::http/service-fn)]
(response-for service-fn :get "/"))