Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[QUESTION] How to insert collection json strings into a table #681

Closed
cadet354 opened this issue Jul 8, 2021 · 2 comments
Closed

[QUESTION] How to insert collection json strings into a table #681

cadet354 opened this issue Jul 8, 2021 · 2 comments
Labels

Comments

@cadet354
Copy link

cadet354 commented Jul 8, 2021

Hi, how to insert collection json strings into a table?
For example, I have a collection of objects, convert them to json strings and send them to a table.

// sorry for scala code
val sb = new StringBuilder()
events.map(ev => json.eventToString(ev)).foreach(s => sb.append(s +"\n"))
val stream = new ByteArrayInputStream(sb.toString().getBytes("UTF-8") )
try {
     sth.write().sendToTable(table, stream, ClickHouseFormat.JSONStringEachRow)
 } catch {
     case ex: java.sql.SQLException => log.error("An error occurred when inserting data into clickhouse", ex)
 }

but an exception occurs:
DB::Exception: Format JSONStringEachRow is not suitable for input (with processors).

I didn't find an example of using JSONStringEachRow in unit tests.

@zhicwu
Copy link
Contributor

zhicwu commented Jul 8, 2021

What's the table look like? JSONStringEachRow seems incorrect here as I only saw JSONStringsEachRow at https://clickhouse.tech/docs/en/interfaces/formats/. If it's just a string, then you probably should use TabSeparated(make sure your JSON strings are all escaped).

Anyway, below is an example in SQL:

drop table aaa if exists;
create table aaa(a String, b String, c UInt32) engine = Memory;
insert into aaa format JSONStringsEachRow
{"a":"1","b":"2","c":"3"}
{"a":"4","b":"5","c":"6"}

@cadet354
Copy link
Author

cadet354 commented Jul 8, 2021

Okay, it works with ClickHouseFormat.JSONEachRow

@cadet354 cadet354 closed this as completed Jul 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants