使用pyflink編寫demo并將任務提交到yarn集群

目錄

背景

一、pyflink安裝

二、編寫demo程序

三、提交yarn前準備

?四、提交任務

五、踩坑記錄

1、提交任務時客戶端出現語法錯誤

2、提交任務時客戶端出現lzma包找不到

3、提交任務時客戶端出現“org.apache.flink.streaming.api.utils.PythonTypeUtils.getCollectionInputFormat does not exist in the JVM”

5、提交任務時taskmanager上報錯出現Permission denied

六、總結


背景

? ? ? ? 最近項目需要,學習研究使用python來開發flink任務,在此做相關筆記。

一、pyflink安裝

? ? ? ? 在本地執行如下命令,需要注意的是,pyflink必須要求python版本大于等于3.6,我本地是3.12

創建虛擬環境

python -m venv venv

切換虛擬環境

venv/bin/activate

執行安裝命令

pip install apache-flink==1.14

?安裝成功后如下

二、編寫demo程序

? ? ? ? 這里直copy官方教程

import argparse
import logging
import sysfrom pyflink.common import Row
from pyflink.table import (EnvironmentSettings, TableEnvironment, TableDescriptor, Schema,DataTypes, FormatDescriptor)
from pyflink.table.expressions import lit, col
from pyflink.table.udf import udtfword_count_data = ["To be, or not to be,--that is the question:--","Whether 'tis nobler in the mind to suffer","The slings and arrows of outrageous fortune","Or to take arms against a sea of troubles,","And by opposing end them?--To die,--to sleep,--","No more; and by a sleep to say we end","The heartache, and the thousand natural shocks","That flesh is heir to,--'tis a consummation","Devoutly to be wish'd. To die,--to sleep;--","To sleep! perchance to dream:--ay, there's the rub;","For in that sleep of death what dreams may come,","When we have shuffled off this mortal coil,","Must give us pause: there's the respect","That makes calamity of so long life;","For who would bear the whips and scorns of time,","The oppressor's wrong, the proud man's contumely,","The pangs of despis'd love, the law's delay,","The insolence of office, and the spurns","That patient merit of the unworthy takes,","When he himself might his quietus make","With a bare bodkin? who would these fardels bear,","To grunt and sweat under a weary life,","But that the dread of something after death,--","The undiscover'd country, from whose bourn","No traveller returns,--puzzles the will,","And makes us rather bear those ills we have","Than fly to others that we know not of?","Thus conscience does make cowards of us all;","And thus the native hue of resolution","Is sicklied o'er with the pale cast of thought;","And enterprises of great pith and moment,","With this regard, their currents turn awry,","And lose the name of action.--Soft you now!","The fair Ophelia!--Nymph, in thy orisons","Be all my sins remember'd."]def word_count(input_path, output_path):t_env = TableEnvironment.create(EnvironmentSettings.in_streaming_mode())# write all the data to one filet_env.get_config().get_configuration().set_string("parallelism.default", "1")# define the sourceif input_path is not None:t_env.create_temporary_table('source',TableDescriptor.for_connector('filesystem').schema(Schema.new_builder().column('word', DataTypes.STRING()).build()).option('path', input_path).format('csv').build())tab = t_env.from_path('source')else:print("Executing word_count example with default input data set.")print("Use --input to specify file input.")tab = t_env.from_elements(map(lambda i: (i,), word_count_data),DataTypes.ROW([DataTypes.FIELD('line', DataTypes.STRING())]))# define the sinkif output_path is not None:t_env.create_temporary_table('sink',TableDescriptor.for_connector('filesystem').schema(Schema.new_builder().column('word', DataTypes.STRING()).column('count', DataTypes.BIGINT()).build()).option('path', output_path).format(FormatDescriptor.for_format('canal-json').build()).build())else:print("Printing result to stdout. Use --output to specify output path.")t_env.create_temporary_table('sink',TableDescriptor.for_connector('print').schema(Schema.new_builder().column('word', DataTypes.STRING()).column('count', DataTypes.BIGINT()).build()).build())@udtf(result_types=[DataTypes.STRING()])def split(line: Row):for s in line[0].split():yield Row(s)# compute word counttab.flat_map(split).alias('word') \.group_by(col('word')) \.select(col('word'), lit(1).count) \.execute_insert('sink') \.wait()# remove .wait if submitting to a remote cluster, refer to# https://nightlies.apache.org/flink/flink-docs-stable/docs/dev/python/faq/#wait-for-jobs-to-finish-when-executing-jobs-in-mini-cluster# for more detailsif __name__ == '__main__':logging.basicConfig(stream=sys.stdout, level=logging.INFO, format="%(message)s")parser = argparse.ArgumentParser()parser.add_argument('--input',dest='input',required=False,help='Input file to process.')parser.add_argument('--output',dest='output',required=False,help='Output file to write results to.')argv = sys.argv[1:]known_args, _ = parser.parse_known_args(argv)word_count(known_args.input, known_args.output)

在本地執行該腳本,輸出如下

三、提交yarn前準備

準備一臺linux服務器,并裝有flink客戶端(我使用的版本是flink1.14.2,這里不說客戶端如何安裝了,下載包解壓安裝即可)

在服務器上搭建pyflink運行環境,參考第一章節

將demo程序上傳到該服務器上

其中env為python虛擬環境目錄,py_env.zip為將env使用zip進行壓縮的文件

?四、提交任務

/home/master/flink-1.14.2/bin/flink run -pyarch py_env.zip -m yarn-cluster -py /home/zhubao/demo.py -pyexec py_env.zip/env/bin/python

看到終端打印如下日志

訪問yarn集群web管理頁面,在running下看到有對應的任務時,即表示任務已經提交到yarn集群

查看任務詳情

五、踩坑記錄

1、提交任務時客戶端出現語法錯誤

SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory]File "main.py", line 55ds.print()^
SyntaxError: invalid syntax

解決方法:上述問題排查發現是flink客戶端版本差異導致,編寫的demo代碼,和flink客戶端版本要一致,否則會出現一些莫名其妙的問題,統一調整flink版本為一致,包括pyflink,flink客戶端等

2、提交任務時客戶端出現lzma包找不到

Traceback (most recent call last):File "/home/zhubao/env/lib/python3.7/site-packages/fastavro/read.py", line 2, in <module>from . import _readFile "fastavro/_read.pyx", line 11, in init fastavro._readFile "/home/master/python3/lib/python3.7/lzma.py", line 27, in <module>from _lzma import *
ModuleNotFoundError: No module named '_lzma'

解決方法:該錯誤表明系統缺少Python的LZMA壓縮模塊依賴(_lzma),這是Python標準庫中處理.xz壓縮文件的底層C擴展模塊,需要進行安裝

# 使用root權限執行如下命令
yum install -y xz-devel python-backports-lzma
# 使用普通用戶執行安裝lzma包命令
pip install backports.lzma -i https://pypi.tuna.tsinghua.edu.cn/simple --trusted-host pypi.tuna.tsinghua.edu.cn

安裝完成后,需要對lzma文件進行修改,找到lzma.py文件,一般在$PYTHON_HOME/lib/python3.7目錄下(根據實際版本),主要是加上try except

修改完成后保存退出,重新執行解決該問題

3、提交任務時客戶端出現“org.apache.flink.streaming.api.utils.PythonTypeUtils.getCollectionInputFormat does not exist in the JVM”

Traceback (most recent call last):File "main.py", line 99, in <module>word_count(known_args.input, known_args.output)File "main.py", line 39, in word_countds = env.from_collection(word_count_data)File "/home/zhubao/env/lib/python3.7/site-packages/pyflink/datastream/stream_execution_environment.py", line 958, in from_collectionreturn self._from_collection(collection, type_info)File "/home/zhubao/env/lib/python3.7/site-packages/pyflink/datastream/stream_execution_environment.py", line 981, in _from_collectionj_input_format = PythonTypeUtils.getCollectionInputFormat(File "/home/zhubao/env/lib/python3.7/site-packages/py4j/java_gateway.py", line 1550, in __getattr__"{0}.{1} does not exist in the JVM".format(self._fqn, name))
py4j.protocol.Py4JError: org.apache.flink.streaming.api.utils.PythonTypeUtils.getCollectionInputFormat does not exist in the JVM

解決方法:還是版本不匹配導致,請確保pyflink與客戶端版本一致

Caused by: java.io.IOException: Failed to execute the command: python -c import pyflink;import os;print(os.path.join(os.path.abspath(os.path.dirname(pyflink.__file__)), 'bin'))
output: Traceback (most recent call last):File "<string>", line 1, in <module>
ImportError: No module named pyflink

解決方法:在flink配置文件中,加上python執行環境,打開flink-conf.yaml文件,一般在$FLINK_HOME/conf目錄下,編輯該文件,在末尾加上python執行環境

python.client.executable: /home/zhubao/env/bin/python

5、提交任務時taskmanager上報錯出現Permission denied

Caused by: java.io.IOException: Cannot run program "/home/zhubao/env/bin/python": error=13, Permission denied

解決方法:找了一些方案,但最終通過將整個python執行環境打包提交到yarn上解決,方法如下

將python環境達成zip包

zip -r py_env.zip /home/zhubao/env/

提交任務命令增加指定環境包與執行環境

/home/master/flink-1.14.2/bin/flink run -pyarch py_env.zip -m yarn-cluster -py /home/zhubao/demo.py -pyexec py_env.zip/env/bin/python

六、總結

? ? ? ? 以上是使用pyflink進行flink任務開發,以及將任務提交到yarn集群方法。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/bicheng/86844.shtml
繁體地址,請注明出處:http://hk.pswp.cn/bicheng/86844.shtml
英文地址,請注明出處:http://en.pswp.cn/bicheng/86844.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Vue 3 最基礎核心知識詳解

Vue3作為現代前端主流框架&#xff0c;是前后端開發者都應當掌握的核心技能。本篇文章將帶你了解vue3的基礎核心知識&#xff0c;適合學習與復習 一、Vue 3 應用創建 1.1 創建Vue應用的基本步驟 // main.js import { createApp } from vue // 1. 導入createApp函數 import …

Bootstrap 5學習教程,從入門到精通,Bootstrap 5 Flex 布局語法知識點及案例(27)

Bootstrap 5 Flex 布局語法知識點及案例 Bootstrap 5 提供了強大的 Flexbox 工具集&#xff0c;讓布局變得更加簡單靈活。以下是 Bootstrap 5 Flex 布局的完整知識點和詳細案例代碼。 一、Flex 布局基礎語法 1. 啟用 Flex 布局 <div class"d-flex">我是一個…

HarmonyOS 5智能單詞應用開發:記憶卡(附:源碼

一、應用概述與核心價值 在語言學習過程中&#xff0c;單詞記憶是基礎也是難點。本文介紹的智能單詞記憶卡應用通過創新的交互設計和科學的學習模式&#xff0c;幫助用戶高效記憶單詞。應用采用ArkUI框架開發&#xff0c;主要特點包括&#xff1a; 雙模式學習系統&#xff1a…

LeetCode--38.外觀數列

前言&#xff1a;之前我不是說&#xff0c;我后續可能會講一下遞歸嗎&#xff0c;現在它來了&#xff0c;這道題會用到回溯的方法&#xff0c;并且比較純粹哦 解題思路&#xff1a; 1.獲取信息&#xff1a;&#xff08;下面這些信息差不多是力扣上面的題目信息了&#xff0c;所…

服務器的安裝與安全設置

1&#xff1a;安裝操作系統 1、創建虛擬機Win49&#xff08;49為序號&#xff09;&#xff0c;并安裝Windows Server 2019操作系統 參考配置&#xff1a;安裝系統的分區大小為20GB&#xff0c;其余分區暫不劃分&#xff0c; 文件系統格式為NTFS&#…

Sensodrive SensoJoint機器人力控關節模組抗振動+Sensodrive力反饋系統精準對接

Sensodrive成立于2003年&#xff0c;起源于德國航空航天中心&#xff08;DLR&#xff09;的LBR項目。公司由一批傳感器技術專家創立&#xff0c;專注于高精度工業扭矩傳感器的研發。憑借二十余年的技術積累&#xff0c;Sensodrive將DLR輕型機器人扭矩技術引入工業領域&#xff…

【AI實踐】Mac一天熟悉AI模型智能體應用(百煉版)

25.6.29增加Gummy 實時/一句話語音識別25.6.28增加Qwen TTS本地音頻和實時播報 背景 準備環境 MacOS M1電腦&#xff08;其他M系列芯片也可以&#xff09; 為了方便python的使用環境&#xff0c;使用Miniconda&#xff1a;下載鏈接&#xff1a;Download Anaconda Distribution…

WEB安全--Java安全--jsp webshell免殺1

1.1、BCEL ClassLoader 介紹&#xff08;僅適用于BCEL 6.0以下&#xff09;&#xff1a; BCEL&#xff08;Apache Commons BCEL?&#xff09;是一個用于分析、創建和操縱Java類文件的工具庫&#xff1b;BCEL的類加載器在解析類名時會對ClassName中有$$BCEL$$標識的類做特殊處…

Valkey與Redis評估對比:開源替代方案的技術演進

#作者&#xff1a;朱雷 文章目錄 1 概述1.1內存數據結構存儲核心特性1.2主流內存數據結構存儲設計與適用場景1.3目前主流內存數據結構存儲對比 2 Valkey 說明2.1 哨兵架構設計2.2 集群架構設計2.3 valkey 使用企業和業內生態? 3 評估指標4 評估結果 1 概述 內存數據結構存儲…

華為云Flexus+DeepSeek征文 | 基于華為云ModelArts Studio安裝NoteGen AI筆記應用程序

華為云FlexusDeepSeek征文 | 基于華為云ModelArts Studio安裝NoteGen AI筆記應用程序 引言一、ModelArts Studio平臺介紹華為云ModelArts Studio簡介ModelArts Studio主要特點 二、NoteGen介紹NoteGen簡介主要特點 三、安裝NoteGen工具下載NoteGen軟件安裝NoteGen工具 四、開通…

BUUCTF在線評測-練習場-WebCTF習題[BJDCTF2020]Easy MD51-flag獲取、解析

解題思路 打開靶場&#xff0c;有個提交框&#xff0c;輸入后url會出現我們提交的參數password http://a48577ed-9a1c-4751-aba0-ae99f1eb8143.node5.buuoj.cn:81/leveldo4.php?password123 查看源碼并沒用發現什么貓膩&#xff0c;抓包在響應頭發現了貓膩 hint: select * …

面向對象三大特性深度解析:封裝、繼承與多態

面向對象三大特性深度解析&#xff1a;封裝、繼承與多態 思維導圖概覽 #mermaid-svg-v2u0XIzKotjyXYei {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-v2u0XIzKotjyXYei .error-icon{fill:#552222;}#mermaid-svg-v2…

mmap映射物理內存之三invalid cache

目錄 流程設計 invalid 命令 內核態invalid 內核態invalid&#xff0c;用戶態mmap物理地址 PAN機制 PAN機制歷程 硬件支持 ARMv8.1-PAN 特性 Linux 內核的適配 軟件模擬 PAN&#xff08;SW PAN&#xff09; 背景 Linux 的實現 總結 前述刷新cache的流程也同樣可…

記憶化搜索(dfs+memo)無環有向圖

這是一道可以當作板子的極簡記憶化搜索 建立a 是鄰接表&#xff0c;其中 a[x] 存儲從節點 x 出發能到達的所有節點。 b[x] 記錄從節點 x 出發的所有邊的權重之和。根據數學原理&#xff0c;我們很容易發現&#xff0c;一個根&#xff08;起點&#xff09;的期望&#xff0c;等…

使用AI豆包寫一個車輛信息管理頁面

記錄一個基本的車輛信息管理頁面&#xff0c;由豆包撰寫完成&#xff0c;只需要微調頁面即可。 主要功能是車輛信息的查詢、新增、編輯&#xff0c;項目用到了uniapp、vue3、ts、uni-ui、z-paging 頁面效果如下&#xff1a; 以上界面均由豆包生成&#xff0c;完成度非常高&am…

《HarmonyOSNext應用防崩指南:30秒定位JS Crash的破案手冊》

《HarmonyOSNext應用防崩指南&#xff1a;30秒定位JS Crash的破案手冊》 ##Harmony OS Next ##Ark Ts ##教育 本文適用于教育科普行業進行學習&#xff0c;有錯誤之處請指出我會修改。 &#x1f4a5; 哇哦&#xff01;JS Crash崩潰日志完全解析手冊 當你的應用突然閃退時&am…

閱讀筆記(3) 單層網絡:回歸(下)

閱讀筆記(3) 單層網絡:回歸(下) 該筆記是DataWhale組隊學習計劃&#xff08;共度AI新圣經&#xff1a;深度學習基礎與概念&#xff09;的Task03 以下內容為個人理解&#xff0c;可能存在不準確或疏漏之處&#xff0c;請以教材為主。 1. 為什么書上要提到決策理論&#xff1f; …

Mac OS系統每次開機啟動后,提示:輸入密碼來解鎖磁盤“Data”,去除提示的解決方法

問題描述&#xff1a; Mac mini外接了一個磁盤&#xff08;EX_Mac&#xff09;為默認使用的系統盤&#xff0c;內置的硬盤&#xff08;Macintosh HD&#xff09;為Mac mini自帶的系統盤 外置硬盤系統每次開機都會掛載內置磁盤&#xff0c;同時會提示需要輸入密碼來解鎖磁盤“…

CSS Flex 布局中flex-shrink: 0使用

flex-shrink: 0 是 CSS Flexbox 布局中的一個關鍵屬性&#xff0c;用于禁止彈性項目&#xff08;flex item&#xff09;在容器空間不足時被壓縮。以下是詳細解釋和示例&#xff1a; 核心作用 當容器的可用空間小于所有彈性項目的總寬度&#xff08;或高度&#xff09;時&#…

WHERE 子句中使用子查詢:深度解析與最佳實踐

&#x1f50d; WHERE 子句中使用子查詢&#xff1a;深度解析與最佳實踐 在 WHERE 子句中使用子查詢是 SQL 的高階技巧&#xff0c;可實現動態條件過濾。以下是全面指南&#xff0c;涵蓋語法、類型、陷阱及優化策略&#xff1a; &#x1f4dc; 一、基礎語法結構 SELECT 列 FR…