public String interpret(String input) { switch (this.interpreter) { case STRING: return getFormattedString(input); case STATIC: return this.format; default: return getFormattedTime(input); } }
/** * 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; }
public PartitionSpec(String name, List<String> sources, Interpreter interpreter, String format, int secondsToRound) { this.name = name; this.sources = sources; this.interpreter = interpreter; this.format = format; setDateTimeFormatter(); this.secondsToRound = secondsToRound; }
@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 testEmptyDate() { PartitionSpec spec = new PartitionSpec("test", Collections.emptyList(), Interpreter.SECONDS, "YYYY-MM-dd HH:mm:ss", 0); assertEquals(null, spec.interpret("")); assertEquals(null, spec.interpret(null)); }
@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")); }
@Test public void testLoadConfg() { BenderConfig config = BenderConfig.load("/com/nextdoor/bender/partition/partition_config.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()); List<String> expected = Arrays.asList("one", "two", "three"); assertEquals(expected, spec.getSources()); }
@Test(expected = RuntimeException.class) public void testInvalidInterpreter() { PartitionSpec spec = new PartitionSpec("test", Collections.emptyList(), Interpreter.STATIC, "static", 0); spec.getFormattedTime("will fail"); }
protected String getFormattedString(String input) { if (input == null) { throw new OperationException("unable to find value for partition '" + this.getName() +"'"); } switch (this.stringFormat) { case NONE: return input; case TOUPPER: return input.toUpperCase(); case TOLOWER: return input.toLowerCase(); default: return input; } }
@Test public void testGetEvaluatedPartitionsString() throws FieldNotFoundException { List<PartitionSpec> partitionSpecs = new ArrayList<PartitionSpec>(1); List<String> sources = Arrays.asList("foo"); PartitionSpec spec = new PartitionSpec("foo", sources, PartitionSpec.Interpreter.STRING); partitionSpecs.add(spec); PartitionOperation op = new PartitionOperation(partitionSpecs); InternalEvent ievent = new InternalEvent("foo", null, 1); DummyStringEvent devent = spy(new DummyStringEvent("")); ievent.setEventObj(devent); doReturn("baz").when(devent).getFieldAsString("foo"); op.perform(ievent); LinkedHashMap<String, String> actual = ievent.getPartitions(); LinkedHashMap<String, String> expected = new LinkedHashMap<String, String>(1); expected.put("foo", "baz"); assertEquals(expected, actual); }
@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")); }
protected String getFormattedString(String input) { if (input == null) { throw new OperationException("unable to find value for partition '" + this.getName() +"'"); } switch (this.stringFormat) { case NONE: return input; case TOUPPER: return input.toUpperCase(); case TOLOWER: return input.toLowerCase(); default: return input; } }
@Test public void testGetEvaluatedPartitionsStringMultipleFields() throws FieldNotFoundException { List<PartitionSpec> partitionSpecs = new ArrayList<PartitionSpec>(1); List<String> sources = Arrays.asList("one", "two"); PartitionSpec spec = new PartitionSpec("foo", sources, PartitionSpec.Interpreter.STRING); partitionSpecs.add(spec); PartitionOperation op = new PartitionOperation(partitionSpecs); InternalEvent ievent = new InternalEvent("foo", null, 1); DummyStringEvent devent = spy(new DummyStringEvent("")); ievent.setEventObj(devent); doThrow(FieldNotFoundException.class).doReturn("5").when(devent).getFieldAsString(any()); op.perform(ievent); LinkedHashMap<String, String> actual = ievent.getPartitions(); LinkedHashMap<String, String> expected = new LinkedHashMap<String, String>(1); expected.put("foo", "5"); assertEquals(expected, actual); }
/** * 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 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")); }
public String interpret(String input) { switch (this.interpreter) { case STRING: return getFormattedString(input); case STATIC: return this.format; default: return getFormattedTime(input); } }
public PartitionSpec(String name, List<String> sources, Interpreter interpreter, String format, int secondsToRound) { this.name = name; this.sources = sources; this.interpreter = interpreter; this.format = format; setDateTimeFormatter(); this.secondsToRound = secondsToRound; }
@Test public void testGetEvaluatedPartitionsStatic() { List<PartitionSpec> partitionSpecs = new ArrayList<PartitionSpec>(1); PartitionSpec spec = new PartitionSpec("foo", Collections.emptyList(), PartitionSpec.Interpreter.STATIC, "123", 0); partitionSpecs.add(spec); PartitionOperation op = new PartitionOperation(partitionSpecs); InternalEvent ievent = new InternalEvent("foo", null, 1); DummyStringEvent devent = new DummyStringEvent(""); ievent.setEventObj(devent); op.perform(ievent); LinkedHashMap<String, String> actual = ievent.getPartitions(); LinkedHashMap<String, String> expected = new LinkedHashMap<String, String>(1); expected.put("foo", "123"); assertEquals(expected, actual); }
@Test public void testString() { PartitionSpec spec = new PartitionSpec("test", Collections.emptyList(), Interpreter.STRING, "string", 0); assertEquals("a_string", spec.interpret("a_string")); }