Fetch Japan Airports and create nodes in neo4j

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 擅長使用 TypeScript、React.js 和 Vue.js 開發Responsive Web Design網站。

此外,Calpa 積極參與香港和台灣的開源社區,曾在2019年的香港開源大會上擔任講者,提供工作經驗和見解。此外,他也在 GitHub 上公開分享個人博客程式碼,已獲得超過300顆星星和60個分支的支持。

更多前端開發技術文章:傳送門