etl和许多关系

时间:2016-04-12 10:26:57

标签: orientdb orientdb-2.1

在文档中,来自csv的etl使用了一对多功能,我想将它扩展到多对多。所以我做了3个配置,一个用于发布,一个用于评论,一个用于关系。帖子和评论都可以,但是当我启动关系时,我遇到了这个错误,我做错了什么?

Show Method Return Values

1 个答案:

答案 0 :(得分:0)

一种可能的解决方案可能是,在通过json文件导入Post和Comment类之后,您可以使用另一个json文件并导入类Relation

{
    "source": { "file": { "path": "/tmp/relation.csv" } },
    "extractor": { "row": {} },
    "transformers": [
        { "csv": { "separator": ","} 
        },
        { "vertex": { "class": "Relation" } }
    ],
    "loader": {
        "orientdb": {
           "dbURL": "plocal:/tmp/test",
           "dbType": "graph",
            "classes": [
                {"name": "Post", "extends": "V"},
                {"name": "Comment", "extends": "V"},
                {"name": "Relation", "extends": "V"},
                {"name": "HasComments", "extends": "E"}
            ],
            "indexes": [
                {"class":"Post", "fields":["id:integer"], "type":"UNIQUE" },
                {"class":"Comment", "fields":["id:integer"], "type":"UNIQUE" }
            ]
        }
    }
}

您将获得这些记录。

enter image description here

使用以下javascript函数

var g=orient.getGraphNoTx();
var relation = g.command("sql","select from Relation");
for(i=0;i<relation.length;i++){
var relationMM=g.command("sql","select postId , commentId from "+ relation[i].getId());
var idPost=relationMM[0].getProperty("postId");
var idComment=relationMM[0].getProperty("commentId");
var post=g.command("sql","select from Post where id = " + idPost);
var comment=g.command("sql","select from Comment where id = " + idComment);
g.command("sql","create edge HasComments from " + post[0].getId() + " to " + comment[0].getId());
}
g.command("sql","drop class Relation unsafe");

您将获得以下结构。

enter image description here

这将是您的图表。

enter image description here

<强>更新

您可以使用此代码检查边缘是否已存在

var counter=g.command("sql","select count(*) from HasComments where out=" + post[0].getId() + " and in=" + comment[0].getId());
if(counter[0].getProperty("count")==0){
    g.command("sql","create edge HasComments from " + post[0].getId() + " to " + comment[0].getId());
}