2017-05-21 95 views
0

我試圖加載一個CSV文件(25 Mb大小,150 000行),其中包含22列成neo4j圖形使用py2neo航班模型化。Neo4j:從CSV文件創建關係是非常慢py2neo

密碼查詢用於一個查詢幷包含節點(機場,城市,飛行和飛機)之間的節點和關係創建。但是在運行代碼時,即使使用定期提交,也需要永久使用。

我不確定我寫的密碼查詢是否已優化,並且可能是緩慢的來源。 對於10 000行,我花了大約10分鐘來建立圖... 任何人都可以幫助我嗎?下面是代碼:

def importFromCSVtoNeo(graph): 
query = ''' 
    USING PERIODIC COMMIT 1000 
    LOAD CSV WITH HEADERS FROM "file:///flights.csv" AS row FIELDTERMINATOR '\t' 
    WITH row 

    MERGE (c_departure:City {cityName: row.cityName_departure}) 
    MERGE (a_departure:Airport {airportName: row.airportName_departure}) 
    MERGE (f_segment1:Flight {airline: row.airline1}) 
    ON CREATE SET f_segment1.class = row.class1, 
        f_segment1.outboundclassgroup = row.outboundclassgroup1 

    MERGE (a_departure)-[:IN]->(c_departure) 
    MERGE (c_departure)-[:HAS]->(a_departure) 
    MERGE (f_segment1)-[:FROM {departAt: row.outbounddeparttime}]->(a_departure) 

    MERGE (c_transfer:City {cityName: row.transferCityName}) 
    MERGE (a_transfer:Airport {airportName: row.airportName_transfer}) 
    MERGE (f_segment1)-[:TO_TRANSFER {transferArriveAt: row.transferArriveAt}]->(a_transfer) 
    MERGE (a_transfer)-[:IN]->(c_transfer) 
    MERGE (c_transfer)-[:HAS]->(a_transfer) 

    MERGE (c_arrival:City {cityName: row.cityName_arrival}) 
    MERGE (a_arrival:Airport {airportName: row.airportName_arrival}) 
    MERGE (f_segment2:Flight {airline: row.airline2}) 
    ON CREATE SET f_segment2.class = row.class2, 
        f_segment2.outboundclassgroup = row.outboundclassgroup2 
    MERGE (f_segment2)-[:TO {arrivalAt: row.outboundarrivaltime}]->(a_arrival) 
    MERGE (f_segment2)-[:FROM_TRANSFER {transferDepartAt: row.transferDepartAt}]->(a_transfer) 
    MERGE (a_arrival)-[:IN]->(c_arrival) 
    MERGE (c_arrival)-[:HAS]->(a_arrival) 


    MERGE (p:Plane {saleprice: row.saleprice}) 
    ON CREATE SET p.depart = row.cityName_departure, 
        p.destination = row.cityName_arrival, 
        p.salechannel = row.salechannel, 
        p.planeDuration = row.planeDuration 
    MERGE (p)-[:HAS_FLIGHTS]->(f_segment1) 
    MERGE (f_segment1)-[:WAIT_FOR {waitingTime: row.waitingTime}]->(f_segment2) 
    ''' 

graph.run(query) 


if __name__ == '__main__': 
    graph = Graph() 
    importFromCSVtoNeo(graph) 

我也試着做一個批處理模式,但性能並沒有得到更好的... 我會知道的任何意見或建議。謝謝 !!

回答

1

我會在啓動腳本之前使用節點屬性的索引,以便讓neo4j在使用MERGE時快速查找(因爲它必須逐行匹配節點)。例如,對於我將使用的第一個節點屬性:

CREATE INDEX ON :City(cityname) 

等等。您可以直接在py2neo中將它們創建爲單個運行語句。

+0

謝謝你的回答!但我也嘗試過,但它並沒有改變速度:s – filipyoo

+0

我試圖在一個熊貓數據框中加載csv文件,然後使用py2neo API填充圖形,但它一直在創建相同的節點速度慢 :/ – filipyoo