2

我在两个DataStream通用记录类型上应用联合运算符。

package com.gslab.com.dataSets;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.apache.avro.Schema;
import org.apache.avro.generic.GenericData;
import org.apache.avro.generic.GenericData.Record;
import org.apache.avro.generic.GenericRecord;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class FlinkBroadcast {
    public static void main(String[] args) throws Exception {

        final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(2);

        List<String> controlMessageList = new ArrayList<String>();
        controlMessageList.add("controlMessage1");
        controlMessageList.add("controlMessage2");

        List<String> dataMessageList = new ArrayList<String>();
        dataMessageList.add("Person1");
        dataMessageList.add("Person2");
        dataMessageList.add("Person3");
        dataMessageList.add("Person4");

        DataStream<String> controlMessageStream  = env.fromCollection(controlMessageList);
        DataStream<String> dataMessageStream  = env.fromCollection(dataMessageList);

        DataStream<GenericRecord> controlMessageGenericRecordStream = controlMessageStream.map(new MapFunction<String, GenericRecord>() {
            @Override
            public GenericRecord map(String value) throws Exception {
                 Record gr = new GenericData.Record(new Schema.Parser().parse(new File("src/main/resources/controlMessageSchema.avsc")));
                 gr.put("TYPE", value);
                 return gr;
            }
        });

        DataStream<GenericRecord> dataMessageGenericRecordStream = dataMessageStream.map(new MapFunction<String, GenericRecord>() {
            @Override
            public GenericRecord map(String value) throws Exception {
                 Record gr = new GenericData.Record(new Schema.Parser().parse(new File("src/main/resources/dataMessageSchema.avsc")));
                 gr.put("FIRSTNAME", value);
                 gr.put("LASTNAME", value+": lastname");
                 return gr;
            }
        });

        //Displaying Generic records
        dataMessageGenericRecordStream.map(new MapFunction<GenericRecord, GenericRecord>() {
            @Override
            public GenericRecord map(GenericRecord value) throws Exception {
                System.out.println("data before union: "+ value);
                return value;
            }
        });

        controlMessageGenericRecordStream.broadcast().union(dataMessageGenericRecordStream).map(new MapFunction<GenericRecord, GenericRecord>() {
            @Override
            public GenericRecord map(GenericRecord value) throws Exception {
                System.out.println("data after union: " + value);
                return value;
            }
        });
        env.execute("stream");
    }
}

输出:

05/09/2016 13:02:13 Map(2/2) switched to FINISHED 
data after union: {"TYPE": "controlMessage1"}
data before union: {"FIRSTNAME": "Person2", "LASTNAME": "Person2: lastname"}
data after union: {"TYPE": "controlMessage1"}
data before union: {"FIRSTNAME": "Person1", "LASTNAME": "Person1: lastname"}
data after union: {"TYPE": "controlMessage2"}
data after union: {"TYPE": "controlMessage2"}
data after union: {"FIRSTNAME": "Person1", "LASTNAME": "Person1"}
data before union: {"FIRSTNAME": "Person4", "LASTNAME": "Person4: lastname"}
data before union: {"FIRSTNAME": "Person3", "LASTNAME": "Person3: lastname"}
data after union: {"FIRSTNAME": "Person2", "LASTNAME": "Person2"}
data after union: {"FIRSTNAME": "Person3", "LASTNAME": "Person3"}
05/09/2016 13:02:13 Map -> Map(2/2) switched to FINISHED 
data after union: {"FIRSTNAME": "Person4", "LASTNAME": "Person4"}
05/09/2016 13:02:13 Map -> Map(1/2) switched to FINISHED 
05/09/2016 13:02:13 Map(1/2) switched to FINISHED 
05/09/2016 13:02:13 Map(2/2) switched to FINISHED 
05/09/2016 13:02:13 Job execution switched to status FINISHED.

如您所见,合并后 dataMessageGenericRecordStream 中的记录不正确。所有字段值都将被第一个字段值替换。

4

2 回答 2

2

我花了几天时间调查这个不同的问题(但仍然涉及 GenericRecord),并找到了根本原因和解决方案。

根本原因:在 Apache Avro“Schema.class”中,“字段”位置是 TRANSIENT,不会被 Kryo 序列化,因此在 Flink 管道中反序列化时被初始化为位置“0”。

请参阅描述这一点并特别提到 kyro 序列化的 JIRA AVRO-1476。

这已在 Avro 1.7.7 中修复

解决方案:Flink 必须使用 Avro 1.7.7(或更高版本)。我已经通过替换 flink-dist_2.11-1.1.3.jar 中的 Avro 类验证了本地计算机中的修复,它纠正了我的问题。

我为此更新了 JIRA 问题:https ://issues.apache.org/jira/browse/FLINK-5039

现在有一个 PR:https ://github.com/apache/flink/pull/2953

我预计它将包含在 Flink 1.1.4 和 1.2.0 版本中。

于 2016-12-08T16:47:31.670 回答
1

我在 DataSet API 中遇到了类似的问题。我正在阅读一些 Avro 文件作为 GenericRecords 并看到了这种奇怪的行为。我使用了这个解决方法——我没有将它们读取为 GenericRecords,而是将它们读取为特定记录(例如 MyAvroObject),然后使用映射将它们转换/类型转换为 GenericRecords。

我编写了一些代码来使用 DataSet API 测试您的用例,它适用于上述解决方法-

public static void maintest(String[] args) throws Exception {
    ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
    env.setParallelism(2);

    List<String> queryList1 = new ArrayList<String>();
    queryList1.add("query1");
    queryList1.add("query2");

    List<String> queryList2 = new ArrayList<String>();
    queryList2.add("QUERY1");
    queryList2.add("QUERY2");
    queryList2.add("QUERY3");
    queryList2.add("QUERY4");

    DataSet<String> dataset1  = env.fromCollection(queryList1);
    DataSet<String> dataset2  = env.fromCollection(queryList2);

    DataSet<GenericRecord> genericDS1 = dataset1.map(new MapFunction<String, GenericRecord>() {
        @Override
        public GenericRecord map(String value) throws Exception {
            Query query = Query.newBuilder().setQuery(value).build();
            return (GenericRecord) query;
        }
    });

    DataSet<GenericRecord> genericDS2 = dataset2.map(new MapFunction<String, GenericRecord>() {
        @Override
        public GenericRecord map(String value) throws Exception {
            SearchEngineQuery searchEngineQuery = SearchEngineQuery.newBuilder().setSeQuery(value).build();
            return (GenericRecord) searchEngineQuery;
        }
    });

    genericDS2.map(new MapFunction<GenericRecord, GenericRecord>() {
        @Override
        public GenericRecord map(GenericRecord value) throws Exception {
            System.out.println("DEBUG: data before union: " + value);
            return value;
        }
    });

    genericDS1.union(genericDS2).map(new MapFunction<GenericRecord, GenericRecord>() {
        @Override
        public GenericRecord map(GenericRecord value) throws Exception {
            System.out.println("DEBUG: data after union: " + value);
            return value;
        }
    }).print();
}

其中 Query 和 SearchEngine 查询是我的 Avro 对象(类似于您的控制消息列表和数据消息列表)。

输出:

{"query": "query1"}
{"se_query": "QUERY1"}
{"se_query": "QUERY3"}
{"query": "query2"}
{"se_query": "QUERY2"}
{"se_query": "QUERY4"}
于 2016-05-12T08:36:41.073 回答