Import List of Villages in Japan to neo4j Database

作者: Calpa Liu

出版:2022-08-23

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 Liu

Avatar

Calpa 是一個四天工作的系統架構師,日常幫助客戶構建一站式網站應用,手機應用,雲端架構。

2023 年 9 月,Calpa 收到來自國內外不同 Web3 團隊的邀請,參加韓國的 KBW 2023 和新加坡的 TOKEN 2049 活動。十二月份,Calpa 將參加台北區塊鏈週的 Hackathon 比賽。

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

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

如果您對系統架構有任何問題,或需要進一步交流,請隨時聯絡 Calpa,他非常歡迎討論。