use of org.apache.flink.table.planner.runtime.utils.TestingRetractSink in project flink by apache.
the class HiveCatalogUdfITCase method testUdf.
private void testUdf(boolean batch) throws Exception {
StreamExecutionEnvironment env = null;
TableEnvironment tEnv;
EnvironmentSettings.Builder settingsBuilder = EnvironmentSettings.newInstance();
if (batch) {
settingsBuilder.inBatchMode();
} else {
settingsBuilder.inStreamingMode();
}
if (batch) {
tEnv = TableEnvironment.create(settingsBuilder.build());
} else {
env = StreamExecutionEnvironment.getExecutionEnvironment();
tEnv = StreamTableEnvironment.create(env, settingsBuilder.build());
}
BatchTestBase.configForMiniCluster(tEnv.getConfig());
tEnv.registerCatalog("myhive", hiveCatalog);
tEnv.useCatalog("myhive");
String innerSql = format("select mygenericudf(myudf(name), 1) as a, mygenericudf(myudf(age), 1) as b," + " s from %s, lateral table(myudtf(name, 1)) as T(s)", sourceTableName);
String selectSql = format("select a, s, sum(b), myudaf(b) from (%s) group by a, s", innerSql);
List<String> results;
if (batch) {
Path p = Paths.get(tempFolder.newFolder().getAbsolutePath(), "test.csv");
final TableSchema sinkSchema = TableSchema.builder().field("name1", Types.STRING()).field("name2", Types.STRING()).field("sum1", Types.INT()).field("sum2", Types.LONG()).build();
final Map<String, String> sinkOptions = new HashMap<>();
sinkOptions.put("connector.type", "filesystem");
sinkOptions.put("connector.path", p.toAbsolutePath().toString());
sinkOptions.put("format.type", "csv");
final CatalogTable sink = new CatalogTableImpl(sinkSchema, sinkOptions, "Comment.");
hiveCatalog.createTable(new ObjectPath(HiveCatalog.DEFAULT_DB, sinkTableName), sink, false);
tEnv.executeSql(format("insert into %s " + selectSql, sinkTableName)).await();
// assert written result
StringBuilder builder = new StringBuilder();
try (Stream<Path> paths = Files.walk(Paths.get(p.toAbsolutePath().toString()))) {
paths.filter(Files::isRegularFile).forEach(path -> {
try {
String content = FileUtils.readFileUtf8(path.toFile());
if (content.isEmpty()) {
return;
}
builder.append(content);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
}
results = Arrays.stream(builder.toString().split("\n")).filter(s -> !s.isEmpty()).collect(Collectors.toList());
} else {
StreamTableEnvironment streamTEnv = (StreamTableEnvironment) tEnv;
TestingRetractSink sink = new TestingRetractSink();
streamTEnv.toRetractStream(tEnv.sqlQuery(selectSql), Row.class).map(new JavaToScala()).addSink((SinkFunction) sink);
env.execute("");
results = JavaScalaConversionUtil.toJava(sink.getRetractResults());
}
results = new ArrayList<>(results);
results.sort(String::compareTo);
Assert.assertEquals(Arrays.asList("1,1,2,2", "2,2,4,4", "3,3,6,6"), results);
}
Aggregations