You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by "ASF GitHub Bot (JIRA)" <ji...@apache.org> on 2018/08/09 12:22:00 UTC

[jira] [Commented] (FLINK-9343) Add Async Example with External Rest API call

    [ https://issues.apache.org/jira/browse/FLINK-9343?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16574763#comment-16574763 ] 

ASF GitHub Bot commented on FLINK-9343:
---------------------------------------

azagrebin commented on a change in pull request #5996: [FLINK-9343] [Example] Add Async Example with External Rest API call
URL: https://github.com/apache/flink/pull/5996#discussion_r208905065
 
 

 ##########
 File path: flink-examples/flink-examples-streaming/src/main/scala/org/apache/flink/streaming/scala/examples/async/AsyncAPIExample.scala
 ##########
 @@ -0,0 +1,93 @@
+/*
+ * 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.async
+
+
+import java.io.{BufferedReader, InputStreamReader}
+import java.util.concurrent.TimeUnit
+
+import org.apache.flink.runtime.concurrent.Executors
+import org.apache.flink.streaming.api.scala.{AsyncDataStream, DataStream, StreamExecutionEnvironment, _}
+import org.apache.flink.streaming.api.scala.async.{AsyncFunction, ResultFuture}
+import org.apache.http.client.methods.HttpGet
+import org.apache.http.impl.client.HttpClientBuilder
+
+import scala.concurrent.{ExecutionContext, Future}
+
+object AsyncAPIExample {
+
+  def main(args: Array[String]) {
+
+    val env = StreamExecutionEnvironment.getExecutionEnvironment
+
+    val input : DataStream[Int] = env.addSource(new SimpleSource())
+
+    val quoteStream: DataStream[(Int, String)] = AsyncDataStream.unorderedWait(
+      input,
+      new AsyncQuoteRequest,
+      1000,
+      TimeUnit.MILLISECONDS,
+      10)
+
+    quoteStream.print()
+
+    env.execute("Async API job")
+  }
+}
+
+class AsyncQuoteRequest extends AsyncFunction[Int, (Int, String)] {
+
+  /** The API specific client that can issue concurrent requests with callbacks */
+
+  /** The context used for the future callbacks */
+  implicit lazy val executor: ExecutionContext = ExecutionContext.global
+
+  lazy val client = new Quote()
+
+  override def asyncInvoke(input: Int, resultFuture: ResultFuture[(Int, String)]): Unit = {
+
+
+    // issue the asynchronous request, receive a future for the result
+    val resultFutureRequested: Future[String] = Future {
+      client.getQuote(input.toString)
+    }
 
 Review comment:
   It can be a bit shorter as in `AsyncIOExample`:
   ```
   Future {
         val result = client.getQuote(input.toString)
         resultFuture.complete(Iterable((input, result)))
   } (ExecutionContext.global)
   ```
   then no need for `implicit lazy val executor`

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


> Add Async Example with External Rest API call
> ---------------------------------------------
>
>                 Key: FLINK-9343
>                 URL: https://issues.apache.org/jira/browse/FLINK-9343
>             Project: Flink
>          Issue Type: Improvement
>          Components: Examples
>    Affects Versions: 1.4.0, 1.4.1, 1.4.2
>            Reporter: Yazdan Shirvany
>            Assignee: Yazdan Shirvany
>            Priority: Minor
>              Labels: pull-request-available
>
> Async I/O is a good way to call External resources such as REST API and enrich the stream with external data.
> Adding example to simulate Async GET api call on an input stream.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)