簡體   English   中英

如何在節點Yelp數據集之間創建Neo4J關系

[英]How to create Neo4J relationship between nodes yelp dataset

我是Neo4j的新手。 我正在嘗試在Neo4j中填充Yelp數據集。 基本上,我對他們提供的三個json文件感興趣,即

user.json

{
    "user_id": "-lGwMGHMC_XihFJNKCJNRg",
    "name": "Gabe",
    "review_count": 277,
    "yelping_since": "2014-10-31",
    "friends": ["Oa84FFGBw1axX8O6uDkmqg", "SRcWERSl4rhm-Bz9zN_J8g", "VMVGukgapRtx3MIydAibkQ", "8sLNQ3dAV35VBCnPaMh1Lw", "87LhHHXbQYWr5wlo5W7_QQ"],
    "useful": 45,
    "funny": 4,
    "cool": 55,
    "fans": 17,
    "elite": [],
    "average_stars": 4.72,
    "compliment_hot": 5,
    "compliment_more": 1,
    "compliment_profile": 0,
    "compliment_cute": 1,
    "compliment_list": 0,
    "compliment_note": 11,
    "compliment_plain": 20,
    "compliment_cool": 15,
    "compliment_funny": 15,
    "compliment_writer": 1,
    "compliment_photos": 8
}

我省略了friends數組中的幾個條目以使輸出可讀

business.json

{
    "business_id": "YDf95gJZaq05wvo7hTQbbQ",
    "name": "Richmond Town Square",
    "neighborhood": "",
    "address": "691 Richmond Rd",
    "city": "Richmond Heights",
    "state": "OH",
    "postal_code": "44143",
    "latitude": 41.5417162,
    "longitude": -81.4931165,
    "stars": 2.0,
    "review_count": 17,
    "is_open": 1,
    "attributes": {
        "RestaurantsPriceRange2": 2,
        "BusinessParking": {
            "garage": false,
            "street": false,
            "validated": false,
            "lot": true,
            "valet": false
        },
        "BikeParking": true,
        "WheelchairAccessible": true
    },
    "categories": ["Shopping", "Shopping Centers"],
    "hours": {
        "Monday": "10:00-21:00",
        "Tuesday": "10:00-21:00",
        "Friday": "10:00-21:00",
        "Wednesday": "10:00-21:00",
        "Thursday": "10:00-21:00",
        "Sunday": "11:00-18:00",
        "Saturday": "10:00-21:00"
    }
}

review.json

{
    "review_id": "VfBHSwC5Vz_pbFluy07i9Q",
    "user_id": "-lGwMGHMC_XihFJNKCJNRg",
    "business_id": "YDf95gJZaq05wvo7hTQbbQ",
    "stars": 5,
    "date": "2016-07-12",
    "text": "My girlfriend and I stayed here for 3 nights and loved it.",
    "useful": 0,
    "funny": 0,
    "cool": 0
}

正如我們在示例文件中看到的那樣,用戶和企業之間的關系是通過review.json文件關聯的。 如何使用review.json文件在userbusiness之間創建關系邊緣。

我還看過Mark Needham的教程,其中他顯示了StackOverflow數據填充,但是在這種情況下,關系文件已經包含有示例數據。 我需要建立一個類似的文件嗎? 如果是,我應該如何解決這個問題? 還是有其他方法可以建立用戶與企業之間的關系?

要做什么,很大程度上取決於您的模型,但是您可以進行3次導入:

//Create Users - does assume the data is unique
CALL apoc.load.json('file:///c://temp//SO//user.json') YIELD value AS user
CREATE (u:User)
SET u = user

然后添加業務:

CALL apoc.load.json('file:///c://temp//SO//business.json') YIELD value AS business
CREATE (b:Business {
            business_id     : business.business_id,
            name            : business.name,
            neighborhood    : business.neighborhood,
            address         : business.address,
            city            : business.city,
            state           : business.state,
            postal_code     : business.postal_code,
            latitude        : business.latitude,
            longitude       : business.longitude,
            stars           : business.stars,
            review_count    : business.review_count,
            is_open         : business.is_open,
            categories      : business.categories
        })

對於企業來說,我們不能只做SET b = business因為JSON具有嵌套的地圖。 因此,您可能想決定是否要使用它們,並且可能必須走另一條路。

最后是評論,這是我們一起加入的地方。

CALL apoc.load.json('file:///c://temp//SO//review.json') YIELD value AS review
CREATE (r:Review)
SET r = review
WITH r
//Match user to a review
MATCH (u:User {user_id: r.user_id})
CREATE (u)-[:HAS_REVIEW]->(r)
WITH r, u
//Match business to a review, and a user to a business
MATCH (b:Business {business_id: r.business_id})
//Merge here in case of multiple reviews
MERGE (u)-[:HAS_REVIEWED]->(b)
CREATE (b)-[:HAS_REVIEW]->(r)

顯然-將標簽/關系更改為所需的類型,並且可能需要根據數據大小等進行調整,因此您可能需要使用apoc.periodic.iterate進行處理。

如果需要,Apoc在這里 (應該使用它!)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM