View Javadoc

1   /*
2    * Copyright 2015 Data Archiving and Networked Services (an institute of
3    * Koninklijke Nederlandse Akademie van Wetenschappen), King's College London,
4    * Georg-August-Universitaet Goettingen Stiftung Oeffentlichen Rechts
5    *
6    * Licensed under the EUPL, Version 1.1 or – as soon they will be approved by
7    * the European Commission - subsequent versions of the EUPL (the "Licence");
8    * You may not use this work except in compliance with the Licence.
9    * You may obtain a copy of the Licence at:
10   *
11   * https://joinup.ec.europa.eu/software/page/eupl
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the Licence is distributed on an "AS IS" basis,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the Licence for the specific language governing
17   * permissions and limitations under the Licence.
18   */
19  
20  package eu.ehri.project.tools;
21  
22  import com.fasterxml.jackson.core.JsonFactory;
23  import com.fasterxml.jackson.core.JsonGenerator;
24  import com.fasterxml.jackson.databind.ObjectMapper;
25  import com.google.common.collect.Lists;
26  import com.google.common.collect.Maps;
27  import com.tinkerpop.blueprints.Direction;
28  import com.tinkerpop.blueprints.Edge;
29  import com.tinkerpop.blueprints.Graph;
30  import com.tinkerpop.blueprints.Vertex;
31  import eu.ehri.project.models.annotations.EntityType;
32  import eu.ehri.project.persistence.Bundle;
33  
34  import java.io.IOException;
35  import java.io.OutputStream;
36  import java.util.List;
37  import java.util.Map;
38  
39  /**
40   * Tool for exporting a graph in a manner conducive
41   * to converting it to other formats, e.g. RDF triples,
42   * in a streaming manner.
43   */
44  public class JsonDataExporter {
45  
46      private static final JsonFactory jsonFactory = new JsonFactory();
47      private static final ObjectMapper jsonMapper = new ObjectMapper();
48  
49      /**
50       * Export the graph as JSON.
51       *
52       * @param graph  the graph database
53       * @param stream the output stream
54       */
55      public static void outputGraph(Graph graph, OutputStream stream)
56              throws IOException {
57  
58          try (JsonGenerator g = jsonFactory.createGenerator(stream)) {
59              g.writeStartArray();
60              for (Vertex vertex : graph.getVertices()) {
61  
62                  Map<String, Object> data = Maps.newHashMap();
63                  data.put(Bundle.ID_KEY, vertex.getProperty(EntityType.ID_KEY));
64                  data.put(Bundle.TYPE_KEY, vertex.getProperty(EntityType.TYPE_KEY));
65                  data.put(Bundle.DATA_KEY, getProperties(vertex));
66                  data.put(Bundle.REL_KEY, getRelations(vertex));
67  
68                  jsonMapper.writeValue(g, data);
69                  g.writeRaw('\n');
70              }
71              g.writeEndArray();
72          }
73      }
74  
75      private static Map<String, Object> getProperties(Vertex vertex) {
76          Map<String,Object> props = Maps.newHashMap();
77          for (String key : vertex.getPropertyKeys()) {
78              if (!key.startsWith("_")) {
79                  props.put(key, vertex.getProperty(key));
80              }
81          }
82          return props;
83      }
84  
85      private static Map<String,List<List<String>>> getRelations(Vertex vertex) {
86          Map<String,List<List<String>>> outRels = Maps.newHashMap();
87          for (Edge e : vertex.getEdges(Direction.OUT)) {
88              String label = e.getLabel();
89              Vertex other = e.getVertex(Direction.IN);
90              String oid = other.getProperty(EntityType.ID_KEY);
91              String otype = other.getProperty(EntityType.TYPE_KEY);
92              if (oid != null && otype != null) {
93                  List<String> ref = Lists.newArrayList(oid, otype);
94                  if (!outRels.containsKey(label)) {
95                      outRels.put(label, Lists.<List<String>>newArrayList());
96                  }
97  
98                  outRels.get(label).add(ref);
99              }
100         }
101         return outRels;
102     }
103  }