private Collection<String> exportIndexes() { final List<String> result = new ArrayList<>(); for ( IndexDefinition index : graph.getIndexes() ) { if ( !index.isConstraintIndex() ) { Iterator<String> propertyKeys = index.getPropertyKeys().iterator(); if ( !propertyKeys.hasNext() ) { throw new IllegalStateException( "Indexes should have at least one property key" ); } String id = propertyKeys.next(); if ( propertyKeys.hasNext() ) { throw new RuntimeException( "Exporting compound indexes is not implemented yet" ); } if ( !index.isMultiTokenIndex() ) { String key = quote( id ); String label = quote( single( index.getLabels() ).name() ); result.add( "create index on :" + label + "(" + key + ")" ); } // We don't know how to deal with multi-token indexes here, so we just ignore them. } } Collections.sort( result ); return result; }
private long appendNodes( PrintWriter out ) { long nodes = 0; for ( Node node : graph.getNodes() ) { appendNode( out, node ); nodes++; } return nodes; }
private Collection<String> exportConstraints() Iterable<ConstraintDefinition> constraints = graph.getConstraints(); int count = 0; if ( constraints instanceof Collection )
@Override public ProgressInfo dump(SubGraph graph, Writer writer, Reporter reporter, ExportConfig config) throws Exception { Consumer<JsonGenerator> consumer = (jsonGenerator) -> { try { writeNodes(graph.getNodes(), reporter, jsonGenerator, config); writeRels(graph.getRelationships(), reporter, jsonGenerator, config); } catch (IOException e) { throw new RuntimeException(e); } }; return dump(writer, reporter, consumer); }
public void write(SubGraph graph, Output output, Reporter reporter, Config config) throws IOException { for (ConstraintDefinition constraintDefinition : graph.getConstraints()) { writeConstraint(output, constraintDefinition); } for (IndexDefinition indexDefinition : graph.getIndexes()) { writeIndex(output, indexDefinition); } for (Node node : graph.getNodes()) { int props = writeNode(output, node); reporter.update(1, 0, props); } for (Relationship rel : graph.getRelationships()) { int props = writeRelationship(output, rel); reporter.update(0, 1, props); } // WRITE END DUMP kryo.writeObject(output, KryoSerializationTypes.DUMP_END.name()); }
public static Map<String,Class> collectPropTypesForRelationships(SubGraph graph) { Map<String,Class> propTypes = new LinkedHashMap<>(); for (Relationship node : graph.getRelationships()) { updateKeyTypes(propTypes, node); } return propTypes; }
private void writeKey(XMLStreamWriter writer, SubGraph ops, ExportConfig config) throws Exception { Map<String, Class> keyTypes = new HashMap<>(); for (Node node : ops.getNodes()) { if (node.getLabels().iterator().hasNext()) { keyTypes.put("label", String.class); } updateKeyTypes(keyTypes, node); } boolean useTypes = config.useTypes(); ExportFormat format = config.getFormat(); if (format == ExportFormat.GEPHI) { keyTypes.put("TYPE", String.class); } writeKey(writer, keyTypes, "node", useTypes); keyTypes.clear(); for (Relationship rel : ops.getRelationships()) { keyTypes.put("label", String.class); updateKeyTypes(keyTypes, rel); } if (format == ExportFormat.GEPHI) { keyTypes.put("TYPE", String.class); } writeKey(writer, keyTypes, "edge", useTypes); }
private void exportRelationships(PrintWriter out, Reporter reporter, int batchSize) { if (graph.getRelationships().iterator().hasNext()) { begin(out); appendRelationships(out, batchSize, reporter); commit(out); out.flush(); } }
public void write(SubGraph graph, Writer writer, Reporter reporter, ExportConfig config) throws Exception { XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance(); XMLStreamWriter xmlWriter = xmlOutputFactory.createXMLStreamWriter(writer); writeHeader(xmlWriter); writeKey(xmlWriter, graph, config); writeGraph(xmlWriter); for (Node node : graph.getNodes()) { int props = writeNode(xmlWriter, node, config); reporter.update(1, 0, props); } for (Relationship rel : graph.getRelationships()) { int props = writeRelationship(xmlWriter, rel, config); reporter.update(0, 1, props); } writeFooter(xmlWriter); }
private long appendRelationships( PrintWriter out ) { long relationships = 0; for ( Node node : graph.getNodes() ) { for ( Relationship rel : node.getRelationships( Direction.OUTGOING ) ) { appendRelationship( out, rel ); relationships++; } } return relationships; }
private List<String> indexesAwait() { List<String> result = new ArrayList<>(); for (IndexDefinition index : graph.getIndexes()) { String label = index.getLabel().name(); String indexAwait = this.exportFormat.indexAwait(label, index.getPropertyKeys()); if (indexAwait != null && !"".equals(indexAwait)) result.add(indexAwait); } return result; }
private long appendRelationships(PrintWriter out, int batchSize, Reporter reporter) { long count = 0; for (Relationship rel : graph.getRelationships()) { if (count > 0 && count % batchSize == 0) restart(out); count++; appendRelationship(out, rel, reporter); } return count; }
private Collection<String> exportConstraints() Iterable<ConstraintDefinition> constraints = graph.getConstraints(); int count = 0; if ( constraints instanceof Collection )
@Procedure @Description("apoc.export.graphml.query(query,file,config) - exports nodes and relationships from the cypher statement as graphml to the provided file") public Stream<ProgressInfo> query(@Name("query") String query, @Name("file") String fileName, @Name("config") Map<String, Object> config) throws Exception { ExportConfig c = new ExportConfig(config); Result result = db.execute(query); SubGraph graph = CypherResultSubGraph.from(result, db, c.getRelsInBetween()); String source = String.format("statement: nodes(%d), rels(%d)", Iterables.count(graph.getNodes()), Iterables.count(graph.getRelationships())); return exportGraphML(fileName, source, graph, c); }
public static Map<String,Class> collectPropTypesForNodes(SubGraph graph) { Map<String,Class> propTypes = new LinkedHashMap<>(); for (Node node : graph.getNodes()) { updateKeyTypes(propTypes, node); } return propTypes; } public static Map<String,Class> collectPropTypesForRelationships(SubGraph graph) {
private void gatherUniqueConstraints() { for (IndexDefinition indexDefinition : graph.getIndexes()) { String label = indexDefinition.getLabel().name(); String prop = Iterables.first(indexDefinition.getPropertyKeys()); indexNames.add(label); indexedProperties.add(prop); if (indexDefinition.isConstraintIndex()) { if (!uniqueConstraints.containsKey(label)) uniqueConstraints.put(label, prop); } } }
private void writeRels(SubGraph graph, CSVWriter out, Reporter reporter, Map<String, Class> relPropTypes, int cols, int offset, int batchSize, String delimiter) { String[] row=new String[cols]; int rels = 0; for (Relationship rel : graph.getRelationships()) { row[offset]=String.valueOf(rel.getStartNode().getId()); row[offset+1]=String.valueOf(rel.getEndNode().getId()); row[offset+2]=rel.getType().name(); collectProps(relPropTypes.keySet(), rel, reporter, row, 3 + offset, delimiter); out.writeNext(row, applyQuotesToAll); rels++; if (batchSize==-1 || rels % batchSize == 0) { reporter.update(0, rels, 0); rels = 0; } } if (rels > 0) { reporter.update(0, rels, 0); } } }
@Procedure @Description("apoc.export.cypher.query(query,file,config) - exports nodes and relationships from the cypher statement incl. indexes as cypher statements to the provided file") public Stream<DataProgressInfo> query(@Name("query") String query, @Name(value = "file",defaultValue = "") String fileName, @Name(value = "config",defaultValue = "{}") Map<String, Object> config) throws IOException { if (Util.isNullOrEmpty(fileName)) fileName=null; ExportConfig c = new ExportConfig(config); Result result = db.execute(query); SubGraph graph = CypherResultSubGraph.from(result, db, c.getRelsInBetween()); String source = String.format("statement: nodes(%d), rels(%d)", Iterables.count(graph.getNodes()), Iterables.count(graph.getRelationships())); return exportCypher(fileName, source, graph, c, false); }
private void exportNodes(PrintWriter out, Reporter reporter, int batchSize) { if (graph.getNodes().iterator().hasNext()) { begin(out); appendNodes(out, batchSize, reporter); commit(out); out.flush(); } }
private List<String> exportIndexes() { List<String> result = new ArrayList<>(); for (IndexDefinition index : graph.getIndexes()) { String label = index.getLabel().name(); Iterable<String> props = index.getPropertyKeys(); if (index.isConstraintIndex()) { String cypher = this.cypherFormat.statementForConstraint(label, Iterables.single(props)); if (cypher != null && !"".equals(cypher)) { result.add(cypher); } } else { String cypher = this.cypherFormat.statementForIndex(label, props); if (cypher != null && !"".equals(cypher)) { result.add(0, cypher); } } } return result; }