Search in sources :

Example 11 with FieldsDescriptor

use of org.apache.apex.malhar.lib.appdata.schemas.FieldsDescriptor in project apex-malhar by apache.

the class GPOMutableTest method copyConstructorTest.

@Test
public void copyConstructorTest() {
    Map<String, Type> fieldToTypeA = Maps.newHashMap();
    fieldToTypeA.put("a", Type.LONG);
    fieldToTypeA.put("b", Type.STRING);
    FieldsDescriptor fdA = new FieldsDescriptor(fieldToTypeA);
    GPOMutable gpoA = new GPOMutable(fdA);
    gpoA.setField("a", 1L);
    gpoA.setField("b", "hello");
    GPOMutable gpoB = new GPOMutable(gpoA);
    Assert.assertEquals("Should have same field values.", gpoA.getFieldLong("a"), gpoB.getFieldLong("a"));
    Assert.assertEquals("Should have same field values.", gpoA.getFieldString("b"), gpoB.getFieldString("b"));
}
Also used : Type(org.apache.apex.malhar.lib.appdata.schemas.Type) FieldsDescriptor(org.apache.apex.malhar.lib.appdata.schemas.FieldsDescriptor) Test(org.junit.Test)

Example 12 with FieldsDescriptor

use of org.apache.apex.malhar.lib.appdata.schemas.FieldsDescriptor in project apex-malhar by apache.

the class GPOUtilsTest method objectSerdeTest.

@Test
public void objectSerdeTest() {
    Map<String, Type> fieldToTypeKey = Maps.newHashMap();
    fieldToTypeKey.put("publisher", Type.STRING);
    fieldToTypeKey.put("advertiser", Type.STRING);
    FieldsDescriptor fdkey = new FieldsDescriptor(fieldToTypeKey);
    Map<String, Type> fieldToTypeAgg = Maps.newHashMap();
    fieldToTypeAgg.put("clicks", Type.LONG);
    fieldToTypeAgg.put("impressions", Type.LONG);
    FieldsDescriptor fdagg = new FieldsDescriptor(fieldToTypeAgg);
    Map<String, Type> fieldToType = Maps.newHashMap();
    fieldToType.put("fdkeys", Type.OBJECT);
    fieldToType.put("fdvalues", Type.OBJECT);
    fieldToType.put("keys", Type.OBJECT);
    fieldToType.put("values", Type.OBJECT);
    Map<String, Serde> fieldToSerde = Maps.newHashMap();
    fieldToSerde.put("fdkeys", SerdeFieldsDescriptor.INSTANCE);
    fieldToSerde.put("fdvalues", SerdeFieldsDescriptor.INSTANCE);
    fieldToSerde.put("keys", SerdeListGPOMutable.INSTANCE);
    fieldToSerde.put("values", SerdeListGPOMutable.INSTANCE);
    FieldsDescriptor metaDataFD = new FieldsDescriptor(fieldToType, fieldToSerde, new PayloadFix());
    GPOMutable gpo = new GPOMutable(metaDataFD);
    GPOMutable key1 = new GPOMutable(fdkey);
    key1.setField("publisher", "google");
    key1.setField("advertiser", "safeway");
    GPOMutable key2 = new GPOMutable(fdkey);
    key2.setField("publisher", "twitter");
    key2.setField("advertiser", "blockbuster");
    GPOMutable agg1 = new GPOMutable(fdagg);
    agg1.setField("clicks", 10L);
    agg1.setField("impressions", 11L);
    GPOMutable agg2 = new GPOMutable(fdagg);
    agg2.setField("clicks", 5L);
    agg2.setField("impressions", 4L);
    List<GPOMutable> keys = Lists.newArrayList(key1, key2);
    List<GPOMutable> aggs = Lists.newArrayList(agg1, agg2);
    gpo.getFieldsObject()[0] = fdkey;
    gpo.getFieldsObject()[1] = fdagg;
    gpo.getFieldsObject()[2] = keys;
    gpo.getFieldsObject()[3] = aggs;
    GPOByteArrayList bal = new GPOByteArrayList();
    byte[] serialized = GPOUtils.serialize(gpo, bal);
    GPOMutable newGPO = GPOUtils.deserialize(metaDataFD, serialized, new MutableInt(0));
    newGPO.setFieldDescriptor(metaDataFD);
    Assert.assertEquals(gpo, newGPO);
    Assert.assertArrayEquals(gpo.getFieldsObject(), newGPO.getFieldsObject());
}
Also used : Type(org.apache.apex.malhar.lib.appdata.schemas.Type) MutableInt(org.apache.commons.lang3.mutable.MutableInt) FieldsDescriptor(org.apache.apex.malhar.lib.appdata.schemas.FieldsDescriptor) Test(org.junit.Test)

Example 13 with FieldsDescriptor

use of org.apache.apex.malhar.lib.appdata.schemas.FieldsDescriptor in project apex-malhar by apache.

the class GPOUtilsTest method testDeserializeToMap.

@Test
@SuppressWarnings("AssertEqualsBetweenInconvertibleTypes")
public void testDeserializeToMap() throws Exception {
    Map<String, Type> fieldToType = Maps.newHashMap();
    fieldToType.put("name", Type.STRING);
    fieldToType.put("longvals", Type.LONG);
    fieldToType.put("stringvals", Type.STRING);
    FieldsDescriptor fd = new FieldsDescriptor(fieldToType);
    String json = SchemaUtils.jarResourceFileToString("deserializeToMapTest.json");
    Map<String, Set<Object>> resultMap = GPOUtils.deserializeToMap(fd, new JSONObject(json));
    Set<Object> names = resultMap.get("name");
    Set<Object> longvals = resultMap.get("longvals");
    Set<Object> stringvals = resultMap.get("stringvals");
    Assert.assertEquals(Sets.newHashSet("tim"), names);
    Assert.assertEquals(Sets.newHashSet(1L, 2L, 3L), longvals);
    Assert.assertEquals(Sets.newHashSet("a", "b", "c"), stringvals);
}
Also used : Type(org.apache.apex.malhar.lib.appdata.schemas.Type) Set(java.util.Set) JSONObject(org.codehaus.jettison.json.JSONObject) FieldsDescriptor(org.apache.apex.malhar.lib.appdata.schemas.FieldsDescriptor) JSONObject(org.codehaus.jettison.json.JSONObject) Test(org.junit.Test)

Example 14 with FieldsDescriptor

use of org.apache.apex.malhar.lib.appdata.schemas.FieldsDescriptor in project apex-malhar by apache.

the class GPOUtilsTest method testSerializationLength.

@Test
public void testSerializationLength() {
    final Map<String, Type> fieldToType = Maps.newHashMap();
    final String tboolean = "tboolean";
    final Boolean tbooleanv = true;
    final String tchar = "tchar";
    final Character tcharv = 'A';
    final String tstring = "tstring";
    final String tstringv = "hello";
    final String tfloat = "tfloat";
    final Float tfloatv = 1.0f;
    final String tdouble = "tdouble";
    final Double tdoublev = 2.0;
    final String tbyte = "tbyte";
    final Byte tbytev = 50;
    final String tshort = "tshort";
    final Short tshortv = 1000;
    final String tinteger = "tinteger";
    final Integer tintegerv = 100000;
    final String tlong = "tlong";
    final Long tlongv = 10000000000L;
    int totalBytes = // boolean
    1 + // char
    2 + 4 + // string
    tstringv.getBytes().length + // float
    4 + // double
    8 + // byte
    1 + // short
    2 + // int
    4 + // long
    8;
    fieldToType.put(tboolean, Type.BOOLEAN);
    fieldToType.put(tchar, Type.CHAR);
    fieldToType.put(tstring, Type.STRING);
    fieldToType.put(tfloat, Type.FLOAT);
    fieldToType.put(tdouble, Type.DOUBLE);
    fieldToType.put(tbyte, Type.BYTE);
    fieldToType.put(tshort, Type.SHORT);
    fieldToType.put(tinteger, Type.INTEGER);
    fieldToType.put(tlong, Type.LONG);
    FieldsDescriptor fd = new FieldsDescriptor(fieldToType);
    GPOMutable gpo = new GPOMutable(fd);
    gpo.setFieldGeneric(tboolean, tbooleanv);
    gpo.setFieldGeneric(tchar, tcharv);
    gpo.setField(tstring, tstringv);
    gpo.setFieldGeneric(tfloat, tfloatv);
    gpo.setFieldGeneric(tdouble, tdoublev);
    gpo.setFieldGeneric(tbyte, tbytev);
    gpo.setFieldGeneric(tshort, tshortv);
    gpo.setFieldGeneric(tinteger, tintegerv);
    gpo.setFieldGeneric(tlong, tlongv);
    int serializeLength = GPOUtils.serializedLength(gpo);
    Assert.assertEquals("The serialized byte length is incorrect.", totalBytes, serializeLength);
}
Also used : FieldsDescriptor(org.apache.apex.malhar.lib.appdata.schemas.FieldsDescriptor) Type(org.apache.apex.malhar.lib.appdata.schemas.Type) Test(org.junit.Test)

Example 15 with FieldsDescriptor

use of org.apache.apex.malhar.lib.appdata.schemas.FieldsDescriptor in project apex-malhar by apache.

the class SerdeListGPOMutableTest method testSerialization.

@Test
public void testSerialization() {
    final Map<String, Type> fieldToType = Maps.newHashMap();
    final String tboolean = "tboolean";
    final Boolean tbooleanv = true;
    final Boolean tbooleanv1 = true;
    final String tchar = "tchar";
    final Character tcharv = 'A';
    final Character tcharv1 = 'b';
    final String tstring = "tstring";
    final String tstringv = "hello";
    final String tstringv1 = "hello1";
    final String tfloat = "tfloat";
    final Float tfloatv = 1.0f;
    final Float tfloatv1 = 2.0f;
    final String tdouble = "tdouble";
    final Double tdoublev = 2.0;
    final Double tdoublev1 = 3.0;
    final String tbyte = "tbyte";
    final Byte tbytev = 50;
    final Byte tbytev1 = 55;
    final String tshort = "tshort";
    final Short tshortv = 1000;
    final Short tshortv1 = 1300;
    final String tinteger = "tinteger";
    final Integer tintegerv = 100000;
    final Integer tintegerv1 = 133000;
    final String tlong = "tlong";
    final Long tlongv = 10000000000L;
    final Long tlongv1 = 10044400000L;
    fieldToType.put(tboolean, Type.BOOLEAN);
    fieldToType.put(tchar, Type.CHAR);
    fieldToType.put(tstring, Type.STRING);
    fieldToType.put(tfloat, Type.FLOAT);
    fieldToType.put(tdouble, Type.DOUBLE);
    fieldToType.put(tbyte, Type.BYTE);
    fieldToType.put(tshort, Type.SHORT);
    fieldToType.put(tinteger, Type.INTEGER);
    fieldToType.put(tlong, Type.LONG);
    FieldsDescriptor fd = new FieldsDescriptor(fieldToType);
    GPOMutable gpo = new GPOMutable(fd);
    gpo.setFieldGeneric(tboolean, tbooleanv);
    gpo.setFieldGeneric(tchar, tcharv);
    gpo.setField(tstring, tstringv);
    gpo.setFieldGeneric(tfloat, tfloatv);
    gpo.setFieldGeneric(tdouble, tdoublev);
    gpo.setFieldGeneric(tbyte, tbytev);
    gpo.setFieldGeneric(tshort, tshortv);
    gpo.setFieldGeneric(tinteger, tintegerv);
    gpo.setFieldGeneric(tlong, tlongv);
    GPOMutable gpo1 = new GPOMutable(fd);
    gpo1.setFieldGeneric(tboolean, tbooleanv1);
    gpo1.setFieldGeneric(tchar, tcharv1);
    gpo1.setField(tstring, tstringv1);
    gpo1.setFieldGeneric(tfloat, tfloatv1);
    gpo1.setFieldGeneric(tdouble, tdoublev1);
    gpo1.setFieldGeneric(tbyte, tbytev1);
    gpo1.setFieldGeneric(tshort, tshortv1);
    gpo1.setFieldGeneric(tinteger, tintegerv1);
    gpo1.setFieldGeneric(tlong, tlongv1);
    List<GPOMutable> mutables = Lists.newArrayList(gpo, gpo1);
    byte[] bytes = SerdeListGPOMutable.INSTANCE.serializeObject(mutables);
    MutableInt offset = new MutableInt(0);
    @SuppressWarnings("unchecked") List<GPOMutable> newMutables = (List<GPOMutable>) SerdeListGPOMutable.INSTANCE.deserializeObject(bytes, offset);
    Assert.assertEquals(mutables, newMutables);
    Assert.assertEquals(bytes.length, offset.intValue());
}
Also used : FieldsDescriptor(org.apache.apex.malhar.lib.appdata.schemas.FieldsDescriptor) Type(org.apache.apex.malhar.lib.appdata.schemas.Type) MutableInt(org.apache.commons.lang3.mutable.MutableInt) List(java.util.List) Test(org.junit.Test)

Aggregations

FieldsDescriptor (org.apache.apex.malhar.lib.appdata.schemas.FieldsDescriptor)24 Type (org.apache.apex.malhar.lib.appdata.schemas.Type)19 Test (org.junit.Test)10 Map (java.util.Map)5 GPOMutable (org.apache.apex.malhar.lib.appdata.gpo.GPOMutable)4 MutableInt (org.apache.commons.lang3.mutable.MutableInt)4 JSONObject (org.codehaus.jettison.json.JSONObject)3 PreparedStatement (java.sql.PreparedStatement)2 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 EventKey (org.apache.apex.malhar.lib.dimensions.DimensionsEvent.EventKey)2 Int2ObjectMap (it.unimi.dsi.fastutil.ints.Int2ObjectMap)1 Set (java.util.Set)1 Serde (org.apache.apex.malhar.lib.appdata.gpo.Serde)1 DimensionalConfigurationSchema (org.apache.apex.malhar.lib.appdata.schemas.DimensionalConfigurationSchema)1 Fields (org.apache.apex.malhar.lib.appdata.schemas.Fields)1