Trích xuất

Introduction to Data Engineering

Vincent Vankrunkelsven

Data Engineer, DataCamp

Trích xuất dữ liệu: nghĩa là gì?

Sơ đồ minh họa giai đoạn trích xuất đơn giản

Introduction to Data Engineering

Trích từ tệp văn bản

Không cấu trúc

  • Văn bản thuần
  • Ví dụ: một chương sách

 

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

Tệp phẳng (flat files)

  • Hàng = bản ghi
  • Cột = thuộc tính
  • Ví dụ: .tsv hoặc .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
Introduction to Data Engineering

JSON

  • JavaScript Object Notation
  • Bán cấu trúc
  • Kiểu nguyên tử
    • number
    • string
    • boolean
    • null
  • Kiểu tổng hợp
    • 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
Introduction to Data Engineering

Dữ liệu trên Web

Yêu cầu (Requests)

Sơ đồ mô hình yêu cầu - phản hồi

Ví dụ

  1. Mở Google
  2. Gửi yêu cầu đến máy chủ Google
  3. Google phản hồi trang web
Introduction to Data Engineering

Dữ liệu trên Web qua API

  • Gửi dữ liệu ở định dạng JSON
  • API: application programming interface
  • Ví dụ
    • 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': .... }
Introduction to Data Engineering

Dữ liệu trong cơ sở dữ liệu

CSDL ứng dụng

  • Giao dịch
  • Thêm/sửa đổi
  • OLTP
  • Hướng hàng (row-oriented)

CSDL phân tích

  • OLAP
  • Hướng cột (column-oriented)
Introduction to Data Engineering

Trích xuất từ cơ sở dữ liệu

Chuỗi kết nối/URI

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

Dùng trong 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)
Introduction to Data Engineering

Cùng luyện tập nào!

Introduction to Data Engineering

Preparing Video For Download...