@Override public Coder<TimestampedValue<T>> getAccumulatorCoder( CoderRegistry registry, Coder<TimestampedValue<T>> inputCoder) throws CannotProvideCoderException { return NullableCoder.of(inputCoder); }
@Override public T decode(InputStream inStream) throws IOException, CoderException { return decode(inStream, Context.NESTED); }
@Override public void encode(@Nullable T value, OutputStream outStream) throws IOException, CoderException { encode(value, outStream, Context.NESTED); }
@Test public void testObserverIsCheap() throws Exception { NullableCoder<Double> coder = NullableCoder.of(DoubleCoder.of()); assertTrue(coder.isRegisterByteSizeObserverCheap(5.0)); }
@Test public void testEncodedSize() throws Exception { NullableCoder<Double> coder = NullableCoder.of(DoubleCoder.of()); assertEquals(1, coder.getEncodedElementByteSize(null)); assertEquals(9, coder.getEncodedElementByteSize(5.0)); }
@Override public CloudObject toCloudObject(NullableCoder target, SdkComponents sdkComponents) { CloudObject base = CloudObject.forClass(NullableCoder.class); return addComponents( base, Collections.<Coder<?>>singletonList(target.getValueCoder()), sdkComponents); }
@Test public void testObserverIsAlwaysCheapForNullValues() throws Exception { NullableCoder<List<String>> coder = NullableCoder.of(ListCoder.of(StringUtf8Coder.of())); assertTrue(coder.isRegisterByteSizeObserverCheap(null)); }
@Test public void testEncodedSizeNested() throws Exception { NullableCoder<String> varLenCoder = NullableCoder.of(StringUtf8Coder.of()); assertEquals(1, varLenCoder.getEncodedElementByteSize(null)); assertEquals(6, varLenCoder.getEncodedElementByteSize("spam")); }
@Test public void testInferKeyCoder() { CoderRegistry registry = CoderRegistry.createDefault(); assertTrue( KafkaIO.inferCoder(registry, LongDeserializer.class).getValueCoder() instanceof VarLongCoder); assertTrue( KafkaIO.inferCoder(registry, StringDeserializer.class).getValueCoder() instanceof StringUtf8Coder); assertTrue( KafkaIO.inferCoder(registry, InstantDeserializer.class).getValueCoder() instanceof InstantCoder); assertTrue( KafkaIO.inferCoder(registry, DeserializerWithInterfaces.class).getValueCoder() instanceof VarLongCoder); }
protected FileResultCoder( Coder<BoundedWindow> windowCoder, Coder<DestinationT> destinationCoder) { this.windowCoder = NullableCoder.of(windowCoder); this.destinationCoder = destinationCoder; }
@Test public void testObserverIsNotCheap() throws Exception { NullableCoder<List<String>> coder = NullableCoder.of(ListCoder.of(StringUtf8Coder.of())); assertFalse(coder.isRegisterByteSizeObserverCheap(ImmutableList.of("hi", "test"))); }
@Override public OutgoingMessage decode(InputStream inStream) throws CoderException, IOException { byte[] elementBytes = ByteArrayCoder.of().decode(inStream); Map<String, String> attributes = ATTRIBUTES_CODER.decode(inStream); long timestampMsSinceEpoch = BigEndianLongCoder.of().decode(inStream); @Nullable String recordId = RECORD_ID_CODER.decode(inStream); return new OutgoingMessage(elementBytes, attributes, timestampMsSinceEpoch, recordId); } }
@Test public void testGloballyOutputCoder() { p.enableAbandonedNodeEnforcement(false); BigEndianLongCoder inputCoder = BigEndianLongCoder.of(); PCollection<Long> output = p.apply(Create.of(1L, 2L).withCoder(inputCoder)).apply(Latest.globally()); Coder<Long> outputCoder = output.getCoder(); assertThat(outputCoder, instanceOf(NullableCoder.class)); assertEquals(inputCoder, ((NullableCoder<?>) outputCoder).getValueCoder()); }
@Override public void encode(OutgoingMessage value, OutputStream outStream) throws CoderException, IOException { ByteArrayCoder.of().encode(value.elementBytes, outStream); ATTRIBUTES_CODER.encode(value.attributes, outStream); BigEndianLongCoder.of().encode(value.timestampMsSinceEpoch, outStream); RECORD_ID_CODER.encode(value.recordId, outStream); }
@Override public NullableCoder<?> fromCloudObject(CloudObject cloudObject) { List<Coder<?>> componentList = getComponents(cloudObject); checkArgument( componentList.size() == 1, "Expected 1 component for %s, got %s", NullableCoder.class.getSimpleName(), componentList.size()); return NullableCoder.of(componentList.get(0)); }
@Override public FailsafeElement<OriginalT, CurrentT> decode(InputStream inStream) throws IOException { OriginalT originalPayload = originalPayloadCoder.decode(inStream); CurrentT currentPayload = currentPayloadCoder.decode(inStream); String errorMessage = STRING_CODER.decode(inStream); String stacktrace = STRING_CODER.decode(inStream); return FailsafeElement.of(originalPayload, currentPayload) .setErrorMessage(errorMessage) .setStacktrace(stacktrace); }
@Override public void encode(FailsafeElement<OriginalT, CurrentT> value, OutputStream outStream) throws IOException { if (value == null) { throw new CoderException("The FailsafeElementCoder cannot encode a null object!"); } originalPayloadCoder.encode(value.getOriginalPayload(), outStream); currentPayloadCoder.encode(value.getPayload(), outStream); STRING_CODER.encode(value.getErrorMessage(), outStream); STRING_CODER.encode(value.getStacktrace(), outStream); }
public Coder<List> getAccumulatorCoder() { AvroCoder valueCoder = null; if (avroSchemaStr != null) { valueCoder = AvroCoder.of(new Schema.Parser().parse(avroSchemaStr)); } return (Coder<List>) (avroSchemaStr == null ? ListCoder.of(NullableCoder.of(StringUtf8Coder.of())) : ListCoder.of(NullableCoder.of(valueCoder))); } }
public Coder<List> getAccumulatorCoder() { AvroCoder valueCoder = null; if (avroSchemaStr != null) { valueCoder = AvroCoder.of(new Schema.Parser().parse(avroSchemaStr)); } return (Coder<List>) (avroSchemaStr == null ? ListCoder.of(NullableCoder.of(StringUtf8Coder.of())) : ListCoder.of(NullableCoder.of(valueCoder))); } }