python flink 1.9 環境搭建

我看網上都是一個版本的中文文檔,有些地方細節不夠清楚,我這裏補充一下。

首先flink在1.9纔開始支持支持python的。開發主要思路是要先把源碼編譯成python的依賴包,然後用pip命令把包集成到python庫。

安裝命令如下:

1.git clone https://github.com/apache/flink.git
2.git fetch origin release-1.9  && git checkout -b release-1.9 origin/release-1.9
3.mvn clean install -DskipTests -Dfast

顯示build success代表成功,如果你沒裝java1.8環境是build不起來的,貌似還需要node npm環境。
在這裏插入圖片描述

4.cd flink/flink-python; python3 setup.py sdist bdist_wheel  (git目錄下有個flink-python目錄)

在這裏插入圖片描述5.
建議使用python3環境安裝
pip3 install dist/*.tar.gz
在這裏插入圖片描述
6.
檢查一下python依賴包是否安裝上了
pip3 list|grep dev
在這裏插入圖片描述關於如何使用這個包
官方文檔地址: https://ci.apache.org/projects/flink/flink-docs-release-1.9/api/python/

單節點集羣的部署

在上面步驟我們給python打上了pyflink1.9的依賴包,但是flink集羣我們還沒有部署。以下是部署步驟

wget https://archive.apache.org/dist/flink/flink-1.9.0/flink-1.9.0-bin-scala_2.12.tgz
tar xvf flink-1.9.0-bin-scala_2.12.tgz
cd flink-1.9.0/bin
./start-cluster.sh
訪問localhost:8081

在這裏插入圖片描述

使用DEMO

flink程序默認可以在本地起一個迷你集羣運行,當然你也可以使用你部署的flink集羣來執行這個腳本。
flink.py程序:

from pyflink.dataset import ExecutionEnvironment
from pyflink.table import TableConfig, DataTypes, BatchTableEnvironment
from pyflink.table.descriptors import Schema, OldCsv, FileSystem

exec_env = ExecutionEnvironment.get_execution_environment()
exec_env.set_parallelism(1)
t_config = TableConfig()
t_env = BatchTableEnvironment.create(exec_env, t_config)

t_env.connect(FileSystem().path('/tmp/input')) \
    .with_format(OldCsv()
                 .line_delimiter(' ')
                 .field('word', DataTypes.STRING())) \
    .with_schema(Schema()
                 .field('word', DataTypes.STRING())) \
    .register_table_source('mySource')

t_env.connect(FileSystem().path('/tmp/output')) \
    .with_format(OldCsv()
                 .field_delimiter('\t')
                 .field('word', DataTypes.STRING())
                 .field('count', DataTypes.BIGINT())) \
    .with_schema(Schema()
                 .field('word', DataTypes.STRING())
                 .field('count', DataTypes.BIGINT())) \
    .register_table_sink('mySink')

t_env.scan('mySource') \
    .group_by('word') \
    .select('word, count(1)') \
    .insert_into('mySink')

t_env.execute("python_job")
(1) 迷你集羣使用方法,python3 flink.py
(2) 使用上述搭建好的集羣使用方法, cd flink-1.9.0/bin && ./flink run -py ~/demo/flink.py

執行成功可以看到:
在這裏插入圖片描述

異常處理

local class incompatible
那大概是你下載錯誤了flink的版本,造成python flink版本和flink自身的版本對不上,上述教程都是1.9的。
還有種可能性就是,flink可執行文件要用 wget https://archive.apache.org/dist/flink/flink-1.9.0/flink-1.9.0-bin-scala_2.12.tgz包裏的,不要用之前git克隆的flink可執行文件。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章