You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by fh...@apache.org on 2018/10/08 11:55:46 UTC
[flink] branch release-1.5 updated: [FLINK-10487] [docs] Fix table
conversion example and add runnable SQL example for Java.
This is an automated email from the ASF dual-hosted git repository.
fhueske pushed a commit to branch release-1.5
in repository https://gitbox.apache.org/repos/asf/flink.git
The following commit(s) were added to refs/heads/release-1.5 by this push:
new 1eb09d1 [FLINK-10487] [docs] Fix table conversion example and add runnable SQL example for Java.
1eb09d1 is described below
commit 1eb09d1fe1ee1ad6525ca751340c0a2f9935b92c
Author: Bowen Li <bo...@gmail.com>
AuthorDate: Wed Oct 3 16:18:49 2018 -0700
[FLINK-10487] [docs] Fix table conversion example and add runnable SQL example for Java.
This closes #6790.
---
docs/dev/table/sql.md | 2 +-
.../table/examples/java/StreamSQLExample.java | 104 +++++++++++++++++++++
.../table/examples/scala/StreamSQLExample.scala | 9 +-
3 files changed, 110 insertions(+), 5 deletions(-)
diff --git a/docs/dev/table/sql.md b/docs/dev/table/sql.md
index 5bb3183..aab1c54 100644
--- a/docs/dev/table/sql.md
+++ b/docs/dev/table/sql.md
@@ -48,7 +48,7 @@ StreamTableEnvironment tableEnv = TableEnvironment.getTableEnvironment(env);
DataStream<Tuple3<Long, String, Integer>> ds = env.addSource(...);
// SQL query with an inlined (unregistered) table
-Table table = tableEnv.toTable(ds, "user, product, amount");
+Table table = tableEnv.fromDataStream(ds, "user, product, amount");
Table result = tableEnv.sqlQuery(
"SELECT SUM(amount) FROM " + table + " WHERE product LIKE '%Rubber%'");
diff --git a/flink-examples/flink-examples-table/src/main/java/org/apache/flink/table/examples/java/StreamSQLExample.java b/flink-examples/flink-examples-table/src/main/java/org/apache/flink/table/examples/java/StreamSQLExample.java
new file mode 100644
index 0000000..99e79a8
--- /dev/null
+++ b/flink-examples/flink-examples-table/src/main/java/org/apache/flink/table/examples/java/StreamSQLExample.java
@@ -0,0 +1,104 @@
+/*
+ * 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.examples.java;
+
+import org.apache.flink.streaming.api.datastream.DataStream;
+import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
+import org.apache.flink.table.api.Table;
+import org.apache.flink.table.api.TableEnvironment;
+import org.apache.flink.table.api.java.StreamTableEnvironment;
+
+import java.util.Arrays;
+
+/**
+ * Simple example for demonstrating the use of SQL on a Stream Table in Java.
+ *
+ * <p>This example shows how to:
+ * - Convert DataStreams to Tables
+ * - Register a Table under a name
+ * - Run a StreamSQL query on the registered Table
+ *
+ */
+public class StreamSQLExample {
+
+ // *************************************************************************
+ // PROGRAM
+ // *************************************************************************
+
+ public static void main(String[] args) throws Exception {
+
+ // set up execution environment
+ StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
+ StreamTableEnvironment tEnv = TableEnvironment.getTableEnvironment(env);
+
+ DataStream<Order> orderA = env.fromCollection(Arrays.asList(
+ new Order(1L, "beer", 3),
+ new Order(1L, "diaper", 4),
+ new Order(3L, "rubber", 2)));
+
+ DataStream<Order> orderB = env.fromCollection(Arrays.asList(
+ new Order(2L, "pen", 3),
+ new Order(2L, "rubber", 3),
+ new Order(4L, "beer", 1)));
+
+ // convert DataStream to Table
+ Table tableA = tEnv.fromDataStream(orderA, "user, product, amount");
+ // register DataStream as Table
+ tEnv.registerDataStream("OrderB", orderB, "user, product, amount");
+
+ // union the two tables
+ Table result = tEnv.sqlQuery("SELECT * FROM " + tableA + " WHERE amount > 2 UNION ALL " +
+ "SELECT * FROM OrderB WHERE amount < 2");
+
+ tEnv.toAppendStream(result, Order.class).print();
+
+ env.execute();
+ }
+
+ // *************************************************************************
+ // USER DATA TYPES
+ // *************************************************************************
+
+ /**
+ * Simple POJO.
+ */
+ public static class Order {
+ public Long user;
+ public String product;
+ public int amount;
+
+ public Order() {
+ }
+
+ public Order(Long user, String product, int amount) {
+ this.user = user;
+ this.product = product;
+ this.amount = amount;
+ }
+
+ @Override
+ public String toString() {
+ return "Order{" +
+ "user=" + user +
+ ", product='" + product + '\'' +
+ ", amount=" + amount +
+ '}';
+ }
+ }
+}
diff --git a/flink-examples/flink-examples-table/src/main/scala/org/apache/flink/table/examples/scala/StreamSQLExample.scala b/flink-examples/flink-examples-table/src/main/scala/org/apache/flink/table/examples/scala/StreamSQLExample.scala
index 3297aec..77db33d 100644
--- a/flink-examples/flink-examples-table/src/main/scala/org/apache/flink/table/examples/scala/StreamSQLExample.scala
+++ b/flink-examples/flink-examples-table/src/main/scala/org/apache/flink/table/examples/scala/StreamSQLExample.scala
@@ -23,7 +23,7 @@ import org.apache.flink.table.api.TableEnvironment
import org.apache.flink.table.api.scala._
/**
- * Simple example for demonstrating the use of SQL on a Stream Table.
+ * Simple example for demonstrating the use of SQL on a Stream Table in Scala.
*
* This example shows how to:
* - Convert DataStreams to Tables
@@ -53,13 +53,14 @@ object StreamSQLExample {
Order(2L, "rubber", 3),
Order(4L, "beer", 1)))
- // register the DataStreams under the name "OrderA" and "OrderB"
- tEnv.registerDataStream("OrderA", orderA, 'user, 'product, 'amount)
+ // convert DataStream to Table
+ var tableA = tEnv.fromDataStream(orderA, 'user, 'product, 'amount)
+ // register DataStream as Table
tEnv.registerDataStream("OrderB", orderB, 'user, 'product, 'amount)
// union the two tables
val result = tEnv.sqlQuery(
- "SELECT * FROM OrderA WHERE amount > 2 UNION ALL " +
+ s"SELECT * FROM $tableA WHERE amount > 2 UNION ALL " +
"SELECT * FROM OrderB WHERE amount < 2")
result.toAppendStream[Order].print()