org.apache.flink.table.client.gateway.local.result.DynamicResult Java Examples

The following examples show how to use org.apache.flink.table.client.gateway.local.result.DynamicResult. 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: LocalExecutor.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public TypedResult<Integer> snapshotResult(SessionContext session, String resultId, int pageSize) throws SqlExecutionException {
	final DynamicResult<?> result = resultStore.getResult(resultId);
	if (result == null) {
		throw new SqlExecutionException("Could not find a result with result identifier '" + resultId + "'.");
	}
	if (!result.isMaterialized()) {
		throw new SqlExecutionException("Invalid result retrieval mode.");
	}
	return ((MaterializedResult<?>) result).snapshot(pageSize);
}
 
Example #2
Source File: LocalExecutor.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public List<Row> retrieveResultPage(String resultId, int page) throws SqlExecutionException {
	final DynamicResult<?> result = resultStore.getResult(resultId);
	if (result == null) {
		throw new SqlExecutionException("Could not find a result with result identifier '" + resultId + "'.");
	}
	if (!result.isMaterialized()) {
		throw new SqlExecutionException("Invalid result retrieval mode.");
	}
	return ((MaterializedResult<?>) result).retrievePage(page);
}
 
Example #3
Source File: LocalExecutor.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public TypedResult<List<Tuple2<Boolean, Row>>> retrieveResultChanges(SessionContext session,
		String resultId) throws SqlExecutionException {
	final DynamicResult<?> result = resultStore.getResult(resultId);
	if (result == null) {
		throw new SqlExecutionException("Could not find a result with result identifier '" + resultId + "'.");
	}
	if (result.isMaterialized()) {
		throw new SqlExecutionException("Invalid result retrieval mode.");
	}
	return ((ChangelogResult<?>) result).retrieveChanges();
}
 
Example #4
Source File: ResultStore.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a result. Might start threads or opens sockets so every created result must be closed.
 */
public <T> DynamicResult<T> createResult(Environment env, TableSchema schema, ExecutionConfig config) {

	final TypeInformation<Row> outputType = Types.ROW_NAMED(schema.getFieldNames(), schema.getFieldTypes());

	if (env.getExecution().isStreamingExecution()) {
		// determine gateway address (and port if possible)
		final InetAddress gatewayAddress = getGatewayAddress(env.getDeployment());
		final int gatewayPort = getGatewayPort(env.getDeployment());

		if (env.getExecution().isChangelogMode()) {
			return new ChangelogCollectStreamResult<>(outputType, config, gatewayAddress, gatewayPort);
		} else {
			return new MaterializedCollectStreamResult<>(
				outputType,
				config,
				gatewayAddress,
				gatewayPort,
				env.getExecution().getMaxTableResultRows());
		}

	} else {
		// Batch Execution
		if (!env.getExecution().isTableMode()) {
			throw new SqlExecutionException("Results of batch queries can only be served in table mode.");
		}
		return new MaterializedCollectBatchResult<>(outputType, config);
	}
}
 
Example #5
Source File: ResultStore.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a result. Might start threads or opens sockets so every created result must be closed.
 */
public <T> DynamicResult<T> createResult(
		Environment env,
		TableSchema schema,
		ExecutionConfig config,
		ClassLoader classLoader) {

	if (env.getExecution().inStreamingMode()) {
		// determine gateway address (and port if possible)
		final InetAddress gatewayAddress = getGatewayAddress(env.getDeployment());
		final int gatewayPort = getGatewayPort(env.getDeployment());

		if (env.getExecution().isChangelogMode() || env.getExecution().isTableauMode()) {
			return new ChangelogCollectStreamResult<>(
					schema,
					config,
					gatewayAddress,
					gatewayPort,
					classLoader);
		} else {
			return new MaterializedCollectStreamResult<>(
					schema,
					config,
					gatewayAddress,
					gatewayPort,
					env.getExecution().getMaxTableResultRows(),
					classLoader);
		}

	} else {
		// Batch Execution
		if (env.getExecution().isTableMode() || env.getExecution().isTableauMode()) {
			return new MaterializedCollectBatchResult<>(schema, config, classLoader);
		} else {
			throw new SqlExecutionException(
					"Results of batch queries can only be served in table or tableau mode.");
		}
	}
}
 
Example #6
Source File: LocalExecutor.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public TypedResult<List<Tuple2<Boolean, Row>>> retrieveResultChanges(SessionContext session,
		String resultId) throws SqlExecutionException {
	final DynamicResult<?> result = resultStore.getResult(resultId);
	if (result == null) {
		throw new SqlExecutionException("Could not find a result with result identifier '" + resultId + "'.");
	}
	if (result.isMaterialized()) {
		throw new SqlExecutionException("Invalid result retrieval mode.");
	}
	return ((ChangelogResult<?>) result).retrieveChanges();
}
 
Example #7
Source File: LocalExecutor.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public TypedResult<Integer> snapshotResult(SessionContext session, String resultId, int pageSize) throws SqlExecutionException {
	final DynamicResult<?> result = resultStore.getResult(resultId);
	if (result == null) {
		throw new SqlExecutionException("Could not find a result with result identifier '" + resultId + "'.");
	}
	if (!result.isMaterialized()) {
		throw new SqlExecutionException("Invalid result retrieval mode.");
	}
	return ((MaterializedResult<?>) result).snapshot(pageSize);
}
 
Example #8
Source File: LocalExecutor.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public List<Row> retrieveResultPage(String resultId, int page) throws SqlExecutionException {
	final DynamicResult<?> result = resultStore.getResult(resultId);
	if (result == null) {
		throw new SqlExecutionException("Could not find a result with result identifier '" + resultId + "'.");
	}
	if (!result.isMaterialized()) {
		throw new SqlExecutionException("Invalid result retrieval mode.");
	}
	return ((MaterializedResult<?>) result).retrievePage(page);
}
 
Example #9
Source File: ResultStore.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a result. Might start threads or opens sockets so every created result must be closed.
 */
public <T> DynamicResult<T> createResult(Environment env, TableSchema schema, ExecutionConfig config) {

	final RowTypeInfo outputType = new RowTypeInfo(schema.getFieldTypes(), schema.getFieldNames());

	if (env.getExecution().inStreamingMode()) {
		// determine gateway address (and port if possible)
		final InetAddress gatewayAddress = getGatewayAddress(env.getDeployment());
		final int gatewayPort = getGatewayPort(env.getDeployment());

		if (env.getExecution().isChangelogMode()) {
			return new ChangelogCollectStreamResult<>(outputType, config, gatewayAddress, gatewayPort);
		} else {
			return new MaterializedCollectStreamResult<>(
				outputType,
				config,
				gatewayAddress,
				gatewayPort,
				env.getExecution().getMaxTableResultRows());
		}

	} else {
		// Batch Execution
		if (!env.getExecution().isTableMode()) {
			throw new SqlExecutionException("Results of batch queries can only be served in table mode.");
		}
		return new MaterializedCollectBatchResult<>(outputType, config);
	}
}
 
Example #10
Source File: LocalExecutor.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public List<Row> retrieveResultPage(String resultId, int page) throws SqlExecutionException {
	final DynamicResult<?> result = resultStore.getResult(resultId);
	if (result == null) {
		throw new SqlExecutionException("Could not find a result with result identifier '" + resultId + "'.");
	}
	if (!result.isMaterialized()) {
		throw new SqlExecutionException("Invalid result retrieval mode.");
	}
	return ((MaterializedResult<?>) result).retrievePage(page);
}
 
Example #11
Source File: LocalExecutor.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public TypedResult<Integer> snapshotResult(String sessionId, String resultId, int pageSize) throws SqlExecutionException {
	final DynamicResult<?> result = resultStore.getResult(resultId);
	if (result == null) {
		throw new SqlExecutionException("Could not find a result with result identifier '" + resultId + "'.");
	}
	if (!result.isMaterialized()) {
		throw new SqlExecutionException("Invalid result retrieval mode.");
	}
	return ((MaterializedResult<?>) result).snapshot(pageSize);
}
 
Example #12
Source File: LocalExecutor.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public TypedResult<List<Tuple2<Boolean, Row>>> retrieveResultChanges(
		String sessionId,
		String resultId) throws SqlExecutionException {
	final DynamicResult<?> result = resultStore.getResult(resultId);
	if (result == null) {
		throw new SqlExecutionException("Could not find a result with result identifier '" + resultId + "'.");
	}
	if (result.isMaterialized()) {
		throw new SqlExecutionException("Invalid result retrieval mode.");
	}
	return ((ChangelogResult<?>) result).retrieveChanges();
}
 
Example #13
Source File: ResultStore.java    From flink with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
public <T> DynamicResult<T> getResult(String resultId) {
	return (DynamicResult<T>) results.get(resultId);
}
 
Example #14
Source File: ResultStore.java    From flink with Apache License 2.0 4 votes vote down vote up
public void storeResult(String resultId, DynamicResult result) {
	results.put(resultId, result);
}
 
Example #15
Source File: LocalExecutor.java    From flink with Apache License 2.0 4 votes vote down vote up
private <C> ResultDescriptor executeQueryInternal(String sessionId, ExecutionContext<C> context, String query) {
	// create table
	final Table table = createTable(context, context.getTableEnvironment(), query);
	// TODO refactor this after Table#execute support all kinds of changes
	// initialize result
	final DynamicResult<C> result = resultStore.createResult(
			context.getEnvironment(),
			removeTimeAttributes(table.getSchema()),
			context.getExecutionConfig(),
			context.getClassLoader());
	final String jobName = sessionId + ": " + query;
	final String tableName = String.format("_tmp_table_%s", Math.abs(query.hashCode()));
	final Pipeline pipeline;
	try {
		// writing to a sink requires an optimization step that might reference UDFs during code compilation
		context.wrapClassLoader(() -> {
			((TableEnvironmentInternal) context.getTableEnvironment()).registerTableSinkInternal(tableName, result.getTableSink());
			table.insertInto(tableName);
		});
		pipeline = context.createPipeline(jobName);
	} catch (Throwable t) {
		// the result needs to be closed as long as
		// it not stored in the result store
		result.close();
		// catch everything such that the query does not crash the executor
		throw new SqlExecutionException("Invalid SQL query.", t);
	} finally {
		// Remove the temporal table object.
		context.wrapClassLoader(() -> {
			context.getTableEnvironment().dropTemporaryTable(tableName);
		});
	}

	// create a copy so that we can change settings without affecting the original config
	Configuration configuration = new Configuration(context.getFlinkConfig());
	// for queries we wait for the job result, so run in attached mode
	configuration.set(DeploymentOptions.ATTACHED, true);
	// shut down the cluster if the shell is closed
	configuration.set(DeploymentOptions.SHUTDOWN_IF_ATTACHED, true);

	// create execution
	final ProgramDeployer deployer = new ProgramDeployer(
			configuration, jobName, pipeline);

	JobClient jobClient;
	// wrap in classloader because CodeGenOperatorFactory#getStreamOperatorClass
	// requires to access UDF in deployer.deploy().
	jobClient = context.wrapClassLoader(() -> {
		try {
			// blocking deployment
			return deployer.deploy().get();
		} catch (Exception e) {
			throw new SqlExecutionException("Error while submitting job.", e);
		}
	});

	String jobId = jobClient.getJobID().toString();
	// store the result under the JobID
	resultStore.storeResult(jobId, result);

	// start result retrieval
	result.startRetrieval(jobClient);

	return new ResultDescriptor(
			jobId,
			removeTimeAttributes(table.getSchema()),
			result.isMaterialized(),
			context.getEnvironment().getExecution().isTableauMode());
}
 
Example #16
Source File: ResultStore.java    From flink with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
public <T> DynamicResult<T> getResult(String resultId) {
	return (DynamicResult<T>) results.get(resultId);
}
 
Example #17
Source File: ResultStore.java    From flink with Apache License 2.0 4 votes vote down vote up
public void storeResult(String resultId, DynamicResult result) {
	results.put(resultId, result);
}
 
Example #18
Source File: LocalExecutor.java    From flink with Apache License 2.0 4 votes vote down vote up
private <C> ResultDescriptor executeQueryInternal(ExecutionContext<C> context, String query) {
	final ExecutionContext<C>.EnvironmentInstance envInst = context.createEnvironmentInstance();

	// create table
	final Table table = createTable(context, envInst.getTableEnvironment(), query);

	// initialize result
	final DynamicResult<C> result = resultStore.createResult(
		context.getMergedEnvironment(),
		removeTimeAttributes(table.getSchema()),
		envInst.getExecutionConfig());

	// create job graph with dependencies
	final String jobName = context.getSessionContext().getName() + ": " + query;
	final JobGraph jobGraph;
	try {
		// writing to a sink requires an optimization step that might reference UDFs during code compilation
		context.wrapClassLoader(() -> {
			envInst.getTableEnvironment().registerTableSink(jobName, result.getTableSink());
			table.insertInto(
				envInst.getQueryConfig(),
				EnvironmentSettings.DEFAULT_BUILTIN_CATALOG,
				EnvironmentSettings.DEFAULT_BUILTIN_DATABASE,
				jobName);
			return null;
		});
		jobGraph = envInst.createJobGraph(jobName);
	} catch (Throwable t) {
		// the result needs to be closed as long as
		// it not stored in the result store
		result.close();
		// catch everything such that the query does not crash the executor
		throw new SqlExecutionException("Invalid SQL query.", t);
	}

	// store the result with a unique id (the job id for now)
	final String resultId = jobGraph.getJobID().toString();
	resultStore.storeResult(resultId, result);

	// create execution
	final ProgramDeployer<C> deployer = new ProgramDeployer<>(
		context, jobName, jobGraph, result, true);

	// start result retrieval
	result.startRetrieval(deployer);

	return new ResultDescriptor(
		resultId,
		removeTimeAttributes(table.getSchema()),
		result.isMaterialized());
}
 
Example #19
Source File: ResultStore.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
public <T> DynamicResult<T> getResult(String resultId) {
	return (DynamicResult<T>) results.get(resultId);
}
 
Example #20
Source File: ResultStore.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
public void storeResult(String resultId, DynamicResult result) {
	results.put(resultId, result);
}
 
Example #21
Source File: LocalExecutor.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
private <C> ResultDescriptor executeQueryInternal(ExecutionContext<C> context, String query) {
	final ExecutionContext.EnvironmentInstance envInst = context.createEnvironmentInstance();

	// create table
	final Table table = createTable(envInst.getTableEnvironment(), query);

	// initialize result
	final DynamicResult<C> result = resultStore.createResult(
		context.getMergedEnvironment(),
		removeTimeAttributes(table.getSchema()),
		envInst.getExecutionConfig());

	// create job graph with dependencies
	final String jobName = context.getSessionContext().getName() + ": " + query;
	final JobGraph jobGraph;
	try {
		// writing to a sink requires an optimization step that might reference UDFs during code compilation
		context.wrapClassLoader(() -> {
			table.writeToSink(result.getTableSink(), envInst.getQueryConfig());
			return null;
		});
		jobGraph = envInst.createJobGraph(jobName);
	} catch (Throwable t) {
		// the result needs to be closed as long as
		// it not stored in the result store
		result.close();
		// catch everything such that the query does not crash the executor
		throw new SqlExecutionException("Invalid SQL query.", t);
	}

	// store the result with a unique id (the job id for now)
	final String resultId = jobGraph.getJobID().toString();
	resultStore.storeResult(resultId, result);

	// create execution
	final ProgramDeployer<C> deployer = new ProgramDeployer<>(
		context, jobName, jobGraph, result, true);

	// start result retrieval
	result.startRetrieval(deployer);

	return new ResultDescriptor(
		resultId,
		removeTimeAttributes(table.getSchema()),
		result.isMaterialized());
}