Guest User

DgraphSample.java

a guest
Sep 11th, 2018
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.68 KB | None | 0 0
  1. package com.sample.dgraph;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Date;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. import java.util.Map;
  8.  
  9. import io.dgraph.DgraphClient;
  10. import io.dgraph.DgraphGrpc;
  11. import io.dgraph.DgraphProto.Operation;
  12. import io.dgraph.Transaction;
  13. import io.dgraph.DgraphGrpc.DgraphStub;
  14. import io.dgraph.DgraphProto.Assigned;
  15. import io.dgraph.DgraphProto.Mutation;
  16. import io.dgraph.DgraphProto.Response;
  17. import io.grpc.ManagedChannel;
  18. import io.grpc.ManagedChannelBuilder;
  19.  
  20. import org.json.JSONArray;
  21. import org.json.JSONObject;
  22. import org.junit.Test;
  23.  
  24. import com.google.protobuf.ByteString;
  25.  
  26. public class DgraphSample {
  27.    
  28.     private static String hostname;
  29.     private static int portNo;
  30.    
  31.     protected static ManagedChannel channel;
  32.     protected static DgraphClient dgraphClient;
  33.    
  34.     public void initializeDgraph() {
  35.         ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost",18271).usePlaintext(true).build();
  36.         DgraphStub stub = DgraphGrpc.newStub(channel);
  37.         DgraphClient dgraphClient = new DgraphClient(stub);
  38.         dgraphClient.alter(Operation.newBuilder().setDropAll(true).build());
  39.         Operation op = Operation.newBuilder().setSchema("name: string @index(term, trigram) . ").build();
  40.         dgraphClient.alter(op);
  41.     }
  42.    
  43.     public DgraphSample() {
  44.         hostname = "localhost";
  45.         portNo = 18271;
  46.  
  47.         if (channel == null || channel.isShutdown() || dgraphClient == null) {
  48.             channel =
  49.                     ManagedChannelBuilder.forAddress(hostname, portNo).usePlaintext(true).build();
  50.             DgraphStub stub = DgraphGrpc.newStub(channel);
  51.             dgraphClient = new DgraphClient(stub);
  52.         }
  53.     }
  54.    
  55.     public void createNodeListInBatch(List<Node> nodeList) {
  56.         Transaction txn = dgraphClient.newTransaction();
  57.         try {
  58.             int i = 0;
  59.             for (Node node : nodeList) {
  60.                 if (i % 3000 == 0) {
  61.                     txn.commit(); //txn commit because it gets destroy after some mutation
  62.                     txn.close();
  63.                     txn = dgraphClient.newTransaction();
  64.                 }
  65.                 i++;
  66.                
  67.                 JSONObject nodeJsonObj = new JSONObject();
  68.                 nodeJsonObj.put("name", node.getName());
  69.  
  70.                 Mutation mu = Mutation.newBuilder().setSetJson(ByteString.copyFromUtf8(nodeJsonObj.toString())).build();
  71.                 txn.mutate(mu);
  72.             }
  73.             txn.commit();
  74.            
  75.         } catch(Exception e) {
  76.             e.printStackTrace();
  77.         } finally {
  78.             txn.discard();
  79.         }
  80.     }
  81.    
  82.     public String createNode(Node node) {
  83.         Transaction txn = dgraphClient.newTransaction();
  84.         try {
  85.             JSONObject nodeJsonObj = new JSONObject();
  86.  
  87.             nodeJsonObj.put("name", node.getName());
  88.  
  89.             Mutation mu = Mutation.newBuilder().setSetJson(ByteString.copyFromUtf8(nodeJsonObj.toString())).build();
  90.             Assigned assigned = txn.mutate(mu);
  91.             txn.commit();
  92.  
  93.             return assigned.getUidsOrThrow("blank-0");
  94.         } catch(Exception e) {
  95.             e.printStackTrace();
  96.         } finally {
  97.             txn.discard();
  98.         }
  99.         return null;
  100.     }
  101.    
  102.     public Node getNodeByPartialSearch(String nodeName) {
  103.         if (nodeName == null || nodeName.equals("")) {
  104.             return null;
  105.         }
  106.         Node node = null;
  107.         Transaction txn = dgraphClient.newTransaction();
  108.         try {
  109.             String query = "query node($a: string) { node(func: allofterms(name, $a)) {uid name _predicate_ } }";
  110.  
  111.             Map<String, String> vars = new HashMap<>();
  112.             vars.put("$a", nodeName);
  113.  
  114.             Response resp = txn.queryWithVars(query, vars);
  115.  
  116.             JSONArray nodeArr = new JSONObject(resp.getJson().toStringUtf8()).optJSONArray("node");
  117.  
  118.             if (nodeArr != null && nodeArr.length() > 0) {
  119.                 for (int i=0; i<nodeArr.length(); i++) {
  120.                     JSONObject nodeObj = nodeArr.optJSONObject(i);
  121.                     if (nodeObj.optString("name").equalsIgnoreCase(nodeName)) {
  122.                         node = new Node(nodeObj.optString("uid"), nodeObj.optString("name"));
  123.                         break;
  124.                     }
  125.                 }
  126.             }
  127.         } catch(Exception e) {
  128.             e.printStackTrace();
  129.         } finally {
  130.             txn.discard();
  131.         }
  132.         return node;
  133.     }
  134.  
  135.     class Node {
  136.         String name;
  137.         String id;
  138.        
  139.         public Node(String name) {
  140.             this.name = name;
  141.         }
  142.        
  143.         public Node(String id, String name) {
  144.             this.id = id;
  145.             this.name = name;
  146.         }
  147.  
  148.         public String getId() {
  149.             return id;
  150.         }
  151.  
  152.         public void setId(String id) {
  153.             this.id = id;
  154.         }
  155.  
  156.         public String getName() {
  157.             return name;
  158.         }
  159.  
  160.         public void setName(String name) {
  161.             this.name = name;
  162.         }
  163.  
  164.         @Override
  165.         public String toString() {
  166.             return "Node [name=" + name + ", id=" + id + "]";
  167.         }
  168.     }
  169.    
  170.     @Test
  171.     public void createDGraphSampleData() {
  172.         DgraphSample dgraph = new DgraphSample();
  173.        
  174.         dgraph.initializeDgraph();
  175.        
  176.         List<Node> nodeList = new ArrayList<>();
  177.         for (int i=1; i<=50000; i++) {
  178.             Node node = new Node("India"+i);
  179.             nodeList.add(node);
  180.         }
  181.        
  182.         long startTime = new Date().getTime();
  183.         dgraph.createNodeListInBatch(nodeList);
  184.         long endTime = new Date().getTime();
  185.         long totalTime = endTime - startTime;
  186.         System.out.println("Total time taken to execute createNodeListInBatch " + totalTime);
  187.         //50k nodes Took 15.16 mins
  188.        
  189.         startTime = new Date().getTime();
  190.         for (int i=1; i<=50000; i++) {
  191.             Node node = new Node("Canada"+i);
  192.             dgraph.createNode(node);
  193.         }
  194.         endTime = new Date().getTime();
  195.         totalTime = endTime - startTime;
  196.         System.out.println("Total time taken to execute createNode one by one " + totalTime);
  197.         //50k nodes Took 72sec
  198.     }
  199.    
  200.     @Test
  201.     public void getDGraphSampleData() {
  202.         DgraphSample dgraph = new DgraphSample();
  203.        
  204.         long startTime = new Date().getTime();
  205.         for (int i=1; i<=50000; i++) {
  206.             dgraph.getNodeByPartialSearch("Canada"+i);
  207.             dgraph.getNodeByPartialSearch("India"+i);
  208.         }
  209.         long endTime = new Date().getTime();
  210.         long totalTime = endTime - startTime;
  211.         System.out.println("For All Node >> Total time taken to execute getNodeByPartialSearch " + totalTime);
  212.         //50k nodes Took 2.17 mins
  213.     }
  214. }
Add Comment
Please, Sign In to add comment