Github user thvasilo commented on a diff in the pull request:

    https://github.com/apache/flink/pull/2761#discussion_r89469773
  
    --- Diff: 
flink-examples/flink-examples-streaming/src/main/scala/org/apache/flink/streaming/scala/examples/ml/IncrementalLearningSkeleton.scala
 ---
    @@ -0,0 +1,169 @@
    +/*
    + * 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.streaming.scala.examples.ml
    +
    +import java.util.concurrent.TimeUnit
    +
    +import org.apache.flink.api.java.utils.ParameterTool
    +import org.apache.flink.api.scala._
    +import org.apache.flink.streaming.api.TimeCharacteristic
    +import 
org.apache.flink.streaming.api.functions.AssignerWithPunctuatedWatermarks
    +import org.apache.flink.streaming.api.functions.source.SourceFunction
    +import 
org.apache.flink.streaming.api.functions.source.SourceFunction.SourceContext
    +import org.apache.flink.streaming.api.scala.StreamExecutionEnvironment
    +import org.apache.flink.streaming.api.scala.function.AllWindowFunction
    +import org.apache.flink.streaming.api.watermark.Watermark
    +import org.apache.flink.streaming.api.windowing.time.Time
    +import org.apache.flink.streaming.api.windowing.windows.TimeWindow
    +import org.apache.flink.util.Collector
    +
    +/**
    +  * Skeleton for incremental machine learning algorithm consisting of a
    +  * pre-computed model, which gets updated for the new inputs and new 
input data
    +  * for which the job provides predictions.
    +  *
    +  * <p>
    +  * This may serve as a base of a number of algorithms, e.g. updating an
    +  * incremental Alternating Least Squares model while also providing the
    +  * predictions.
    +  *
    +  * <p>
    +  * This example shows how to use:
    +  * <ul>
    +  * <li>Connected streams
    +  * <li>CoFunctions
    +  * <li>Tuple data types
    +  * </ul>
    +  */
    +object IncrementalLearningSkeleton {
    +
    +  // 
*************************************************************************
    +  // PROGRAM
    +  // 
*************************************************************************
    +
    +  def main(args: Array[String]): Unit = {
    +    // Checking input parameters
    +    val params = ParameterTool.fromArgs(args)
    +
    +    // set up the execution environment
    +    val env = StreamExecutionEnvironment.getExecutionEnvironment
    +    env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime)
    +
    +    // build new model on every second of new data
    +    val trainingData = env.addSource(new FiniteTrainingDataSource)
    +    val newData = env.addSource(new FiniteNewDataSource)
    +
    +    val model = trainingData
    +      .assignTimestampsAndWatermarks(new LinearTimestamp)
    +      .timeWindowAll(Time.of(5000, TimeUnit.MILLISECONDS))
    +      .apply(new PartialModelBuilder)
    +
    +    // use partial model for newData
    +    val prediction = newData.connect(model).map(
    +      (_: Int) => 0,
    +      (_: Array[Double]) => 1
    +    )
    +
    +    // emit result
    +    if (params.has("output")) {
    +      prediction.writeAsText(params.get("output"))
    +    } else {
    +      println("Printing result to stdout. Use --output to specify output 
path.")
    +      prediction.print()
    +    }
    +
    +    // execute program
    +    env.execute("Streaming Incremental Learning")
    +  }
    +
    +  // 
*************************************************************************
    +  // USER FUNCTIONS
    +  // 
*************************************************************************
    +
    +  /**
    +    * Feeds new data for newData. By default it is implemented as 
constantly
    +    * emitting the Integer 1 in a loop.
    +    */
    +  private class FiniteNewDataSource extends SourceFunction[Int] {
    +    var counter: Int = 0
    +
    +    override def run(ctx: SourceContext[Int]) = {
    +      Thread.sleep(15)
    +      while (counter < 50) {
    +        ctx.collect(getNewData)
    +      }
    +    }
    +
    +    def getNewData = {
    +      Thread.sleep(5)
    +      counter += 1
    +      1
    +    }
    +
    +    override def cancel() = {
    +      // No cleanup needed
    +    }
    +  }
    +
    +  /**
    +    * Feeds new training data for the partial model builder. By default it 
is
    +    * implemented as constantly emitting the Integer 1 in a loop.
    +    */
    +  private class FiniteTrainingDataSource extends SourceFunction[Int] {
    +    var counter = 0
    +
    +    override def run(ctx: SourceContext[Int]) = {
    +      while (counter < 8200) ctx.collect(getTrainingData)
    +    }
    +
    +    def getTrainingData = {
    +      counter += 1
    +      1
    +    }
    +
    +    override def cancel() = {
    +      // No cleanup needed
    +    }
    +  }
    +
    +  private class LinearTimestamp extends 
AssignerWithPunctuatedWatermarks[Int] {
    +    var counter = 0L
    +
    +    override def extractTimestamp(element: Int, previousElementTimestamp: 
Long): Long = {
    +      counter += 10L
    +      counter
    +    }
    +
    +    override def checkAndGetNextWatermark(lastElement: Int, 
extractedTimestamp: Long) = {
    +      new Watermark(counter - 1)
    +    }
    +  }
    +
    +  /**
    +    * Builds up-to-date partial models on new training data.
    +    */
    +  private class PartialModelBuilder extends AllWindowFunction[Int, 
Array[Double], TimeWindow] {
    +    override def apply(window: TimeWindow,
    +                       input: Iterable[Int],
    +                       out: Collector[Array[Double]]): Unit = {
    +      out.collect(Array[Double](1.0))
    --- End diff --
    
    Same here, the `buildPartialModel` function serves an illustrative purpose 
in the Java code.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to