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 日常幫助客戶構建一站式網站應用,手機應用,雲端架構

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 個分支。