本文主要通过样例代码介绍如何使用只读实例。

注意事项

  • 数据导入只能将数据导入主实例。
  • 通过Gremlin DSL写数据只能将数据写入主实例。
  • 查询请求可以发送给主实例或只读实例。

样例代码

样例模拟社交场景,进行新增查询、建立关系、查询关系等操作。
  • 依赖包
    <dependency>
        <groupId>org.apache.tinkerpop</groupId>
        <artifactId>gremlin-driver</artifactId>
        <version>3.4.3</version>
    </dependency>
  • 实例信息
    主实例:
    host: $master_host
    
    只读实例1:
    host: $read_host1
    
    只读实例2:
    host: $read_host2
    
    账号信息:
    username: $username
    password: $password
  • 实例链接YAML文件
    • 主实例YAML文件 remote-master.yaml,用来初始化写客户端
      # remote-master.yaml
      hosts: [$master_host]
      port: $port
      username: $username
      password: $password
      connectionPool: {
        maxSize: 16,
        maxInProcessPerConnection: 2,
        maxContentLength: 81928192
      }
      serializer: { 
        className: org.apache.tinkerpop.gremlin.driver.ser.GraphBinaryMessageSerializerV1 
      }
    • 查询实例YAML文件 remote-read.yaml,可配置一批GDB实例来进行查询任务,客户端有负载均衡功能。
      # remote-read.yaml
      hosts: [$master_host,$read_host1,$read_host2]
      port: $port
      username: $username
      password: $password
      connectionPool: {
        maxSize: 48,
        maxInProcessPerConnection: 2,
        maxContentLength: 81928192
      }
      serializer: { 
        className: org.apache.tinkerpop.gremlin.driver.ser.GraphBinaryMessageSerializerV1 
      }
  • 参考代码
    package com.alibaba.gdb.test;
    
    import org.apache.tinkerpop.gremlin.driver.*;
    
    import java.io.File;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    /**
     * @author xxx
     * @date 2021/01/19
     */
    public class ReadInstance {
        private static Cluster masterCluster = null;
        private static Client  masterClient = null;
    
        private static Cluster readCluster = null;
        private static Client  readClient = null;
    
        private static final String masterYamlFile = "remote-master.yaml";
        private static final String readYamlFile = "remote-read.yaml";
    
        // 返回写client
        public Client getMasterClient() throws Exception {
            if (masterClient == null) {
                File file = new File(masterYamlFile);
                masterCluster = Cluster.build(file).create();
                masterClient = masterCluster.connect().init();
            }
            return masterClient;
        }
    
        // 返回查询client
        public Client getReadClient() throws Exception {
            if (readClient == null) {
                File file = new File(readYamlFile);
                readCluster = Cluster.build(file).create();
                readClient = readCluster.connect().init();
            }
            return readClient;
        }
    
        // 新写入一个用户
        public boolean addUser(String userId, String name, int age, double height, String city) throws Exception {
            String dsl = "g.addV('user').property(id, userId).property('name', name).property('age', age).property('height', height).property('city', city)";
            Map<String , Object> map = new HashMap<>();
            map.put("userId", userId);
            map.put("name", name);
            map.put("age", age);
            map.put("height", height);
            map.put("city", city);
            List<Result> resultList = getMasterClient().submit(dsl, map).all().join();
            return true;
        }
    
        // 新建立两个用户的关系
        public boolean addReleationship(String userId1, String userId2, long timeStamp) throws Exception {
            String dsl = "g.addE('friend').from(V(userId1)).to(V(userId2)).property('timeStamp', timeStamp)";
            Map<String , Object> map = new HashMap<>();
            map.put("userId1", userId1);
            map.put("userId2", userId2);
            map.put("timeStamp", timeStamp);
            List<Result> resultList = getMasterClient().submit(dsl, map).all().join();
            return true;
        }
    
        // 查询某个用户所有的好友中和自己年龄一行的好友,返回好友的信息
        public List<Result> querySameAgeFriend(String userId) throws Exception {
            String dsl = "g.V().hasId(userId).as('a').both('friend').where(eq('a')).by('age').valueMap()";
            Map<String , Object> map = new HashMap<>();
            map.put("userId", userId);
            List<Result> resultList = getReadClient().submit(dsl, map).all().join();
            return resultList;
        }
    
        // 查询用户好友的好友, 哪些还不是自己的好友,返回他们的信息
        public List<Result> query2HopFriend(String userId) throws Exception {
            String dsl = "g.V().hasId(userId).both('friend').aggregate('my_friend').both('friend').where(without('my_friend')).valueMap()";
            Map<String , Object> map = new HashMap<>();
            map.put("userId", userId);
            List<Result> resultList = getReadClient().submit(dsl, map).all().join();
            return resultList;
        }
    }