本文整理汇总了Java中org.apache.beam.sdk.coders.Coder.encode方法的典型用法代码示例。如果您正苦于以下问题:Java Coder.encode方法的具体用法?Java Coder.encode怎么用?Java Coder.encode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.beam.sdk.coders.Coder
的用法示例。
在下文中一共展示了Coder.encode方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testDecoderWith
import org.apache.beam.sdk.coders.Coder; //导入方法依赖的package包/类
private <T> void testDecoderWith(Coder<T> coder, T... expected) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
for (T value : expected) {
int size = baos.size();
coder.encode(value, baos);
// Pad an arbitrary byte when values encode to zero bytes
if (baos.size() - size == 0) {
baos.write(0);
}
}
Iterator<T> decoder =
new DataStreamDecoder<>(coder, new ByteArrayInputStream(baos.toByteArray()));
Object[] actual = Iterators.toArray(decoder, Object.class);
assertArrayEquals(expected, actual);
assertFalse(decoder.hasNext());
assertFalse(decoder.hasNext());
thrown.expect(NoSuchElementException.class);
decoder.next();
}
示例2: encode
import org.apache.beam.sdk.coders.Coder; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
@Override
public void encode(
RawUnionValue union,
OutputStream outStream,
Context context)
throws IOException, CoderException {
int index = getIndexForEncoding(union);
// Write out the union tag.
VarInt.encode(index, outStream);
// Write out the actual value.
Coder<Object> coder = (Coder<Object>) elementCoders.get(index);
coder.encode(
union.getValue(),
outStream,
context);
}
示例3: GrowthTracker
import org.apache.beam.sdk.coders.Coder; //导入方法依赖的package包/类
GrowthTracker(
final SerializableFunction<OutputT, KeyT> keyFn,
final Coder<KeyT> outputKeyCoder,
GrowthState<OutputT, KeyT, TerminationStateT> state,
Growth.TerminationCondition<?, TerminationStateT> terminationCondition) {
this.coderFunnel =
new Funnel<OutputT>() {
@Override
public void funnel(OutputT from, PrimitiveSink into) {
try {
// Rather than hashing the output itself, hash the output key.
KeyT outputKey = keyFn.apply(from);
outputKeyCoder.encode(outputKey, Funnels.asOutputStream(into));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
};
this.terminationCondition = terminationCondition;
this.state = state;
this.isOutputComplete = state.isOutputComplete;
this.pollWatermark = state.pollWatermark;
this.terminationState = state.terminationState;
this.pending = Lists.newLinkedList(state.pending);
}
示例4: testByteCount
import org.apache.beam.sdk.coders.Coder; //导入方法依赖的package包/类
/**
* A utility method that passes the given (unencoded) elements through
* coder's registerByteSizeObserver() and encode() methods, and confirms
* they are mutually consistent. This is useful for testing coder
* implementations.
*/
public static <T> void testByteCount(Coder<T> coder, Coder.Context context, T[] elements)
throws Exception {
TestElementByteSizeObserver observer = new TestElementByteSizeObserver();
try (CountingOutputStream os = new CountingOutputStream(ByteStreams.nullOutputStream())) {
for (T elem : elements) {
coder.registerByteSizeObserver(elem, observer);
coder.encode(elem, os, context);
observer.advance();
}
long expectedLength = os.getCount();
if (!context.isWholeStream) {
assertEquals(expectedLength, observer.getSum());
}
assertEquals(elements.length, observer.getCount());
}
}
示例5: snapshotKeyGroupState
import org.apache.beam.sdk.coders.Coder; //导入方法依赖的package包/类
/**
* Snapshots the state {@code (stateName -> (valueCoder && (namespace -> value)))} for a given
* {@code keyGroupIdx}.
*
* @param keyGroupIdx the id of the key-group to be put in the snapshot.
* @param out the stream to write to.
*/
public void snapshotKeyGroupState(int keyGroupIdx, DataOutputStream out) throws Exception {
int localIdx = getIndexForKeyGroup(keyGroupIdx);
Map<String, Tuple2<Coder<?>, Map<String, ?>>> stateTable = stateTables[localIdx];
Preconditions.checkState(stateTable.size() <= Short.MAX_VALUE,
"Too many States: " + stateTable.size() + ". Currently at most "
+ Short.MAX_VALUE + " states are supported");
out.writeShort(stateTable.size());
for (Map.Entry<String, Tuple2<Coder<?>, Map<String, ?>>> entry : stateTable.entrySet()) {
out.writeUTF(entry.getKey());
Coder coder = entry.getValue().f0;
InstantiationUtil.serializeObject(out, coder);
Map<String, ?> map = entry.getValue().f1;
out.writeInt(map.size());
for (Map.Entry<String, ?> entry1 : map.entrySet()) {
StringUtf8Coder.of().encode(entry1.getKey(), out);
coder.encode(entry1.getValue(), out);
}
}
}
示例6: hash
import org.apache.beam.sdk.coders.Coder; //导入方法依赖的package包/类
/**
* Encodes the given element using the given coder and hashes the encoding.
*/
static <T> long hash(T element, Coder<T> coder) throws CoderException, IOException {
try (HashingOutputStream stream =
new HashingOutputStream(Hashing.murmur3_128(), ByteStreams.nullOutputStream())) {
coder.encode(element, stream, Context.OUTER);
return stream.hash().asLong();
}
}
示例7: encodeToSafeStream
import org.apache.beam.sdk.coders.Coder; //导入方法依赖的package包/类
/**
* Encodes {@code value} to the given {@code stream}, which should be a stream that never throws
* {@code IOException}, such as {@code ByteArrayOutputStream} or
* {@link ExposedByteArrayOutputStream}.
*/
private static <T> void encodeToSafeStream(
Coder<T> coder, T value, OutputStream stream, Coder.Context context) throws CoderException {
try {
coder.encode(value, new UnownedOutputStream(stream), context);
} catch (IOException exn) {
Throwables.propagateIfPossible(exn, CoderException.class);
throw new IllegalArgumentException(
"Forbidden IOException when writing to OutputStream", exn);
}
}
示例8: encode
import org.apache.beam.sdk.coders.Coder; //导入方法依赖的package包/类
@VisibleForTesting
static <T> byte[] encode(
Coder<T> coder, Coder.Context context, T value) throws CoderException, IOException {
@SuppressWarnings("unchecked")
Coder<T> deserializedCoder = SerializableUtils.clone(coder);
ByteArrayOutputStream os = new ByteArrayOutputStream();
deserializedCoder.encode(value, new UnownedOutputStream(os), context);
return os.toByteArray();
}
示例9: toByteArray
import org.apache.beam.sdk.coders.Coder; //导入方法依赖的package包/类
/**
* Utility method for serializing an object using the specified coder.
*
* @param value Value to serialize.
* @param coder Coder to serialize with.
* @param <T> type of value that is serialized
* @return Byte array representing serialized object.
*/
public static <T> byte[] toByteArray(T value, Coder<T> coder) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
coder.encode(value, baos, new Coder.Context(true));
} catch (IOException e) {
throw new IllegalStateException("Error encoding value: " + value, e);
}
return baos.toByteArray();
}
示例10: encode
import org.apache.beam.sdk.coders.Coder; //导入方法依赖的package包/类
@Override
public void encode(KV<Integer, WindowedValue<?>> kv, OutputStream out)
throws IOException {
Coder<WindowedValue<?>> coder = idsToCoders.get(kv.getKey());
VarIntCoder.of().encode(kv.getKey(), out);
coder.encode(kv.getValue(), out);
}
示例11: encode
import org.apache.beam.sdk.coders.Coder; //导入方法依赖的package包/类
@Override
public void encode(InvoiceGroupingKey value, OutputStream outStream) throws IOException {
Coder<String> stringCoder = StringUtf8Coder.of();
stringCoder.encode(value.startDate(), outStream);
stringCoder.encode(value.endDate(), outStream);
stringCoder.encode(value.productAccountKey(), outStream);
stringCoder.encode(value.usageGroupingKey(), outStream);
stringCoder.encode(value.description(), outStream);
stringCoder.encode(String.valueOf(value.unitPrice()), outStream);
stringCoder.encode(value.unitPriceCurrency(), outStream);
stringCoder.encode(value.poNumber(), outStream);
}