使用自定义接收器的spark结构化流中的输入行数

时间:2018-01-26 16:59:01

标签: apache-spark apache-spark-sql spark-streaming spark-structured-streaming

我在结构化流中使用自定义接收器(spark 2.2.0),并注意到spark会对输入行数产生错误的指标 - 它始终为零。

我的流程构建:

StreamingQuery writeStream = session
            .readStream()
            .schema(RecordSchema.fromClass(TestRecord.class))
            .option(OPTION_KEY_DELIMITER, OPTION_VALUE_DELIMITER_TAB)
            .option(OPTION_KEY_QUOTE, OPTION_VALUE_QUOTATION_OFF)
            .csv(s3Path.toString())
            .as(Encoders.bean(TestRecord.class))
            .flatMap(
                ((FlatMapFunction<TestRecord, TestOutputRecord>) (u) -> {
                    List<TestOutputRecord> list = new ArrayList<>();
                    try {
                        TestOutputRecord result = transformer.convert(u);
                        list.add(result);
                    } catch (Throwable t) {
                        System.err.println("Failed to convert a record");
                        t.printStackTrace();
                    }

                    return list.iterator();
                }),
                Encoders.bean(TestOutputRecord.class))
        .map(new DataReinforcementMapFunction<>(), Encoders.bean(TestOutputRecord.clazz))
        .writeStream()
        .trigger(Trigger.ProcessingTime(WRITE_FREQUENCY, TimeUnit.SECONDS))
        .format(MY_WRITER_FORMAT)
        .outputMode(OutputMode.Append())
        .queryName("custom-sink-stream")
        .start();

        writeStream.processAllAvailable();
        writeStream.stop();

日志:

Streaming query made progress: {
  "id" : "a8a7fbc2-0f06-4197-a99a-114abae24964",
  "runId" : "bebc8a0c-d3b2-4fd6-8710-78223a88edc7",
  "name" : "custom-sink-stream",
  "timestamp" : "2018-01-25T18:39:52.949Z",
  "numInputRows" : 0,
  "inputRowsPerSecond" : 0.0,
  "processedRowsPerSecond" : 0.0,
  "durationMs" : {
    "getOffset" : 781,
    "triggerExecution" : 781
  },
  "stateOperators" : [ ],
  "sources" : [ {
    "description" : "FileStreamSource[s3n://test-bucket/test]",
    "startOffset" : {
      "logOffset" : 0
    },
    "endOffset" : {
      "logOffset" : 0
    },
    "numInputRows" : 0,
    "inputRowsPerSecond" : 0.0,
    "processedRowsPerSecond" : 0.0
  } ],
  "sink" : {
    "description" : "com.mycompany.spark.MySink@f82a99"
  }
}

我是否必须在自定义接收器中填充任何指标才能跟踪进度?或者当它从s3存储桶读取时,它可能是FileStreamSource中的问题吗?

1 个答案:

答案 0 :(得分:0)

问题与在我的自定义接收器中使用dataset.rdd创建新计划有关,因此StreamExecution不了解它,因此无法获取指标。

data.rdd.mapPartitions替换data.queryExecution.toRdd.mapPartitions可解决问题。