@Override public VALUE load(KEY key) throws Exception { return loadingFunction.apply(key); } });
@Override public boolean readNextRecord() throws IOException { if (currentRecordIndex >= numRecords) { return false; } Object record = reader.read(null, decoder); currentRecord = (mode.parseFn == null) ? ((T) record) : mode.parseFn.apply((GenericRecord) record); currentRecordIndex++; return true; }
@Override public TableDestination getDestination(ValueInSingleWindow<T> element) { TableDestination res = tableFunction.apply(element); checkArgument( res != null, "result of tableFunction can not be null, but %s returned null for element: %s", tableFunction, element); return res; }
private List<V> mergeToSingleton(Iterable<V> values) { List<V> singleton = new ArrayList<>(); singleton.add(combiner.apply(values)); return singleton; }
@Override public Object[] addInput(Object[] accumulator, DataT value) { for (int i = 0; i < combineFnCount; ++i) { Object input = extractInputFns.get(i).apply(value); accumulator[i] = combineFns.get(i).addInput(accumulator[i], input); } return accumulator; }
@ProcessElement public void processElement(@Element T element, OutputReceiver<T> r) { if (predicate.apply(element)) { r.output(element); } } }))
private static <KeyT, ValueT, OutT> SerializableFunction<KafkaRecord<KeyT, ValueT>, OutT> unwrapKafkaAndThen( final SerializableFunction<KV<KeyT, ValueT>, OutT> fn) { return record -> fn.apply(record.getKV()); } ///////////////////////////////////////////////////////////////////////////////////////
@Setup public void setup() { if (spec.getProducerFactoryFn() != null) { producer = spec.getProducerFactoryFn().apply(producerConfig); } else { producer = new KafkaProducer<>(producerConfig); } }
@ProcessElement public void processElement(@Element T element, OutputReceiver<T> r) { Instant timestamp = fn.apply(element); checkNotNull( timestamp, "Timestamps for WithTimestamps cannot be null. Timestamp provided by %s.", fn); r.outputWithTimestamp(element, timestamp); }
@Override public Row apply(T input) { Row row = toRowFunction.apply(input); return Select.selectRow( row, fieldAggregation.fieldsToAggregate, row.getSchema(), fieldAggregation.inputSubSchema); } }
/** * Wraps a {@link SerializableFunction} as a {@link Contextful} of {@link Fn} with empty {@link * Requirements}. */ public static <InputT, OutputT> Contextful<Fn<InputT, OutputT>> fn( final SerializableFunction<InputT, OutputT> fn) { return new Contextful<>((element, c) -> fn.apply(element), Requirements.empty()); }
@ProcessElement public void processElement(@Element Row row, OutputReceiver<OutputT> o) { o.output(outputSchemaCoder.getFromRowFunction().apply(row)); } }))
@Override public void outputWithTimestamp(Row output, Instant timestamp) { outputReceiver.outputWithTimestamp(schemaCoder.getFromRowFunction().apply(output), timestamp); } }
protected static <ActualT> SuccessOrFailure doChecks( PAssertionSite site, ActualT actualContents, SerializableFunction<ActualT, Void> checkerFn) { try { checkerFn.apply(actualContents); return SuccessOrFailure.success(); } catch (Throwable t) { return SuccessOrFailure.failure(site, t); } }
@Test public void testFromRowWithGetters() throws NoSuchSchemaException { SchemaRegistry registry = SchemaRegistry.createDefault(); SimpleBean bean = createSimple("string"); Row row = registry.getToRowFunction(SimpleBean.class).apply(bean); // Test that the fromRowFunction simply returns the original object back. SimpleBean extracted = registry.getFromRowFunction(SimpleBean.class).apply(row); assertSame(bean, extracted); }
@Test public void testToTableRow_array() { TableRow row = toTableRow().apply(ARRAY_ROW); assertThat(row, hasEntry("ids", Arrays.asList(123L, 124L))); assertThat(row.size(), equalTo(1)); }
@Test public void testReturnsConverterForNumericTypes() { for (TypeName numericType : TypeName.NUMERIC_TYPES) { SerializableFunction<BigDecimal, ? extends Number> converter = BigDecimalConverter.forSqlType(numericType); assertNotNull(converter); assertNotNull(converter.apply(BigDecimal.TEN)); } }
@Test public void allPanesEmpty() { SerializableFunction<Iterable<ValueInSingleWindow<Integer>>, Iterable<Integer>> extractor = PaneExtractors.allPanes(); Iterable<ValueInSingleWindow<Integer>> noPanes = ImmutableList.of(); assertThat(extractor.apply(noPanes), emptyIterable()); } }
@Test public void testToTableRow_flat() { TableRow row = toTableRow().apply(FLAT_ROW); assertThat(row.size(), equalTo(5)); assertThat(row, hasEntry("id", 123L)); assertThat(row, hasEntry("value", 123.456)); assertThat(row, hasEntry("name", "test")); assertThat(row, hasEntry("valid", false)); }
@Test public void testToTableRow_null_row() { TableRow row = toTableRow().apply(NULL_FLAT_ROW); assertThat(row.size(), equalTo(5)); assertThat(row, hasEntry("id", null)); assertThat(row, hasEntry("value", null)); assertThat(row, hasEntry("name", null)); assertThat(row, hasEntry("timestamp", null)); assertThat(row, hasEntry("valid", null)); } }