Search in sources :

Example 6 with Value

use of org.msgpack.value.Value in project embulk by embulk.

the class TestJsonParserPlugin method useSkipInvalidEscapeString.

@Test
public void useSkipInvalidEscapeString() throws Exception {
    ConfigSource config = this.config.deepCopy().set("invalid_string_escapes", "SKIP");
    transaction(config, fileInput("{\"\\a\":\"b\"}\\"));
    List<Object[]> records = Pages.toObjects(plugin.newSchema(), output.pages);
    assertEquals(1, records.size());
    Object[] record = records.get(0);
    Map<Value, Value> map = ((Value) record[0]).asMapValue().map();
    assertEquals(newString("b"), map.get(newString("")));
}
Also used : ConfigSource(org.embulk.config.ConfigSource) Value(org.msgpack.value.Value) Test(org.junit.Test)

Example 7 with Value

use of org.msgpack.value.Value in project embulk by embulk.

the class CsvFormatterPlugin method open.

@Override
public PageOutput open(TaskSource taskSource, final Schema schema, FileOutput output) {
    final PluginTask task = taskSource.loadTask(PluginTask.class);
    final LineEncoder encoder = new LineEncoder(output, task);
    final TimestampFormatter[] timestampFormatters = Timestamps.newTimestampColumnFormatters(task, schema, task.getColumnOptions());
    final char delimiter = task.getDelimiterChar();
    final QuotePolicy quotePolicy = task.getQuotePolicy();
    final char quote = task.getQuoteChar() != '\0' ? task.getQuoteChar() : '"';
    final char escape = task.getEscapeChar().or(quotePolicy == QuotePolicy.NONE ? '\\' : quote);
    final String newlineInField = task.getNewlineInField().getString();
    final String nullString = task.getNullString();
    // create a file
    encoder.nextFile();
    // write header
    if (task.getHeaderLine()) {
        writeHeader(schema, encoder, delimiter, quotePolicy, quote, escape, newlineInField, nullString);
    }
    return new PageOutput() {

        private final PageReader pageReader = new PageReader(schema);

        private final String delimiterString = String.valueOf(delimiter);

        public void add(Page page) {
            pageReader.setPage(page);
            while (pageReader.nextRecord()) {
                schema.visitColumns(new ColumnVisitor() {

                    public void booleanColumn(Column column) {
                        addDelimiter(column);
                        if (!pageReader.isNull(column)) {
                            addValue(Boolean.toString(pageReader.getBoolean(column)));
                        } else {
                            addNullString();
                        }
                    }

                    public void longColumn(Column column) {
                        addDelimiter(column);
                        if (!pageReader.isNull(column)) {
                            addValue(Long.toString(pageReader.getLong(column)));
                        } else {
                            addNullString();
                        }
                    }

                    public void doubleColumn(Column column) {
                        addDelimiter(column);
                        if (!pageReader.isNull(column)) {
                            addValue(Double.toString(pageReader.getDouble(column)));
                        } else {
                            addNullString();
                        }
                    }

                    public void stringColumn(Column column) {
                        addDelimiter(column);
                        if (!pageReader.isNull(column)) {
                            addValue(pageReader.getString(column));
                        } else {
                            addNullString();
                        }
                    }

                    public void timestampColumn(Column column) {
                        addDelimiter(column);
                        if (!pageReader.isNull(column)) {
                            Timestamp value = pageReader.getTimestamp(column);
                            addValue(timestampFormatters[column.getIndex()].format(value));
                        } else {
                            addNullString();
                        }
                    }

                    public void jsonColumn(Column column) {
                        addDelimiter(column);
                        if (!pageReader.isNull(column)) {
                            Value value = pageReader.getJson(column);
                            addValue(value.toJson());
                        } else {
                            addNullString();
                        }
                    }

                    private void addDelimiter(Column column) {
                        if (column.getIndex() != 0) {
                            encoder.addText(delimiterString);
                        }
                    }

                    private void addValue(String v) {
                        encoder.addText(setEscapeAndQuoteValue(v, delimiter, quotePolicy, quote, escape, newlineInField, nullString));
                    }

                    private void addNullString() {
                        encoder.addText(nullString);
                    }
                });
                encoder.addNewLine();
            }
        }

        public void finish() {
            encoder.finish();
        }

        public void close() {
            encoder.close();
        }
    };
}
Also used : TimestampFormatter(org.embulk.spi.time.TimestampFormatter) LineEncoder(org.embulk.spi.util.LineEncoder) PageReader(org.embulk.spi.PageReader) Page(org.embulk.spi.Page) Timestamp(org.embulk.spi.time.Timestamp) ColumnVisitor(org.embulk.spi.ColumnVisitor) PageOutput(org.embulk.spi.PageOutput) Column(org.embulk.spi.Column) Value(org.msgpack.value.Value)

Example 8 with Value

use of org.msgpack.value.Value in project embulk by embulk.

the class PageTestUtils method buildPage.

public static List<Page> buildPage(BufferAllocator bufferAllocator, Schema schema, Object... values) {
    MockPageOutput output = new MockPageOutput();
    try (PageBuilder builder = new PageBuilder(bufferAllocator, schema, output)) {
        int idx = 0;
        while (idx < values.length) {
            for (int column = 0; column < builder.getSchema().getColumnCount(); ++column) {
                Object value = values[idx++];
                if (value == null) {
                    builder.setNull(column);
                } else if (value instanceof Boolean) {
                    builder.setBoolean(column, (Boolean) value);
                } else if (value instanceof Double) {
                    builder.setDouble(column, (Double) value);
                } else if (value instanceof Long) {
                    builder.setLong(column, (Long) value);
                } else if (value instanceof String) {
                    builder.setString(column, (String) value);
                } else if (value instanceof Timestamp) {
                    builder.setTimestamp(column, (Timestamp) value);
                } else if (value instanceof Value) {
                    builder.setJson(column, (Value) value);
                } else {
                    throw new IllegalStateException("Unsupported type in test utils: " + value.toString());
                }
            }
            builder.addRecord();
        }
        builder.finish();
    }
    return output.pages;
}
Also used : Value(org.msgpack.value.Value) Timestamp(org.embulk.spi.time.Timestamp) MockPageOutput(org.embulk.spi.TestPageBuilderReader.MockPageOutput)

Example 9 with Value

use of org.msgpack.value.Value in project embulk by embulk.

the class TestJsonParser method testExponentialInteger2.

@Test
public void testExponentialInteger2() throws Exception {
    final JsonParser parser = new JsonParser();
    final Value msgpackValue = parser.parse("123e2");
    assertTrue(msgpackValue.getValueType().isNumberType());
    // TODO: Consider this needs to be an integer?
    // See: https://github.com/embulk/embulk/issues/775
    assertTrue(msgpackValue.getValueType().isFloatType());
    assertFalse(msgpackValue.getValueType().isIntegerType());
    assertFalse(msgpackValue.getValueType().isStringType());
    assertEquals(12300.0, msgpackValue.asFloatValue().toDouble(), 0.000000001);
    // Not sure this |toString| is to be tested...
    assertEquals("12300.0", msgpackValue.asFloatValue().toString());
}
Also used : Value(org.msgpack.value.Value) Test(org.junit.Test)

Example 10 with Value

use of org.msgpack.value.Value in project embulk by embulk.

the class TestJsonParser method testExponentialInteger1.

@Test
public void testExponentialInteger1() throws Exception {
    final JsonParser parser = new JsonParser();
    final Value msgpackValue = parser.parse("12345e3");
    assertTrue(msgpackValue.getValueType().isNumberType());
    // TODO: Consider this needs to be an integer?
    // See: https://github.com/embulk/embulk/issues/775
    assertTrue(msgpackValue.getValueType().isFloatType());
    assertFalse(msgpackValue.getValueType().isIntegerType());
    assertFalse(msgpackValue.getValueType().isStringType());
    assertEquals(12345000.0, msgpackValue.asFloatValue().toDouble(), 0.000000001);
    // Not sure this |toString| is to be tested...
    assertEquals("1.2345E7", msgpackValue.asFloatValue().toString());
}
Also used : Value(org.msgpack.value.Value) Test(org.junit.Test)

Aggregations

Value (org.msgpack.value.Value)13 Test (org.junit.Test)10 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2 ConfigSource (org.embulk.config.ConfigSource)2 Column (org.embulk.spi.Column)2 Timestamp (org.embulk.spi.time.Timestamp)2 MessagePacker (org.msgpack.core.MessagePacker)2 TDBulkImportSession (com.treasuredata.client.model.TDBulkImportSession)1 TDColumn (com.treasuredata.client.model.TDColumn)1 TDTable (com.treasuredata.client.model.TDTable)1 File (java.io.File)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 GZIPInputStream (java.util.zip.GZIPInputStream)1 GZIPOutputStream (java.util.zip.GZIPOutputStream)1 Decoder (loghub.Decoder)1