抽取

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 入门

Web 上的数据

请求

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

示例

  1. 浏览到 Google
  2. 向 Google 服务器发出请求
  3. Google 返回网页
Data Engineering 入门

通过 API 获取 Web 数据

  • 以 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...