Python ETL 交接 === ###### tags: `【bmwlab】` ETL --- - 絕對位置 - 執行檔:/home/collaborator/ems/json/raw_data_etl - 執行檔(code):/home/collaborator/ems/json/raw_data_etl.py - 目的: 把傳入得json檔案解析,send to mongoDB - json file location(sent by 北水): /home/collaborator/ems/json/ - CLI: > `pyinstaller -F raw_data_etl.py` - note: need to change line notify - crontab: - goal: set run schedule - schedule: ![](https://i.imgur.com/UFGFzzx.png) - `15 1`: 1:15am (they send data from 1. and 13.) - `* * *` : every day - ` cd /home/collaborator/ems/json && ./raw_data_etl`: cd to directory and run file - `--num 1`: each thread run 1 file - `--dstPath /home/bmwlab/drinking_station_ETL/write_to_DB_over/`: processed json file location - `--SendMsg 1`: 1 to send msg, 0 not send - token path: /home/collaborator/ems/json/LineNotifyToken.txt - CLI: - 查看: `sudo crontab -l` - edit: `sudo crontab -e` 北水處接收資料的server設定 --- ## 廠商需求 :::warning 需要防火牆開啟 140.118.122.118 兩個對外 port port 10688/UDP 接收資料 --> 他們進去後會弄一個小小的網頁做為查看端口是否開啟 port 3306/TCP 資料維護用 --> 要作為MySQL的端口使用 ::: ### 開啟兩個對外 port > $ sudo iptables -I INPUT -p udp --dport 10688 -j ACCEPT > $ sudo iptables -I INPUT -p tcp --dport 3306 -j ACCEPT 查看端口是否開放 > $ sudo iptables -L -n ![](https://i.imgur.com/Kx4S5Az.png) - port 10688 小小的網頁(需要連接其他外網) ![](https://i.imgur.com/B7ou5Jn.png) ## MySQL ### 安裝 MySQL 分別為伺服端、客戶端以及開發中一些MySQL資料庫的函式庫 > $ sudo apt-get install mysql-server > $ sudo apt install mysql-client > $ sudo apt install libmysqlclient-dev ### 測試資料庫是否安裝成功 安裝netstat > $ sudo apt-get install net-tools netstat 的功用是顯示通訊協定統計資料以及目前的TCP/IP 網路連線,所以我們要使用它來查看已安裝好的 MySQL 是否有連線監聽。 透過 netstat 指令來查看 MySQL 是否安裝成功 > $ sudo netstat -tap | grep mysql ### 另外建立一個透過密碼驗證登入的使用者 >$ sudo mysql * 透過 CREATE 語法建立使用者 localhost 是指定這個使用者可以登入的來源只有本機; % 則是指所有來源皆可 (這邊帳密問Ren) > CREATE USER 'user_name'@'localhost' IDENTIFIED BY 'password'; > CREATE USER 'user_name'@'%' IDENTIFIED BY 'password'; * 透過 SELECT 語法確認使用者已經成功被建立: > SELECT user, authentication_string, plugin, host FROM mysql.user WHERE user = 'user_name'; ![](https://i.imgur.com/S8umOQL.png) ### 設置 MySQL 允許遠端訪問 編輯 mysqld.cnf > $ sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf 將 `bind-address = 127.0.0.1` 註解起來 設定好對外連接的port (此port須為TCP) 完成後鍵盤輸入 :wq 儲存並退出 vim ![](https://i.imgur.com/2sMp8F8.png) * 嘗試登入 >$ mysql -u user_name -p * 執行授權命令 在 MySQL 環境下執行授權命令(授權給遠程任何電腦登錄資料庫) 由於要直接給予全部操作的權限,所以是 ALL privileges ,如果不需要全部操作的權限、例如只需要查詢或插入資料,則可以改成對應的 SELECT 、INSERT 等,而 WordPress 是給予權限的資料庫名稱,改為 * 則可以給予使用者所有資料庫的權限 >GRANT ALL PRIVILEGES ON *.* TO 'user_name'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION; * 更新設定 >flush privileges; * 退出 MySQL 服務 >exit * 重啟 MySQL 完成並生效所有的設定 >service mysql restart ### 為廠商建立DB > CREATE DATABASE mdph; 只針對該DB給予廠商權限 > GRANT ALL PRIVILEGES ON WordPress.* TO 'user_name'@'localhost'; 查看所有DB > SHOW DATABASES; ![](https://i.imgur.com/vzzfBL6.png) ### 用他們的帳戶登入MySQL且直連為他們創建的DB >$ mysql -u ems -p mdph 四周間九支水表的程式運行紀錄 --- [四周間九支水表的程式運行紀錄](https://hackmd.io/@RenJhang/HJ1ygsnYK) 直飲台 DS API Document --- [直飲台 DS API Document](https://hackmd.io/U1pTK7nkSGGxctDbUPFUGw) Drinking Station Question: --- https://hackmd.io/@RenJhang/rywgHIE6d DS 此計畫合作相關人員資料 --- ## 北水處 黃先生 ![](https://i.imgur.com/fFeYjIr.png =400x200) ## 弓銓 李經理 ![](https://i.imgur.com/EdSs4TG.jpg =400x200) ## 弓銓 楊先生 ![](https://i.imgur.com/gVM0wAE.jpg =400x200) Deploy DrinkingStation API process --- ## Master Node <font color="#f00">**寫程式要放上pod的人從這裡開始看**</font> **First of All: Have a docker hub account !** ### Step 1 : Use the command to build the bin file - Login the server >$ `cd /home/smartcampus/yaml/drinking_station/` - 如果有舊的執行檔,把舊的執行檔刪掉,再build一次,生出新的執行檔 >$ rm drinking_station - build (這個執行檔的名稱會依據所屬資料夾的名稱進行命名) >$ `go build -tags netgo --ldflags '-extldflags "-lm -lstdc++ -static"'` ### Step 1 : Dockerfile (只有第一次要包Image的時候要建) * 用 alpine 的 Go 語言 Image 來編譯出執行檔 * 7799 is the port of the pod * Because mongo db is build on the pod, **"mongo-0.mongo.mongo.svc.cluster.local:27017"** is the pod URL. * golang code 裡面讀的環境變數是docker file裡面的環境變數 * 解決時差問題 * 把執行檔xinxing加到這個路徑,這個路徑是docker的路徑 * ENTRYPOINT: Define the default command that will be executed when the container is running. * EXPOSE 指 container 對外的port,在與外界溝通時使用。還是7799,轉成5409則是另外再轉的 <font color="#f00">這邊所有的`xinxing_analysis`同時指的是包成的執行檔以及路徑</font> ``` FROM golang:alpine ENV EXPORTER_PORT ":7799" ENV MONGO_URL "mongo-0.mongo.mongo.svc.cluster.local:27017" ENV MONGO_URL2 "mongo-1.mongo.mongo.svc.cluster.local:27017" ENV MONGO_URL3 "mongo-2.mongo.mongo.svc.cluster.local:27017" ENV DATABASE "admin" ENV USERNAME "admin" ENV PASSWORD "bmwee8097218" RUN apk --no-cache add tzdata && \ ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \ echo "Asia/Shanghai" > /etc/timezone ADD drinking_station /usr/bin/drinking_station ENTRYPOINT ["drinking_station"] EXPOSE $EXPORTER_PORT ``` **Build image** drinking_station是取的image的名子,直接把舊的蓋掉就可以了 >$ sudo docker build -t drinking_station_api . **Tag image** 把他做tag,取名子為renxjhang/drinking_station_api 用來傳到個人的docker hub (用push) 要抓下來用的話則要pull 新的版本就加一: v1改成v2 ...這樣 >$ sudo docker tag drinking_station_api renxjhang/drinking_station_api:v1 >$ sudo docker images **Push到docker hub** - 登出 docker目前的帳號 >$ sudo docker logout - 登入自己的帳號 >$ sudo docker login - 把Image Push到自己的docker hub(ex. "xiehuangjun/"是黃鈞的repository) >$ sudo docker push renxjhang/drinking_station_api:v1 - 如果你建的Image檔不在Server裡,就跟黃鈞說要怎麼pull你的Image檔就好,如果有在server裡就把像面途中橘色框框的東西告訴黃鈞就好 ex. `renxjhang/drinking_station_api:v1` <font color="#f00">※所有Image要跑都要把Image Push到docker hub上才能跑</font> <font color="#f00">**寫程式要放上pod的人做到這裡**</font> 直飲台 Drinking station 設計用圖 --- https://hackmd.io/@RenJhang/Bk6K55E9_ Meeting Minute --- [Meeting Minute](https://hackmd.io/@RenJhang/HJSE6-ZYO) 更多資料 --- [直飲台 DS API Document](https://hackmd.io/U1pTK7nkSGGxctDbUPFUGw) [Drinking Station Question](https://hackmd.io/@RenJhang/rywgHIE6d) [此計畫合作相關人員](https://hackmd.io/@RenJhang/rkUwI0Ybt) [Deploy DrinkingStation API process](https://hackmd.io/@RenJhang/BJkuCFDkF) [北水處接收資料的server設定](https://hackmd.io/BpCfvJkNSy-nKIVB7VqKmA) [直飲台 Drinking station 設計用圖](https://hackmd.io/@RenJhang/Bk6K55E9_)
{"metaMigratedAt":"2023-06-16T17:09:56.590Z","metaMigratedFrom":"Content","title":"Python ETL 交接","breaks":true,"contributors":"[{\"id\":\"056436d3-7d04-4858-ab73-773963d7cd00\",\"add\":7045,\"del\":481}]"}
Expand menu