本文整理汇总了Java中org.apache.flink.util.InstantiationUtil.serializeObject方法的典型用法代码示例。如果您正苦于以下问题:Java InstantiationUtil.serializeObject方法的具体用法?Java InstantiationUtil.serializeObject怎么用?Java InstantiationUtil.serializeObject使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.flink.util.InstantiationUtil
的用法示例。
在下文中一共展示了InstantiationUtil.serializeObject方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: snapshotKeyGroupState
import org.apache.flink.util.InstantiationUtil; //导入方法依赖的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);
}
}
}
示例2: testSerialization
import org.apache.flink.util.InstantiationUtil; //导入方法依赖的package包/类
@Test
public void testSerialization() throws Exception {
rowOrcInputFormat =
new OrcRowInputFormat(getPath(TEST_FILE_FLAT), TEST_SCHEMA_FLAT, new Configuration());
rowOrcInputFormat.selectFields(0, 4, 1);
rowOrcInputFormat.addPredicate(
new OrcRowInputFormat.Equals("_col1", PredicateLeaf.Type.STRING, "M"));
byte[] bytes = InstantiationUtil.serializeObject(rowOrcInputFormat);
OrcRowInputFormat copy = InstantiationUtil.deserializeObject(bytes, getClass().getClassLoader());
FileInputSplit[] splits = copy.createInputSplits(1);
copy.openInputFormat();
copy.open(splits[0]);
assertFalse(copy.reachedEnd());
Row row = copy.nextRecord(null);
assertNotNull(row);
assertEquals(3, row.getArity());
// check first row
assertEquals(1, row.getField(0));
assertEquals(500, row.getField(1));
assertEquals("M", row.getField(2));
}
示例3: testSerializability
import org.apache.flink.util.InstantiationUtil; //导入方法依赖的package包/类
@Test
public void testSerializability() throws IOException, ClassNotFoundException {
final Tuple3<Class<? extends SpecificRecord>, SpecificRecord, Row> testData = AvroTestUtils.getComplexTestData();
final AvroRowSerializationSchema serOrig = new AvroRowSerializationSchema(testData.f0);
final AvroRowDeserializationSchema deserOrig = new AvroRowDeserializationSchema(testData.f0);
byte[] serBytes = InstantiationUtil.serializeObject(serOrig);
byte[] deserBytes = InstantiationUtil.serializeObject(deserOrig);
AvroRowSerializationSchema serCopy =
InstantiationUtil.deserializeObject(serBytes, Thread.currentThread().getContextClassLoader());
AvroRowDeserializationSchema deserCopy =
InstantiationUtil.deserializeObject(deserBytes, Thread.currentThread().getContextClassLoader());
final byte[] bytes = serCopy.serialize(testData.f2);
deserCopy.deserialize(bytes);
deserCopy.deserialize(bytes);
final Row actual = deserCopy.deserialize(bytes);
assertEquals(testData.f2, actual);
}
示例4: store
import org.apache.flink.util.InstantiationUtil; //导入方法依赖的package包/类
@Override
public RetrievableStateHandle<T> store(T state) throws Exception {
Exception latestException = null;
for (int attempt = 0; attempt < 10; attempt++) {
Path filePath = getNewFilePath();
try (FSDataOutputStream outStream = fs.create(filePath, FileSystem.WriteMode.NO_OVERWRITE)) {
InstantiationUtil.serializeObject(outStream, state);
return new RetrievableStreamStateHandle<T>(filePath, outStream.getPos());
}
catch (Exception e) {
latestException = e;
}
}
throw new Exception("Could not open output stream for state backend", latestException);
}
示例5: testSerializeEnumSerializer
import org.apache.flink.util.InstantiationUtil; //导入方法依赖的package包/类
@Test
public void testSerializeEnumSerializer() throws Exception {
EnumSerializer<PublicEnum> serializer = new EnumSerializer<>(PublicEnum.class);
// verify original transient parameters
assertEquals(PublicEnum.FOO.ordinal(), serializer.getValueToOrdinal().get(PublicEnum.FOO).intValue());
assertEquals(PublicEnum.BAR.ordinal(), serializer.getValueToOrdinal().get(PublicEnum.BAR).intValue());
assertEquals(PublicEnum.PETER.ordinal(), serializer.getValueToOrdinal().get(PublicEnum.PETER).intValue());
assertEquals(PublicEnum.NATHANIEL.ordinal(), serializer.getValueToOrdinal().get(PublicEnum.NATHANIEL).intValue());
assertEquals(PublicEnum.EMMA.ordinal(), serializer.getValueToOrdinal().get(PublicEnum.EMMA).intValue());
assertEquals(PublicEnum.PAULA.ordinal(), serializer.getValueToOrdinal().get(PublicEnum.PAULA).intValue());
assertTrue(Arrays.equals(PublicEnum.values(), serializer.getValues()));
byte[] serializedSerializer = InstantiationUtil.serializeObject(serializer);
// deserialize and re-verify transient parameters
serializer = InstantiationUtil.deserializeObject(serializedSerializer, Thread.currentThread().getContextClassLoader());
assertEquals(PublicEnum.FOO.ordinal(), serializer.getValueToOrdinal().get(PublicEnum.FOO).intValue());
assertEquals(PublicEnum.BAR.ordinal(), serializer.getValueToOrdinal().get(PublicEnum.BAR).intValue());
assertEquals(PublicEnum.PETER.ordinal(), serializer.getValueToOrdinal().get(PublicEnum.PETER).intValue());
assertEquals(PublicEnum.NATHANIEL.ordinal(), serializer.getValueToOrdinal().get(PublicEnum.NATHANIEL).intValue());
assertEquals(PublicEnum.EMMA.ordinal(), serializer.getValueToOrdinal().get(PublicEnum.EMMA).intValue());
assertEquals(PublicEnum.PAULA.ordinal(), serializer.getValueToOrdinal().get(PublicEnum.PAULA).intValue());
assertTrue(Arrays.equals(PublicEnum.values(), serializer.getValues()));
}
示例6: ensureSerializable
import org.apache.flink.util.InstantiationUtil; //导入方法依赖的package包/类
public static void ensureSerializable(Object obj) {
try {
InstantiationUtil.serializeObject(obj);
} catch (Exception e) {
throw new InvalidProgramException("Object " + obj + " is not serializable", e);
}
}
示例7: replace
import org.apache.flink.util.InstantiationUtil; //导入方法依赖的package包/类
/**
* Replaces a state handle in ZooKeeper and discards the old state handle.
*
* @param pathInZooKeeper Destination path in ZooKeeper (expected to exist and start with a '/')
* @param expectedVersion Expected version of the node to replace
* @param state The new state to replace the old one
* @throws Exception If a ZooKeeper or state handle operation fails
*/
public void replace(String pathInZooKeeper, int expectedVersion, T state) throws Exception {
checkNotNull(pathInZooKeeper, "Path in ZooKeeper");
checkNotNull(state, "State");
final String path = normalizePath(pathInZooKeeper);
RetrievableStateHandle<T> oldStateHandle = get(path, false);
RetrievableStateHandle<T> newStateHandle = storage.store(state);
boolean success = false;
try {
// Serialize the new state handle. This writes the state to the backend.
byte[] serializedStateHandle = InstantiationUtil.serializeObject(newStateHandle);
// Replace state handle in ZooKeeper.
client.setData()
.withVersion(expectedVersion)
.forPath(path, serializedStateHandle);
success = true;
} catch (KeeperException.NoNodeException e) {
throw new ConcurrentModificationException("ZooKeeper unexpectedly modified", e);
} finally {
if (success) {
oldStateHandle.discardState();
} else {
newStateHandle.discardState();
}
}
}
示例8: write
import org.apache.flink.util.InstantiationUtil; //导入方法依赖的package包/类
@Override
public void write(DataOutputView out) throws IOException {
super.write(out);
try (final DataOutputViewStream outViewWrapper = new DataOutputViewStream(out)) {
InstantiationUtil.serializeObject(outViewWrapper, tupleClass);
}
}
示例9: write
import org.apache.flink.util.InstantiationUtil; //导入方法依赖的package包/类
@Override
public void write(DataOutputView out) throws IOException {
super.write(out);
try (final DataOutputViewStream outViewWrapper = new DataOutputViewStream(out)) {
InstantiationUtil.serializeObject(outViewWrapper, componentClass);
}
}
示例10: testSerializeReconfiguredEnumSerializer
import org.apache.flink.util.InstantiationUtil; //导入方法依赖的package包/类
@Test
public void testSerializeReconfiguredEnumSerializer() throws Exception {
// mock the previous ordering of enum constants to be BAR, PAULA, NATHANIEL
PublicEnum[] mockPreviousOrder = {PublicEnum.BAR, PublicEnum.PAULA, PublicEnum.NATHANIEL};
// now, the actual order of FOO, BAR, PETER, NATHANIEL, EMMA, PAULA will be the "new wrong order"
EnumSerializer<PublicEnum> serializer = new EnumSerializer<>(PublicEnum.class);
// verify that the serializer is first using the "wrong order" (i.e., the initial new configuration)
assertEquals(PublicEnum.FOO.ordinal(), serializer.getValueToOrdinal().get(PublicEnum.FOO).intValue());
assertEquals(PublicEnum.BAR.ordinal(), serializer.getValueToOrdinal().get(PublicEnum.BAR).intValue());
assertEquals(PublicEnum.PETER.ordinal(), serializer.getValueToOrdinal().get(PublicEnum.PETER).intValue());
assertEquals(PublicEnum.NATHANIEL.ordinal(), serializer.getValueToOrdinal().get(PublicEnum.NATHANIEL).intValue());
assertEquals(PublicEnum.EMMA.ordinal(), serializer.getValueToOrdinal().get(PublicEnum.EMMA).intValue());
assertEquals(PublicEnum.PAULA.ordinal(), serializer.getValueToOrdinal().get(PublicEnum.PAULA).intValue());
// reconfigure and verify compatibility
CompatibilityResult<PublicEnum> compatResult = serializer.ensureCompatibility(
new EnumSerializer.EnumSerializerConfigSnapshot<>(PublicEnum.class, mockPreviousOrder));
assertFalse(compatResult.isRequiresMigration());
// serialize and deserialize again the serializer
byte[] serializedSerializer = InstantiationUtil.serializeObject(serializer);
serializer = InstantiationUtil.deserializeObject(serializedSerializer, Thread.currentThread().getContextClassLoader());
// verify that after the serializer was read, the reconfigured constant ordering is untouched
PublicEnum[] expectedOrder = {PublicEnum.BAR, PublicEnum.PAULA, PublicEnum.NATHANIEL, PublicEnum.FOO, PublicEnum.PETER, PublicEnum.EMMA};
int i = 0;
for (PublicEnum constant : expectedOrder) {
assertEquals(i, serializer.getValueToOrdinal().get(constant).intValue());
i++;
}
assertTrue(Arrays.equals(expectedOrder, serializer.getValues()));
}
示例11: testSerializable
import org.apache.flink.util.InstantiationUtil; //导入方法依赖的package包/类
@Test
public void testSerializable() throws Exception {
ResourceSpec rs1 = ResourceSpec.newBuilder().
setCpuCores(1.0).
setHeapMemoryInMB(100).
setGPUResource(1.1).
build();
byte[] buffer = InstantiationUtil.serializeObject(rs1);
ResourceSpec rs2 = InstantiationUtil.deserializeObject(buffer, ClassLoader.getSystemClassLoader());
assertEquals(rs1, rs2);
}
示例12: convertKeyedBackendState
import org.apache.flink.util.InstantiationUtil; //导入方法依赖的package包/类
/**
* This is public so that we can use it when restoring a legacy snapshot
* in {@code AbstractStreamOperatorTestHarness}.
*/
public static KeyGroupsStateHandle convertKeyedBackendState(
HashMap<String, KvStateSnapshot<?, ?, ?, ?>> oldKeyedState,
int parallelInstanceIdx,
long checkpointID) throws Exception {
if (null != oldKeyedState) {
CheckpointStreamFactory checkpointStreamFactory = new MemCheckpointStreamFactory(MAX_SIZE);
CheckpointStreamFactory.CheckpointStateOutputStream keyedStateOut =
checkpointStreamFactory.createCheckpointStateOutputStream(checkpointID, 0L);
try {
final long offset = keyedStateOut.getPos();
InstantiationUtil.serializeObject(keyedStateOut, oldKeyedState);
StreamStateHandle streamStateHandle = keyedStateOut.closeAndGetHandle();
keyedStateOut = null; // makes IOUtils.closeQuietly(...) ignore this
if (null != streamStateHandle) {
KeyGroupRangeOffsets keyGroupRangeOffsets =
new KeyGroupRangeOffsets(parallelInstanceIdx, parallelInstanceIdx, new long[]{offset});
return new MigrationKeyGroupStateHandle(keyGroupRangeOffsets, streamStateHandle);
}
} finally {
IOUtils.closeQuietly(keyedStateOut);
}
}
return null;
}
示例13: store
import org.apache.flink.util.InstantiationUtil; //导入方法依赖的package包/类
@Override
public RetrievableStateHandle<SubmittedJobGraph> store(SubmittedJobGraph state) throws IOException {
ByteStreamStateHandle byteStreamStateHandle = new TestByteStreamStateHandleDeepCompare(
String.valueOf(UUID.randomUUID()),
InstantiationUtil.serializeObject(state));
return new RetrievableStreamStateHandle<>(byteStreamStateHandle);
}
示例14: fromSerializable
import org.apache.flink.util.InstantiationUtil; //导入方法依赖的package包/类
public static StreamStateHandle fromSerializable(String handleName, Serializable value) throws IOException {
return new TestByteStreamStateHandleDeepCompare(handleName, InstantiationUtil.serializeObject(value));
}
示例15: addAndLock
import org.apache.flink.util.InstantiationUtil; //导入方法依赖的package包/类
/**
* Creates a state handle, stores it in ZooKeeper and locks it. A locked node cannot be removed by
* another {@link ZooKeeperStateHandleStore} instance as long as this instance remains connected
* to ZooKeeper.
*
* <p><strong>Important</strong>: This will <em>not</em> store the actual state in
* ZooKeeper, but create a state handle and store it in ZooKeeper. This level of indirection
* makes sure that data in ZooKeeper is small.
*
* <p>The operation will fail if there is already an node under the given path
*
* @param pathInZooKeeper Destination path in ZooKeeper (expected to *not* exist yet)
* @param state State to be added
*
* @return The Created {@link RetrievableStateHandle}.
* @throws Exception If a ZooKeeper or state handle operation fails
*/
public RetrievableStateHandle<T> addAndLock(
String pathInZooKeeper,
T state) throws Exception {
checkNotNull(pathInZooKeeper, "Path in ZooKeeper");
checkNotNull(state, "State");
final String path = normalizePath(pathInZooKeeper);
RetrievableStateHandle<T> storeHandle = storage.store(state);
boolean success = false;
try {
// Serialize the state handle. This writes the state to the backend.
byte[] serializedStoreHandle = InstantiationUtil.serializeObject(storeHandle);
// Write state handle (not the actual state) to ZooKeeper. This is expected to be
// smaller than the state itself. This level of indirection makes sure that data in
// ZooKeeper is small, because ZooKeeper is designed for data in the KB range, but
// the state can be larger.
// Create the lock node in a transaction with the actual state node. That way we can prevent
// race conditions with a concurrent delete operation.
client.inTransaction().create().withMode(CreateMode.PERSISTENT).forPath(path, serializedStoreHandle)
.and().create().withMode(CreateMode.EPHEMERAL).forPath(getLockPath(path))
.and().commit();
success = true;
return storeHandle;
}
catch (KeeperException.NodeExistsException e) {
throw new ConcurrentModificationException("ZooKeeper unexpectedly modified", e);
}
finally {
if (!success) {
// Cleanup the state handle if it was not written to ZooKeeper.
if (storeHandle != null) {
storeHandle.discardState();
}
}
}
}