python db2查詢_如何將DB2查詢轉換為python腳本

python db2查詢

Many companies are running common data analytics tasks using python scripts. They are asking employees to convert scripts that may currently exist in SAS or other toolsets to python. One step of this process is being able to pull in the same data with the new techniques. This article is about converting DB2 queries into python scripts.

許多公司正在使用python腳本運行常見的數據分析任務。 他們要求員工將SAS或其他工具集中當前可能存在的腳本轉換為python。 這一過程的第一步是能夠使用新技術提取相同的數據。 本文是關于將DB2查詢轉換為python腳本的。

How do you convert your queries to python? It may sound overwhelming but it’s easier than you think. Once you have a template for your data source, all you need to do is change the query and output filename.

您如何將查詢轉換為python? 聽起來可能令人難以置信,但比您想像的要容易。 一旦有了用于數據源的模板,您要做的就是更改查詢和輸出文件名。

There are several ways you can do this, but I will outline an intuitive template that allows you to run a DB2 query on your local laptop/desktop.

有幾種方法可以執行此操作,但是我將概述一個直觀的模板,該模板允許您在本地筆記本電腦/臺式機上運行DB2查詢。

在DiscoDonuts使用您的DB2專業知識 (Use your DB2 expertise at DiscoDonuts)

Let’s pretend you work at a large donut company, DiscoDonuts. You have a query to you run the following query against DB2. Typically you might use a tool such as DataStudio. Pretty simple.

假設您在一家大型甜甜圈公司DiscoDonuts工作。 您有一個查詢要針對DB2運行以下查詢。 通常,您可能會使用諸如DataStudio之類的工具。 很簡單

SELECT store_id, donut_style, date, volume, net_sales
FROM donutsdb.sales_data
WHERE date = '2020-08-16'
WITH UR;

Now you have your manager asking you to start using python. Take a deep breath; it’s not that hard. After the code is set up, you just need to update two fields, the name of your output file and your query itself. Then you hit Run. How simple is that?

現在,您的經理要求您開始使用python。 深吸一口氣; 這并不難。 設置代碼后,您只需要更新兩個字段,即輸出文件的名稱和查詢本身。 然后您點擊運行。 這有多簡單?

初始一次性設置 (The initial one-time setup)

If you haven’t already, you will need to contact your IT department to have a tool (“IDE”) installed (such as PyCharm, VSCode, Jupyter Notebooks).

如果尚未安裝,則需要聯系您的IT部門以安裝工具(“ IDE”)(例如PyCharm,VSCode,Jupyter Notebooks)。

To connect to DB2, you will need to enter your own company’s database, hostname, and port id. Most likely, you already have this information in whatever tool you are currently using.

要連接到DB2,您將需要輸入自己公司的數據庫,主機名和端口ID。 您很可能已經在當前使用的任何工具中獲得了此信息。

模板 (The template)

First, fill in the database connection information. Now you can save this template for use time and time again.

首先,填寫數據庫連接信息。 現在,您可以一次又一次保存此模板以供使用。

For each query you want to run, you update the output filename and the actual query itself. The query is passed to DB2 so it is in the same DB2 format you are already using.

對于每個要運行的查詢,您將更新輸出文件名和實際查詢本身。 該查詢將傳遞到DB2,因此它與您已經在使用的DB2格式相同。

import ibm_db
import ibm_db_dbi
import pandas as pd# name your output file (and path if needed)
output_filename = "donut_sales.csv"# enter your query between the triple quotes
query = """ SELECT store_id, donut_style, date, volume, net_sales
FROM donutsdb.sales_data
WHERE date = '2020-08-16'
WITH UR;
"""# one way to do credentialing
import getpass as gp
uid=input('Enter uid: ')
pwd=gp.getpass('Enter password (hidden): ')# connect to your database
db = (
"DRIVER = {IBM DB2 ODBC DRIVER - DB2COPY1};"
"DATABASE=<your donut database>;"
"HOSTNAME=<your db2 hostname>;"
"PORT=<your db2 port ####>;"
"PROTOCAL=TCPIP;"
'UID='+uid+';'
'PWD='+pwd+';')
ibm_db_conn = ibm_db.connect(db, "", "")
pconn = ibm_db_dbi.Connection(ibm_db_conn)#optional if you are using the accelerator #ibm_db.exec_immediate(ibm_db_conn, "SET CURRENT QUERY ACCELERATION = ALL") df = pd.read_sql(query, pconn)
df.to_csv(output_filename,index=False)

Just hit Run. You will be asked to enter your credentials, your query will run on DB2, the data will be transferred back to your script and your file will be created!

只需點擊運行。 系統將要求您輸入憑據,查詢將在DB2上運行,數據將被傳輸回腳本并創建您的文件!

The data frame created can serve as your data for further analysis within the python script if you choose.

如果選擇的話,創建的數據框可以用作數據,以便在python腳本中進行進一步分析。

下一個查詢的三個步驟 (Three steps for your next query)

  1. Update your output filename

    更新您的輸出文件名
  2. Update your query

    更新查詢
  3. Hit Run!

    點擊運行!

結論 (Conclusion)

It is not that hard to transfer your DB2 SQL knowledge to python. This is a great skill to have and share with others.

將DB2 SQL知識轉移到python并不難。 與他人共享和分享這是一項偉大的技能。

* I always welcome feedback. If you have another technique, share it in the responses. There are many ways to approach a problem and I have presented just one of many. Code is ever-evolving so what works today may not work tomorrow.

*我隨時歡迎您提供反饋。 如果您有其他技術,請在回復中分享。 解決問題的方法有很多,我只介紹了其中一種。 代碼在不斷發展,因此今天行之有效的明天可能行不通。

翻譯自: https://towardsdatascience.com/how-to-convert-db2-queries-to-python-scripts-f46960ed8df9

python db2查詢

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

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

相關文章

Dapper基礎知識三

在下剛畢業工作&#xff0c;之前實習有用到Dapper&#xff1f;這幾天新項目想用上Dapper&#xff0c;在下比較菜鳥&#xff0c;這塊只是個人對Dapper的一種總結。 Dapper&#xff0c;當項目在開發的時候&#xff0c;在沒有必要使用依賴注入的時候&#xff0c;如何做到對項目的快…

deeplearning4j

deeplearning4j 是基于java的深度學習庫&#xff0c;當然&#xff0c;它有許多特點&#xff0c;但暫時還沒學那么深入&#xff0c;所以就不做介紹了 需要學習dl4j&#xff0c;無從下手&#xff0c;就想著先看看官網的examples&#xff0c;于是&#xff0c;下載了examples程序&a…

PostgreSQL 11 1Kw TPCC , 1億 TPCB 7*24 強壓耐久測試

標簽 PostgreSQL , tpcc , tpcb 背景 TPCC, TPCB是工業標準的OLTP類型業務的數據庫測試&#xff0c;包含大量的讀、寫、更新、刪除操作。 7*24小時強壓耐久測試&#xff0c;主要看數據庫在長時間最大壓力下的 性能、穩定性、可靠性。 測試CASE &#xff1a; 1、1000萬 tpcc 2、…

推理編程_答案集編程的知識表示和推理

推理編程Read about the difference between declarative and imperative programming and learn from code examples (Answer Set Programming, Python and C).了解聲明式和命令式編程之間的區別&#xff0c;并從代碼示例(答案集編程&#xff0c;Python和C)中學習。 介紹 (In…

給Hadoop初學者的一些建議

我們介紹了新手學習hadoop的入門注意事項。這篇來談談hadoop核心知識學習。 hadoop核心知識學習: hadoop分為hadoop1.X和hadoop2.X&#xff0c;并且還有hadoop生態系統。這里只能慢慢介紹了。一口也吃不成胖子。 那么下面我們以hadoop2.x為例進行詳細介紹&#xff1a; Hadoop…

Guide AHOI2017 洛谷P3720

Description 農場主John最近在網上買了一輛新車&#xff0c;在購買汽車配件時&#xff0c;John不小心點了兩次“提交”按鈕。導致汽車上安裝了兩套GPS系統&#xff0c;更糟糕的是John在使用GPS導航時&#xff0c;兩套系統常常給出不同的路線。從地圖上看&#xff0c;John居住的…

穩坐視頻云行業第一,阿里云將用邊緣計算開辟新賽道

“CDN競爭的上半場已結束&#xff0c;中國視頻云市場格局已定&#xff0c;邊緣計算將成為下半場發展的新賽道。” 4月10日&#xff0c;阿里云視頻云總經理、邊緣計算負責人朱照遠在第七屆“亞太內容分發大會”暨CDN峰會表示。朱照遠認為&#xff0c;阿里云依靠齊全的產品矩陣、…

愛因斯坦提出的邏輯性問題_提出正確問題的重要性

愛因斯坦提出的邏輯性問題We live in a world that values answers. We were taught in school to learn how to answer questions in exams, we were conditioned to go to work knowing that we need to have the answers and our society, by and large, focuses on finding…

python安裝包

由于Google、YouTube等大型公司的推廣&#xff0c;Python編程語言越來越受歡迎&#xff0c;很多編程愛好者&#xff0c;也將Python做為了首先的編程語言。 今天我們就來講一下&#xff0c;學習的第一步&#xff0c;安裝Python IDLE編輯器&#xff0c;也它的調試和使用。 第一步…

104 權限 sudo 解壓縮

主要內容:https://www.cnblogs.com/pyyu/articles/9355477.html 1 查看系統版本信息: #查看系統版本信息 cat /etc/redhat-release CentOS Linux release 7.4.1708 (Core) #查看內核版本號 uname -r 3.10.0-693.el7.x86_64 #查看系統多少位 uname -m x86_64 #查看內核所有信息…

Cloud Native 介紹

為什么80%的碼農都做不了架構師&#xff1f;>>> 背景 Cloud Native表面看起來比較容易理解&#xff0c;但是細思好像又有些模糊不清&#xff1a;Cloud Native和Cloud關系是啥&#xff1f;它用來解決什么問題&#xff1f;它是一個新技術還是一個新的方法&#xff1f…

餐廳數據分析報告_如何使用數據科學選擇理想的餐廳設計場所

餐廳數據分析報告空間數據科學 (Spatial Data Science) Designing any product requires a lot of analysis and research. It is also true for designing any building. Before we begin to design any building, we collect information about the location where we are de…

P2P原理及UDP穿透簡單說明

本文章出自cnntec.com的AZ貓著&#xff0c;如需要轉發&#xff0c;請注明來自cnntec.com Peer-To-Peer縮寫P2P 中文稱之為對等聯網。 用途于交流&#xff0c;比如QQ&#xff0c;MSN等等。 文件傳輸、分布式數據計算等等。 這里我們主要是是簡單講解一下UDP實現NAT的穿透&…

PCB genesis 大孔擴孔(不用G84命令)實現方法

PCB鉆孔時,當鉆刀>6.3mm時,超出鉆孔范圍,鉆孔工序是沒有這么大的鉆刀,當這種情況,工程CAM會都采用G84命令用小孔擴孔的方式制作, 在這里介紹一種如果不用G84命令,用程序實現將大孔生成小孔鉆孔達到擴孔的目的。 一.我們先了解一下G84命令擴孔 孔尺寸大小 孔密度 連一篇文章有…

一年沒做出量化策略_量化信念:如何做出更好的決定

一年沒做出量化策略By Stuart George, Executive Director of Design Technology at MethodMethod設計技術執行總監Stuart George When Andrew Mason, founder of Groupon, wanted to improve his email conversion metrics, he turned to data analysis. His team tested the…

Android Jetpack組件之數據庫Room詳解(二)

本文涉及Library的版本如下&#xff1a; androidx.room:room-runtime:2.1.0-alpha03androidx.room:room-compiler:2.1.0-alpha03(注解編譯器)回顧一下安卓的SQLiteOpenHelper相關類 首先放一個關于安卓數據庫的類圖: SQLiteOpenHelper是一個抽象類&#xff0c;通常自己實現數據…

圖像識別中的深度學習

來源&#xff1a;《中國計算機學會通訊》第8期《專題》 作者&#xff1a;王曉剛 深度學習發展歷史 深度學習是近十年來人工智能領域取得的重要突破。它在語音識別、自然語言處理、計算機視覺、圖像與視頻分析、多媒體等諸多領域的應用取得了巨大成功。現有的深度學習模型屬于神…

多個css樣式合并到一個“目錄”css文件中

執行訪問jsp后發現沒有效果 同樣的代碼&#xff0c;在html中效果對比如下&#xff1a; 具體原因&#xff1a;不清楚&#xff0c;暫時記著~~~在jsp中不支持import這種css樣式的引用 轉載于:https://www.cnblogs.com/mangwusuozhi/p/10050108.html

Git 學習筆記之 merge

Merge: 1、Fast-forward&#xff08;快進式&#xff09; 2、recursice strategy (策略合并&#xff0c;三方合并) Fast-forward 策略合并 //創建一個文件夾&#xff0c;并初始化 Git mkdir GitDemo cd GitDemo git init//初次提交&#xff0c;創建 master 分支 touch master.tx…

熊貓直播 使用什么sdk_沒什么可花的-但是16項基本操作才能讓您開始使用熊貓

熊貓直播 使用什么sdkPython has become the go-to programming language for many data scientists and machine learning researchers. One essential data processing tool for them to make this choice is the pandas library. For sure, the pandas library is so versat…