2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/WriteHelpers.h>
|
|
|
|
#include <IO/WriteBufferValidUTF8.h>
|
2018-06-10 19:22:49 +00:00
|
|
|
#include <Formats/JSONEachRowRowOutputStream.h>
|
|
|
|
#include <Formats/FormatFactory.h>
|
|
|
|
#include <Formats/BlockOutputStreamFromRowOutputStream.h>
|
2016-02-18 11:44:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
|
2018-06-08 01:51:55 +00:00
|
|
|
JSONEachRowRowOutputStream::JSONEachRowRowOutputStream(WriteBuffer & ostr_, const Block & sample, const FormatSettings & settings)
|
|
|
|
: ostr(ostr_), settings(settings)
|
2016-02-18 11:44:50 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t columns = sample.columns();
|
|
|
|
fields.resize(columns);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < columns; ++i)
|
|
|
|
{
|
|
|
|
WriteBufferFromString out(fields[i]);
|
2018-08-06 10:02:26 +00:00
|
|
|
writeJSONString(sample.getByPosition(i).name, out, settings);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2016-02-18 11:44:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void JSONEachRowRowOutputStream::writeField(const IColumn & column, const IDataType & type, size_t row_num)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
writeString(fields[field_number], ostr);
|
|
|
|
writeChar(':', ostr);
|
2017-07-05 16:28:57 +00:00
|
|
|
type.serializeTextJSON(column, row_num, ostr, settings);
|
2017-04-01 07:20:54 +00:00
|
|
|
++field_number;
|
2016-02-18 11:44:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void JSONEachRowRowOutputStream::writeFieldDelimiter()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
writeChar(',', ostr);
|
2016-02-18 11:44:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void JSONEachRowRowOutputStream::writeRowStartDelimiter()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
writeChar('{', ostr);
|
2016-02-18 11:44:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void JSONEachRowRowOutputStream::writeRowEndDelimiter()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
writeCString("}\n", ostr);
|
|
|
|
field_number = 0;
|
2016-02-18 11:44:50 +00:00
|
|
|
}
|
|
|
|
|
2018-06-10 19:22:49 +00:00
|
|
|
|
|
|
|
void registerOutputFormatJSONEachRow(FormatFactory & factory)
|
|
|
|
{
|
|
|
|
factory.registerOutputFormat("JSONEachRow", [](
|
|
|
|
WriteBuffer & buf,
|
|
|
|
const Block & sample,
|
|
|
|
const Context &,
|
|
|
|
const FormatSettings & format_settings)
|
|
|
|
{
|
|
|
|
return std::make_shared<BlockOutputStreamFromRowOutputStream>(
|
|
|
|
std::make_shared<JSONEachRowRowOutputStream>(buf, sample, format_settings), sample);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-02-18 11:44:50 +00:00
|
|
|
}
|