优化将CSV数据导入neo4j

时间:2015-07-27 16:04:54

标签: php csv neo4j cypher

我写了一个导入片段来填充我的Neo4J数据库,其中包含城镇节点和与他们相关的县。代码看起来像

<?php
function readCSV($csvFile){
$file_handle = fopen($csvFile, 'r');
$lineCount=0;
while (!feof($file_handle) ) {
    $line_of_text[] = fgetcsv($file_handle, 1024, ';', '"');
    $lineCount++;
  }
fclose($file_handle);
return array($line_of_text,$lineCount);
}

// Create an Index for Town and for Country

$queryString = '
CREATE INDEX ON :Country (name)
';
$query = new Everyman\Neo4j\Cypher\Query($client, $queryString);
$result = $query->getResultSet();

$queryString = '
CREATE INDEX ON :Town (name)
';
$query = new Everyman\Neo4j\Cypher\Query($client, $queryString);
$result = $query->getResultSet();

// Set path to CSV file
$importFile = 'files/import_city_country.csv';
$completeResult = readCSV($importFile);
$dataFile = $completeResult[0];
$maxLines = $completeResult[1];

for ($row = 1; $row < $maxLines; ++ $row) {
$countryData = array();

if(!is_null($dataFile[$row][0]))
{

    // Define parameters for the queries
    $params =array(
    "nameCountry" => trim($dataFile[$row][0]),
    "nameTown" => trim($dataFile[$row][1]),
    "uuid" => uniqid(),
    );

    # Now check if we know that country already to avoid double entries
    $queryString = '
    MATCH (c:Country {name: {nameCountry}})
    RETURN c
    ';

    $query = new Everyman\Neo4j\Cypher\Query($client, $queryString,$params);
    $result = $query->getResultSet();

    if(COUNT($result)==0) // Country doesnt exist!
    {
        $queryString = '
        MERGE (c:Country {name: {nameCountry}} )
        set 
            c.uuid = {uuid},
            c.created = timestamp()
        RETURN c
        ';

        $query = new Everyman\Neo4j\Cypher\Query($client, $queryString,$params);
        $result = $query->getResultSet();    
    }

    # Now check if we know that town already
    $queryString = '
    MATCH (t:Town {name: {nameTown}})
    RETURN t
    ';

    $query = new Everyman\Neo4j\Cypher\Query($client, $queryString,$params);
    $result = $query->getResultSet();

    if(COUNT($result)==0) // Town doesnt exist!
    {
        $queryString = '
        MERGE (t:Town {name: {nameTown}} )
        set 
            t.created = timestamp()
        RETURN t
        ';

        $query = new Everyman\Neo4j\Cypher\Query($client, $queryString,$params);
        $result = $query->getResultSet();   

        // Relate town to country

        $queryString = '
        MATCH (c:Country {name: {nameCountry}}), (t:Town {name: {nameTown}})      
        MERGE (t)-[:BELONGS_TO]->(c);
        ';

        $query = new Everyman\Neo4j\Cypher\Query($client, $queryString,$params);
        $result = $query->getResultSet();              

    }

} // Excel Last Line is not Null - go on

} // Next Row

?>

典型的CSV行似乎是

Country City
Albania Tirana

这一切都运行正常 - 但在电脑上导入9.000行需要30多分钟。我知道系统需要检查每条记录是否已经存在,并且还要在城镇和乡村之间建立关系,但是对于如此大量的CSV线,它似乎很长。

您是否有建议如何改进导入代码?

谢谢, Balael

顺便说一句:任何机会在这里插入代码而不编辑每一行并添加4个空格 - 对于更长的代码来说有点无聊......

1 个答案:

答案 0 :(得分:2)

如果可能的话,在neo4j-shell中使用LOAD CSV,并且不要编写自己的代码来处理CSV。

这对您有用的主要是允许您将多个项目批量处理为单个事务USING PERIODIC COMMIT

如果您想远程使用REST API(我假设您已经在这里做了),那么请查看语言绑定对batch operations的支持。您编写的代码将花费大量时间来回服务器,可能会将CSV的每一行转换为请求/响应,这将很慢。最好一次批量处理多个并将它们作为一个操作运行,这将有助于最大限度地减少您拥有的协议开销。