擷取

Data Engineering 入門

Vincent Vankrunkelsven

Data Engineer, DataCamp

擷取資料:是什麼意思?

表示簡易擷取階段的圖示

Data Engineering 入門

從文字檔擷取

非結構化

  • 純文字
  • 例如:書中的章節

 

Call me Ishmael. Some years ago, never 
mind how long precisely, having little or
no money in my purse, and nothing particular
to interest me on shore, I thought ....

平面檔案

  • 列 = 記錄
  • 欄 = 屬性
  • 例如:.tsv.csv

 

Year,Make,Model,Price
1997,Ford,E350,3000.00
1999,Chevy,"Venture Extended Edition",4900.00
1999,Chevy,"Venture Extended Edition",5000.00
1996,Jeep,Grand Cherokee,4799.00
Data Engineering 入門

JSON

  • JavaScript Object Notation
  • 半結構化
  • 原子型別
    • number
    • string
    • boolean
    • null
  • 複合型別
    • array
    • object
{
  "an_object": {
    "nested": [
      "one",
      "two",
      "three",
      {
        "key": "four"
      }
    ]
  }
}
import json

result = json.loads('{"key_1": "value_1", "key_2":"value_2"}') print(result["key_1"])
value_1
Data Engineering 入門

網路上的資料

請求(Requests)

表示請求-回應模型的圖示

範例

  1. 前往 Google
  2. 向 Google 伺服器發出請求
  3. Google 回傳網頁
Data Engineering 入門

透過 API 取得網路資料

  • 以 JSON 格式傳送資料
  • API:application programming interface(應用程式介面)
  • 範例
    • GitHub API
{ "login": "datacamp", "type": "Organization", "created_at": "2013-05-14T13:01:20Z" }
  • Hackernews API
import requests

response = requests.get("https://hacker-news.firebaseio.com/v0/item/16222426.json") print(response.json())
{'by': 'neis', 'descendants': 0, 'id': 16222426, 'score': 17, 'time': 1516800333, 'title': .... }
Data Engineering 入門

資料庫中的資料

應用型資料庫

  • 交易處理
  • 新增或變更
  • OLTP
  • 列導向

分析型資料庫

  • OLAP
  • 欄導向
Data Engineering 入門

從資料庫擷取

連線字串/URI

postgresql://[user[:password]@][host][:port]

在 Python 中使用

import sqlalchemy
connection_uri = "postgresql://repl:password@localhost:5432/pagila" 
db_engine = sqlalchemy.create_engine(connection_uri)

import pandas as pd pd.read_sql("SELECT * FROM customer", db_engine)
Data Engineering 入門

一起來練習吧!

Data Engineering 入門

Preparing Video For Download...