东方ETL很多很多

时间:2016-02-18 15:32:32

标签: orientdb

ETL for M:M

我们在RDBMS中有以下表格:

  1. ExternalMessageConfig(id,name,...);
  2. ExternalMessageDataConfig(id,dataname,...);
  3. ExternalMessageConfigMapping(messageConfigId,dataConfigId)(M:M)(链接表)。
  4. 我们创建了2个顶点:ExternalMessageConfig和ExternalMessageDataConfig。

    我们在ExternalMessageConfig中创建了:out_ExternalMessageDataConfig TYPE LINKLIST来填充ExternalMessageDataConfig。

    问题:

    • 如何编写ETL来填充out_ExternalMessageDataConfig?

    或者

    • 如何编写创建CREATE LINK查询?

    ORIENT DB版本:2.0.13

1 个答案:

答案 0 :(得分:3)

目标是:一旦将表从RDBMS导入到OrientDB(通过ETL工具),以下脚本允许您在具有多对多关系的各种实体之间创建链接(目前ETL不可能)。

默认方案涉及以下步骤/条件:

1)运行ETL导入3个表:表1(left_table),表2(right_table),表3(mm-table包含关系m:n的id)

2)在orient数据库中,将有三个包含数据的类

3)运行代码,该代码将创建连接left_table的边 - > table_right,取消mm-table现在无用的类,取消left_table和right_table的字段'id',因为在OrientDB id中不需要。

<强> JAVASCRIPT

Function with parameters: "leftTable" , "rightTable" , "mmTable" , "nameIdLeft" , "nameIdRight" , "nameEdge"

var g=orient.getGraphNoTx();
g.command("sql","CREATE CLASS " + nameEdge + " EXTENDS E");
var MM_table = g.command("sql","select from " + mmTable);
for(i=0;i<MM_table.length;i++){
var recordMM=g.command("sql","select " + nameIdLeft + " , " + nameIdRight + " from "+ MM_table[i].getId());
var idLeft=recordMM[0].getProperty(nameIdLeft);
var idRight=recordMM[0].getProperty(nameIdRight);
var left=g.command("sql","select from " + leftTable + " where " + nameIdLeft + " = " + idLeft);
var right=g.command("sql","select from " + rightTable + " where " + nameIdRight + " = " + idRight);
g.command("sql","create edge Left_Right from " + left[0].getId() + " to " + right[0].getId());
}
g.command("sql","drop class " + mmTable + " unsafe");
g.command("sql","UPDATE " + leftTable + " REMOVE " + nameIdLeft);
g.command("sql","UPDATE " + rightTable + " REMOVE " + nameIdRight);

来自工作室的电话:

select myFunction("Animal","Person","MMtable","idAnimal","idPerson","Left_Right")

<强> JAVA

public class ManyToMany {

    static final String REMOTE      = "remote:localhost/";
    static final String NOMEDB      = "MoltiAmolti";
    static final String CURRENTPATH = REMOTE + NOMEDB;

    public static void main(String[] args) throws IOException {

        OServerAdmin serverAdmin = new OServerAdmin(CURRENTPATH).connect("root", "root");

        OrientGraphNoTx g = new OrientGraphFactory(CURRENTPATH).getNoTx();

        createEdgeManyToMany(g, "Animal", "Person", "mmTable", "idAnimal", "idPerson", "Left_Right");
    }


    public static void createEdgeManyToMany (OrientGraphNoTx  g, String leftTable, String rightTable, String mmTable, 
                         String nameIdLeft, String nameIdRight, String nameEdge){

        System.out.println("Start processing...");

        //query on mmTable to save 2 id
        String getMMvertex = "select from "+mmTable;
        Iterable<Vertex> mm_vertex = g.command(new OSQLSynchQuery<Vertex>(getMMvertex)).execute();
        List<OrientVertex> listaVertex = new ArrayList<OrientVertex>();
        CollectionUtils.addAll(listaVertex, mm_vertex.iterator());

        //for each record
        String idLeft = ""; 
        String idRight = "";
        Iterable<Vertex> vertexLeft;
        Iterable<Vertex> vertexRight;
        String queryGetLeft = "";
        String queryGetRight = "";
        String queryEdge = "";
        String tmpIdleft = "";
        String tmpIdRight = "";

        //create class edge
        OClass edge = g.createEdgeType(nameEdge, "E");

        for (int i = 0; i<listaVertex.size(); i++) {
            System.out.println("Record Many-to-Many n°"+(i+1));
            idLeft = listaVertex.get(i).getProperty(nameIdLeft).toString();
            idRight = listaVertex.get(i).getProperty(nameIdRight).toString();

            queryGetLeft = "SELECT FROM " + leftTable + " WHERE " + nameIdLeft + " = " + idLeft;
            vertexLeft = g.command(new OCommandSQL(queryGetLeft)).execute();

            queryGetRight = "SELECT FROM " + rightTable + " WHERE " + nameIdRight + " = " + idRight;
            vertexRight = g.command(new OCommandSQL(queryGetRight)).execute();

            //create edge from vertex-left to vertex-right
            tmpIdleft = vertexRight.iterator().next().getId().toString();
            tmpIdRight = vertexLeft.iterator().next().getId().toString();
            queryEdge = "CREATE EDGE "+nameEdge+" FROM "+tmpIdleft+" TO "+tmpIdRight;
            g.command(new OCommandSQL(queryEdge)).execute();

            System.out.println("   - Created edge: "+queryEdge);
        }

        //remove id from origin vertex now useless
        g.command(new OCommandSQL("DROP PROPERTY "+leftTable+"."+nameIdLeft+" FORCE")).execute();
        g.command(new OCommandSQL("UPDATE "+leftTable+" REMOVE "+nameIdLeft+"")).execute();
        g.command(new OCommandSQL("DROP PROPERTY "+rightTable+"."+nameIdRight+" FORCE")).execute();
        g.command(new OCommandSQL("UPDATE "+rightTable+" REMOVE "+nameIdRight+"")).execute();
        System.out.println("Update old vertex eliminating id fields");
        g.command(new OCommandSQL("DROP class "+mmTable+" unsafe")).execute();
        System.out.println("Remove class many-to-many");

        System.out.println("End!"); 
        g.shutdown();
    }
}

enter image description here

修改

'WHITOUT EDGE BUT with LINKLIST'

好的,所以你想要拥有它的结果应该是这样的: enter image description here

代码没有太多改动,不同的是添加到LeftTable中的Linklist属性并创建了指向rightTable的链接。

JAVA CHANGES

public static void createEdgeManyToMany (OrientGraphNoTx  g, String leftTable, String rightTable, String mmTable, 
                                  String nameIdLeft, String nameIdRight, String link) {

    System.out.println("Start processing...");

    //query on mmTable to save 2 id
    String getMMvertex = "select from "+mmTable;
    Iterable<Vertex> mm_vertex = g.command(new OSQLSynchQuery<Vertex>(getMMvertex)).execute();
    List<OrientVertex> listaVertex = new ArrayList<OrientVertex>();
    CollectionUtils.addAll(listaVertex, mm_vertex.iterator());

    //for each record
    String idLeft = ""; 
    String idRight = "";
    Iterable<Vertex> vertexLeft;
    Iterable<Vertex> vertexRight;
    String queryGetLeft = "";
    String queryGetRight = "";
    String queryEdge = "";
    String tmpIdleft = "";
    String tmpIdRight = "";
    String queryLink = "";
    boolean created = false;

    for (int i = 0; i<listaVertex.size(); i++) {
        System.out.println("Record Many-to-Many n°"+(i+1));
        idLeft = listaVertex.get(i).getProperty(nameIdLeft).toString();
        idRight = listaVertex.get(i).getProperty(nameIdRight).toString();

        queryGetLeft = "SELECT FROM " + leftTable + " WHERE " + nameIdLeft + " = " + idLeft;
        vertexLeft = g.command(new OCommandSQL(queryGetLeft)).execute();

        queryGetRight = "SELECT FROM " + rightTable + " WHERE " + nameIdRight + " = " + idRight;
        vertexRight = g.command(new OCommandSQL(queryGetRight)).execute();

        //create edge from vertex-left to vertex-right
        tmpIdleft = vertexLeft.iterator().next().getId().toString();
        tmpIdRight = vertexRight.iterator().next().getId().toString();

        //add in the schema class the property link
        if (!created) {
            OClass leftTableClass = g.getRawGraph().getMetadata().getSchema().getClass(leftTable);
            OClass rightTableclass = g.getRawGraph().getMetadata().getSchema().getClass(rightTable);
            leftTableClass.createProperty(link, OType.LINKLIST, rightTableclass);
            created = true;
        }

        //without edge but with link
        queryLink = "update "+tmpIdleft+" ADD "+link+" = "+tmpIdRight;
        g.command(new OCommandSQL(queryLink)).execute();

        System.out.println("   - Created link: "+queryLink);
    }
    //remove id from origin vertex now useless
    g.command(new OCommandSQL("DROP PROPERTY "+leftTable+"."+nameIdLeft+" FORCE")).execute();
    g.command(new OCommandSQL("UPDATE "+leftTable+" REMOVE "+nameIdLeft+"")).execute();
    g.command(new OCommandSQL("DROP PROPERTY "+rightTable+"."+nameIdRight+" FORCE")).execute();
    g.command(new OCommandSQL("UPDATE "+rightTable+" REMOVE "+nameIdRight+"")).execute();
    System.out.println("Update old vertex eliminating id fields");
    g.command(new OCommandSQL("DROP class "+mmTable+" unsafe")).execute();
    System.out.println("Remove class many-to-many");

    System.out.println("End!"); 
    g.shutdown();
}

JAVASCRIPT CHANGES

参数:leftTable,rightTable,mmTable,nameIdLeft,nameIdRight,nameLink

var g=orient.getGraphNoTx();
g.command("sql","CREATE PROPERTY " + leftTable + "." + nameLink + " linklist " + rightTable);
var MM_table = g.command("sql","select from " + mmTable);
for(i=0;i<MM_table.length;i++){
    var recordMM=g.command("sql","select " + nameIdLeft + " , " + nameIdRight + " from "+ MM_table[i].getId());
    var idLeft=recordMM[i].getProperty(nameIdLeft);
    var idRight=recordMM[i].getProperty(nameIdRight);
    var left=g.command("sql","select from " + leftTable + " where " + nameIdLeft + " = " + idLeft);
    var right=g.command("sql","select from " + rightTable + " where " + nameIdRight + " = " + idRight);
    g.command("sql","update " + left[0].getId() + " add " + nameLink + " = [ " + right[0].getId()+ " ]");
}
g.command("sql","drop class " + mmTable + " unsafe");
g.command("sql","UPDATE " + leftTable + " REMOVE " + nameIdLeft);
g.command("sql","UPDATE " + rightTable + " REMOVE " + nameIdRight);