gideonkorir commented on a change in pull request #3509: NIFI-6295 fix deserialization issues with NiFiRecordSerDe for hive3streaming URL: https://github.com/apache/nifi/pull/3509#discussion_r313978834
########## File path: nifi-nar-bundles/nifi-hive-bundle/nifi-hive3-processors/src/test/java/org/apache/hive/streaming/TestNiFiRecordSerDe.java ########## @@ -0,0 +1,360 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.hive.streaming; + +import org.apache.hadoop.hive.common.type.Date; +import org.apache.hadoop.hive.common.type.HiveDecimal; +import org.apache.hadoop.hive.common.type.Timestamp; +import org.apache.hadoop.hive.serde.serdeConstants; +import org.apache.hadoop.hive.serde2.SerDeException; +import org.apache.hadoop.io.ObjectWritable; +import org.apache.nifi.serialization.SimpleRecordSchema; +import org.apache.nifi.serialization.record.DataType; +import org.apache.nifi.serialization.record.MapRecord; +import org.apache.nifi.serialization.record.Record; +import org.apache.nifi.serialization.record.RecordField; +import org.apache.nifi.serialization.record.RecordFieldType; +import org.apache.nifi.serialization.record.RecordSchema; +import org.apache.nifi.util.MockComponentLog; +import org.junit.Test; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Properties; + +import static org.junit.Assert.assertEquals; + +public class TestNiFiRecordSerDe { + + @Test + public void testSimpleFields() throws SerDeException { + NiFiRecordSerDe serDe = createSerDe("bytec,shortc,intc,longc,boolc,floatc,doublec,stringc,varcharc,charc,binaryc,datec,timestampc,decimalc", + "tinyint:smallint:int:bigint:boolean:float:double:string:varchar(50):char(1):binary:date:timestamp:decimal" + ); + RecordSchema schema = new SimpleRecordSchema( + Arrays.asList( + new RecordField("bytec", RecordFieldType.BYTE.getDataType()), + new RecordField("shortc", RecordFieldType.SHORT.getDataType()), + new RecordField("intc", RecordFieldType.INT.getDataType()), + new RecordField("longc", RecordFieldType.LONG.getDataType()), + new RecordField("boolc", RecordFieldType.BOOLEAN.getDataType()), + new RecordField("floatc", RecordFieldType.FLOAT.getDataType()), + new RecordField("doublec", RecordFieldType.DOUBLE.getDataType()), + new RecordField("stringc", RecordFieldType.STRING.getDataType()), + new RecordField("varcharc", RecordFieldType.STRING.getDataType()), + new RecordField("charc", RecordFieldType.CHAR.getDataType()), + new RecordField("binaryc", RecordFieldType.ARRAY.getArrayDataType(RecordFieldType.BYTE.getDataType())), + new RecordField("datec", RecordFieldType.DATE.getDataType("yyyy-MM-dd")), + new RecordField("timestampc", RecordFieldType.TIMESTAMP.getDataType("yyyy-MM-dd HH:mm:ss")), + new RecordField("decimalc", RecordFieldType.DOUBLE.getDataType()) + ) + ); + + long currentTimeMillis = System.currentTimeMillis(); + + Object deserialized = serDe.deserialize(new ObjectWritable(new MapRecord(schema, new HashMap(){ + { + put("bytec", Byte.valueOf((byte)2)); + put("shortc", Short.valueOf((short)45)); + put("intc", Integer.valueOf(95)); + put("longc", Long.valueOf(876L)); + put("boolc", Boolean.TRUE); + put("floatc", 4.56f); + put("doublec", 2.3445); + put("stringc", "test"); + put("varcharc", "test2"); + put("charc", 'c'); + put("binaryc", new byte[]{ (byte)1 }); + put("datec", new java.sql.Date(currentTimeMillis)); + put("timestampc", new java.sql.Timestamp(currentTimeMillis)); + put("decimalc", 0.45); + } + }))); + assert(deserialized instanceof List); + Date date = new Date(); + date.setTimeInMillis(currentTimeMillis); + Timestamp ts = new Timestamp(); + ts.setTimeInMillis(currentTimeMillis); + + List<Object> fields = (List<Object>)deserialized; + assertEquals(Byte.valueOf("2"), fields.get(0)); + assertEquals(Short.valueOf("45"), fields.get(1)); + assertEquals(Integer.valueOf(95), fields.get(2)); + assertEquals(Long.valueOf(876L), fields.get(3)); + assertEquals(Boolean.TRUE, fields.get(4)); + assertEquals(4.56f, fields.get(5)); + assertEquals(2.3445, fields.get(6)); + assertEquals("test", fields.get(7)); + assertEquals("test2", fields.get(8)); + assertEquals("c", fields.get(9)); + //assertEquals(AvroTypeUtil.convertByteArray(new Object[]{ (byte)1 }).array(), (byte[])fields.get(10)); Review comment: I'm thinking it probably should, would have caught the bug you saw ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
