API到数据库? [英] API to Database?

查看:26
本文介绍了API到数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请假设我对我将要提到的任何事情一无所知,因为我真的不知道.

<小时>

大多数 OpenData 站点都可以以 .csv 或 .json 格式导出所提供的文件 (

在本例中,我使用 JavaScript 来检索数据,但您可以使用任何您想要的代码.您可以继续使用 JavaScript 来解密数据、操作它并将其推送到数据库中.

还有其他云服务允许您将不同服务的 API 端点相互连接,例如 IFTTT 和 Zapier,但我不确定它们是否与开放数据连接.

Please presume that I do not know anything about any of the things I will be mentioning because I really do not.


Most OpenData sites have the possibility of exporting the presented file either in for example .csv or .json formats (Example). They also always have an API tab (Example API).

I presume using the API would mean that if the data is updated you would receive the change whereas exporting it as .csv would mean the content will not be changed anymore.

My questions is: how does one use this API code to display the same table one would get when exporting a .csv file.

Would you use a database to extract this information? What kind of database and how do you link the API to the database?

解决方案

I presume using the API would mean that if the data is updated you would receive the change whereas exporting it as .csv would mean the content will not be changed anymore.

You are correct in the sense that, if you download the csv to your computer, that csv file won't be updated any more.
An API is something you would call - in this case, you can call the API, saying "Hey, do you have the latest data on xxx?", and you will be given back the latest information about what you have asked. This does not mean though, that this site will notify you when there's a new update - you will have to keep calling the API (every hour, every day etc) to see if there are any changes.

My questions is: how does one use this API code to display the same table one would get when exporting a .csv file.

You would:

  1. Call the API from a server code, or a cloud service
  2. Let the server code or cloud service decipher (or "Parse") the response
  3. Use the deciphered response to create a table made out of HTML, or to place it into a database

Would you use a database to extract this information? What kind of database and how do you link the API to the database?

You wouldn't necessarily need a database to extract information, although a database would be nice to place the final data inside.
You would first need some sort of way to "call the REST API". There are many ways to do this - using Shell Script, using Python, using Excel VBA etc.
I understand this is hard to visualize, so here is an example of step 1, where you can retrieve information.
Try placing in the below URL (taken from the site you showed us) in your address bar of your Chrome browser, and hit enter http://opendata.brussels.be/api/records/1.0/search/?dataset=associations-clubs-sportifs

See how it gives back a lot of text with many brackets and commas? You've basically asked the site to give you some data, and this is the response they gave back (different browsers work differently - IE asks you to download the response as a .json file). You've basically called an API.

To see this data more cleanly, open your developer tools of your Chrome browser, and enter the following JavaScript code

var url = 'http://opendata.brussels.be/api/records/1.0/search/?dataset=associations-clubs-sportifs';

var xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
xhr.onload = function() {
    if (xhr.status === 200) {
        // success
        console.log(JSON.parse(xhr.responseText));
    } else {
        // error
        console.log(JSON.parse(xhr.responseText));
    }
};
xhr.send();

When you hit enter, a response will come back, stating "Object". If you click through the arrows, you can see this is a cleaner version of the data we just saw - more human readable.

In this case, I used JavaScript to retrieve the data, but you can use whatever code you want. You could proceed to use JavaScript to decipher the data, manipulate it, and push it into a database.

kintone is an online cloud database where you can customize it to run JavaScript codes, and have it store the data in their database, so you'll have the data stored online like in the below image. This is just one example of a database you can use.

There are other cloud services which allow you to connect API end points of different services with each other, like IFTTT and Zapier, but I'm not sure if they connect with open data.

这篇关于API到数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆