提取非表格数据

Python 中的 ETL 与 ELT

Jake Roach

Data Engineer

提取非表格数据

高亮显示提取环节的 ETL 流水线。

Python 中的 ETL 与 ELT

非表格数据类型

大多数产出与消费的数据是非结构化数据

  • 文本
  • 音频
  • 图像
  • 视频
  • 空间数据
  • IoT

非结构化数据源的转换过程。

1 https://mitsloan.mit.edu/ideas-made-to-matter/tapping-power-unstructured-data
Python 中的 ETL 与 ELT

使用 API 与 JSON 数据

API (Application Programming Interface)

  • 位于数据源之上的软件层
  • 阻止与数据库的直接交互

$$

数据工程师使用 API 与数据库交互。

JSON (JavaScript Object Notation)

  • 键值对
  • 无固定模式
  • 外观与 dict 类似
{
    "key": "value",
    ...
    "open": 0.121875
}
Python 中的 ETL 与 ELT

用 pandas 读取 JSON 文件

{
    "timestamps": [863703000, 863789400, ...],
    "open": [0.121875, 0.098438, ...],
    "close": [...],
    "volume": [...]
}

使用 .read_json() 函数

# 读取上述格式的 JSON 文件
raw_stock_data = pd.read_json("raw_stock_data.json", orient="columns")
1 https://pandas.pydata.org/docs/reference/api/pandas.read_json.html
Python 中的 ETL 与 ELT

嵌套或非结构化 JSON 数据

数据不一定可直接变为 DataFrame

  • 嵌套对象
  • 可变"模式"
{
    "863703000": {
        "volume": 1443120000,
        "price": {
            "close": 0.09791,
            "open": 0.12187
        }
    }, 
    "863789400": {
        ...
    }, ...
}
Python 中的 ETL 与 ELT

用 json 读取 JSON 文件

import json

with open("raw_stock_data.json", "r") as file:
    # Load the file into a dictionary
    raw_stock_data = json.load(file)

# Confirm the type of the raw_stock_data variable
print(type(raw_stock_data))
<class 'dict'>
Python 中的 ETL 与 ELT

让我们一起练习吧!

Python 中的 ETL 与 ELT

Preparing Video For Download...