org.apache.flink.api.common.io.RichInputFormat Java Examples

The following examples show how to use org.apache.flink.api.common.io.RichInputFormat. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ContinuousFileReaderOperator.java    From flink with Apache License 2.0 6 votes vote down vote up
private void cleanUp() throws Exception {
	LOG.debug("cleanup, state={}", state);

	RunnableWithException[] runClose = {
			() -> sourceContext.close(),
			() -> output.close(),
			() -> format.close(),
			() -> {
				if (this.format instanceof RichInputFormat) {
					((RichInputFormat) this.format).closeInputFormat();
				}
			}};
	Exception firstException = null;

	for (RunnableWithException r : runClose) {
		try {
			r.run();
		} catch (Exception e) {
			firstException = ExceptionUtils.firstOrSuppressed(e, firstException);
		}
	}
	currentSplit = null;
	if (firstException != null) {
		throw firstException;
	}
}
 
Example #2
Source File: ContinuousFileReaderOperator.java    From flink with Apache License 2.0 6 votes vote down vote up
private void loadSplit(T split) throws IOException {
	Preconditions.checkState(state != ReaderState.READING && state != ReaderState.CLOSED, "can't load split in state %s", state);
	Preconditions.checkNotNull(split, "split is null");
	LOG.debug("load split: {}", split);
	currentSplit = split;
	if (this.format instanceof RichInputFormat) {
		((RichInputFormat) this.format).openInputFormat();
	}
	if (format instanceof CheckpointableInputFormat && currentSplit.getSplitState() != null) {
		// recovering after a node failure with an input
		// format that supports resetting the offset
		((CheckpointableInputFormat<T, Serializable>) format).
				reopen(currentSplit, currentSplit.getSplitState());
	} else {
		// we either have a new split, or we recovered from a node
		// failure but the input format does not support resetting the offset.
		format.open(currentSplit);
	}

	// reset the restored state to null for the next iteration
	currentSplit.resetSplitState();
}
 
Example #3
Source File: CollectionExecutor.java    From flink with Apache License 2.0 6 votes vote down vote up
private <OUT> List<OUT> executeDataSource(GenericDataSourceBase<?, ?> source, int superStep)
		throws Exception {
	@SuppressWarnings("unchecked")
	GenericDataSourceBase<OUT, ?> typedSource = (GenericDataSourceBase<OUT, ?>) source;
	// build the runtime context and compute broadcast variables, if necessary
	TaskInfo taskInfo = new TaskInfo(typedSource.getName(), 1, 0, 1, 0);
	
	RuntimeUDFContext ctx;

	MetricGroup metrics = new UnregisteredMetricsGroup();
	if (RichInputFormat.class.isAssignableFrom(typedSource.getUserCodeWrapper().getUserCodeClass())) {
		ctx = superStep == 0 ? new RuntimeUDFContext(taskInfo, userCodeClassLoader, executionConfig, cachedFiles, accumulators, metrics) :
				new IterationRuntimeUDFContext(taskInfo, userCodeClassLoader, executionConfig, cachedFiles, accumulators, metrics);
	} else {
		ctx = null;
	}
	return typedSource.executeOnCollections(ctx, executionConfig);
}
 
Example #4
Source File: CollectionExecutor.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
private <OUT> List<OUT> executeDataSource(GenericDataSourceBase<?, ?> source, int superStep)
		throws Exception {
	@SuppressWarnings("unchecked")
	GenericDataSourceBase<OUT, ?> typedSource = (GenericDataSourceBase<OUT, ?>) source;
	// build the runtime context and compute broadcast variables, if necessary
	TaskInfo taskInfo = new TaskInfo(typedSource.getName(), 1, 0, 1, 0);
	
	RuntimeUDFContext ctx;

	MetricGroup metrics = new UnregisteredMetricsGroup();
	if (RichInputFormat.class.isAssignableFrom(typedSource.getUserCodeWrapper().getUserCodeClass())) {
		ctx = superStep == 0 ? new RuntimeUDFContext(taskInfo, userCodeClassLoader, executionConfig, cachedFiles, accumulators, metrics) :
				new IterationRuntimeUDFContext(taskInfo, userCodeClassLoader, executionConfig, cachedFiles, accumulators, metrics);
	} else {
		ctx = null;
	}
	return typedSource.executeOnCollections(ctx, executionConfig);
}
 
Example #5
Source File: CollectionExecutor.java    From flink with Apache License 2.0 6 votes vote down vote up
private <OUT> List<OUT> executeDataSource(GenericDataSourceBase<?, ?> source, int superStep)
		throws Exception {
	@SuppressWarnings("unchecked")
	GenericDataSourceBase<OUT, ?> typedSource = (GenericDataSourceBase<OUT, ?>) source;
	// build the runtime context and compute broadcast variables, if necessary
	TaskInfo taskInfo = new TaskInfo(typedSource.getName(), 1, 0, 1, 0);
	
	RuntimeUDFContext ctx;

	MetricGroup metrics = new UnregisteredMetricsGroup();
	if (RichInputFormat.class.isAssignableFrom(typedSource.getUserCodeWrapper().getUserCodeClass())) {
		ctx = superStep == 0 ? new RuntimeUDFContext(taskInfo, userCodeClassLoader, executionConfig, cachedFiles, accumulators, metrics) :
				new IterationRuntimeUDFContext(taskInfo, userCodeClassLoader, executionConfig, cachedFiles, accumulators, metrics);
	} else {
		ctx = null;
	}
	return typedSource.executeOnCollections(ctx, executionConfig);
}
 
Example #6
Source File: InputFormatSourceFunction.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void close() throws Exception {
	format.close();
	if (format instanceof RichInputFormat) {
		((RichInputFormat) format).closeInputFormat();
	}
}
 
Example #7
Source File: ContinuousFileReaderOperator.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void open() throws Exception {
	super.open();

	checkState(this.serializer != null, "The serializer has not been set. " +
		"Probably the setOutputType() was not called. Please report it.");

	this.state = ReaderState.IDLE;
	if (this.format instanceof RichInputFormat) {
		((RichInputFormat) this.format).setRuntimeContext(getRuntimeContext());
	}
	this.format.configure(new Configuration());

	this.sourceContext = StreamSourceContexts.getSourceContext(
		getOperatorConfig().getTimeCharacteristic(),
		getProcessingTimeService(),
		new Object(), // no actual locking needed
		getContainingTask().getStreamStatusMaintainer(),
		output,
		getRuntimeContext().getExecutionConfig().getAutoWatermarkInterval(),
		-1);

	this.reusedRecord = serializer.createInstance();
	this.completedSplitsCounter = getMetricGroup().counter("numSplitsProcessed");
	this.splits = this.splits == null ? new PriorityQueue<>() : this.splits;

	if (!splits.isEmpty()) {
		enqueueProcessRecord();
	}
}
 
Example #8
Source File: InputFormatSourceFunction.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void close() throws Exception {
	format.close();
	if (format instanceof RichInputFormat) {
		((RichInputFormat) format).closeInputFormat();
	}
}
 
Example #9
Source File: InputFormatSourceFunction.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void run(SourceContext<OUT> ctx) throws Exception {
	try {

		Counter completedSplitsCounter = getRuntimeContext().getMetricGroup().counter("numSplitsProcessed");
		if (isRunning && format instanceof RichInputFormat) {
			((RichInputFormat) format).openInputFormat();
		}

		OUT nextElement = serializer.createInstance();
		while (isRunning) {
			format.open(splitIterator.next());

			// for each element we also check if cancel
			// was called by checking the isRunning flag

			while (isRunning && !format.reachedEnd()) {
				nextElement = format.nextRecord(nextElement);
				if (nextElement != null) {
					ctx.collect(nextElement);
				} else {
					break;
				}
			}
			format.close();
			completedSplitsCounter.inc();

			if (isRunning) {
				isRunning = splitIterator.hasNext();
			}
		}
	} finally {
		format.close();
		if (format instanceof RichInputFormat) {
			((RichInputFormat) format).closeInputFormat();
		}
		isRunning = false;
	}
}
 
Example #10
Source File: InputFormatSourceFunction.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public void open(Configuration parameters) throws Exception {
	StreamingRuntimeContext context = (StreamingRuntimeContext) getRuntimeContext();

	if (format instanceof RichInputFormat) {
		((RichInputFormat) format).setRuntimeContext(context);
	}
	format.configure(parameters);

	provider = context.getInputSplitProvider();
	serializer = typeInfo.createSerializer(getRuntimeContext().getExecutionConfig());
	splitIterator = getInputSplits();
	isRunning = splitIterator.hasNext();
}
 
Example #11
Source File: GenericDataSourceBase.java    From flink with Apache License 2.0 5 votes vote down vote up
protected List<OUT> executeOnCollections(RuntimeContext ctx, ExecutionConfig executionConfig) throws Exception {
	@SuppressWarnings("unchecked")
	InputFormat<OUT, InputSplit> inputFormat = (InputFormat<OUT, InputSplit>) this.formatWrapper.getUserCodeObject();
	//configure the input format
	inputFormat.configure(this.parameters);

	//open the input format
	if (inputFormat instanceof RichInputFormat) {
		((RichInputFormat) inputFormat).setRuntimeContext(ctx);
		((RichInputFormat) inputFormat).openInputFormat();
	}

	List<OUT> result = new ArrayList<OUT>();
	
	// splits
	InputSplit[] splits = inputFormat.createInputSplits(1);
	TypeSerializer<OUT> serializer = getOperatorInfo().getOutputType().createSerializer(executionConfig);
	
	for (InputSplit split : splits) {
		inputFormat.open(split);
		
		while (!inputFormat.reachedEnd()) {
			OUT next = inputFormat.nextRecord(serializer.createInstance());
			if (next != null) {
				result.add(serializer.copy(next));
			}
		}
		
		inputFormat.close();
	}
	
	//close the input format
	if (inputFormat instanceof RichInputFormat) {
		((RichInputFormat) inputFormat).closeInputFormat();
	}

	return result;
}
 
Example #12
Source File: InputFormatSourceFunction.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void run(SourceContext<OUT> ctx) throws Exception {
	try {

		Counter completedSplitsCounter = getRuntimeContext().getMetricGroup().counter("numSplitsProcessed");
		if (isRunning && format instanceof RichInputFormat) {
			((RichInputFormat) format).openInputFormat();
		}

		OUT nextElement = serializer.createInstance();
		while (isRunning) {
			format.open(splitIterator.next());

			// for each element we also check if cancel
			// was called by checking the isRunning flag

			while (isRunning && !format.reachedEnd()) {
				nextElement = format.nextRecord(nextElement);
				if (nextElement != null) {
					ctx.collect(nextElement);
				} else {
					break;
				}
			}
			format.close();
			completedSplitsCounter.inc();

			if (isRunning) {
				isRunning = splitIterator.hasNext();
			}
		}
	} finally {
		format.close();
		if (format instanceof RichInputFormat) {
			((RichInputFormat) format).closeInputFormat();
		}
		isRunning = false;
	}
}
 
Example #13
Source File: InputFormatSourceFunction.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public void open(Configuration parameters) throws Exception {
	StreamingRuntimeContext context = (StreamingRuntimeContext) getRuntimeContext();

	if (format instanceof RichInputFormat) {
		((RichInputFormat) format).setRuntimeContext(context);
	}
	format.configure(parameters);

	provider = context.getInputSplitProvider();
	serializer = typeInfo.createSerializer(getRuntimeContext().getExecutionConfig());
	splitIterator = getInputSplits();
	isRunning = splitIterator.hasNext();
}
 
Example #14
Source File: GenericDataSourceBase.java    From flink with Apache License 2.0 5 votes vote down vote up
protected List<OUT> executeOnCollections(RuntimeContext ctx, ExecutionConfig executionConfig) throws Exception {
	@SuppressWarnings("unchecked")
	InputFormat<OUT, InputSplit> inputFormat = (InputFormat<OUT, InputSplit>) this.formatWrapper.getUserCodeObject();
	//configure the input format
	inputFormat.configure(this.parameters);

	//open the input format
	if (inputFormat instanceof RichInputFormat) {
		((RichInputFormat) inputFormat).setRuntimeContext(ctx);
		((RichInputFormat) inputFormat).openInputFormat();
	}

	List<OUT> result = new ArrayList<OUT>();
	
	// splits
	InputSplit[] splits = inputFormat.createInputSplits(1);
	TypeSerializer<OUT> serializer = getOperatorInfo().getOutputType().createSerializer(executionConfig);
	
	for (InputSplit split : splits) {
		inputFormat.open(split);
		
		while (!inputFormat.reachedEnd()) {
			OUT next = inputFormat.nextRecord(serializer.createInstance());
			if (next != null) {
				result.add(serializer.copy(next));
			}
		}
		
		inputFormat.close();
	}
	
	//close the input format
	if (inputFormat instanceof RichInputFormat) {
		((RichInputFormat) inputFormat).closeInputFormat();
	}

	return result;
}
 
Example #15
Source File: InputFormatSourceFunction.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public void close() throws Exception {
	format.close();
	if (format instanceof RichInputFormat) {
		((RichInputFormat) format).closeInputFormat();
	}
}
 
Example #16
Source File: InputFormatSourceFunction.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public void run(SourceContext<OUT> ctx) throws Exception {
	try {

		Counter completedSplitsCounter = getRuntimeContext().getMetricGroup().counter("numSplitsProcessed");
		if (isRunning && format instanceof RichInputFormat) {
			((RichInputFormat) format).openInputFormat();
		}

		OUT nextElement = serializer.createInstance();
		while (isRunning) {
			format.open(splitIterator.next());

			// for each element we also check if cancel
			// was called by checking the isRunning flag

			while (isRunning && !format.reachedEnd()) {
				nextElement = format.nextRecord(nextElement);
				if (nextElement != null) {
					ctx.collect(nextElement);
				} else {
					break;
				}
			}
			format.close();
			completedSplitsCounter.inc();

			if (isRunning) {
				isRunning = splitIterator.hasNext();
			}
		}
	} finally {
		format.close();
		if (format instanceof RichInputFormat) {
			((RichInputFormat) format).closeInputFormat();
		}
		isRunning = false;
	}
}
 
Example #17
Source File: InputFormatSourceFunction.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public void open(Configuration parameters) throws Exception {
	StreamingRuntimeContext context = (StreamingRuntimeContext) getRuntimeContext();

	if (format instanceof RichInputFormat) {
		((RichInputFormat) format).setRuntimeContext(context);
	}
	format.configure(parameters);

	provider = context.getInputSplitProvider();
	serializer = typeInfo.createSerializer(getRuntimeContext().getExecutionConfig());
	splitIterator = getInputSplits();
	isRunning = splitIterator.hasNext();
}
 
Example #18
Source File: GenericDataSourceBase.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
protected List<OUT> executeOnCollections(RuntimeContext ctx, ExecutionConfig executionConfig) throws Exception {
	@SuppressWarnings("unchecked")
	InputFormat<OUT, InputSplit> inputFormat = (InputFormat<OUT, InputSplit>) this.formatWrapper.getUserCodeObject();
	//configure the input format
	inputFormat.configure(this.parameters);

	//open the input format
	if (inputFormat instanceof RichInputFormat) {
		((RichInputFormat) inputFormat).setRuntimeContext(ctx);
		((RichInputFormat) inputFormat).openInputFormat();
	}

	List<OUT> result = new ArrayList<OUT>();
	
	// splits
	InputSplit[] splits = inputFormat.createInputSplits(1);
	TypeSerializer<OUT> serializer = getOperatorInfo().getOutputType().createSerializer(executionConfig);
	
	for (InputSplit split : splits) {
		inputFormat.open(split);
		
		while (!inputFormat.reachedEnd()) {
			OUT next = inputFormat.nextRecord(serializer.createInstance());
			if (next != null) {
				result.add(serializer.copy(next));
			}
		}
		
		inputFormat.close();
	}
	
	//close the input format
	if (inputFormat instanceof RichInputFormat) {
		((RichInputFormat) inputFormat).closeInputFormat();
	}

	return result;
}