public static <T> List<T> listFromXml(InputStream xml, Class<T> clazz) throws SerializationException { JAXBContext context; try { context = JAXBContext.newInstance(GenericListWrapper.class, clazz); Unmarshaller unmarshaller = context.createUnmarshaller(); @SuppressWarnings("unchecked") GenericListWrapper<T> wrapper = (GenericListWrapper<T>) unmarshaller.unmarshal(new StreamSource(xml), GenericListWrapper.class) .getValue(); return wrapper.getItems(); } catch (JAXBException e) { throw new SerializationException(e); } }
protected <OUT> SingleOutputStreamOperator<OUT, ?> addCoFunction(String functionName, final Function function, TypeWrapper<IN1> in1TypeWrapper, TypeWrapper<IN2> in2TypeWrapper, TypeWrapper<OUT> outTypeWrapper, CoInvokable<IN1, IN2, OUT> functionInvokable) { @SuppressWarnings({ "unchecked", "rawtypes" }) SingleOutputStreamOperator<OUT, ?> returnStream = new SingleOutputStreamOperator( environment, functionName, outTypeWrapper); try { dataStream1.jobGraphBuilder.addCoTask(returnStream.getId(), functionInvokable, in1TypeWrapper, in2TypeWrapper, outTypeWrapper, functionName, SerializationUtils.serialize((Serializable) function), environment.getDegreeOfParallelism()); } catch (SerializationException e) { throw new RuntimeException("Cannot serialize user defined function"); } dataStream1.connectGraph(dataStream1, returnStream.getId(), 1); dataStream1.connectGraph(dataStream2, returnStream.getId(), 2); // TODO consider iteration return returnStream; }
private DataStreamSink<OUT> addSink(DataStream<OUT> inputStream, SinkFunction<OUT> sinkFunction, TypeWrapper<OUT> inTypeWrapper) { DataStreamSink<OUT> returnStream = new DataStreamSink<OUT>(environment, "sink", outTypeWrapper); try { jobGraphBuilder.addStreamVertex(returnStream.getId(), new SinkInvokable<OUT>( sinkFunction), inTypeWrapper, null, "sink", SerializationUtils .serialize(sinkFunction), degreeOfParallelism); } catch (SerializationException e) { throw new RuntimeException("Cannot serialize SinkFunction"); } inputStream.connectGraph(inputStream.copy(), returnStream.getId(), 0); return returnStream; }
/** * Creates a new DataStream that contains the given elements. The elements * must all be of the same type, for example, all of the String or Integer. * The sequence of elements must not be empty. Furthermore, the elements * must be serializable (as defined in java.io.Serializable), because the * execution environment may ship the elements into the cluster. * * @param data * The collection of elements to create the DataStream from. * @param <OUT> * type of the returned stream * @return The DataStream representing the elements. */ public <OUT extends Serializable> DataStreamSource<OUT> fromElements(OUT... data) { if (data.length == 0) { throw new IllegalArgumentException( "fromElements needs at least one element as argument"); } TypeWrapper<OUT> outTypeWrapper = new ObjectTypeWrapper<OUT>(data[0]); DataStreamSource<OUT> returnStream = new DataStreamSource<OUT>(this, "elements", outTypeWrapper); try { SourceFunction<OUT> function = new FromElementsFunction<OUT>(data); jobGraphBuilder.addStreamVertex(returnStream.getId(), new SourceInvokable<OUT>(function), null, outTypeWrapper, "source", SerializationUtils.serialize(function), 1); } catch (SerializationException e) { throw new RuntimeException("Cannot serialize elements"); } return returnStream; }
/** * Creates a DataStream from the given non-empty collection. The type of the * DataStream is that of the elements in the collection. The elements need * to be serializable (as defined by java.io.Serializable), because the * framework may move the elements into the cluster if needed. * * @param data * The collection of elements to create the DataStream from. * @param <OUT> * type of the returned stream * @return The DataStream representing the elements. */ public <OUT extends Serializable> DataStreamSource<OUT> fromCollection(Collection<OUT> data) { if (data == null) { throw new NullPointerException("Collection must not be null"); } if (data.isEmpty()) { throw new IllegalArgumentException("Collection must not be empty"); } TypeWrapper<OUT> outTypeWrapper = new ObjectTypeWrapper<OUT>(data.iterator().next()); DataStreamSource<OUT> returnStream = new DataStreamSource<OUT>(this, "elements", outTypeWrapper); try { SourceFunction<OUT> function = new FromElementsFunction<OUT>(data); jobGraphBuilder.addStreamVertex(returnStream.getId(), new SourceInvokable<OUT>( new FromElementsFunction<OUT>(data)), null, new ObjectTypeWrapper<OUT>(data .iterator().next()), "source", SerializationUtils.serialize(function), 1); } catch (SerializationException e) { throw new RuntimeException("Cannot serialize collection"); } return returnStream; }
/** * Ads a data source thus opening a {@link DataStream}. * * @param function * the user defined function * @param parallelism * number of parallel instances of the function * @param <OUT> * type of the returned stream * @return the data stream constructed */ public <OUT> DataStreamSource<OUT> addSource(SourceFunction<OUT> function, int parallelism) { TypeWrapper<OUT> outTypeWrapper = new FunctionTypeWrapper<OUT>(function, SourceFunction.class, 0); DataStreamSource<OUT> returnStream = new DataStreamSource<OUT>(this, "source", outTypeWrapper); try { jobGraphBuilder.addStreamVertex(returnStream.getId(), new SourceInvokable<OUT>(function), null, outTypeWrapper, "source", SerializationUtils.serialize(function), parallelism); } catch (SerializationException e) { throw new RuntimeException("Cannot serialize SourceFunction"); } return returnStream; }
public Color(String serializedString) { String[] args = serializedString.replace("{", "").replace("}", "").split(":"); if (args.length != 3) throw new SerializationException("Invalid color string: " + serializedString); red = Integer.parseInt(args[0]); green = Integer.parseInt(args[1]); blue = Integer.parseInt(args[2]); }
/** * Deserialize a {@link LoadProfileEstimator} according to {@link #serialize(LoadProfileEstimator, JSONArray)}. * * @param jsonObject that should be deserialized * @return the {@link LoadProfileEstimator} */ private LoadProfileEstimator deserializeEstimator(JSONObject jsonObject) { if (jsonObject.has("key")) { final String key = jsonObject.getString("key"); final LoadProfileEstimator estimator = LoadProfileEstimators.createFromSpecification(key, this.configuration); if (estimator == null) { throw new SerializationException("Could not create estimator for key " + key); } return estimator; } else if (jsonObject.has("load")) { final LoadProfile load = JsonSerializables.deserialize(jsonObject.getJSONObject("load"), LoadProfile.class); return new ConstantLoadProfileEstimator(load); } throw new SerializationException(String.format("Cannot deserialize load estimator from %s.", jsonObject)); }
/** * Deserializes an object. * * @param json that should be serialized * @return the deserialized object */ @SuppressWarnings("unchecked") default T deserialize(JSONObject json) { if (JsonSerializables.isJsonNull(json)) return null; try { final Class<?> classTag = JsonSerializables.getClassTag(json); if (classTag == null) { throw new IllegalArgumentException(String.format("Cannot determine class from %s.", json)); } return this.deserialize(json, (Class<? extends T>) classTag); } catch (ClassNotFoundException e) { throw new SerializationException("Could not load class.", e); } }
@Override protected boolean innerHasNext() { if (cachedElement != null) { return true; } else { try { cachedElement = readNext(); return cachedElement != null; } catch (final SerializationException e) { return false; } } }
public boolean isEmptyOrInconsistent() { try { getFirstValue(); getLastValue(); } catch (final Throwable t) { if (Throwables.isCausedByType(t, SerializationException.class)) { //e.g. fst: unable to find class for code 88 after version upgrade log.warn("Table data for [%s] is inconsistent and needs to be reset. Exception during getLastValue: %s", hashKey, t.toString()); return true; } else { //unexpected exception, since RemoteFastSerializingSerde only throws SerializingException throw Throwables.propagate(t); } } try (ICloseableIterator<File> files = readRangeFiles(null, null).iterator()) { boolean noFileFound = true; while (files.hasNext()) { final File file = files.next(); if (!file.exists()) { log.warn("Table data for [%s] is inconsistent and needs to be reset. Missing file: [%s]", hashKey, file); return true; } noFileFound = false; } return noFileFound; } }
@SuppressWarnings("unchecked") @Override public synchronized E fromBytes(final byte[] bytes) { if (bytes.length == 0) { return null; } try { return (E) coder.toObject(bytes); } catch (final Throwable t) { throw new SerializationException(t); } }
public static String toXml(Object obj) throws SerializationException { StringWriter sw = new StringWriter(); try { JAXBContext context = JAXBContext.newInstance(obj.getClass()); Marshaller m = context.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); m.marshal(obj, sw); return sw.toString(); } catch (JAXBException e) { throw new SerializationException(e); } }
/** * Deserializes an XML String * * @param xml * @return */ public static Object fromXml(String xml, String packageName) throws SerializationException { StringReader in = new StringReader(xml); try { JAXBContext context = JAXBContext.newInstance(packageName); Object obj = context .createUnmarshaller().unmarshal(in); return obj; } catch (JAXBException e) { throw new SerializationException(e); } }
/** * Deserialize the XML InputStream into an instance of provided class * * @param xml * @param objectClass * @return * @throws SerializationException */ @SuppressWarnings("unchecked") public static <T> T fromXml(InputStream xml, Class<T> objectClass) throws SerializationException { try { JAXBContext context = JAXBContext.newInstance(objectClass); T obj = (T) context .createUnmarshaller().unmarshal(xml); return obj; } catch (JAXBException e) { throw new SerializationException(e); } }
public byte[] serialize(Serializable o) { try { return xStream.toXML(o).getBytes(); } catch (BaseException e) { throw new SerializationException(e); } }
public static String serialize(final Object object) { if (!(object instanceof Serializable)) { throw new SerializationException(Messages.bind(Messages.Throwable_NotSerializable_Message, object)); } byte[] bytes = SerializationUtils.serialize((Serializable) object); return new String(Base64.encodeBase64(bytes)); }
/** * Operator used for directing tuples to specific named outputs using an * {@link OutputSelector}. Calling this method on an operator creates a new * {@link SplitDataStream}. * * @param outputSelector * The user defined {@link OutputSelector} for directing the * tuples. * @return The {@link SplitDataStream} */ public SplitDataStream<OUT> split(OutputSelector<OUT> outputSelector) { try { jobGraphBuilder.setOutputSelector(id, SerializationUtils.serialize(outputSelector)); } catch (SerializationException e) { throw new RuntimeException("Cannot serialize OutputSelector"); } return new SplitDataStream<OUT>(this); }
/** * Internal function for passing the user defined functions to the JobGraph * of the job. * * @param functionName * name of the function * @param function * the user defined function * @param functionInvokable * the wrapping JobVertex instance * @param <R> * type of the return stream * @return the data stream constructed */ protected <R> SingleOutputStreamOperator<R, ?> addFunction(String functionName, final Function function, TypeWrapper<OUT> inTypeWrapper, TypeWrapper<R> outTypeWrapper, StreamInvokable<OUT, R> functionInvokable) { DataStream<OUT> inputStream = this.copy(); @SuppressWarnings({ "unchecked", "rawtypes" }) SingleOutputStreamOperator<R, ?> returnStream = new SingleOutputStreamOperator(environment, functionName, outTypeWrapper); try { jobGraphBuilder.addStreamVertex(returnStream.getId(), functionInvokable, inTypeWrapper, outTypeWrapper, functionName, SerializationUtils.serialize((Serializable) function), degreeOfParallelism); } catch (SerializationException e) { throw new RuntimeException("Cannot serialize user defined function"); } connectGraph(inputStream, returnStream.getId(), 0); if (inputStream instanceof IterativeDataStream) { IterativeDataStream<OUT> iterativeStream = (IterativeDataStream<OUT>) inputStream; returnStream.addIterationSource(iterativeStream.iterationID.toString(), iterativeStream.waitTime); } return returnStream; }
public void setUserInvokable(StreamInvokable<?,?> invokableObject) { if (invokableObject != null) { config.setClass(USER_FUNCTION, invokableObject.getClass()); try { config.setBytes(SERIALIZEDUDF, SerializationUtils.serialize(invokableObject)); } catch (SerializationException e) { throw new RuntimeException("Cannot serialize invokable object " + invokableObject.getClass(), e); } } }
public Object getFunction() { try { return SerializationUtils.deserialize(config.getBytes(FUNCTION, null)); } catch (SerializationException e) { throw new RuntimeException("Cannot deserialize invokable object", e); } }
/** * Deserialize a given JSON datatype. The following cases are supported: * <ul> * <li>{@code json} is a (JSON) {@code null} value;</li> * <li>{@code json} is a basic (JSON) datatype;</li> * <li>{@code json} is a {@link Class}-tagged {@link JSONObject} that corresponds to a {@link JsonSerializable};</li> * <li>{@code json} is a {@link JSONArray} with {@link Class}-tagged {@link JSONObject}s that correspond to a * {@link JsonSerializable}s - in this case, the result type is a {@link List}.</li> * </ul> * * @param json the JSON data * @return the deserialization result */ public static Object deserialize(Object json) { if (isJsonNull(json)) return null; else if (isUnconvertedInstance(json)) return json; else if (json instanceof JSONObject) return deserialize((JSONObject) json); else if (json instanceof JSONArray) return deserializeAllAsList((JSONArray) json); throw new SerializationException(String.format("Don't know how to deserialize %s.", json)); }
/** * Deserialize the XML String into an instance of the provided class * * @param xml * @param objectClass * @return * @throws SerializationException */ public static <T> T fromXml(String xml, Class<T> objectClass) throws SerializationException { return fromXml(new ByteArrayInputStream(xml.getBytes()), objectClass); }
/** * Deserialize the XML String into a List of instances of provided class * * @param xml * @param clazz * @return * @throws SerializationException */ public static <T> List<T> listFromXml(String xml, Class<T> clazz) throws SerializationException { return listFromXml(new ByteArrayInputStream(xml.getBytes()), clazz); }