Fork me on GitHub
#babashka
<
2023-03-05
>
sheluchin22:03:13

I'm trying to use babashka to import json to duckdb. The SQL syntax for it is like SELECT * FROM read_json_auto('/dev/stdin');. I have babashka.process/shell giving me the json I need, but I'm not sure how to pass it to a function like json->duckdb-table. The obvious workaround is to save the output to a file first, but I'm trying to avoid that step.

borkdude22:03:46

(-> (shell {:out :string} ...) :out)
will return the output as a string

sheluchin22:03:39

Yeah, that much I know. json->duckdb-table isn't expecting a string though. It just needs to read from stdin. Something like:

(jdbc/execute! datasource
               (-> (hh/select :*)
                   (hh/from [[:read_json_auto "/dev/stdin"]])))
and I'm not sure how to align the p/shell call with the above.

borkdude22:03:48

{:in "your-input"}

sheluchin01:03:58

https://github.com/babashka/babashka/wiki/Bash-and-Babashka-equivalents#pipe-operator is that example correct?

#!/usr/bin/env bb

(require '[babashka.process :as p :refer [shell]])

(-> (shell {:out :string} "echo hello") (shell "cat"))
$ echo "hello" | /usr/bin/cat
hello
$ bb test.clj
hello
{:proc #object[java.lang.ProcessImpl 0x767c2076 "Process[pid=380396, exitValue=0]"], :exit 0, :in #object[java.lang.ProcessImpl$ProcessPipeOutputStream 0x7fc308f "java.lang.ProcessImpl$Pro
cessPipeOutputStream@7fc308f"], :out #object[java.lang.ProcessBuilder$NullInputStream 0x410ae671 "java.lang.ProcessBuilder$NullInputStream@410ae671"], :err #object[java.lang.ProcessBuilder
$NullInputStream 0x410ae671 "java.lang.ProcessBuilder$NullInputStream@410ae671"], :prev {:proc #object[java.lang.ProcessImpl 0x7f8c72d "Process[pid=380393, exitValue=0]"], :exit 0, :in #ob
ject[java.lang.ProcessBuilder$NullOutputStream 0x50516867 "java.lang.ProcessBuilder$NullOutputStream@50516867"], :out "hello\n", :err #object[java.lang.ProcessBuilder$NullInputStream 0x410
ae671 "java.lang.ProcessBuilder$NullInputStream@410ae671"], :prev nil, :cmd ["echo" "hello"]}, :cmd ["cat"]}

borkdude09:03:47

You're probably using an older version of bb which still prints the last result. Please upgrade

sheluchin19:03:58

Thanks @U04V15CAJ, that was it.