本文主要通過樣例代碼介紹如何使用只讀實例。

注意事項

  • 數據導入只能將數據導入主實例。
  • 通過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;
        }
    }