Import List of Villages in Japan to neo4j Database

Import List of Villages in Japan to neo4j Database
作者: Calpa Liu
字數:1801
出版日期:August 23, 2022
更新日期:August 23, 2022

A `village` is contained within a prefecture, and a `district` has many `villages` or `towns` that not overlap an no uncovered area.

A village is contained within a prefecture, and a district has many villages or towns that not overlap an no uncovered area.

By importing the table from wikipedia, we can get the name, en (english name), prefecture, district and area details of the village.

So we can use the following code to get the data and run the cypher query.

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

  const villages = data[0].map((village) => {
    return {
      area: village["Area (in km²)"],
      district: village["District"],
      name: village["Japanese"],
      prefecture: village["Prefecture"],
      en: village["Village"],
    };
  });

  const result = await ctx.session.run(geography.createVillages, { villages });

  logger.info(`Villages created`);

  return result;
};

And the cypher is pretty simple, for each village, create Village node and set area, en property. Create a district node and bind the village, district and prefecture relationships.

UNWIND $villages as village
MERGE (v:Village:Municipal{name: village.name})
SET v.area = village.area,
    v.en = village.en

WITH v, village
MERGE (d:District{name: village.district})
MERGE (v)-[:IN]->(d)

WITH v, village, village.prefecture as prefecture, d
MATCH (p:Prefecture)
WHERE p.en = toLower(prefecture)
  OR p.en = toLower(
    replace(
      replace(prefecture, 'ō', 'o'),
      'Ō',
      'O'
    )
  )
MERGE (v)-[:IN]->(p)
MERGE (d)-[:IN]->(p)

Thoughts

By building the fundamentals of data in the database, I know more than yesterday.

Reference

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

Calpa 擅長使用 TypeScript、React.js 和 Vue.js 開發Responsive Web Design網站。

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

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