Search in sources :

Example 1 with Value

use of org.msgpack.value.Value in project LogHub by fbacchella.

the class TestMsgpack method testmapsimple.

@Test
public void testmapsimple() throws IOException, DecodeException {
    Decoder d = new Msgpack();
    Map<Value, Value> destination = new HashMap<>();
    destination.put(ValueFactory.newString("a"), ValueFactory.newString("0"));
    destination.put(ValueFactory.newString("b"), ValueFactory.newInteger(1));
    destination.put(ValueFactory.newString("c"), ValueFactory.newBoolean(false));
    Value[] subdestination = new Value[4];
    subdestination[0] = ValueFactory.newString("0");
    subdestination[1] = ValueFactory.newInteger(1);
    subdestination[2] = ValueFactory.newFloat(2.0);
    subdestination[3] = ValueFactory.newNil();
    destination.put(ValueFactory.newString("d"), ValueFactory.newArray(subdestination));
    Value v = ValueFactory.newMap(destination);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    MessagePacker packer = MessagePack.newDefaultPacker(out);
    packer.packValue(v);
    packer.close();
    byte[] packed = out.toByteArray();
    Map<String, Object> e = d.decode(ConnectionContext.EMPTY, packed);
    testContent(e);
}
Also used : MessagePacker(org.msgpack.core.MessagePacker) HashMap(java.util.HashMap) Value(org.msgpack.value.Value) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Decoder(loghub.Decoder) Test(org.junit.Test)

Example 2 with Value

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

the class JsonParserPlugin method run.

@Override
public void run(TaskSource taskSource, Schema schema, FileInput input, PageOutput output) {
    PluginTask task = taskSource.loadTask(PluginTask.class);
    final boolean stopOnInvalidRecord = task.getStopOnInvalidRecord();
    // record column
    final Column column = schema.getColumn(0);
    try (PageBuilder pageBuilder = newPageBuilder(schema, output);
        FileInputInputStream in = new FileInputInputStream(input)) {
        while (in.nextFile()) {
            boolean evenOneJsonParsed = false;
            try (JsonParser.Stream stream = newJsonStream(in, task)) {
                Value value;
                while ((value = stream.next()) != null) {
                    try {
                        if (!value.isMapValue()) {
                            throw new JsonRecordValidateException(String.format("A Json record must not represent map value but it's %s", value.getValueType().name()));
                        }
                        pageBuilder.setJson(column, value);
                        pageBuilder.addRecord();
                        evenOneJsonParsed = true;
                    } catch (JsonRecordValidateException e) {
                        if (stopOnInvalidRecord) {
                            throw new DataException(String.format("Invalid record: %s", value.toJson()), e);
                        }
                        log.warn(String.format("Skipped record (%s): %s", e.getMessage(), value.toJson()));
                    }
                }
            } catch (IOException | JsonParseException e) {
                if (Exec.isPreview() && evenOneJsonParsed) {
                    // ignore in preview if at least one JSON is already parsed.
                    break;
                }
                throw new DataException(e);
            }
        }
        pageBuilder.finish();
    }
}
Also used : PageBuilder(org.embulk.spi.PageBuilder) IOException(java.io.IOException) JsonParseException(org.embulk.spi.json.JsonParseException) DataException(org.embulk.spi.DataException) FileInputInputStream(org.embulk.spi.util.FileInputInputStream) Column(org.embulk.spi.Column) Value(org.msgpack.value.Value) JsonParser(org.embulk.spi.json.JsonParser)

Example 3 with Value

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

the class TestJsonParser method testOrdinaryInteger.

@Test
public void testOrdinaryInteger() throws Exception {
    final JsonParser parser = new JsonParser();
    final Value msgpackValue = parser.parse("12345");
    assertTrue(msgpackValue.getValueType().isNumberType());
    assertTrue(msgpackValue.getValueType().isIntegerType());
    assertFalse(msgpackValue.getValueType().isFloatType());
    assertFalse(msgpackValue.getValueType().isStringType());
    assertEquals(12345, msgpackValue.asIntegerValue().asInt());
}
Also used : Value(org.msgpack.value.Value) Test(org.junit.Test)

Example 4 with Value

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

the class TestJsonParser method testOrdinaryFloat.

@Test
public void testOrdinaryFloat() throws Exception {
    final JsonParser parser = new JsonParser();
    final Value msgpackValue = parser.parse("12345.12");
    assertTrue(msgpackValue.getValueType().isNumberType());
    assertTrue(msgpackValue.getValueType().isFloatType());
    assertFalse(msgpackValue.getValueType().isIntegerType());
    assertFalse(msgpackValue.getValueType().isStringType());
    assertEquals(12345.12, msgpackValue.asFloatValue().toDouble(), 0.000000001);
    // Not sure this |toString| is to be tested...
    assertEquals("12345.12", msgpackValue.asFloatValue().toString());
}
Also used : Value(org.msgpack.value.Value) Test(org.junit.Test)

Example 5 with Value

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

the class TestJsonParserPlugin method useUnEscapeInvalidEscapeString.

@Test
public void useUnEscapeInvalidEscapeString() throws Exception {
    ConfigSource config = this.config.deepCopy().set("invalid_string_escapes", "UNESCAPE");
    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("a")));
}
Also used : ConfigSource(org.embulk.config.ConfigSource) 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 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)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