Search code examples
gremlingraph-databasesamazon-neptune

how to create vertices without matching property values in gremlin?


My data model as below:
node:customer_entity
~id telephone:String Name ~label
c1 969874293 Tien customer_entity
c1 987654145 Henry customer_entity
c1 954682174 Bobby customer_entity
node:membership_card
~id Card_No_:String created_at ~label
c1 969874293 22/01/2014 membership_card
c1 190019688 14/08/2009 membership_card
c1 954682174 08/12/2019 membership_card
I created phone_CDP node by using my query 
    g.V().hasLabel('customer_entity').
    as('phone_m').values('telephone').
    as('value_phone').
    addV('phone_CDP').
    property('id',select('value_phone')).
    addE('alias').to('phone_m') `

    g.V().hasLabel('membership_card').
    as('phone_l').values('Card_No_').as('value_phone').
    addV('phone_CDP').property('id',select('value_phone')).
    addE('alias').to('phone_l')
 my output:
id
969874293
987654145
954682174
969874293
190019688
954682174
 As you see i have two ids: 969874293 and 954682174 .So want to get one id value, how to do in my query? 
result i want :
id
969874293
987654145
954682174
190019688

Solution

  • You can create your sample graph with:

    g = TinkerGraph.open().traversal()
    
    g.addV('customer_entity').property('telephone', '969874293').
        addV('customer_entity').property('telephone', '987654145').
        addV('customer_entity').property('telephone', '954682174')
    
    g.addV('membership_card').property('Card_No_', '969874293').
        addV('membership_card').property('Card_No_', '190019688').
        addV('membership_card').property('Card_No_', '954682174')
    

    You can add the phone_CDP vertices with:

    g.V().hasLabel('customer_entity', 'membership_card').
        coalesce(values('telephone'),values('Card_No_')).as('phid').dedup().
        addV('phone_CDP').property('phoneId', select('phid')).elementMap()
    

    ==>[id:12,label:phone_CDP,phoneId:969874293]
    ==>[id:14,label:phone_CDP,phoneId:987654145]
    ==>[id:16,label:phone_CDP,phoneId:954682174]
    ==>[id:18,label:phone_CDP,phoneId:190019688]

    You can add the edges with:

    g.V().hasLabel('customer_entity', 'membership_card').as('vfrom').
        coalesce(values('telephone'),values('Card_No_')).as('phid').
      V().has('phoneId').as('vto').values('phoneId').where(eq('phid')).
      select('vfrom').addE('alias').to('vto')
    

    ==>e[20][0-alias->12]
    ==>e[21][2-alias->14]
    ==>e[22][4-alias->16]
    ==>e[23][6-alias->12]
    ==>e[24][8-alias->18]
    ==>e[25][10-alias->16]