org.apache.flink.table.client.config.entries.ViewEntry Java Examples

The following examples show how to use org.apache.flink.table.client.config.entries.ViewEntry. 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: CliClient.java    From flink with Apache License 2.0 6 votes vote down vote up
private void callDropView(SqlCommandCall cmdCall) {
	final String name = cmdCall.operands[0];
	final ViewEntry view = context.getViews().get(name);

	if (view == null) {
		printExecutionError(CliStrings.MESSAGE_VIEW_NOT_FOUND);
		return;
	}

	try {
		// perform and validate change
		context.removeView(name);
		executor.validateSession(context);
		printInfo(CliStrings.MESSAGE_VIEW_REMOVED);
	} catch (SqlExecutionException e) {
		// rollback change
		context.addView(view);
		printExecutionException(CliStrings.MESSAGE_VIEW_NOT_REMOVED, e);
	}
}
 
Example #2
Source File: CliClient.java    From flink with Apache License 2.0 6 votes vote down vote up
private void callCreateView(SqlCommandCall cmdCall) {
	final String name = cmdCall.operands[0];
	final String query = cmdCall.operands[1];

	final ViewEntry previousView = context.getViews().get(name);
	if (previousView != null) {
		printExecutionError(CliStrings.MESSAGE_VIEW_ALREADY_EXISTS);
		return;
	}

	try {
		// perform and validate change
		context.addView(ViewEntry.create(name, query));
		executor.validateSession(context);
		printInfo(CliStrings.MESSAGE_VIEW_CREATED);
	} catch (SqlExecutionException e) {
		// rollback change
		context.removeView(name);
		printExecutionException(e);
	}
}
 
Example #3
Source File: Environment.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * Enriches an environment with new/modified properties or views and returns the new instance.
 */
public static Environment enrich(
		Environment env,
		Map<String, String> properties,
		Map<String, ViewEntry> views) {
	final Environment enrichedEnv = new Environment();

	// merge tables
	enrichedEnv.tables = new LinkedHashMap<>(env.getTables());
	enrichedEnv.tables.putAll(views);

	// merge functions
	enrichedEnv.functions = new HashMap<>(env.getFunctions());

	// enrich execution properties
	enrichedEnv.execution = ExecutionEntry.enrich(env.execution, properties);

	// enrich deployment properties
	enrichedEnv.deployment = DeploymentEntry.enrich(env.deployment, properties);

	return enrichedEnv;
}
 
Example #4
Source File: CliClient.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
private void callCreateView(SqlCommandCall cmdCall) {
	final String name = cmdCall.operands[0];
	final String query = cmdCall.operands[1];

	final ViewEntry previousView = context.getViews().get(name);
	if (previousView != null) {
		printExecutionError(CliStrings.MESSAGE_VIEW_ALREADY_EXISTS);
		return;
	}

	try {
		// perform and validate change
		context.addView(ViewEntry.create(name, query));
		executor.validateSession(context);
		printInfo(CliStrings.MESSAGE_VIEW_CREATED);
	} catch (SqlExecutionException e) {
		// rollback change
		context.removeView(name);
		printExecutionException(e);
	}
}
 
Example #5
Source File: CliClient.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
private void callDropView(SqlCommandCall cmdCall) {
	final String name = cmdCall.operands[0];
	final ViewEntry view = context.getViews().get(name);

	if (view == null) {
		printExecutionError(CliStrings.MESSAGE_VIEW_NOT_FOUND);
		return;
	}

	try {
		// perform and validate change
		context.removeView(name);
		executor.validateSession(context);
		printInfo(CliStrings.MESSAGE_VIEW_REMOVED);
	} catch (SqlExecutionException e) {
		// rollback change
		context.addView(view);
		printExecutionException(CliStrings.MESSAGE_VIEW_NOT_REMOVED, e);
	}
}
 
Example #6
Source File: ExecutionContext.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private void registerView(ViewEntry viewEntry) {
	try {
		tableEnv.registerTable(viewEntry.getName(), tableEnv.sqlQuery(viewEntry.getQuery()));
	} catch (Exception e) {
		throw new SqlExecutionException(
			"Invalid view '" + viewEntry.getName() + "' with query:\n" + viewEntry.getQuery()
				+ "\nCause: " + e.getMessage());
	}
}
 
Example #7
Source File: ExecutionContext.java    From flink with Apache License 2.0 5 votes vote down vote up
private void registerTemporaryView(ViewEntry viewEntry) {
	try {
		tableEnv.createTemporaryView(viewEntry.getName(), tableEnv.sqlQuery(viewEntry.getQuery()));
	} catch (Exception e) {
		throw new SqlExecutionException(
			"Invalid view '" + viewEntry.getName() + "' with query:\n" + viewEntry.getQuery()
				+ "\nCause: " + e.getMessage());
	}
}
 
Example #8
Source File: Environment.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Enriches an environment with new/modified properties or views and returns the new instance.
 */
public static Environment enrich(
		Environment env,
		Map<String, String> properties,
		Map<String, ViewEntry> views) {
	final Environment enrichedEnv = new Environment();

	// merge catalogs
	enrichedEnv.catalogs = new LinkedHashMap<>(env.getCatalogs());

	// merge tables
	enrichedEnv.tables = new LinkedHashMap<>(env.getTables());
	enrichedEnv.tables.putAll(views);

	// merge functions
	enrichedEnv.functions = new HashMap<>(env.getFunctions());

	// enrich execution properties
	enrichedEnv.execution = ExecutionEntry.enrich(env.execution, properties);

	// enrich configuration properties
	enrichedEnv.configuration = ConfigurationEntry.enrich(env.configuration, properties);

	// enrich deployment properties
	enrichedEnv.deployment = DeploymentEntry.enrich(env.deployment, properties);

	return enrichedEnv;
}
 
Example #9
Source File: ExecutionContext.java    From flink with Apache License 2.0 5 votes vote down vote up
private void registerView(ViewEntry viewEntry) {
	try {
		tableEnv.registerTable(viewEntry.getName(), tableEnv.sqlQuery(viewEntry.getQuery()));
	} catch (Exception e) {
		throw new SqlExecutionException(
			"Invalid view '" + viewEntry.getName() + "' with query:\n" + viewEntry.getQuery()
				+ "\nCause: " + e.getMessage());
	}
}
 
Example #10
Source File: ExecutionContext.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private EnvironmentInstance() {
	// create environments
	if (mergedEnv.getExecution().isStreamingExecution()) {
		streamExecEnv = createStreamExecutionEnvironment();
		execEnv = null;
		tableEnv = StreamTableEnvironment.create(streamExecEnv);
	} else if (mergedEnv.getExecution().isBatchExecution()) {
		streamExecEnv = null;
		execEnv = createExecutionEnvironment();
		tableEnv = BatchTableEnvironment.create(execEnv);
	} else {
		throw new SqlExecutionException("Unsupported execution type specified.");
	}

	// create query config
	queryConfig = createQueryConfig();

	// register table sources
	tableSources.forEach(tableEnv::registerTableSource);

	// register table sinks
	tableSinks.forEach(tableEnv::registerTableSink);

	// register user-defined functions
	registerFunctions();

	// register views and temporal tables in specified order
	mergedEnv.getTables().forEach((name, entry) -> {
		// if registering a view fails at this point,
		// it means that it accesses tables that are not available anymore
		if (entry instanceof ViewEntry) {
			final ViewEntry viewEntry = (ViewEntry) entry;
			registerView(viewEntry);
		} else if (entry instanceof TemporalTableEntry) {
			final TemporalTableEntry temporalTableEntry = (TemporalTableEntry) entry;
			registerTemporalTable(temporalTableEntry);
		}
	});
}
 
Example #11
Source File: ExecutionContext.java    From flink with Apache License 2.0 4 votes vote down vote up
private EnvironmentInstance() {
	// create settings
	final EnvironmentSettings settings = mergedEnv.getExecution().getEnvironmentSettings();

	// create environments
	if (mergedEnv.getExecution().isStreamingPlanner()) {
		streamExecEnv = createStreamExecutionEnvironment();
		execEnv = null;

		final Map<String, String> executorProperties = settings.toExecutorProperties();
		executor = lookupExecutor(executorProperties, streamExecEnv);
		tableEnv = createStreamTableEnvironment(streamExecEnv, settings, executor);
	} else if (mergedEnv.getExecution().isBatchPlanner()) {
		streamExecEnv = null;
		execEnv = createExecutionEnvironment();
		executor = null;
		tableEnv = BatchTableEnvironment.create(execEnv);
	} else {
		throw new SqlExecutionException("Unsupported execution type specified.");
	}

	// set table configuration
	mergedEnv.getConfiguration().asMap().forEach((k, v) ->
		tableEnv.getConfig().getConfiguration().setString(k, v));

	// register catalogs
	catalogs.forEach(tableEnv::registerCatalog);

	// create query config
	queryConfig = createQueryConfig();

	// register table sources
	tableSources.forEach(tableEnv::registerTableSource);

	// register table sinks
	tableSinks.forEach(tableEnv::registerTableSink);

	// register user-defined functions
	registerFunctions();

	// register views and temporal tables in specified order
	mergedEnv.getTables().forEach((name, entry) -> {
		// if registering a view fails at this point,
		// it means that it accesses tables that are not available anymore
		if (entry instanceof ViewEntry) {
			final ViewEntry viewEntry = (ViewEntry) entry;
			registerView(viewEntry);
		} else if (entry instanceof TemporalTableEntry) {
			final TemporalTableEntry temporalTableEntry = (TemporalTableEntry) entry;
			registerTemporalTable(temporalTableEntry);
		}
	});

	// set current catalog
	if (sessionContext.getCurrentCatalog().isPresent()) {
		tableEnv.useCatalog(sessionContext.getCurrentCatalog().get());
	} else if (mergedEnv.getExecution().getCurrentCatalog().isPresent()) {
		tableEnv.useCatalog(mergedEnv.getExecution().getCurrentCatalog().get());
	}

	// set current database
	if (sessionContext.getCurrentDatabase().isPresent()) {
		tableEnv.useDatabase(sessionContext.getCurrentDatabase().get());
	} else if (mergedEnv.getExecution().getCurrentDatabase().isPresent()) {
		tableEnv.useDatabase(mergedEnv.getExecution().getCurrentDatabase().get());
	}
}
 
Example #12
Source File: SessionContext.java    From flink with Apache License 2.0 4 votes vote down vote up
public void addView(ViewEntry viewEntry) {
	views.put(viewEntry.getName(), viewEntry);
}
 
Example #13
Source File: SessionContext.java    From flink with Apache License 2.0 4 votes vote down vote up
public Map<String, ViewEntry> getViews() {
	return Collections.unmodifiableMap(views);
}
 
Example #14
Source File: LocalExecutorITCase.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
@Test
public void testValidateSession() throws Exception {
	final Executor executor = createDefaultExecutor(clusterClient);
	final SessionContext session = new SessionContext("test-session", new Environment());

	executor.validateSession(session);

	session.addView(ViewEntry.create("AdditionalView1", "SELECT 1"));
	session.addView(ViewEntry.create("AdditionalView2", "SELECT * FROM AdditionalView1"));
	executor.validateSession(session);

	List<String> actualTables = executor.listTables(session);
	List<String> expectedTables = Arrays.asList(
		"AdditionalView1",
		"AdditionalView2",
		"TableNumber1",
		"TableNumber2",
		"TableSourceSink",
		"TestView1",
		"TestView2");
	assertEquals(expectedTables, actualTables);

	session.removeView("AdditionalView1");
	try {
		executor.validateSession(session);
		fail();
	} catch (SqlExecutionException e) {
		// AdditionalView2 needs AdditionalView1
	}

	session.removeView("AdditionalView2");
	executor.validateSession(session);

	actualTables = executor.listTables(session);
	expectedTables = Arrays.asList(
		"TableNumber1",
		"TableNumber2",
		"TableSourceSink",
		"TestView1",
		"TestView2");
	assertEquals(expectedTables, actualTables);
}
 
Example #15
Source File: SessionContext.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
public Map<String, ViewEntry> getViews() {
	return Collections.unmodifiableMap(views);
}
 
Example #16
Source File: SessionContext.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
public void addView(ViewEntry viewEntry) {
	views.put(viewEntry.getName(), viewEntry);
}
 
Example #17
Source File: LocalExecutorITCase.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test
public void testValidateSession() throws Exception {
	final Executor executor = createDefaultExecutor(clusterClient);
	final SessionContext session = new SessionContext("test-session", new Environment());

	executor.validateSession(session);

	session.addView(ViewEntry.create("AdditionalView1", "SELECT 1"));
	session.addView(ViewEntry.create("AdditionalView2", "SELECT * FROM AdditionalView1"));
	executor.validateSession(session);

	List<String> actualTables = executor.listTables(session);
	List<String> expectedTables = Arrays.asList(
		"TableNumber1",
		"TableNumber2",
		"TableSourceSink",
		"TestView1",
		"TestView2",
		"AdditionalView1",
		"AdditionalView2");
	assertEquals(expectedTables, actualTables);

	session.removeView("AdditionalView1");
	try {
		executor.validateSession(session);
		fail();
	} catch (SqlExecutionException e) {
		// AdditionalView2 needs AdditionalView1
	}

	session.removeView("AdditionalView2");
	executor.validateSession(session);

	actualTables = executor.listTables(session);
	expectedTables = Arrays.asList(
		"TableNumber1",
		"TableNumber2",
		"TableSourceSink",
		"TestView1",
		"TestView2");
	assertEquals(expectedTables, actualTables);
}