本文整理汇总了Java中org.apache.beam.sdk.coders.Coder.decode方法的典型用法代码示例。如果您正苦于以下问题:Java Coder.decode方法的具体用法?Java Coder.decode怎么用?Java Coder.decode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.beam.sdk.coders.Coder
的用法示例。
在下文中一共展示了Coder.decode方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: decode
import org.apache.beam.sdk.coders.Coder; //导入方法依赖的package包/类
@VisibleForTesting
static <T> T decode(
Coder<T> coder, Coder.Context context, byte[] bytes) throws CoderException, IOException {
@SuppressWarnings("unchecked")
Coder<T> deserializedCoder = SerializableUtils.clone(coder);
byte[] buffer;
if (context == Coder.Context.NESTED) {
buffer = new byte[bytes.length + 1];
System.arraycopy(bytes, 0, buffer, 0, bytes.length);
buffer[bytes.length] = 1;
} else {
buffer = bytes;
}
CountingInputStream cis = new CountingInputStream(new ByteArrayInputStream(buffer));
T value = deserializedCoder.decode(new UnownedInputStream(cis), context);
assertThat("consumed bytes equal to encoded bytes", cis.getCount(),
equalTo((long) bytes.length));
return value;
}
示例2: restoreKeyGroupState
import org.apache.beam.sdk.coders.Coder; //导入方法依赖的package包/类
/**
* Restore 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 in the stream to read from.
* @param userCodeClassLoader the class loader that will be used to deserialize
* the valueCoder.
*/
public void restoreKeyGroupState(int keyGroupIdx, DataInputStream in,
ClassLoader userCodeClassLoader) throws Exception {
int localIdx = getIndexForKeyGroup(keyGroupIdx);
Map<String, Tuple2<Coder<?>, Map<String, ?>>> stateTable = stateTables[localIdx];
int numStates = in.readShort();
for (int i = 0; i < numStates; ++i) {
String stateName = in.readUTF();
Coder coder = InstantiationUtil.deserializeObject(in, userCodeClassLoader);
Tuple2<Coder<?>, Map<String, ?>> tuple2 = stateTable.get(stateName);
if (tuple2 == null) {
tuple2 = new Tuple2<>();
tuple2.f0 = coder;
tuple2.f1 = new HashMap<>();
stateTable.put(stateName, tuple2);
}
Map<String, Object> map = (Map<String, Object>) tuple2.f1;
int mapSize = in.readInt();
for (int j = 0; j < mapSize; j++) {
String namespace = StringUtf8Coder.of().decode(in);
Object value = coder.decode(in);
map.put(namespace, value);
}
}
}
示例3: decodeFromSafeStream
import org.apache.beam.sdk.coders.Coder; //导入方法依赖的package包/类
/**
* Decodes a value from the given {@code stream}, which should be a stream that never throws
* {@code IOException}, such as {@code ByteArrayInputStream} or
* {@link ExposedByteArrayInputStream}.
*/
private static <T> T decodeFromSafeStream(
Coder<T> coder, InputStream stream, Coder.Context context) throws CoderException {
try {
return coder.decode(new UnownedInputStream(stream), context);
} catch (IOException exn) {
Throwables.propagateIfPossible(exn, CoderException.class);
throw new IllegalArgumentException(
"Forbidden IOException when reading from InputStream", exn);
}
}
示例4: readRows
import org.apache.beam.sdk.coders.Coder; //导入方法依赖的package包/类
private List<TableRow> readRows(String filename) throws IOException {
Coder<TableRow> coder = TableRowJsonCoder.of();
List<TableRow> tableRows = Lists.newArrayList();
try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
String line;
while ((line = reader.readLine()) != null) {
TableRow tableRow = coder.decode(
new ByteArrayInputStream(line.getBytes(StandardCharsets.UTF_8)), Context.OUTER);
tableRows.add(tableRow);
}
}
return tableRows;
}
示例5: fromByteArray
import org.apache.beam.sdk.coders.Coder; //导入方法依赖的package包/类
/**
* Utility method for deserializing a byte array using the specified coder.
*
* @param serialized bytearray to be deserialized.
* @param coder Coder to deserialize with.
* @param <T> Type of object to be returned.
* @return Deserialized object.
*/
public static <T> T fromByteArray(byte[] serialized, Coder<T> coder) {
ByteArrayInputStream bais = new ByteArrayInputStream(serialized);
try {
return coder.decode(bais, new Coder.Context(true));
} catch (IOException e) {
throw new IllegalStateException("Error decoding bytes for coder: " + coder, e);
}
}
示例6: decode
import org.apache.beam.sdk.coders.Coder; //导入方法依赖的package包/类
@Override
public KV<Integer, WindowedValue<?>> decode(InputStream in)
throws IOException {
Integer id = VarIntCoder.of().decode(in);
Coder<WindowedValue<?>> coder = idsToCoders.get(id);
WindowedValue<?> value = coder.decode(in);
return KV.<Integer, WindowedValue<?>>of(id, value);
}
示例7: decode
import org.apache.beam.sdk.coders.Coder; //导入方法依赖的package包/类
@Override
public InvoiceGroupingKey decode(InputStream inStream) throws IOException {
Coder<String> stringCoder = StringUtf8Coder.of();
return new AutoValue_BillingEvent_InvoiceGroupingKey(
stringCoder.decode(inStream),
stringCoder.decode(inStream),
stringCoder.decode(inStream),
stringCoder.decode(inStream),
stringCoder.decode(inStream),
Double.parseDouble(stringCoder.decode(inStream)),
stringCoder.decode(inStream),
stringCoder.decode(inStream));
}