zjffdu commented on a change in pull request #10306: [FLINK-13943][table-api] Provide utility method to convert Flink table to Java List URL: https://github.com/apache/flink/pull/10306#discussion_r350504511
########## File path: flink-table/flink-table-api-java-bridge/src/main/java/org/apache/flink/table/utils/TableResultUtils.java ########## @@ -0,0 +1,122 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.table.utils; + +import org.apache.flink.annotation.Experimental; +import org.apache.flink.api.common.ExecutionConfig; +import org.apache.flink.api.common.JobExecutionResult; +import org.apache.flink.api.common.accumulators.Accumulator; +import org.apache.flink.api.common.accumulators.SerializedListAccumulator; +import org.apache.flink.api.common.typeinfo.TypeInformation; +import org.apache.flink.api.common.typeutils.TypeSerializer; +import org.apache.flink.api.java.Utils; +import org.apache.flink.streaming.api.datastream.DataStream; +import org.apache.flink.streaming.api.datastream.DataStreamSink; +import org.apache.flink.table.api.Table; +import org.apache.flink.table.api.TableEnvironment; +import org.apache.flink.table.api.TableSchema; +import org.apache.flink.table.api.internal.TableImpl; +import org.apache.flink.table.sinks.StreamTableSink; +import org.apache.flink.table.sinks.TableSink; +import org.apache.flink.table.types.DataType; +import org.apache.flink.types.Row; +import org.apache.flink.util.AbstractID; + +import java.util.List; + +/** + * A collection of utilities for fetching table results. + * + * <p>NOTE: Methods in this utility class are experimental and can only be used for demonstration or testing + * small table results. Please DO NOT use them in production or on large tables. + */ +@Experimental +public class TableResultUtils { + + /** + * Convert Flink table to Java list. + * + * @param table Flink table to convert + * @return Converted Java list + */ + public static List<Row> tableResultToList(Table table) { + final TableEnvironment tEnv = ((TableImpl) table).getTableEnvironment(); + + final String id = new AbstractID().toString(); + final TypeSerializer<Row> serializer = table.getSchema().toRowType().createSerializer(new ExecutionConfig()); + final Utils.CollectHelper<Row> outputFormat = new Utils.CollectHelper<>(id, serializer); + final TableResultSink sink = new TableResultSink(table, outputFormat); + + final String sinkName = "tableResultSink" + id; Review comment: `tableResultSink_` would be more readably when you check log or web ui. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
