抽取

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 对象表示法
  • 半结构化
  • 原子类型
    • 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 入门

网络上的数据

请求

表示请求-响应模型的示意图

示例

  1. 访问 Google
  2. 向 Google 服务器发起请求
  3. Google 返回网页
Data Engineering 入门

通过 API 获取网页数据

  • 以 JSON 格式发送数据
  • API:应用程序编程接口
  • 示例
    • Twitter API
{ "statuses": [{ "created_at": "Mon May 06 20:01:29 +0000 2019", "text": "this is a tweet"}] }
  • 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 入门

Vamos praticar!

Data Engineering 入门

Preparing Video For Download...