Fetch Japan Airports and create nodes in neo4j

作者: Calpa Liu
字數:2248
出版日期:August 22, 2022
更新日期:August 22, 2022

I would like to know more about Japan, such as visualizing the relationship and nodes of the `First-Class` Airports in Japan, and the city it located and the prefecture.

I would like to know more about Japan, such as visualizing the relationship and nodes of the First-Class Airports in Japan, and the city it located and the prefecture.

With the following cypher query, my curiosity can be fulfilled.

MATCH (p:Prefecture)<-[:IN]-(c1:City)-[:HAS]->(a:Airport)-[:IS]->(c2:Classification)
WHERE c2.name = 'First-class'
RETURN p, c1, a, c2

Setup database

Convert the table in the List of airports in Japan in Wikipedia to json, and import them to the neo4j database.

The airport node should contain the nameICAOIATAnode properties and link to the municipality (city), a new node with the label Classification (First-classSecond-classThird-classOtherMilitary).

So we use axios to fetch the data, and the code should be like this:

const createAirports = async (ctx) => {
  const url = `https://www.wikitable2json.com/api/List_of_airports_in_Japan?table=0&keyRows=1`
  const { data } = await axios.get(url)

  const airports = data[0].map((airport) => {
    const object = airport;
    object.cities = object.Municipality.split(' / ')
    object.name = object['Airport name']
    object.classification = object['Classification']
    object.prefecture = object['Prefecture'].split(' / ')
    return object;
  })

  const result = await ctx.session.run(geography.createAirports, { airports })

  logger.info(`Airports created`)
  return data
}

And we can use the following cypher to create the airport node, relate the airport to the city:

UNWIND $airports as airport
MERGE (a:Airport{name: airport.name})
SET a.ICAO = airport.ICAO,
    a.IATA = airport.IATA

WITH a, airport
MERGE (c1:Classification{name: airport.classification})
MERGE (a)-[:IS]->(c1)

WITH a, airport
UNWIND airport.cities as city
UNWIND airport.prefecture as prefecture
MATCH (c2:City{en: city})-[:IN]->(p:Prefecture)
WHERE p.en = toLower(prefecture)
MERGE (c2)-[:HAS]->(a)

The source code is available in calpa - japan - Github.

Reference

  1. calpa - japan - Github
  2. List of airports in Japan
感謝您閱讀我的文章。歡迎隨時分享你的想法。
關於 Calpa

Calpa 日常幫助客戶構建一站式網站應用,手機應用,雲端架構

2023 年 9 月,Calpa 應邀出席韓國 Korea Blockchain Week 2023,新加坡 TOKEN 2049。十二月份,Calpa 將參加 Taipei Blockchain Week Hackathon。

Calpa 擅長運用各種前沿技術,包括 TypeScript、React.js 和 Vue.js,開發具有 Responsive Web Design 的網站。另外,他透過運用 aws-cdk 和 TypeScript 技術的融合,實現了基於雲端的「基礎即代碼」(Infrastructure as Code)部署策略,建立了多套高效且具有可伸縮性的全端架構。

同時,Calpa 積極參與各個社群,活躍於香港和台灣的開源社區,分享前沿知識。他曾在 2019 年的香港開源大會中擔任重要講者,為聽眾提供寶貴的工作經驗和深刻見解。此外,Calpa 在 GitHub 上公開分享了個人博客程式碼,已獲得超過 300 顆星星和 60 個分支。