非表格型 NoSQL 数据库

NoSQL 入门

Jake Roach

Data Engineer

文档型数据库

定义: 一种以灵活的半结构化格式存储数据的 NoSQL 工具,由键-值、键-数组、键-对象对组成(类似 JSON)。

$$

$$

Postgres JSON 标志。

{
    "title": "Python for Data Analysis",
    "price": 53.99,
    "topics": [
        "Data Science",
        "Data Analytics",
        ...
    ],
    "author": {
        "first": "William"
        ...
    }
}
NoSQL 入门

使用 Postgres JSON 查询 JSON 数据

存储在 Postgres 表中的高亮 JSON 数据。

SELECT
    books -> 'title' AS title,
    books -> 'price' AS price
FROM data_science_resources
WHERE 
    books -> 'author' ->> 'last' = 'Viafore';

得到如下输出:

Postgres JSON 查询输出。

NoSQL 入门

连接 Postgres 数据库

import sqlalchemy

# Create a connection string, and an engine
connection_string = "postgresql+psycopg2://<user>:<password>@<host>:<port>/<database>"
db_engine = sqlalchemy.create_engine(connection_string)

创建到 Postgres 的连接:

  • 组装连接字符串
  • 使用 sqlalchemy.create_engine 创建引擎
  • 练习前将提供 db_engine 变量
NoSQL 入门

编写并执行 Postgres JSON 查询

import pandas as pd

# Build the query
query = """
    SELECT
        books -> 'title' AS title,
        books -> 'price' AS price
    FROM data_science_resources;
"""

# Execute the query
result = pd.read_sql(query, db_engine)
print(result)

编写并执行查询:

  • 在字符串中构建查询
  • querydb_engine 传给 pd.read_sql()
  • 打印结果 DataFrame
NoSQL 入门

其他非表格型 NoSQL 数据库

键值
{
    "name": "Jane Doe",
    "age": 25,
    "email": "[email protected]"
}

$$

Redis 标志。

图数据库的示意图。

NoSQL 入门

¡Vamos a practicar!

NoSQL 入门

Preparing Video For Download...