@Test public void testDeserialize() { tsFileDeserializer = new TsFileDeserializer(); assertEquals(PrimitiveObjectInspector.PrimitiveCategory.LONG, ((PrimitiveTypeInfo)columnTypes.get(0)).getPrimitiveCategory()); Writable worryWritable1 = new Text(); try { tsFileDeserializer.deserialize(columnNames, columnTypes, worryWritable1, "device_1"); fail("Expect a TsFileSerDeException to be thrown!"); } catch (TsFileSerDeException e) { assertEquals("Expecting a MapWritable", e.getMessage()); } MapWritable worryWritable2 = new MapWritable(); worryWritable2.put(new Text("device_id"), new Text("device_2")); worryWritable2.put(new Text("time_stamp"), new LongWritable(1L)); worryWritable2.put(new Text("sensor_1"), new LongWritable(1L)); try { assertNull(tsFileDeserializer.deserialize(columnNames, columnTypes, worryWritable2, "device_1")); } catch (TsFileSerDeException e) { fail("Don't expect a TsFileSerDeException to be Thrown!"); } MapWritable worryWritable3 = new MapWritable(); worryWritable3.put(new Text("device_id"), new Text("device_1")); worryWritable3.put(new Text("time_stamp"), new LongWritable(1L)); worryWritable3.put(new Text("sensor_1"), new IntWritable(1)); try { tsFileDeserializer.deserialize(columnNames, columnTypes, worryWritable3, "device_1"); fail("Expect a TsFileSerDeException to be thrown!"); } catch (TsFileSerDeException e) { assertEquals("Unexpected data type: " + worryWritable3.get(new Text("sensor_1")).getClass().getName() + " for Date TypeInfo: " + PrimitiveObjectInspector.PrimitiveCategory.LONG, e.getMessage()); } MapWritable writable = new MapWritable(); writable.put(new Text("device_id"), new Text("device_1")); writable.put(new Text("time_stamp"), new LongWritable(1L)); writable.put(new Text("sensor_1"), new LongWritable(1000000L)); try { Object result = tsFileDeserializer.deserialize(columnNames, columnTypes, writable, "device_1"); assertTrue(result instanceof List); List<Object> row = (List<Object>) result; assertEquals(columnNames.size(), row.size()); assertEquals(1L, row.get(0)); assertEquals(1000000L, row.get(1)); } catch (TsFileSerDeException e) { fail("Don't expect a TsFileSerDeException to be Thrown!"); } }
@SuppressWarnings("squid:S3776") public Object deserialize(List<String> columnNames, List<TypeInfo> columnTypes, Writable writable, String deviceId) throws TsFileSerDeException { if(!(writable instanceof MapWritable)) { throw new TsFileSerDeException("Expecting a MapWritable"); } if(row == null || row.size() != columnNames.size()) { row = new ArrayList<>(columnNames.size()); } else { row.clear(); } MapWritable mapWritable = (MapWritable) writable; if (!Objects.equals(mapWritable.get(new Text("device_id")).toString(), deviceId)) { return null; } LOG.debug("device_id: {}", mapWritable.get(new Text("device_id")).toString()); LOG.debug("time_stamp: {}" , mapWritable.get(new Text("time_stamp"))); for(int i = 0; i < columnNames.size(); i++) { TypeInfo columnType = columnTypes.get(i); String columnName = columnNames.get(i); Writable data = mapWritable.get(new Text(columnName)); if (data == null || data instanceof NullWritable) { row.add(null); continue; } if (columnType.getCategory() != ObjectInspector.Category.PRIMITIVE) { throw new TsFileSerDeException("Unknown TypeInfo: " + columnType.getCategory()); } PrimitiveObjectInspector.PrimitiveCategory type = ((PrimitiveTypeInfo) columnType).getPrimitiveCategory(); switch (type) { case BOOLEAN: if (data instanceof BooleanWritable) { row.add(((BooleanWritable)data).get()); } else { throw new TsFileSerDeException("Unexpected data type: " + data.getClass().getName() + " for Date TypeInfo: " + type); } break; case INT: if (data instanceof IntWritable) { row.add(((IntWritable)data).get()); } else { throw new TsFileSerDeException("Unexpected data type: " + data.getClass().getName() + " for Date TypeInfo: " + type); } break; case LONG: if (data instanceof LongWritable) { row.add(((LongWritable)data).get()); } else { throw new TsFileSerDeException("Unexpected data type: " + data.getClass().getName() + " for Date TypeInfo: " + type); } break; case FLOAT: if (data instanceof FloatWritable) { row.add(((FloatWritable)data).get()); } else { throw new TsFileSerDeException("Unexpected data type: " + data.getClass().getName() + " for Date TypeInfo: " + type); } break; case DOUBLE: if (data instanceof DoubleWritable) { row.add(((DoubleWritable)data).get()); } else { throw new TsFileSerDeException("Unexpected data type: " + data.getClass().getName() + " for Date TypeInfo: " + type); } break; case STRING: if (data instanceof Text) { row.add(data.toString()); } else { throw new TsFileSerDeException("Unexpected data type: " + data.getClass().getName() + " for Date TypeInfo: " + type); } break; case TIMESTAMP: if (data instanceof LongWritable) { row.add(new Timestamp(((LongWritable)data).get())); } else { throw new TsFileSerDeException("Unexpected data type: " + data.getClass().getName() + " for Date TypeInfo: " + type); } break; default: throw new TsFileSerDeException("Unknown TypeInfo: " + columnType.getCategory()); } } return row; }
TsFileDeserializer { @SuppressWarnings("squid:S3776") public Object deserialize(List<String> columnNames, List<TypeInfo> columnTypes, Writable writable, String deviceId) throws TsFileSerDeException { if(!(writable instanceof MapWritable)) { throw new TsFileSerDeException("Expecting a MapWritable"); } if(row == null || row.size() != columnNames.size()) { row = new ArrayList<>(columnNames.size()); } else { row.clear(); } MapWritable mapWritable = (MapWritable) writable; if (!Objects.equals(mapWritable.get(new Text("device_id")).toString(), deviceId)) { return null; } LOG.debug("device_id: {}", mapWritable.get(new Text("device_id")).toString()); LOG.debug("time_stamp: {}" , mapWritable.get(new Text("time_stamp"))); for(int i = 0; i < columnNames.size(); i++) { TypeInfo columnType = columnTypes.get(i); String columnName = columnNames.get(i); Writable data = mapWritable.get(new Text(columnName)); if (data == null || data instanceof NullWritable) { row.add(null); continue; } if (columnType.getCategory() != ObjectInspector.Category.PRIMITIVE) { throw new TsFileSerDeException("Unknown TypeInfo: " + columnType.getCategory()); } PrimitiveObjectInspector.PrimitiveCategory type = ((PrimitiveTypeInfo) columnType).getPrimitiveCategory(); switch (type) { case BOOLEAN: if (data instanceof BooleanWritable) { row.add(((BooleanWritable)data).get()); } else { throw new TsFileSerDeException("Unexpected data type: " + data.getClass().getName() + " for Date TypeInfo: " + type); } break; case INT: if (data instanceof IntWritable) { row.add(((IntWritable)data).get()); } else { throw new TsFileSerDeException("Unexpected data type: " + data.getClass().getName() + " for Date TypeInfo: " + type); } break; case LONG: if (data instanceof LongWritable) { row.add(((LongWritable)data).get()); } else { throw new TsFileSerDeException("Unexpected data type: " + data.getClass().getName() + " for Date TypeInfo: " + type); } break; case FLOAT: if (data instanceof FloatWritable) { row.add(((FloatWritable)data).get()); } else { throw new TsFileSerDeException("Unexpected data type: " + data.getClass().getName() + " for Date TypeInfo: " + type); } break; case DOUBLE: if (data instanceof DoubleWritable) { row.add(((DoubleWritable)data).get()); } else { throw new TsFileSerDeException("Unexpected data type: " + data.getClass().getName() + " for Date TypeInfo: " + type); } break; case STRING: if (data instanceof Text) { row.add(data.toString()); } else { throw new TsFileSerDeException("Unexpected data type: " + data.getClass().getName() + " for Date TypeInfo: " + type); } break; case TIMESTAMP: if (data instanceof LongWritable) { row.add(new Timestamp(((LongWritable)data).get())); } else { throw new TsFileSerDeException("Unexpected data type: " + data.getClass().getName() + " for Date TypeInfo: " + type); } break; default: throw new TsFileSerDeException("Unknown TypeInfo: " + columnType.getCategory()); } } return row; } }
TsFileDeserializer { @SuppressWarnings("squid:S3776") public Object deserialize(List<String> columnNames, List<TypeInfo> columnTypes, Writable writable, String deviceId) throws TsFileSerDeException { if(!(writable instanceof MapWritable)) { throw new TsFileSerDeException("Expecting a MapWritable"); } if(row == null || row.size() != columnNames.size()) { row = new ArrayList<>(columnNames.size()); } else { row.clear(); } MapWritable mapWritable = (MapWritable) writable; if (!Objects.equals(mapWritable.get(new Text("device_id")).toString(), deviceId)) { return null; } LOG.debug("device_id: {}", mapWritable.get(new Text("device_id")).toString()); LOG.debug("time_stamp: {}" , mapWritable.get(new Text("time_stamp"))); for(int i = 0; i < columnNames.size(); i++) { TypeInfo columnType = columnTypes.get(i); String columnName = columnNames.get(i); Writable data = mapWritable.get(new Text(columnName)); if (data == null || data instanceof NullWritable) { row.add(null); continue; } if (columnType.getCategory() != ObjectInspector.Category.PRIMITIVE) { throw new TsFileSerDeException("Unknown TypeInfo: " + columnType.getCategory()); } PrimitiveObjectInspector.PrimitiveCategory type = ((PrimitiveTypeInfo) columnType).getPrimitiveCategory(); switch (type) { case BOOLEAN: if (data instanceof BooleanWritable) { row.add(((BooleanWritable)data).get()); } else { throw new TsFileSerDeException("Unexpected data type: " + data.getClass().getName() + " for Date TypeInfo: " + type); } break; case INT: if (data instanceof IntWritable) { row.add(((IntWritable)data).get()); } else { throw new TsFileSerDeException("Unexpected data type: " + data.getClass().getName() + " for Date TypeInfo: " + type); } break; case LONG: if (data instanceof LongWritable) { row.add(((LongWritable)data).get()); } else { throw new TsFileSerDeException("Unexpected data type: " + data.getClass().getName() + " for Date TypeInfo: " + type); } break; case FLOAT: if (data instanceof FloatWritable) { row.add(((FloatWritable)data).get()); } else { throw new TsFileSerDeException("Unexpected data type: " + data.getClass().getName() + " for Date TypeInfo: " + type); } break; case DOUBLE: if (data instanceof DoubleWritable) { row.add(((DoubleWritable)data).get()); } else { throw new TsFileSerDeException("Unexpected data type: " + data.getClass().getName() + " for Date TypeInfo: " + type); } break; case STRING: if (data instanceof Text) { row.add(data.toString()); } else { throw new TsFileSerDeException("Unexpected data type: " + data.getClass().getName() + " for Date TypeInfo: " + type); } break; case TIMESTAMP: if (data instanceof LongWritable) { row.add(new Timestamp(((LongWritable)data).get())); } else { throw new TsFileSerDeException("Unexpected data type: " + data.getClass().getName() + " for Date TypeInfo: " + type); } break; default: throw new TsFileSerDeException("Unknown TypeInfo: " + columnType.getCategory()); } } return row; }  }
TsFileDeserializer { @SuppressWarnings("squid:S3776") public Object deserialize(List<String> columnNames, List<TypeInfo> columnTypes, Writable writable, String deviceId) throws TsFileSerDeException { if(!(writable instanceof MapWritable)) { throw new TsFileSerDeException("Expecting a MapWritable"); } if(row == null || row.size() != columnNames.size()) { row = new ArrayList<>(columnNames.size()); } else { row.clear(); } MapWritable mapWritable = (MapWritable) writable; if (!Objects.equals(mapWritable.get(new Text("device_id")).toString(), deviceId)) { return null; } LOG.debug("device_id: {}", mapWritable.get(new Text("device_id")).toString()); LOG.debug("time_stamp: {}" , mapWritable.get(new Text("time_stamp"))); for(int i = 0; i < columnNames.size(); i++) { TypeInfo columnType = columnTypes.get(i); String columnName = columnNames.get(i); Writable data = mapWritable.get(new Text(columnName)); if (data == null || data instanceof NullWritable) { row.add(null); continue; } if (columnType.getCategory() != ObjectInspector.Category.PRIMITIVE) { throw new TsFileSerDeException("Unknown TypeInfo: " + columnType.getCategory()); } PrimitiveObjectInspector.PrimitiveCategory type = ((PrimitiveTypeInfo) columnType).getPrimitiveCategory(); switch (type) { case BOOLEAN: if (data instanceof BooleanWritable) { row.add(((BooleanWritable)data).get()); } else { throw new TsFileSerDeException("Unexpected data type: " + data.getClass().getName() + " for Date TypeInfo: " + type); } break; case INT: if (data instanceof IntWritable) { row.add(((IntWritable)data).get()); } else { throw new TsFileSerDeException("Unexpected data type: " + data.getClass().getName() + " for Date TypeInfo: " + type); } break; case LONG: if (data instanceof LongWritable) { row.add(((LongWritable)data).get()); } else { throw new TsFileSerDeException("Unexpected data type: " + data.getClass().getName() + " for Date TypeInfo: " + type); } break; case FLOAT: if (data instanceof FloatWritable) { row.add(((FloatWritable)data).get()); } else { throw new TsFileSerDeException("Unexpected data type: " + data.getClass().getName() + " for Date TypeInfo: " + type); } break; case DOUBLE: if (data instanceof DoubleWritable) { row.add(((DoubleWritable)data).get()); } else { throw new TsFileSerDeException("Unexpected data type: " + data.getClass().getName() + " for Date TypeInfo: " + type); } break; case STRING: if (data instanceof Text) { row.add(data.toString()); } else { throw new TsFileSerDeException("Unexpected data type: " + data.getClass().getName() + " for Date TypeInfo: " + type); } break; case TIMESTAMP: if (data instanceof LongWritable) { row.add(new Timestamp(((LongWritable)data).get())); } else { throw new TsFileSerDeException("Unexpected data type: " + data.getClass().getName() + " for Date TypeInfo: " + type); } break; default: throw new TsFileSerDeException("Unknown TypeInfo: " + columnType.getCategory()); } } return row; }  @SuppressWarnings("squid:S3776") // Suppress high Cognitive Complexity warning Object deserialize(List<String> columnNames, List<TypeInfo> columnTypes,
                            Writable writable, String deviceId); }
TsFileDeserializer { @SuppressWarnings("squid:S3776") public Object deserialize(List<String> columnNames, List<TypeInfo> columnTypes, Writable writable, String deviceId) throws TsFileSerDeException { if(!(writable instanceof MapWritable)) { throw new TsFileSerDeException("Expecting a MapWritable"); } if(row == null || row.size() != columnNames.size()) { row = new ArrayList<>(columnNames.size()); } else { row.clear(); } MapWritable mapWritable = (MapWritable) writable; if (!Objects.equals(mapWritable.get(new Text("device_id")).toString(), deviceId)) { return null; } LOG.debug("device_id: {}", mapWritable.get(new Text("device_id")).toString()); LOG.debug("time_stamp: {}" , mapWritable.get(new Text("time_stamp"))); for(int i = 0; i < columnNames.size(); i++) { TypeInfo columnType = columnTypes.get(i); String columnName = columnNames.get(i); Writable data = mapWritable.get(new Text(columnName)); if (data == null || data instanceof NullWritable) { row.add(null); continue; } if (columnType.getCategory() != ObjectInspector.Category.PRIMITIVE) { throw new TsFileSerDeException("Unknown TypeInfo: " + columnType.getCategory()); } PrimitiveObjectInspector.PrimitiveCategory type = ((PrimitiveTypeInfo) columnType).getPrimitiveCategory(); switch (type) { case BOOLEAN: if (data instanceof BooleanWritable) { row.add(((BooleanWritable)data).get()); } else { throw new TsFileSerDeException("Unexpected data type: " + data.getClass().getName() + " for Date TypeInfo: " + type); } break; case INT: if (data instanceof IntWritable) { row.add(((IntWritable)data).get()); } else { throw new TsFileSerDeException("Unexpected data type: " + data.getClass().getName() + " for Date TypeInfo: " + type); } break; case LONG: if (data instanceof LongWritable) { row.add(((LongWritable)data).get()); } else { throw new TsFileSerDeException("Unexpected data type: " + data.getClass().getName() + " for Date TypeInfo: " + type); } break; case FLOAT: if (data instanceof FloatWritable) { row.add(((FloatWritable)data).get()); } else { throw new TsFileSerDeException("Unexpected data type: " + data.getClass().getName() + " for Date TypeInfo: " + type); } break; case DOUBLE: if (data instanceof DoubleWritable) { row.add(((DoubleWritable)data).get()); } else { throw new TsFileSerDeException("Unexpected data type: " + data.getClass().getName() + " for Date TypeInfo: " + type); } break; case STRING: if (data instanceof Text) { row.add(data.toString()); } else { throw new TsFileSerDeException("Unexpected data type: " + data.getClass().getName() + " for Date TypeInfo: " + type); } break; case TIMESTAMP: if (data instanceof LongWritable) { row.add(new Timestamp(((LongWritable)data).get())); } else { throw new TsFileSerDeException("Unexpected data type: " + data.getClass().getName() + " for Date TypeInfo: " + type); } break; default: throw new TsFileSerDeException("Unknown TypeInfo: " + columnType.getCategory()); } } return row; }  @SuppressWarnings("squid:S3776") // Suppress high Cognitive Complexity warning Object deserialize(List<String> columnNames, List<TypeInfo> columnTypes,
                            Writable writable, String deviceId);  }