简体   繁体   English

从 API 对 PostgreSQL 表的响应

[英]From API responses to PostgreSQL tables

Context语境

I'm searching for an efficient and robust workflow to dump data from API responses to a PostgreSQL database.我正在寻找一种高效且强大的工作流程来将 API 响应中的数据转储到 PostgreSQL 数据库。

Detailed explanation详细解释

I have access to a project RESTfulAPI , to which I can send some requests (Fig. 1) and which gives me responses as JSON data.我可以访问一个项目RESTfulAPI ,我可以向其发送一些请求(图 1),并以JSON数据的形式给出响应

Eg when using postman for example, I can build a query using all the parameters that are taken into account by the API, and which may for example looks like:例如,当使用postman时,我可以使用 API 考虑的所有参数构建查询,例如:

cURL 中的请求示例 Fig. 1: Request example in cURL .图 1: cURL中的请求示例。

The corresponding response looks like a bunch of those:相应的响应看起来像一堆:

[
    {
        "id": "128",
        "user": "UUID-981729jqdwm91888r",
        "description": {
            "producer": "John Wayne",
            "title": "Alamo",
            "year": 1960
        },
        "preview": {
            "imageURL": "11231068.jpg"
        }
    },
    {
    ...
    }
]

On the other hand, I've a PostgreSQL database (inside a docker container ) which is already structured to admit the response data.另一方面,我有一个PostgreSQL数据库(在docker 容器内),该数据库已经构造为允许响应数据。 For example, all the individual film features from the previous result have to be recorded in a table 'films' (Fig. 2).例如,之前结果中的所有单个电影特征都必须记录在'films'表中(图 2)。

PostgreSQL 表示例
Fig. 2: An example of what the resulting PostgreSQL table would look like.图 2:生成的 PostgreSQL 表的示例。


The thing is, I don't know from where to start as I always queried the API through my web browser or Postman until now.问题是,我不知道从哪里开始,因为我一直通过我的 web 浏览器或 Postman 查询 API。

Summarized , I have to dump API responses in a structured set of PostgreSQL tables as follow;总而言之,我必须将 API 响应转储到一组结构化的 PostgreSQL 表中,如下所示;
one API route that is requested --> one JSON response (with multiple features) --> one PostgreSQL table (with multiple rows)
one row corresponding to one of the response features).一行对应于响应特征之一)。

Question问题

How could I technically do that;我怎么能在技术上做到这一点? would it be possible to build API requests directly in SQL (I have the feeling it's a super bad idea) or do I have to use an other language or tool (for security reasons or simply for practicality)?是否可以直接在 SQL 中构建 API 请求(我觉得这是一个非常糟糕的主意)还是我必须使用其他语言或工具(出于安全原因或仅出于实用性)?

When searching the web for such thing I almost always found answer to "how to make API calls to a Postgres database?", which is obviously not what I want.在搜索 web 时,我几乎总是找到“如何使 API 调用 Postgres 数据库?”的答案,这显然不是我想要的。

Try to make a service which can尝试做一个服务

  • Call your Rest API and get the data致电您的 Rest API 并获取数据
  • Structured the data for DB table结构化数据库表的数据
  • Then put the data in Database然后将数据放入数据库

You can set a time interval/ scheduled time for this job done.您可以设置完成此工作的时间间隔/预定时间。

You can use golang to build this service or other language you familiar with.你可以使用 golang 来构建这个服务或者其他你熟悉的语言。

So if I understand you correctly, you want to take data from an web API and dump them into a database of some form, probably for data processing later.因此,如果我理解正确,您想从 web API 获取数据并将它们转储到某种形式的数据库中,可能用于以后的数据处理。

To my surprise, there does exists such a thing in Postgres to do what you want, though you will need to relax the pure SQL requirement.令我惊讶的是,Postgres 中确实存在这样的事情来做你想做的事,尽管你需要放宽纯粹的 SQL 要求。 If you are willing to use PL/PGSQL, there exists this plugin that adds a HTTP client for use in Postgres.如果你愿意使用 PL/PGSQL,有这个插件添加了一个 HTTP 客户端用于 Postgres。

For a one off script and you don't mind working with procedural language extensions in Postgres, I imagine this would be fine.对于一次性脚本并且您不介意在 Postgres 中使用过程语言扩展,我想这会很好。 However, sanity wise, a Python script would be safer, simpler and overall easier to develop than the PGSQL.然而,明智的做法是,Python 脚本会比 PGSQL 更安全、更简单且总体上更容易开发。 Personally, I would go with the Python route but I am not a database administrator which factors into my preference.就个人而言,我会使用 go 与 Python 路线,但我不是数据库管理员,这会影响我的偏好。

In fact, if you don't care for purity of working with a relational database or are not as comfortable, you could use an ORM like SQLAlchemy to simplify this task and later on have the objects be serialized into Python objects with some upfront work.实际上,如果您不关心使用关系数据库的纯度或不那么舒服,您可以使用 ORM 之类的SQLAlchemy来简化此任务,然后再将对象序列化为 ZA7F5F325426B9282171 对象。 If you dislike the idea of an ORM, you can also use SQLAlchemy to abstract and handle the database connection and tables setup.如果您不喜欢 ORM 的想法,您还可以使用 SQLAlchemy 来抽象和处理数据库连接和表设置。

Of course, if you're comfortable with SQL, there exists more barebone libraries that setups a database connection to Postgres and includes support for cursors, prepared statements and what not.当然,如果您对 SQL 感到满意,还有更多的准系统库可以设置与 Postgres 的数据库连接,并包括对游标、预准备语句等的支持。 For Python, there's psycopg2 ;对于 Python,有psycopg2 for JavaScript there is node-postgres对于 JavaScript 有node-postgres

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM