/** * Provided a PartitionSpec this method attempts to retrieve each field from the deserialized * event object. * * @param devent deserialized event to extract partitions from */ protected LinkedHashMap<String, String> getPartitions(DeserializedEvent devent) { int numPartSpecs = partitionSpecs.size(); /* * Loop through each partition spec fetching the associated field from the event. Set to null if * field does not exist. */ LinkedHashMap<String, String> partitions = new LinkedHashMap<String, String>(numPartSpecs); for (PartitionSpec spec : partitionSpecs) { String key = null; for (String source : spec.getSources()) { try { key = devent.getFieldAsString(source); if (key != null) { break; } } catch (FieldNotFoundException e) { continue; } } partitions.put(spec.getName(), spec.interpret(key)); } return partitions; }
/** * Provided a PartitionSpec this method attempts to retrieve each field from the deserialized * event object. * * @param devent deserialized event to extract partitions from */ protected LinkedHashMap<String, String> getPartitions(DeserializedEvent devent) { int numPartSpecs = partitionSpecs.size(); /* * Loop through each partition spec fetching the associated field from the event. Set to null if * field does not exist. */ LinkedHashMap<String, String> partitions = new LinkedHashMap<String, String>(numPartSpecs); for (PartitionSpec spec : partitionSpecs) { String key = null; for (String source : spec.getSources()) { try { key = devent.getFieldAsString(source); if (key != null) { break; } } catch (FieldNotFoundException e) { continue; } } partitions.put(spec.getName(), spec.interpret(key)); } return partitions; }
@Test public void testRoundToSeconds() { PartitionSpec spec = new PartitionSpec("test", Collections.emptyList(), Interpreter.MILLISECONDS, "YYYY-MM-dd HH:mm:ss", 300); assertEquals("2017-01-19 05:05:00", spec.interpret("1484802359000")); }
@Test public void testDateFormatterMilliseconds() { PartitionSpec spec = new PartitionSpec("test", Collections.emptyList(), Interpreter.MILLISECONDS, "YYYY-MM-dd HH:mm:ss", 0); assertEquals("2017-01-19 05:05:59", spec.interpret("1484802359000")); }
@Test public void testStatic() { PartitionSpec spec = new PartitionSpec("test", Collections.emptyList(), Interpreter.STATIC, "static", 0); assertEquals("static", spec.interpret("")); }
@Test public void testStringConstructor() { PartitionSpec spec = new PartitionSpec("test", Collections.emptyList()); assertEquals("a_string", spec.interpret("a_string")); }
@Test public void testDateFormatterSeconds() { PartitionSpec spec = new PartitionSpec("test", Collections.emptyList(), Interpreter.SECONDS, "YYYY-MM-dd HH:mm:ss", 0); assertEquals("2017-01-19 05:05:59", spec.interpret("1484802359")); }
@Test public void testString() { PartitionSpec spec = new PartitionSpec("test", Collections.emptyList(), Interpreter.STRING, "string", 0); assertEquals("a_string", spec.interpret("a_string")); }
@Test public void testLoadConfgTime() { BenderConfig config = BenderConfig.load("/com/nextdoor/bender/partition/partition_config_time.json"); PartitionOperationConfig op = (PartitionOperationConfig) config.getSources().get(0).getOperationConfigs().get(0); PartitionSpec spec = op.getPartitionSpecs().get(0); assertEquals("dt", spec.getName()); assertEquals(Interpreter.SECONDS, spec.getInterpreter()); List<String> expected = Arrays.asList("epoch"); assertEquals(expected, spec.getSources()); assertEquals("2017-01-19 05:05:59", spec.interpret("1484802359")); }
@Test public void testStringFormatLower() { BenderConfig config = BenderConfig.load("/com/nextdoor/bender/partition/partition_config_format.json"); PartitionOperationConfig op = (PartitionOperationConfig) config.getSources().get(0).getOperationConfigs().get(0); PartitionSpec spec = op.getPartitionSpecs().get(0); spec.setStringFormat(StringFormat.TOLOWER); assertEquals("type", spec.getName()); assertEquals(Interpreter.STRING, spec.getInterpreter()); assertEquals("thisisatest", spec.interpret("ThisIsATest")); } }
@Test public void testStringFormatUpper() { BenderConfig config = BenderConfig.load("/com/nextdoor/bender/partition/partition_config_format.json"); PartitionOperationConfig op = (PartitionOperationConfig) config.getSources().get(0).getOperationConfigs().get(0); PartitionSpec spec = op.getPartitionSpecs().get(0); assertEquals("type", spec.getName()); assertEquals(Interpreter.STRING, spec.getInterpreter()); assertEquals(StringFormat.TOUPPER, spec.getStringFormat()); assertEquals("THISISATEST", spec.interpret("ThisIsATest")); }