com.d3x.morpheus.json.JsonSinkSplit Maven / Gradle / Ivy
/*
* Copyright (C) 2014-2018 D3X Systems - All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.d3x.morpheus.json;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.time.LocalDate;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import com.d3x.morpheus.frame.DataFrame;
import com.d3x.morpheus.frame.DataFrameColumn;
import com.d3x.morpheus.frame.DataFrameException;
import com.d3x.morpheus.frame.DataFrameRow;
import com.d3x.morpheus.frame.DataFrameValue;
import com.d3x.morpheus.range.Range;
import com.d3x.morpheus.util.Resource;
import com.d3x.morpheus.util.text.Formats;
import com.d3x.morpheus.util.text.printer.Printer;
import com.google.gson.stream.JsonWriter;
/**
* A JsonSink implementation that writes json compatible with Pandas "index" json format
*
* @param the row key type
* @param the column key type
*
* This is open source software released under the Apache 2.0 License
*
* @author Xavier Witdouck
*/
public class JsonSinkSplit extends JsonSinkBase {
private JsonWriter writer;
private JsonSink.Options options;
@Override()
public synchronized void write(JsonWriter writer, DataFrame frame, Options options) {
try {
this.options = options;
this.writer = writer;
this.writer.beginObject();
this.writer.name("columns");
this.writer.beginArray();
writeColKeys(frame);
this.writer.endArray();
this.writer.name("index");
this.writer.beginArray();
writeRowKeys(frame);
this.writer.endArray();
this.writer.name("data");
this.writer.beginArray();
writeRows(frame);
this.writer.endArray();
this.writer.endObject();
} catch (Exception ex) {
throw new DataFrameException("Failed to write DataFrame to JSON output", ex);
}
}
/**
* Writes the column keys for the frame
* @param frame the frame context
* @throws IOException if there is an I/O error
*/
private void writeColKeys(DataFrame,?> frame) throws IOException {
final Formats formats = options.getFormats();
final Class> keyType = frame.cols().keyClass();
final Printer