Search in sources :

Example 26 with TransformContext

use of io.cdap.cdap.etl.api.TransformContext in project hydrator-plugins by cdapio.

the class ProjectionTransformTest method testRenameFields.

@Test
public void testRenameFields() throws Exception {
    Schema schema = Schema.recordOf("three", Schema.Field.of("x", Schema.of(Schema.Type.INT)), Schema.Field.of("y", Schema.of(Schema.Type.DOUBLE)), Schema.Field.of("z", Schema.arrayOf(Schema.of(Schema.Type.INT))));
    StructuredRecord input = StructuredRecord.builder(schema).set("x", 1).set("y", 3.14).set("z", new int[] { 1, 2, 3 }).build();
    ProjectionTransform.ProjectionTransformConfig config = new ProjectionTransform.ProjectionTransformConfig(null, "x:y,y:z,z:x", null, null);
    Transform<StructuredRecord, StructuredRecord> transform = new ProjectionTransform(config);
    TransformContext transformContext = new MockTransformContext();
    transform.initialize(transformContext);
    MockEmitter<StructuredRecord> emitter = new MockEmitter<>();
    transform.transform(input, emitter);
    StructuredRecord output = emitter.getEmitted().get(0);
    Schema expectedSchema = Schema.recordOf("three.projected", Schema.Field.of("y", Schema.of(Schema.Type.INT)), Schema.Field.of("z", Schema.of(Schema.Type.DOUBLE)), Schema.Field.of("x", Schema.arrayOf(Schema.of(Schema.Type.INT))));
    Assert.assertEquals(expectedSchema, output.getSchema());
    Assert.assertEquals(1, output.<Integer>get("y").intValue());
    Assert.assertTrue(Math.abs(3.14 - (Double) output.get("z")) < 0.000001);
    Assert.assertArrayEquals(new int[] { 1, 2, 3 }, (int[]) output.get("x"));
}
Also used : MockTransformContext(io.cdap.cdap.etl.mock.transform.MockTransformContext) TransformContext(io.cdap.cdap.etl.api.TransformContext) MockTransformContext(io.cdap.cdap.etl.mock.transform.MockTransformContext) MockEmitter(io.cdap.cdap.etl.mock.common.MockEmitter) Schema(io.cdap.cdap.api.data.schema.Schema) StructuredRecord(io.cdap.cdap.api.data.format.StructuredRecord) Test(org.junit.Test)

Example 27 with TransformContext

use of io.cdap.cdap.etl.api.TransformContext in project hydrator-plugins by cdapio.

the class ProjectionTransformTest method testConvertToFloat.

@Test
public void testConvertToFloat() throws Exception {
    ProjectionTransform.ProjectionTransformConfig config = new ProjectionTransform.ProjectionTransformConfig(null, null, "intField:float,longField:float", null);
    Transform<StructuredRecord, StructuredRecord> transform = new ProjectionTransform(config);
    TransformContext transformContext = new MockTransformContext();
    transform.initialize(transformContext);
    MockEmitter<StructuredRecord> emitter = new MockEmitter<>();
    transform.transform(SIMPLE_TYPES_RECORD, emitter);
    StructuredRecord output = emitter.getEmitted().get(0);
    Schema expectedSchema = Schema.recordOf("record.projected", Schema.Field.of("booleanField", Schema.of(Schema.Type.BOOLEAN)), Schema.Field.of("intField", Schema.of(Schema.Type.FLOAT)), Schema.Field.of("longField", Schema.of(Schema.Type.FLOAT)), Schema.Field.of("floatField", Schema.of(Schema.Type.FLOAT)), Schema.Field.of("doubleField", Schema.of(Schema.Type.DOUBLE)), Schema.Field.of("bytesField", Schema.of(Schema.Type.BYTES)), Schema.Field.of("stringField", Schema.of(Schema.Type.STRING)));
    Assert.assertEquals(expectedSchema, output.getSchema());
    Assert.assertTrue((Boolean) output.get("booleanField"));
    Assert.assertEquals(28f, output.get("intField"), 0.0001f);
    Assert.assertEquals(99f, output.get("longField"), 0.0001f);
    Assert.assertTrue(Math.abs(2.71f - (Float) output.get("floatField")) < 0.000001);
    Assert.assertTrue(Math.abs(3.14 - (Double) output.get("doubleField")) < 0.000001);
    Assert.assertArrayEquals(Bytes.toBytes("foo"), (byte[]) output.get("bytesField"));
    Assert.assertEquals("bar", output.get("stringField"));
}
Also used : MockTransformContext(io.cdap.cdap.etl.mock.transform.MockTransformContext) TransformContext(io.cdap.cdap.etl.api.TransformContext) MockTransformContext(io.cdap.cdap.etl.mock.transform.MockTransformContext) MockEmitter(io.cdap.cdap.etl.mock.common.MockEmitter) Schema(io.cdap.cdap.api.data.schema.Schema) StructuredRecord(io.cdap.cdap.api.data.format.StructuredRecord) Test(org.junit.Test)

Example 28 with TransformContext

use of io.cdap.cdap.etl.api.TransformContext in project hydrator-plugins by cdapio.

the class ProjectionTransformTest method testDropFields.

@Test
public void testDropFields() throws Exception {
    Schema schema = Schema.recordOf("three", Schema.Field.of("x", Schema.of(Schema.Type.INT)), Schema.Field.of("y", Schema.of(Schema.Type.DOUBLE)), Schema.Field.of("z", Schema.arrayOf(Schema.of(Schema.Type.INT))));
    StructuredRecord input = StructuredRecord.builder(schema).set("x", 1).set("y", 3.14).set("z", new int[] { 1, 2, 3 }).build();
    ProjectionTransform.ProjectionTransformConfig config = new ProjectionTransform.ProjectionTransformConfig("y, z", null, null, null);
    Transform<StructuredRecord, StructuredRecord> transform = new ProjectionTransform(config);
    TransformContext transformContext = new MockTransformContext();
    transform.initialize(transformContext);
    MockEmitter<StructuredRecord> emitter = new MockEmitter<>();
    transform.transform(input, emitter);
    StructuredRecord output = emitter.getEmitted().get(0);
    Schema expectedSchema = Schema.recordOf("three.projected", Schema.Field.of("x", Schema.of(Schema.Type.INT)));
    Assert.assertEquals(expectedSchema, output.getSchema());
    Assert.assertEquals(1, output.<Integer>get("x").intValue());
}
Also used : MockTransformContext(io.cdap.cdap.etl.mock.transform.MockTransformContext) TransformContext(io.cdap.cdap.etl.api.TransformContext) MockTransformContext(io.cdap.cdap.etl.mock.transform.MockTransformContext) MockEmitter(io.cdap.cdap.etl.mock.common.MockEmitter) Schema(io.cdap.cdap.api.data.schema.Schema) StructuredRecord(io.cdap.cdap.api.data.format.StructuredRecord) Test(org.junit.Test)

Example 29 with TransformContext

use of io.cdap.cdap.etl.api.TransformContext in project hydrator-plugins by cdapio.

the class ProjectionTransformTest method testConvertToLong.

@Test
public void testConvertToLong() throws Exception {
    ProjectionTransform.ProjectionTransformConfig config = new ProjectionTransform.ProjectionTransformConfig(null, null, "intField:long,floatField:long,doubleField:long", null);
    Transform<StructuredRecord, StructuredRecord> transform = new ProjectionTransform(config);
    TransformContext transformContext = new MockTransformContext();
    transform.initialize(transformContext);
    MockEmitter<StructuredRecord> emitter = new MockEmitter<>();
    transform.transform(SIMPLE_TYPES_RECORD, emitter);
    StructuredRecord output = emitter.getEmitted().get(0);
    Schema expectedSchema = Schema.recordOf("record.projected", Schema.Field.of("booleanField", Schema.of(Schema.Type.BOOLEAN)), Schema.Field.of("intField", Schema.of(Schema.Type.LONG)), Schema.Field.of("longField", Schema.of(Schema.Type.LONG)), Schema.Field.of("floatField", Schema.of(Schema.Type.LONG)), Schema.Field.of("doubleField", Schema.of(Schema.Type.LONG)), Schema.Field.of("bytesField", Schema.of(Schema.Type.BYTES)), Schema.Field.of("stringField", Schema.of(Schema.Type.STRING)));
    Assert.assertEquals(expectedSchema, output.getSchema());
    Assert.assertTrue((Boolean) output.get("booleanField"));
    Assert.assertEquals(28L, output.<Long>get("intField").longValue());
    Assert.assertEquals(99L, output.<Long>get("longField").longValue());
    Assert.assertEquals(3, output.<Long>get("floatField").longValue());
    Assert.assertEquals(3, output.<Long>get("doubleField").longValue());
    Assert.assertArrayEquals(Bytes.toBytes("foo"), (byte[]) output.get("bytesField"));
    Assert.assertEquals("bar", output.get("stringField"));
}
Also used : MockTransformContext(io.cdap.cdap.etl.mock.transform.MockTransformContext) TransformContext(io.cdap.cdap.etl.api.TransformContext) MockTransformContext(io.cdap.cdap.etl.mock.transform.MockTransformContext) MockEmitter(io.cdap.cdap.etl.mock.common.MockEmitter) Schema(io.cdap.cdap.api.data.schema.Schema) StructuredRecord(io.cdap.cdap.api.data.format.StructuredRecord) Test(org.junit.Test)

Example 30 with TransformContext

use of io.cdap.cdap.etl.api.TransformContext in project hydrator-plugins by cdapio.

the class DecoderTest method testBase32Decoder.

@Test
public void testBase32Decoder() throws Exception {
    String test = "This is a test for testing base32 decoding";
    Transform<StructuredRecord, StructuredRecord> encoder = new Encoder(new Encoder.Config("a:BASE32", OUTPUT.toString()));
    TransformContext encoderContext = new MockTransformContext();
    encoder.initialize(encoderContext);
    MockEmitter<StructuredRecord> emitterEncoded = new MockEmitter<>();
    encoder.transform(StructuredRecord.builder(INPUT).set("a", test).set("b", "2").set("c", "3").set("d", "4").set("e", "5").build(), emitterEncoded);
    Base32 base32 = new Base32();
    byte[] expected = base32.encode(test.getBytes("UTF-8"));
    byte[] actual = emitterEncoded.getEmitted().get(0).get("a");
    Assert.assertEquals(2, emitterEncoded.getEmitted().get(0).getSchema().getFields().size());
    Assert.assertArrayEquals(expected, actual);
    Transform<StructuredRecord, StructuredRecord> decoder = new Decoder(new Decoder.Config("a:BASE32", OUTPUTSTR.toString()));
    MockTransformContext context = new MockTransformContext();
    decoder.initialize(context);
    MockEmitter<StructuredRecord> emitterDecoded = new MockEmitter<>();
    decoder.transform(emitterEncoded.getEmitted().get(0), emitterDecoded);
    Assert.assertEquals(2, emitterDecoded.getEmitted().get(0).getSchema().getFields().size());
    Assert.assertEquals(test, emitterDecoded.getEmitted().get(0).get("a"));
    Assert.assertEquals(0, encoderContext.getFailureCollector().getValidationFailures().size());
}
Also used : MockTransformContext(io.cdap.cdap.etl.mock.transform.MockTransformContext) MockEmitter(io.cdap.cdap.etl.mock.common.MockEmitter) StructuredRecord(io.cdap.cdap.api.data.format.StructuredRecord) TransformContext(io.cdap.cdap.etl.api.TransformContext) MockTransformContext(io.cdap.cdap.etl.mock.transform.MockTransformContext) Base32(org.apache.commons.codec.binary.Base32) Test(org.junit.Test)

Aggregations

TransformContext (io.cdap.cdap.etl.api.TransformContext)37 StructuredRecord (io.cdap.cdap.api.data.format.StructuredRecord)35 MockTransformContext (io.cdap.cdap.etl.mock.transform.MockTransformContext)35 Test (org.junit.Test)34 MockEmitter (io.cdap.cdap.etl.mock.common.MockEmitter)30 Schema (io.cdap.cdap.api.data.schema.Schema)15 ValidationException (io.cdap.cdap.etl.api.validation.ValidationException)4 Base32 (org.apache.commons.codec.binary.Base32)4 Base64 (org.apache.commons.codec.binary.Base64)4 Cause (io.cdap.cdap.etl.api.validation.ValidationFailure.Cause)3 MockPipelineConfigurer (io.cdap.cdap.etl.mock.common.MockPipelineConfigurer)3 StageMetrics (io.cdap.cdap.etl.api.StageMetrics)2 DefaultStageMetrics (io.cdap.cdap.etl.common.DefaultStageMetrics)2 NoopStageStatisticsCollector (io.cdap.cdap.etl.common.NoopStageStatisticsCollector)2 StageStatisticsCollector (io.cdap.cdap.etl.common.StageStatisticsCollector)2 TrackedMultiOutputTransform (io.cdap.cdap.etl.common.TrackedMultiOutputTransform)2 Hex (org.apache.commons.codec.binary.Hex)2 ImmutableList (com.google.common.collect.ImmutableList)1 MockMultiOutputEmitter (io.cdap.cdap.etl.mock.common.MockMultiOutputEmitter)1 HashMap (java.util.HashMap)1