Social networking

Updated at:

Social networking scenarios use highly connected data models. Graph Database (GDB) offers native graph model support for social applications, which helps you better match and understand your data. GDB can significantly improve the development efficiency and quality of social applications while reducing the overhead from data model conversions.

1. Data model

This topic uses the public Twitter social relationship dataset as an example. For more information, see the data model reference download.

GDB uses a property graph model to represent and process data. The data model is abstracted as shown in the following figure:
Note
  • User: A node that represents a user.
  • Follow: An edge that represents a one-way relationship, such as a user following another user.
This example uses the following sample data:
  • Vertex file:
    ~id,name:string
    1,a0e05a4a-65c0-11e9-a5ce-00163e0416f8
    2,a0e05e28-65c0-11e9-a5ce-00163e0416f8
    3,a0e05f86-65c0-11e9-a5ce-00163e0416f8
    4,a0e0606c-65c0-11e9-a5ce-00163e0416f8
    5,a0e0613e-65c0-11e9-a5ce-00163e0416f8
    6,a0e06206-65c0-11e9-a5ce-00163e0416f8
  • Edge file:
    ~id,~from,~to,weight:double
    212416660,1116299,4377946,0.443259
    212416661,1116300,4377946,0.0303036
    212416662,181406,4377946,0.753659
    212416663,4084735,4377946,0.991974
    212416664,1937755,4377946,0.79248

2. Create an instance

You can create a GDB instance. For more information, see Create a primary instance.
Note
  • After the instance is created, you can view its information on the Instance List page. It typically takes 3 to 5 minutes to create an instance.
  • After you create a GDB instance, you must create an account and password and configure a security group to obtain access permissions for the instance. For more information, see Create an account and Configure a whitelist.

3. Data import

GDB supports importing data from multiple data sources. You can use one of the following methods to import data:

4. Connect to an instance

You can connect to a GDB instance using one of the following five methods:

5. Usage examples

  • Simple queries
    • Data statistics:
      // Count the number of vertices.
      gremlin> g.V().count()
      ==>41999999
      
      // Count the number of edges.
      gremlin> g.E().count()
      ==>22191165
    • Filter queries and sort queries:
      // Query the user whose name is a0e05a4a-65c0-11e9-a5ce-00163e0416f8.
      gremlin> g.V().has('name','a0e05a4a-65c0-11e9-a5ce-00163e0416f8').valueMap(true)   // This is a conditional query, similar to SELECT ... WHERE .... Modify the content in has() as needed.
      ==>[id:1,label:vertex,name:[a0e05a4a-65c0-11e9-a5ce-00163e0416f8]]
      
      // Query the follow list of the user whose name is a0e05a4a-65c0-11e9-a5ce-00163e0416f8.
      gremlin> g.V().has('name','a0e05a4a-65c0-11e9-a5ce-00163e0416f8').outE().valueMap(true) // This is a conditional query, similar to SELECT ... WHERE .... Modify the content in has() as needed.
      ==>[id:217089344,label:edge,weight:0.769055]
      ==>[id:220429042,label:edge,weight:0.290449]
      ==>[id:227652991,label:edge,weight:0.962171]
      ==>[id:234881614,label:edge,weight:0.0887247]
      ==>[id:250193757,label:edge,weight:0.756271]
      ==>[id:252223359,label:edge,weight:0.990445]
      ==>[id:252494754,label:edge,weight:0.494867]
      ==>[id:254012304,label:edge,weight:0.788503]
      ==>[id:260893506,label:edge,weight:0.247677]
      ==>[id:228404583,label:edge,weight:0.0742597]
      ==>[id:243912806,label:edge,weight:0.906016]
      ==>[id:262031400,label:edge,weight:0.649892]
      
      // Query the follow list of the user whose name is a0e05a4a-65c0-11e9-a5ce-00163e0416f8 and sort the list by weight in descending order.
      gremlin> g.V().has('name','a0e05a4a-65c0-11e9-a5ce-00163e0416f8').outE().order().by('weight', decr).valueMap(true) // The has() part controls the query condition, and the order().by() part controls the sorting condition.
      ==>[id:252223359,label:edge,weight:0.990445]
      ==>[id:227652991,label:edge,weight:0.962171]
      ==>[id:243912806,label:edge,weight:0.906016]
      ==>[id:254012304,label:edge,weight:0.788503]
      ==>[id:217089344,label:edge,weight:0.769055]
      ==>[id:250193757,label:edge,weight:0.756271]
      ==>[id:262031400,label:edge,weight:0.649892]
      ==>[id:310064671,label:edge,weight:0.639453]
      ==>[id:316084412,label:edge,weight:0.595669]
      ==>[id:277559997,label:edge,weight:0.538571]
      ==>[id:252494754,label:edge,weight:0.494867]
      ==>[id:291708246,label:edge,weight:0.387777]
      ==>[id:281304400,label:edge,weight:0.382627]
      ==>[id:310008546,label:edge,weight:0.333313]
      ==>[id:220429042,label:edge,weight:0.290449]
      ==>[id:260893506,label:edge,weight:0.247677]
      ==>[id:300018487,label:edge,weight:0.228707]
      ==>[id:234881614,label:edge,weight:0.0887247]
      ==>[id:289510146,label:edge,weight:0.078113]
      ==>[id:228404583,label:edge,weight:0.0742597]
  • Common scenarios:
    • k-hop neighbors:
      // Query the 2-hop follow relationship for user 23.
      gremlin> g.V(23).repeat(outE('edge').otherV().simplePath()).times(2).path() // The outE() part controls the edge type to query, and the times() part controls the query depth.
      ==>[v[23],e[239197952][23-edge->19201],v[19201],e[229218134][19201-edge->18246],v[18246]]
      ==>[v[23],e[239197952][23-edge->19201],v[19201],e[216091024][19201-edge->2586961],v[2586961]]
      ==>[v[23],e[267069904][23-edge->2587481],v[2587481],e[225145280][2587481-edge->1018015],v[1018015]]
      ==>[v[23],e[267069904][23-edge->2587481],v[2587481],e[284567757][2587481-edge->1022162],v[1022162]]
      ==>[v[23],e[267069904][23-edge->2587481],v[2587481],e[235313604][2587481-edge->10467758],v[10467758]]
      ==>[v[23],e[267069904][23-edge->2587481],v[2587481],e[265280161][2587481-edge->15350178],v[15350178]]
      ==>[v[23],e[267069904][23-edge->2587481],v[2587481],e[280654355][2587481-edge->1796334],v[1796334]]
      ......
    • Shortest path:
      // Query the shortest path between user 23 and user 5924864 with a maximum depth of 2.
      gremlin> g.V(23).repeat(bothE().otherV().simplePath())
                  .until(hasId(5924864).or().loops().is(gt(2L)))  // The hasId() part controls the end ID, and the gt() part controls the query depth.
                      .hasId(5924864).path().dedup()
      ==>[v[23],v[2587481],v[5924864]]
      5.4
    • Common neighbors:
      // Query the common neighbors of user 23 and user 5924864.
      gremlin> g.V(23).repeat(bothE().otherV().simplePath()).times(2).hasId(5924864).path().dedup()  // The hasId() part controls the end ID.
      ==>[v[23],e[267069904][23-edge->2587481],v[2587481],e[267070009][5924864-edge->2587481],v[5924864]]
    • Finding top influencers:
      // In social networking, top influencers are often measured by the number of followers. More followers mean more popularity. Find the three users with the most followers.
      gremlin> g.V().project('user','degree').by().by(inE().count()).order().by(select('degree'), desc).limit(3) // The by(inE().count()) part controls the counting logic. The order().by(select('degree'), desc).limit(3) part controls the sorting logic.
      ==>[v[23],e[267069904][23-edge->2587481],v[2587481],e[267070009][5924864-edge->2587481],v[5924864]]
      ==>[v[23],e[267069904][23-edge->2587481],v[2587481],e[316011732][2587481-edge->5924864],v[5924864]]
      ==>[user:v[1],degree:1090]
      ==>[user:v[24],degree:890]
      ==>[user:v[65],degree:768]
    • Collaborative recommendation:
      // Recommend users who have common neighbors with user 1, and sort them by the number of common neighbors.
      gremlin> g.V(1).both().aggregate("my_friend").both().has(id, neq(1)).as("ff")
                  .flatMap(__.both().where(within("my_friend")).count()).as("comm_cnt").order().by(desc)
                      .select("ff", "comm_cnt").dedup()
      ==>[ff:v[591712],comm_cnt:10]
      ==>[ff:v[60911],comm_cnt:10]
      ==>[ff:v[4470],comm_cnt:10]
      ==>[ff:v[47129],comm_cnt:10]
      ==>[ff:v[1],comm_cnt:10]
      ==>[ff:v[316284],comm_cnt:10]
      ==>[ff:v[472652],comm_cnt:9]
      ==>[ff:v[52057],comm_cnt:9]
      ==>[ff:v[531386],comm_cnt:9]
      ...

6. Customer outcomes

A social media company managed a graph with 100 million vertices (user data) and 1.6 billion edges (social relationships). The company previously used a 32 core, 256 GB MySQL database for storage and experienced low query performance, which often resulted in query timeouts. Using GDB, they built a social graph with users as vertices, user online status as properties, and friend relationships as edges. This improved their query performance by more than 100 times and reduced response times from timing out to completing in milliseconds.