You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by di...@apache.org on 2022/04/27 12:37:36 UTC
[flink] branch master updated: [python][docs] Update examples to use print method to view the table results
This is an automated email from the ASF dual-hosted git repository.
dianfu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/flink.git
The following commit(s) were added to refs/heads/master by this push:
new fb00e1c4596 [python][docs] Update examples to use print method to view the table results
fb00e1c4596 is described below
commit fb00e1c45966ce353b79eee690e3196538326f6c
Author: Dian Fu <di...@apache.org>
AuthorDate: Wed Apr 27 17:57:07 2022 +0800
[python][docs] Update examples to use print method to view the table results
---
.../docs/dev/python/table/intro_to_table_api.md | 157 +++++++++++-----
.../docs/dev/python/table/udfs/overview.md | 18 +-
.../docs/dev/python/table/udfs/python_udfs.md | 72 +++-----
.../docs/dev/table/functions/systemFunctions.md | 28 +--
.../docs/dev/python/table/intro_to_table_api.md | 203 +++++++++++++--------
.../docs/dev/python/table/udfs/python_udfs.md | 29 +--
.../docs/dev/table/functions/systemFunctions.md | 26 +--
7 files changed, 314 insertions(+), 219 deletions(-)
diff --git a/docs/content.zh/docs/dev/python/table/intro_to_table_api.md b/docs/content.zh/docs/dev/python/table/intro_to_table_api.md
index 64b795b012f..fb4008e4cc5 100644
--- a/docs/content.zh/docs/dev/python/table/intro_to_table_api.md
+++ b/docs/content.zh/docs/dev/python/table/intro_to_table_api.md
@@ -68,7 +68,7 @@ table_env.execute_sql("""
# 通过 Table API 创建一张表:
source_table = table_env.from_path("datagen")
# 或者通过 SQL 查询语句创建一张表:
-source_table = table_env.sql_query("SELECT * FROM datagen")
+# source_table = table_env.sql_query("SELECT * FROM datagen")
result_table = source_table.select(source_table.id + 1, source_table.data)
@@ -76,7 +76,7 @@ result_table = source_table.select(source_table.id + 1, source_table.data)
# 将 Table API 结果表数据写入 sink 表:
result_table.execute_insert("print").wait()
# 或者通过 SQL 查询语句来写入 sink 表:
-table_env.execute_sql("INSERT INTO print SELECT * FROM datagen").wait()
+# table_env.execute_sql("INSERT INTO print SELECT * FROM datagen").wait()
```
{{< top >}}
@@ -131,56 +131,58 @@ env_settings = EnvironmentSettings.in_batch_mode()
table_env = TableEnvironment.create(env_settings)
table = table_env.from_elements([(1, 'Hi'), (2, 'Hello')])
-table.to_pandas()
+table.execute().print()
```
结果为:
```text
- _1 _2
-0 1 Hi
-1 2 Hello
++----------------------+--------------------------------+
+| _1 | _2 |
++----------------------+--------------------------------+
+| 1 | Hi |
+| 2 | Hello |
++----------------------+--------------------------------+
```
你也可以创建具有指定列名的表:
```python
table = table_env.from_elements([(1, 'Hi'), (2, 'Hello')], ['id', 'data'])
-table.to_pandas()
+table.execute().print()
```
结果为:
```text
- id data
-0 1 Hi
-1 2 Hello
++----------------------+--------------------------------+
+| id | data |
++----------------------+--------------------------------+
+| 1 | Hi |
+| 2 | Hello |
++----------------------+--------------------------------+
```
-默认情况下,表结构是从数据中自动提取的。
-
-如果自动生成的表模式不符合你的要求,你也可以手动指定:
+默认情况下,表结构是从数据中自动提取的。 如果自动生成的表模式不符合你的要求,你也可以手动指定:
```python
-table_without_schema = table_env.from_elements([(1, 'Hi'), (2, 'Hello')], ['id', 'data'])
+table = table_env.from_elements([(1, 'Hi'), (2, 'Hello')], ['id', 'data'])
# 默认情况下,“id” 列的类型是 64 位整型
-default_type = table_without_schema.to_pandas()["id"].dtype
-print('By default the type of the "id" column is %s.' % default_type)
+print('By default the type of the "id" column is %s.' % table.get_schema().get_field_data_type("id"))
from pyflink.table import DataTypes
table = table_env.from_elements([(1, 'Hi'), (2, 'Hello')],
DataTypes.ROW([DataTypes.FIELD("id", DataTypes.TINYINT()),
DataTypes.FIELD("data", DataTypes.STRING())]))
# 现在 “id” 列的类型是 8 位整型
-type = table.to_pandas()["id"].dtype
-print('Now the type of the "id" column is %s.' % type)
+print('Now the type of the "id" column is %s.' % table.get_schema().get_field_data_type("id"))
```
结果为:
```text
-默认情况下,“id” 列的类型是 64 位整型。
-现在 “id” 列的类型是 8 位整型。
+By default the type of the "id" column is BIGINT.
+Now the type of the "id" column is TINYINT.
```
### 通过 DDL 创建
@@ -209,16 +211,61 @@ table_env.execute_sql("""
)
""")
table = table_env.from_path("random_source")
-table.to_pandas()
+table.execute().print()
```
结果为:
```text
- id data
-0 2 5
-1 1 4
-2 3 6
++----+----------------------+--------+
+| op | id | data |
++----+----------------------+--------+
+| +I | 1 | 4 |
+| +I | 2 | 5 |
+| +I | 3 | 6 |
++----+----------------------+--------+
+```
+
+### 通过 TableDescriptor 创建
+
+你也可以通过 TableDescriptor 来创建表. 这种方式等价于通过 SQL DDL 语句的方式.
+
+```python
+from pyflink.table import EnvironmentSettings, TableEnvironment, TableDescriptor, Schema, DataTypes
+
+# create a stream TableEnvironment
+env_settings = EnvironmentSettings.in_streaming_mode()
+table_env = TableEnvironment.create(env_settings)
+
+table_env.create_temporary_table(
+ 'random_source',
+ TableDescriptor.for_connector('datagen')
+ .schema(Schema.new_builder()
+ .column('id', DataTypes.BIGINT())
+ .column('data', DataTypes.TINYINT())
+ .build())
+ .option('fields.id.kind', 'sequence')
+ .option('fields.id.start', '1')
+ .option('fields.id.end', '3')
+ .option('fields.data.kind', 'sequence')
+ .option('fields.data.start', '4')
+ .option('fields.data.end', '6')
+ .build())
+
+table = table_env.from_path("random_source")
+table.execute().print()
+```
+
+The results are as following:
+
+```text
++----+----------------------+--------+
+| op | id | data |
++----+----------------------+--------+
+| +I | 1 | 4 |
+| +I | 2 | 5 |
+| +I | 3 | 6 |
++----+----------------------+--------+
```
### 通过 Catalog 创建
@@ -241,15 +288,18 @@ table_env.create_temporary_view('source_table', table)
# 从 catalog 中获取 Table API 表
new_table = table_env.from_path('source_table')
-new_table.to_pandas()
+new_table.execute().print()
```
结果为:
```text
- id data
-0 1 Hi
-1 2 Hello
++----+----------------------+--------------------------------+
+| op | id | data |
++----+----------------------+--------------------------------+
+| +I | 1 | Hi |
+| +I | 2 | Hello |
++----+----------------------+--------------------------------+
```
{{< top >}}
@@ -284,14 +334,17 @@ revenue = orders \
.group_by(orders.name) \
.select(orders.name, orders.revenue.sum.alias('rev_sum'))
-revenue.to_pandas()
+revenue.execute().print()
```
结果为:
```text
- name rev_sum
-0 Jack 30
++--------------------------------+----------------------+
+| name | rev_sum |
++--------------------------------+----------------------+
+| Jack | 30 |
++--------------------------------+----------------------+
```
Table API 也支持 [行操作]({{< ref "docs/dev/table/tableapi" >}}#row-based-operations)的 API, 这些行操作包括 [Map Operation]({{< ref "docs/dev/table/tableapi" >}}#row-based-operations),
@@ -313,21 +366,24 @@ orders = table_env.from_elements([('Jack', 'FRANCE', 10), ('Rose', 'ENGLAND', 30
['name', 'country', 'revenue'])
map_function = udf(lambda x: pd.concat([x.name, x.revenue * 10], axis=1),
- result_type=DataTypes.ROW(
- [DataTypes.FIELD("name", DataTypes.STRING()),
- DataTypes.FIELD("revenue", DataTypes.BIGINT())]),
- func_type="pandas")
+ result_type=DataTypes.ROW(
+ [DataTypes.FIELD("name", DataTypes.STRING()),
+ DataTypes.FIELD("revenue", DataTypes.BIGINT())]),
+ func_type="pandas")
-orders.map(map_function).to_pandas()
+orders.map(map_function).execute().print()
```
结果为:
```text
- name revenue
-0 Jack 100
-1 Rose 300
-2 Jack 200
++--------------------------------+----------------------+
+| name | revenue |
++--------------------------------+----------------------+
+| Jack | 100 |
+| Rose | 300 |
+| Jack | 200 |
++--------------------------------+----------------------+
```
### SQL 查询
@@ -463,20 +519,23 @@ table_env.execute_sql("""
table = table_env.from_path("sql_source")
# 或者通过 SQL 查询语句创建表
-table = table_env.sql_query("SELECT * FROM sql_source")
+# table = table_env.sql_query("SELECT * FROM sql_source")
# 将表中的数据写出
-table.to_pandas()
+table.execute().print()
```
结果为:
```text
- id data
-0 2 5
-1 1 4
-2 4 7
-3 3 6
++----+----------------------+--------+
+| op | id | data |
++----+----------------------+--------+
+| +I | 1 | 4 |
+| +I | 2 | 5 |
+| +I | 3 | 6 |
+| +I | 4 | 7 |
++----+----------------------+--------+
```
{{< top >}}
@@ -516,7 +575,7 @@ with res.collect() as results:
```python
table = table_env.from_elements([(1, 'Hi'), (2, 'Hello')], ['id', 'data'])
-table.to_pandas()
+print(table.to_pandas())
```
结果为:
diff --git a/docs/content.zh/docs/dev/python/table/udfs/overview.md b/docs/content.zh/docs/dev/python/table/udfs/overview.md
index 08636b01728..741e5960ba7 100644
--- a/docs/content.zh/docs/dev/python/table/udfs/overview.md
+++ b/docs/content.zh/docs/dev/python/table/udfs/overview.md
@@ -31,22 +31,16 @@ functions]({{< ref "docs/dev/python/table/udfs/python_udfs" >}}) which process d
[vectorized Python user-defined functions]({{< ref "docs/dev/python/table/udfs/vectorized_python_udfs" >}})
which process data one batch at a time.
-## Bundling UDFs
+## 打包 UDFs
-To run Python UDFs (as well as Pandas UDFs) in any non-local mode, it is strongly recommended
-bundling your Python UDF definitions using the config option [`python-files`]({{< ref "docs/dev/python/python_config" >}}#python-files),
-if your Python UDFs live outside the file where the `main()` function is defined.
-Otherwise, you may run into `ModuleNotFoundError: No module named 'my_udf'`
-if you define Python UDFs in a file called `my_udf.py`.
+如果你在非 local 模式下运行 Python UDFs 和 Pandas UDFs,且 Python UDFs 没有定义在含 `main()` 入口的 Python 主文件中,强烈建议你通过 [`python-files`]({{< ref "docs/dev/python/python_config" >}}#python-files) 配置项指定 Python UDF 的定义。
+否则,如果你将 Python UDFs 定义在名为 `my_udf.py` 的文件中,你可能会遇到 `ModuleNotFoundError: No module named 'my_udf'` 这样的报错。
-## Loading resources in UDFs
+## 在 UDF 中载入资源
-There are scenarios when you want to load some resources in UDFs first, then running computation
-(i.e., `eval`) over and over again, without having to re-load the resources.
-For example, you may want to load a large deep learning model only once,
-then run batch prediction against the model multiple times.
+有时候,我们想在 UDF 中只载入一次资源,然后反复使用该资源进行计算。例如,你想在 UDF 中首先载入一个巨大的深度学习模型,然后使用该模型多次进行预测。
-Overriding the `open` method of `UserDefinedFunction` is exactly what you need.
+你要做的是重载 `UserDefinedFunction` 类的 `open` 方法。
```python
class Predict(ScalarFunction):
diff --git a/docs/content.zh/docs/dev/python/table/udfs/python_udfs.md b/docs/content.zh/docs/dev/python/table/udfs/python_udfs.md
index f16c433decc..4377474cabb 100644
--- a/docs/content.zh/docs/dev/python/table/udfs/python_udfs.md
+++ b/docs/content.zh/docs/dev/python/table/udfs/python_udfs.md
@@ -163,7 +163,6 @@ my_table.left_outer_join_lateral(split(my_table.a).alias("word", "length"))
table_env.create_temporary_function("split", udtf(Split(), result_types=[DataTypes.STRING(), DataTypes.INT()]))
table_env.sql_query("SELECT a, word, length FROM MyTable, LATERAL TABLE(split(a)) as T(word, length)")
table_env.sql_query("SELECT a, word, length FROM MyTable LEFT JOIN LATERAL TABLE(split(a)) as T(word, length) ON TRUE")
-
```
除此之外,还支持在 Python Table API 程序中使用 Java / Scala 表值函数。
@@ -312,23 +311,23 @@ t = table_env.from_elements([(1, 2, "Lee"),
(7, 8, "Lee")]).alias("value", "count", "name")
# call function "inline" without registration in Table API
-result = t.group_by(t.name).select(weighted_avg(t.value, t.count).alias("avg")).to_pandas()
-print(result)
+result = t.group_by(t.name).select(weighted_avg(t.value, t.count).alias("avg")).execute()
+result.print()
# register function
table_env.create_temporary_function("weighted_avg", WeightedAvg())
# call registered function in Table API
-result = t.group_by(t.name).select(call("weighted_avg", t.value, t.count).alias("avg")).to_pandas()
-print(result)
+result = t.group_by(t.name).select(call("weighted_avg", t.value, t.count).alias("avg")).execute()
+result.print()
# register table
table_env.create_temporary_view("source", t)
# call registered function in SQL
result = table_env.sql_query(
- "SELECT weighted_avg(`value`, `count`) AS avg FROM source GROUP BY name").to_pandas()
-print(result)
+ "SELECT weighted_avg(`value`, `count`) AS avg FROM source GROUP BY name").execute()
+result.print()
# use the general Python aggregate function in GroupBy Window Aggregation
tumble_window = Tumble.over(lit(1).hours) \
@@ -338,9 +337,8 @@ tumble_window = Tumble.over(lit(1).hours) \
result = t.window(tumble_window) \
.group_by(col('w'), col('name')) \
.select(col('w').start, col('w').end, weighted_avg(col('value'), col('count'))) \
- .to_pandas()
-print(result)
-
+ .execute()
+result.print()
```
The `accumulate(...)` method of our `WeightedAvg` class takes three input arguments. The first one is the accumulator
@@ -448,6 +446,7 @@ The following example shows how to define your own aggregate function and call i
```python
from pyflink.common import Row
from pyflink.table import DataTypes, TableEnvironment, EnvironmentSettings
+from pyflink.table.expressions import col
from pyflink.table.udf import udtaf, TableAggregateFunction
class Top2(TableAggregateFunction):
@@ -481,20 +480,28 @@ table_env = TableEnvironment.create(env_settings)
# top2 = udtaf(Top2(), result_type=DataTypes.ROW([DataTypes.FIELD("a", DataTypes.BIGINT())]), accumulator_type=DataTypes.ARRAY(DataTypes.BIGINT()))
top2 = udtaf(Top2())
t = table_env.from_elements([(1, 'Hi', 'Hello'),
- (3, 'Hi', 'hi'),
- (5, 'Hi2', 'hi'),
- (7, 'Hi', 'Hello'),
- (2, 'Hi', 'Hello')], ['a', 'b', 'c'])
+ (3, 'Hi', 'hi'),
+ (5, 'Hi2', 'hi'),
+ (7, 'Hi', 'Hello'),
+ (2, 'Hi', 'Hello')],
+ ['a', 'b', 'c'])
# call function "inline" without registration in Table API
-result = t.group_by(t.b).flat_aggregate(top2).select('*').to_pandas()
+t.group_by(t.b).flat_aggregate(top2).select(col('*')).execute().print()
# the result is:
-# b a
-# 0 Hi2 5.0
-# 1 Hi2 NaN
-# 2 Hi 7.0
-# 3 Hi 3.0
++----+--------------------------------+----------------------+
+| op | b | a |
++----+--------------------------------+----------------------+
+| +I | Hi | 1 |
+| +I | Hi | <NULL> |
+| -D | Hi | 1 |
+| -D | Hi | <NULL> |
+| +I | Hi | 7 |
+| +I | Hi | 3 |
+| +I | Hi2 | 5 |
+| +I | Hi2 | <NULL> |
++----+--------------------------------+----------------------+
```
@@ -550,28 +557,3 @@ class ListViewConcatTableAggregateFunction(TableAggregateFunction):
def get_result_type(self):
return DataTypes.ROW([DataTypes.FIELD("a", DataTypes.STRING())])
```
-
-## 打包 UDFs
-
-如果你在非 local 模式下运行 Python UDFs 和 Pandas UDFs,且 Python UDFs 没有定义在含 `main()` 入口的 Python 主文件中,强烈建议你通过 [`python-files`]({{< ref "docs/dev/python/python_config" >}}#python-files) 配置项指定 Python UDF 的定义。
-否则,如果你将 Python UDFs 定义在名为 `my_udf.py` 的文件中,你可能会遇到 `ModuleNotFoundError: No module named 'my_udf'` 这样的报错。
-
-## 在 UDF 中载入资源
-
-有时候,我们想在 UDF 中只载入一次资源,然后反复使用该资源进行计算。例如,你想在 UDF 中首先载入一个巨大的深度学习模型,然后使用该模型多次进行预测。
-
-你要做的是重载 `UserDefinedFunction` 类的 `open` 方法。
-
-```python
-class Predict(ScalarFunction):
- def open(self, function_context):
- import pickle
-
- with open("resources.zip/resources/model.pkl", "rb") as f:
- self.model = pickle.load(f)
-
- def eval(self, x):
- return self.model.predict(x)
-
-predict = udf(Predict(), result_type=DataTypes.DOUBLE(), func_type="pandas")
-```
diff --git a/docs/content.zh/docs/dev/table/functions/systemFunctions.md b/docs/content.zh/docs/dev/table/functions/systemFunctions.md
index 9da22067829..1fc61f6d5e2 100644
--- a/docs/content.zh/docs/dev/table/functions/systemFunctions.md
+++ b/docs/content.zh/docs/dev/table/functions/systemFunctions.md
@@ -196,13 +196,13 @@ Known Limitations:
| 接口 | 用法举例 | 描述 |
|-|-|-|
-| withColumns(*)| select("withColumns(*)") = select("a, b, c, d, e") | 全部列 |
-| withColumns(m to n) | select("withColumns(2 to 4)") = select("b, c, d") | 第 m 到第 n 列 |
-| withColumns(m, n, k) | select("withColumns(1, 3, e)") = select("a, c, e") | 第 m、n、k 列 |
-| withColumns(m, n to k) | select("withColumns(1, 3 to 5)") = select("a, c, d ,e") | 以上两种用法的混合 |
-| withoutColumns(m to n) | select("withoutColumns(2 to 4)") = select("a, e") | 不选从第 m 到第 n 列 |
-| withoutColumns(m, n, k) | select("withoutColumns(1, 3, 5)") = select("b, d") | 不选第 m、n、k 列 |
-| withoutColumns(m, n to k) | select("withoutColumns(1, 3 to 5)") = select("b") | 以上两种用法的混合 |
+| withColumns($(*)) | select(withColumns($("*"))) = select($("a"), $("b"), $("c"), $("d"), $("e")) | 全部列 |
+| withColumns(m to n) | select(withColumns(range(2, 4))) = select($("b"), $("c"), $("d")) | 第 m 到第 n 列 |
+| withColumns(m, n, k) | select(withColumns(lit(1), lit(3), $("e"))) = select($("a"), $("c"), $("e")) | 第 m、n、k 列 |
+| withColumns(m, n to k) | select(withColumns(lit(1), range(3, 5))) = select($("a"), $("c"), $("d"), $("e")) | 以上两种用法的混合 |
+| withoutColumns(m to n) | select(withoutColumns(range(2, 4))) = select($("a"), $("e")) | 不选从第 m 到第 n 列 |
+| withoutColumns(m, n, k) | select(withoutColumns(lit(1), lit(3), lit(5))) = select($("b"), $("d")) | 不选第 m、n、k 列 |
+| withoutColumns(m, n to k) | select(withoutColumns(lit(1), range(3, 5))) = select($("b")) | 以上两种用法的混合 |
列函数可用于所有需要列字段的地方,例如 `select、groupBy、orderBy、UDFs` 等函数,例如:
@@ -210,22 +210,22 @@ Known Limitations:
{{< tab "Java" >}}
```java
table
- .groupBy("withColumns(1 to 3)")
- .select("withColumns(a to b), myUDAgg(myUDF(withColumns(5 to 20)))");
+ .groupBy(withColumns(range(1, 3)))
+ .select(withColumns(range("a", "b")), myUDAgg(myUDF(withColumns(range(5, 20)))));
```
{{< /tab >}}
{{< tab "Scala" >}}
```scala
table
- .groupBy(withColumns(1 to 3))
- .select(withColumns('a to 'b), myUDAgg(myUDF(withColumns(5 to 20))))
+ .groupBy(withColumns(range(1, 3)))
+ .select(withColumns('a to 'b), myUDAgg(myUDF(withColumns(5 to 20))))
```
{{< /tab >}}
{{< tab "Python" >}}
```python
-table \
- .group_by("withColumns(1 to 3)") \
- .select("withColumns(a to b), myUDAgg(myUDF(withColumns(5 to 20)))")
+table
+ .group_by(with_columns(range_(1, 3)))
+ .select(with_columns(range_('a', 'b')), myUDAgg(myUDF(with_columns(range_(5, 20)))))
```
{{< /tab >}}
{{< /tabs >}}
diff --git a/docs/content/docs/dev/python/table/intro_to_table_api.md b/docs/content/docs/dev/python/table/intro_to_table_api.md
index 35d570d39aa..938e0379955 100644
--- a/docs/content/docs/dev/python/table/intro_to_table_api.md
+++ b/docs/content/docs/dev/python/table/intro_to_table_api.md
@@ -68,7 +68,7 @@ table_env.execute_sql("""
# create a Table from a Table API query:
source_table = table_env.from_path("datagen")
# or create a Table from a SQL query:
-source_table = table_env.sql_query("SELECT * FROM datagen")
+# source_table = table_env.sql_query("SELECT * FROM datagen")
result_table = source_table.select(source_table.id + 1, source_table.data)
@@ -76,7 +76,7 @@ result_table = source_table.select(source_table.id + 1, source_table.data)
# emit a Table API result Table to a sink table:
result_table.execute_insert("print").wait()
# or emit results via SQL query:
-table_env.execute_sql("INSERT INTO print SELECT * FROM datagen").wait()
+# table_env.execute_sql("INSERT INTO print SELECT * FROM datagen").wait()
```
{{< top >}}
@@ -84,7 +84,7 @@ table_env.execute_sql("INSERT INTO print SELECT * FROM datagen").wait()
Create a TableEnvironment
---------------------------
-The `TableEnvironment` is a central concept of the Table API and SQL integration. The following code example shows how to create a TableEnvironment:
+`TableEnvironment` is a central concept of the Table API and SQL integration. The following code example shows how to create a TableEnvironment:
```python
from pyflink.table import EnvironmentSettings, TableEnvironment
@@ -121,7 +121,7 @@ A `Table` is always bound to a specific `TableEnvironment`. It is not possible t
### Create using a List Object
-You can create a Table from a list object:
+You can create a Table from a list object, this is usually used when writing examples or unit tests.
```python
from pyflink.table import EnvironmentSettings, TableEnvironment
@@ -131,61 +131,63 @@ env_settings = EnvironmentSettings.in_batch_mode()
table_env = TableEnvironment.create(env_settings)
table = table_env.from_elements([(1, 'Hi'), (2, 'Hello')])
-table.to_pandas()
+table.execute().print()
```
-The result is:
+The results are as following:
```text
- _1 _2
-0 1 Hi
-1 2 Hello
++----------------------+--------------------------------+
+| _1 | _2 |
++----------------------+--------------------------------+
+| 1 | Hi |
+| 2 | Hello |
++----------------------+--------------------------------+
```
You can also create the Table with specified column names:
```python
table = table_env.from_elements([(1, 'Hi'), (2, 'Hello')], ['id', 'data'])
-table.to_pandas()
+table.execute().print()
```
-The result is:
+The results are as following:
```text
- id data
-0 1 Hi
-1 2 Hello
++----------------------+--------------------------------+
+| id | data |
++----------------------+--------------------------------+
+| 1 | Hi |
+| 2 | Hello |
++----------------------+--------------------------------+
```
-By default the table schema is extracted from the data automatically.
-
-If the automatically generated table schema isn't satisfactory, you can specify it manually:
+By default, the table schema is extracted from the data automatically. If the automatically generated table schema isn't satisfactory, you can also specify it manually:
```python
-table_without_schema = table_env.from_elements([(1, 'Hi'), (2, 'Hello')], ['id', 'data'])
-# by default the type of the "id" column is 64 bit int
-default_type = table_without_schema.to_pandas()["id"].dtype
-print('By default the type of the "id" column is %s.' % default_type)
+table = table_env.from_elements([(1, 'Hi'), (2, 'Hello')], ['id', 'data'])
+# by default, the type of the "id" column is BIGINT
+print('By default the type of the "id" column is %s.' % table.get_schema().get_field_data_type("id"))
from pyflink.table import DataTypes
table = table_env.from_elements([(1, 'Hi'), (2, 'Hello')],
DataTypes.ROW([DataTypes.FIELD("id", DataTypes.TINYINT()),
DataTypes.FIELD("data", DataTypes.STRING())]))
-# now the type of the "id" column is 8 bit int
-type = table.to_pandas()["id"].dtype
-print('Now the type of the "id" column is %s.' % type)
+# now the type of the "id" column is set as TINYINT
+print('Now the type of the "id" column is %s.' % table.get_schema().get_field_data_type("id"))
```
-The result is:
+The results are as following:
```text
-By default the type of the "id" column is int64.
-Now the type of the "id" column is int8.
+By default the type of the "id" column is BIGINT.
+Now the type of the "id" column is TINYINT.
```
-### Create using a Connector
+### Create using DDL statements
-You can create a Table using connector DDL:
+You can also create a Table using SQL DDL statements.
```python
from pyflink.table import EnvironmentSettings, TableEnvironment
@@ -209,21 +211,66 @@ table_env.execute_sql("""
)
""")
table = table_env.from_path("random_source")
-table.to_pandas()
+table.execute().print()
+```
+
+The results are as following:
+
+```text
++----+----------------------+--------+
+| op | id | data |
++----+----------------------+--------+
+| +I | 1 | 4 |
+| +I | 2 | 5 |
+| +I | 3 | 6 |
++----+----------------------+--------+
+```
+
+### Create using TableDescriptor
+
+TableDescriptor is another way to define a Table. It's equivalent to SQL DDL statements.
+
+```python
+from pyflink.table import EnvironmentSettings, TableEnvironment, TableDescriptor, Schema, DataTypes
+
+# create a stream TableEnvironment
+env_settings = EnvironmentSettings.in_streaming_mode()
+table_env = TableEnvironment.create(env_settings)
+
+table_env.create_temporary_table(
+ 'random_source',
+ TableDescriptor.for_connector('datagen')
+ .schema(Schema.new_builder()
+ .column('id', DataTypes.BIGINT())
+ .column('data', DataTypes.TINYINT())
+ .build())
+ .option('fields.id.kind', 'sequence')
+ .option('fields.id.start', '1')
+ .option('fields.id.end', '3')
+ .option('fields.data.kind', 'sequence')
+ .option('fields.data.start', '4')
+ .option('fields.data.end', '6')
+ .build())
+
+table = table_env.from_path("random_source")
+table.execute().print()
```
-The result is:
+The results are as following:
```text
- id data
-0 2 5
-1 1 4
-2 3 6
++----+----------------------+--------+
+| op | id | data |
++----+----------------------+--------+
+| +I | 1 | 4 |
+| +I | 2 | 5 |
+| +I | 3 | 6 |
++----+----------------------+--------+
```
### Create using a Catalog
-A `TableEnvironment` maintains a map of catalogs of tables which are created with an identifier.
+`TableEnvironment` maintains a map of catalogs of tables which are created with an identifier.
The tables in a catalog may either be temporary, and tied to the lifecycle of a single Flink session, or permanent, and visible across multiple Flink sessions.
@@ -241,15 +288,18 @@ table_env.create_temporary_view('source_table', table)
# create Table API table from catalog
new_table = table_env.from_path('source_table')
-new_table.to_pandas()
+new_table.execute().print()
```
-The result is:
+The results are as following:
```text
- id data
-0 1 Hi
-1 2 Hello
++----+----------------------+--------------------------------+
+| op | id | data |
++----+----------------------+--------------------------------+
+| +I | 1 | Hi |
+| +I | 2 | Hello |
++----+----------------------+--------------------------------+
```
{{< top >}}
@@ -284,14 +334,17 @@ revenue = orders \
.group_by(orders.name) \
.select(orders.name, orders.revenue.sum.alias('rev_sum'))
-revenue.to_pandas()
+revenue.execute().print()
```
-The result is:
+The results are as following:
```text
- name rev_sum
-0 Jack 30
++--------------------------------+----------------------+
+| name | rev_sum |
++--------------------------------+----------------------+
+| Jack | 30 |
++--------------------------------+----------------------+
```
The [Row-based Operations]({{< ref "docs/dev/table/tableapi" >}}#row-based-operations) are also supported in Python Table API, which include [Map Operation]({{< ref "docs/dev/table/tableapi" >}}#row-based-operations),
@@ -313,21 +366,24 @@ orders = table_env.from_elements([('Jack', 'FRANCE', 10), ('Rose', 'ENGLAND', 30
['name', 'country', 'revenue'])
map_function = udf(lambda x: pd.concat([x.name, x.revenue * 10], axis=1),
- result_type=DataTypes.ROW(
- [DataTypes.FIELD("name", DataTypes.STRING()),
- DataTypes.FIELD("revenue", DataTypes.BIGINT())]),
- func_type="pandas")
+ result_type=DataTypes.ROW(
+ [DataTypes.FIELD("name", DataTypes.STRING()),
+ DataTypes.FIELD("revenue", DataTypes.BIGINT())]),
+ func_type="pandas")
-orders.map(map_function).to_pandas()
+orders.map(map_function).execute().print()
```
-The result is:
+The results are as following:
```text
- name revenue
-0 Jack 100
-1 Rose 300
-2 Jack 200
++--------------------------------+----------------------+
+| name | revenue |
++--------------------------------+----------------------+
+| Jack | 100 |
+| Rose | 300 |
+| Jack | 200 |
++--------------------------------+----------------------+
```
### Write SQL Queries
@@ -379,7 +435,7 @@ table_env.execute_sql("""
""").wait()
```
-The result is:
+The results are as following:
```text
2> +I(4,11)
@@ -433,7 +489,7 @@ table_env.create_temporary_view('table_api_table', table)
table_env.execute_sql("INSERT INTO table_sink SELECT * FROM table_api_table").wait()
```
-The result is:
+The results are as following:
```text
6> +I(1,Hi)
@@ -463,20 +519,23 @@ table_env.execute_sql("""
table = table_env.from_path("sql_source")
# or create the table from a sql query
-table = table_env.sql_query("SELECT * FROM sql_source")
+# table = table_env.sql_query("SELECT * FROM sql_source")
# emit the table
-table.to_pandas()
+table.execute().print()
```
-The result is:
+The results are as following:
```text
- id data
-0 2 5
-1 1 4
-2 4 7
-3 3 6
++----+----------------------+--------+
+| op | id | data |
++----+----------------------+--------+
+| +I | 1 | 4 |
+| +I | 2 | 5 |
+| +I | 3 | 6 |
+| +I | 4 | 7 |
++----+----------------------+--------+
```
{{< top >}}
@@ -504,7 +563,7 @@ with res.collect() as results:
print(result)
```
-The result is:
+The results are as following:
```text
<Row(2, 'Hi', 'Hello')>
@@ -517,10 +576,10 @@ You can call the "to_pandas" method to [convert a `Table` object to a pandas Dat
```python
table = table_env.from_elements([(1, 'Hi'), (2, 'Hello')], ['id', 'data'])
-table.to_pandas()
+print(table.to_pandas())
```
-The result is:
+The results are as following:
```text
id data
@@ -550,7 +609,7 @@ table = table_env.from_elements([(1, 'Hi'), (2, 'Hello')], ['id', 'data'])
table.execute_insert("sink_table").wait()
```
-The result is:
+The results are as following:
```text
6> +I(1,Hi)
@@ -602,7 +661,7 @@ statement_set.add_insert_sql("INSERT INTO second_sink_table SELECT * FROM simple
statement_set.execute().wait()
```
-The result is:
+The results are as following:
```text
7> +I(1,Hi)
@@ -640,7 +699,7 @@ table = table1 \
print(table.explain())
```
-The result is:
+The results are as following:
```text
== Abstract Syntax Tree ==
@@ -712,7 +771,7 @@ statement_set.add_insert("black_hole_sink_table", table2)
print(statement_set.explain())
```
-The result is:
+The results are as following:
```text
== Abstract Syntax Tree ==
diff --git a/docs/content/docs/dev/python/table/udfs/python_udfs.md b/docs/content/docs/dev/python/table/udfs/python_udfs.md
index c33a51bf900..8e4526c30a7 100644
--- a/docs/content/docs/dev/python/table/udfs/python_udfs.md
+++ b/docs/content/docs/dev/python/table/udfs/python_udfs.md
@@ -313,23 +313,23 @@ t = table_env.from_elements([(1, 2, "Lee"),
(7, 8, "Lee")]).alias("value", "count", "name")
# call function "inline" without registration in Table API
-result = t.group_by(t.name).select(weighted_avg(t.value, t.count).alias("avg")).to_pandas()
-print(result)
+result = t.group_by(t.name).select(weighted_avg(t.value, t.count).alias("avg")).execute()
+result.print()
# register function
table_env.create_temporary_function("weighted_avg", WeightedAvg())
# call registered function in Table API
-result = t.group_by(t.name).select(call("weighted_avg", t.value, t.count).alias("avg")).to_pandas()
-print(result)
+result = t.group_by(t.name).select(call("weighted_avg", t.value, t.count).alias("avg")).execute()
+result.print()
# register table
table_env.create_temporary_view("source", t)
# call registered function in SQL
result = table_env.sql_query(
- "SELECT weighted_avg(`value`, `count`) AS avg FROM source GROUP BY name").to_pandas()
-print(result)
+ "SELECT weighted_avg(`value`, `count`) AS avg FROM source GROUP BY name").execute()
+result.print()
# use the general Python aggregate function in GroupBy Window Aggregation
tumble_window = Tumble.over(lit(1).hours) \
@@ -339,9 +339,8 @@ tumble_window = Tumble.over(lit(1).hours) \
result = t.window(tumble_window) \
.group_by(col('w'), col('name')) \
.select(col('w').start, col('w').end, weighted_avg(col('value'), col('count'))) \
- .to_pandas()
-print(result)
-
+ .execute()
+result.print()
```
The `accumulate(...)` method of our `WeightedAvg` class takes three input arguments. The first one is the accumulator
@@ -449,6 +448,7 @@ The following example shows how to define your own aggregate function and call i
```python
from pyflink.common import Row
from pyflink.table import DataTypes, TableEnvironment, EnvironmentSettings
+from pyflink.table.expressions import col
from pyflink.table.udf import udtaf, TableAggregateFunction
class Top2(TableAggregateFunction):
@@ -482,13 +482,14 @@ table_env = TableEnvironment.create(env_settings)
# top2 = udtaf(Top2(), result_type=DataTypes.ROW([DataTypes.FIELD("a", DataTypes.BIGINT())]), accumulator_type=DataTypes.ARRAY(DataTypes.BIGINT()))
top2 = udtaf(Top2())
t = table_env.from_elements([(1, 'Hi', 'Hello'),
- (3, 'Hi', 'hi'),
- (5, 'Hi2', 'hi'),
- (7, 'Hi', 'Hello'),
- (2, 'Hi', 'Hello')], ['a', 'b', 'c'])
+ (3, 'Hi', 'hi'),
+ (5, 'Hi2', 'hi'),
+ (7, 'Hi', 'Hello'),
+ (2, 'Hi', 'Hello')],
+ ['a', 'b', 'c'])
# call function "inline" without registration in Table API
-result = t.group_by(t.b).flat_aggregate(top2).select('*').to_pandas()
+t.group_by(t.b).flat_aggregate(top2).select(col('*')).execute().print()
# the result is:
# b a
diff --git a/docs/content/docs/dev/table/functions/systemFunctions.md b/docs/content/docs/dev/table/functions/systemFunctions.md
index e505f468ed8..78449cdf49b 100644
--- a/docs/content/docs/dev/table/functions/systemFunctions.md
+++ b/docs/content/docs/dev/table/functions/systemFunctions.md
@@ -197,13 +197,13 @@ The usage of the column function is illustrated in the following table. (Suppose
| API | Usage | Description |
|-|-|-|
-| withColumns(*)| select("withColumns(*)") | select("*") = select("a, b, c, d, e") | all the columns |
-| withColumns(m to n) | select("withColumns(2 to 4)") = select("b, c, d") | columns from m to n |
-| withColumns(m, n, k) | select("withColumns(1, 3, e)") = select("a, c, e") | columns m, n, k |
-| withColumns(m, n to k) | select("withColumns(1, 3 to 5)") = select("a, c, d ,e") | mixing of the above two representation |
-| withoutColumns(m to n) | select("withoutColumns(2 to 4)") = select("a, e") | deselect columns from m to n |
-| withoutColumns(m, n, k) | select("withoutColumns(1, 3, 5)") = select("b, d") | deselect columns m, n, k |
-| withoutColumns(m, n to k) | select("withoutColumns(1, 3 to 5)") = select("b") | mixing of the above two representation |
+| withColumns($(*)) | select(withColumns($("*"))) = select($("a"), $("b"), $("c"), $("d"), $("e")) | all the columns |
+| withColumns(m to n) | select(withColumns(range(2, 4))) = select($("b"), $("c"), $("d")) | columns from m to n |
+| withColumns(m, n, k) | select(withColumns(lit(1), lit(3), $("e"))) = select($("a"), $("c"), $("e")) | columns m, n, k |
+| withColumns(m, n to k) | select(withColumns(lit(1), range(3, 5))) = select($("a"), $("c"), $("d"), $("e")) | mixing of the above two representation |
+| withoutColumns(m to n) | select(withoutColumns(range(2, 4))) = select($("a"), $("e")) | deselect columns from m to n |
+| withoutColumns(m, n, k) | select(withoutColumns(lit(1), lit(3), lit(5))) = select($("b"), $("d")) | deselect columns m, n, k |
+| withoutColumns(m, n to k) | select(withoutColumns(lit(1), range(3, 5))) = select($("b")) | mixing of the above two representation |
The column functions can be used in all places where column fields are expected, such as `select, groupBy, orderBy, UDFs etc.` e.g.:
@@ -211,22 +211,22 @@ The column functions can be used in all places where column fields are expected,
{{< tab "Java" >}}
```java
table
- .groupBy("withColumns(1 to 3)")
- .select("withColumns(a to b), myUDAgg(myUDF(withColumns(5 to 20)))");
+ .groupBy(withColumns(range(1, 3)))
+ .select(withColumns(range("a", "b")), myUDAgg(myUDF(withColumns(range(5, 20)))));
```
{{< /tab >}}
{{< tab "Scala" >}}
```scala
table
- .groupBy(withColumns(1 to 3))
- .select(withColumns('a to 'b), myUDAgg(myUDF(withColumns(5 to 20))))
+ .groupBy(withColumns(range(1, 3)))
+ .select(withColumns('a to 'b), myUDAgg(myUDF(withColumns(5 to 20))))
```
{{< /tab >}}
{{< tab "Python" >}}
```python
table \
- .group_by("withColumns(1 to 3)") \
- .select("withColumns(a to b), myUDAgg(myUDF(withColumns(5 to 20)))")
+ .group_by(with_columns(range_(1, 3))) \
+ .select(with_columns(range_('a', 'b')), myUDAgg(myUDF(with_columns(range_(5, 20)))))
```
{{< /tab >}}
{{< /tabs >}}