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

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

asfgit closed pull request #6790: [FLINK-10487] [table] fix invalid Flink SQL 
example and add runnable SQL example for Java API
URL: https://github.com/apache/flink/pull/6790
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/docs/dev/table/sql.md b/docs/dev/table/sql.md
index a9fd94f10d5..9b9b40d4081 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 00000000000..99e79a8a8cb
--- /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 3297aec418d..77db33dc861 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()


 

----------------------------------------------------------------
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:
us...@infra.apache.org


> fix invalid Flink SQL example
> -----------------------------
>
>                 Key: FLINK-10487
>                 URL: https://issues.apache.org/jira/browse/FLINK-10487
>             Project: Flink
>          Issue Type: Bug
>          Components: Documentation, Table API &amp; SQL
>    Affects Versions: 1.6.0, 1.6.1, 1.7.0
>            Reporter: Bowen Li
>            Assignee: Bowen Li
>            Priority: Major
>              Labels: pull-request-available
>             Fix For: 1.7.0
>
>
> On https://ci.apache.org/projects/flink/flink-docs-master/dev/table/sql.html 
> , the example is :
> {code:java}
> StreamExecutionEnvironment env = 
> StreamExecutionEnvironment.getExecutionEnvironment();
> StreamTableEnvironment tableEnv = TableEnvironment.getTableEnvironment(env);
> // ingest a DataStream from an external source
> DataStream<Tuple3<Long, String, Integer>> ds = env.addSource(...);
> // SQL query with an inlined (unregistered) table
> Table table = tableEnv.toTable(ds, "user, product, amount");
> Table result = tableEnv.sqlQuery(
>   "SELECT SUM(amount) FROM " + table + " WHERE product LIKE '%Rubber%'");
> ...
> {code}
> But there's no API of {{toTable()}} in StreamTableEnvironment 
> cc [~fhueske]



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

Reply via email to