Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions docs/libraries/java/info/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,66 @@ MyStringGraphInfoLoader customLoader = new MyStringGraphInfoLoader();
GraphInfo graphInfo = customLoader.loadGraphInfo(URI.create("db://mydatabase/graphs/graph1/graph.yml"));
```

### Save Graph Info

The java-info module also provides functionality to save graph metadata to YAML files using the `GraphSaver` interface. Here's an example of how to use it:

```java
import org.apache.graphar.info.GraphInfo;
import org.apache.graphar.info.saver.GraphInfoSaver;
import org.apache.graphar.info.saver.impl.LocalFileSystemYamlGraphSaver;
import java.net.URI;

// Create or obtain a GraphInfo object
GraphInfo graphInfo = createOrLoadGraphInfo(); // your method to create or load GraphInfo

// Create a GraphSaver instance
GraphInfoSaver graphSaver = new LocalFileSystemYamlGraphSaver();

// Save the graph info to a directory
String savePath = "/path/to/save/directory";
try {
graphSaver.save(URI.create(savePath), graphInfo);
System.out.println("Graph info saved successfully to " + savePath);
} catch (IOException e) {
System.err.println("Failed to save graph info: " + e.getMessage());
e.printStackTrace();
}
```

This will save the graph metadata as a set of YAML files:
- One main graph YAML file (e.g., `graph-name.graph.yaml`)
- One YAML file for each vertex type (e.g., `person.vertex.yaml`)
- One YAML file for each edge type (e.g., `person_knows_person.edge.yaml`)

Alternatively, you can use the dump method to convert graph info to a string and store it anywhere:

```java
import org.apache.graphar.info.GraphInfo;
import org.apache.graphar.info.EdgeInfo;
import org.apache.graphar.info.VertexInfo;
import java.net.URI;

// Create or obtain a GraphInfo object
GraphInfo graphInfo = createOrLoadGraphInfo(); // your method to create or load GraphInfo

// Set custom storage URIs for vertex and edge info files
for (VertexInfo vertexInfo : graphInfo.getVertexInfos()) {
graphInfo.setStoreUri(vertexInfo, URI.create("db://path/vertex/" + vertexInfo.getType() + ".vertex.yaml"));
}

for (EdgeInfo edgeInfo : graphInfo.getEdgeInfos()) {
graphInfo.setStoreUri(edgeInfo, URI.create("db://path/edge/" + edgeInfo.getConcat() + ".edge.yaml"));
}

// Convert graph info to YAML string
String graphYamlString = graphInfo.dump();

// Now you can store the YAML string anywhere you want
// For example, save to a database, send over network, etc.
saveYamlStringToDatabase(graphYamlString);
```

### Building

To build the graphar-info module, you need:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@
package org.apache.graphar.info;

import java.net.URI;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand All @@ -37,6 +35,23 @@ public class GraphInfo {
private final Map<String, VertexInfo> vertexType2VertexInfo;
private final Map<String, EdgeInfo> edgeConcat2EdgeInfo;
private final VersionInfo version;
private final Map<String, URI> types2StoreUri;

public GraphInfo(
String name,
Map<URI, VertexInfo> vertexInfos,
Map<URI, EdgeInfo> edgeInfos,
URI uri,
String version) {
this(
name,
new ArrayList<>(vertexInfos.values()),
new ArrayList<>(edgeInfos.values()),
uri,
version);
vertexInfos.forEach((key, value) -> types2StoreUri.put(value.getType() + ".vertex", key));
edgeInfos.forEach((key, value) -> types2StoreUri.put(value.getConcat() + ".edge", key));
Comment thread
yangxk1 marked this conversation as resolved.
}

public GraphInfo(
String name,
Expand Down Expand Up @@ -68,6 +83,7 @@ public GraphInfo(
.collect(
Collectors.toUnmodifiableMap(
EdgeInfo::getConcat, Function.identity()));
this.types2StoreUri = new HashMap<>();
}

private GraphInfo(
Expand Down Expand Up @@ -103,6 +119,13 @@ private GraphInfo(
this.version = version;
this.vertexType2VertexInfo = vertexType2VertexInfo;
this.edgeConcat2EdgeInfo = edgeConcat2EdgeInfo;
this.types2StoreUri = new HashMap<>();
}

public String dump(URI storeUri) {
Yaml yaml = new Yaml(GraphYaml.getRepresenter(), GraphYaml.getDumperOptions());
GraphYaml graphYaml = new GraphYaml(storeUri, this);
return yaml.dump(graphYaml);
}

public String dump() {
Expand Down Expand Up @@ -212,6 +235,34 @@ public VersionInfo getVersion() {
return version;
}

public void setStoreUri(VertexInfo vertexInfo, URI storeUri) {
this.types2StoreUri.put(vertexInfo.getType() + ".vertex", storeUri);
}

public void setStoreUri(EdgeInfo edgeInfo, URI storeUri) {
this.types2StoreUri.put(edgeInfo.getConcat() + ".edge", storeUri);
}

public URI getStoreUri(VertexInfo vertexInfo) {
String type = vertexInfo.getType() + ".vertex";
if (types2StoreUri.containsKey(type)) {
return types2StoreUri.get(type);
}
return URI.create(type + ".yaml");
}

public URI getStoreUri(EdgeInfo edgeInfo) {
String type = edgeInfo.getConcat() + ".edge";
if (types2StoreUri.containsKey(type)) {
return types2StoreUri.get(type);
}
return URI.create(type + ".yaml");
}

public Map<String, URI> getTypes2Uri() {
return types2StoreUri;
}

private void checkVertexExist(String type) {
if (!hasVertexInfo(type)) {
throw new IllegalArgumentException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@

import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;
import org.apache.graphar.info.EdgeInfo;
import org.apache.graphar.info.GraphInfo;
Expand All @@ -41,7 +41,7 @@ public abstract class BaseGraphInfoLoader implements GraphInfoLoader {

public abstract EdgeInfo loadEdgeInfo(URI edgeYamlUri) throws IOException;

public GraphInfo buildGraphInfoFromGraphYaml(URI baseUri, GraphYaml graphYaml)
protected GraphInfo buildGraphInfoFromGraphYaml(URI baseUri, GraphYaml graphYaml)
throws IOException {

URI defaultBaseUri = baseUri.resolve(".");
Expand All @@ -50,16 +50,16 @@ public GraphInfo buildGraphInfoFromGraphYaml(URI baseUri, GraphYaml graphYaml)
}

// load vertices
List<VertexInfo> vertexInfos = new ArrayList<>(graphYaml.getVertices().size());
Map<URI, VertexInfo> vertexInfos = new TreeMap<>();
for (String vertexYamlPath : graphYaml.getVertices()) {
URI vertexInfoUri = baseUri.resolve(vertexYamlPath);
vertexInfos.add(loadVertexInfo(vertexInfoUri));
vertexInfos.put(vertexInfoUri, loadVertexInfo(vertexInfoUri));
}
// load edges
List<EdgeInfo> edgeInfos = new ArrayList<>(graphYaml.getEdges().size());
Map<URI, EdgeInfo> edgeInfos = new TreeMap<>();
for (String edgeYamlPath : graphYaml.getEdges()) {
URI edgeInfoUri = baseUri.resolve(edgeYamlPath);
edgeInfos.add(loadEdgeInfo(edgeInfoUri));
edgeInfos.put(edgeInfoUri, loadEdgeInfo(edgeInfoUri));
}
return new GraphInfo(
graphYaml.getName(),
Expand All @@ -69,7 +69,7 @@ public GraphInfo buildGraphInfoFromGraphYaml(URI baseUri, GraphYaml graphYaml)
graphYaml.getVersion());
}

public VertexInfo buildVertexInfoFromGraphYaml(VertexYaml vertexYaml) {
protected VertexInfo buildVertexInfoFromVertexYaml(VertexYaml vertexYaml) {
return new VertexInfo(
vertexYaml.getType(),
vertexYaml.getChunk_size(),
Expand All @@ -80,7 +80,7 @@ public VertexInfo buildVertexInfoFromGraphYaml(VertexYaml vertexYaml) {
vertexYaml.getVersion());
}

public EdgeInfo buildEdgeInfoFromGraphYaml(EdgeYaml edgeYaml) {
protected EdgeInfo buildEdgeInfoFromEdgeYaml(EdgeYaml edgeYaml) {
return new EdgeInfo(
edgeYaml.getSrc_type(),
edgeYaml.getEdge_type(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ public VertexInfo loadVertexInfo(URI vertexYamlUri) throws IOException {
Reader yaml = readYaml(vertexYamlUri);
Yaml vertexYamlLoader = new Yaml(new Constructor(VertexYaml.class, new LoaderOptions()));
VertexYaml vertexYaml = vertexYamlLoader.load(yaml);
return buildVertexInfoFromGraphYaml(vertexYaml);
return buildVertexInfoFromVertexYaml(vertexYaml);
}

@Override
public EdgeInfo loadEdgeInfo(URI edgeYamlUri) throws IOException {
Reader yaml = readYaml(edgeYamlUri);
Yaml edgeYamlLoader = new Yaml(new Constructor(EdgeYaml.class, new LoaderOptions()));
EdgeYaml edgeYaml = edgeYamlLoader.load(yaml);
return buildEdgeInfoFromGraphYaml(edgeYaml);
return buildEdgeInfoFromEdgeYaml(edgeYaml);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ public VertexInfo loadVertexInfo(URI vertexYamlUri) throws IOException {
InputStream yaml = readYaml(vertexYamlUri);
Yaml edgeYamlLoader = new Yaml(new Constructor(VertexYaml.class, new LoaderOptions()));
Comment thread
yangxk1 marked this conversation as resolved.
Outdated
VertexYaml edgeYaml = edgeYamlLoader.load(yaml);
return buildVertexInfoFromGraphYaml(edgeYaml);
return buildVertexInfoFromVertexYaml(edgeYaml);
}

@Override
public EdgeInfo loadEdgeInfo(URI edgeYamlUri) throws IOException {
InputStream yaml = readYaml(edgeYamlUri);
Yaml edgeYamlLoader = new Yaml(new Constructor(EdgeYaml.class, new LoaderOptions()));
EdgeYaml edgeYaml = edgeYamlLoader.load(yaml);
return buildEdgeInfoFromGraphYaml(edgeYaml);
return buildEdgeInfoFromEdgeYaml(edgeYaml);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ public VertexInfo loadVertexInfo(URI vertexYamlUri) throws IOException {
String yaml = readYaml(vertexYamlUri);
Yaml vertexYamlLoader = new Yaml(new Constructor(VertexYaml.class, new LoaderOptions()));
VertexYaml vertexYaml = vertexYamlLoader.load(yaml);
return buildVertexInfoFromGraphYaml(vertexYaml);
return buildVertexInfoFromVertexYaml(vertexYaml);
}

@Override
public EdgeInfo loadEdgeInfo(URI edgeYamlUri) throws IOException {
String yaml = readYaml(edgeYamlUri);
Yaml edgeYamlLoader = new Yaml(new Constructor(EdgeYaml.class, new LoaderOptions()));
EdgeYaml edgeYaml = edgeYamlLoader.load(yaml);
return buildEdgeInfoFromGraphYaml(edgeYaml);
return buildEdgeInfoFromEdgeYaml(edgeYaml);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.graphar.info.saver;

import java.io.IOException;
import java.io.Writer;
import java.net.URI;
import org.apache.graphar.info.EdgeInfo;
import org.apache.graphar.info.GraphInfo;
import org.apache.graphar.info.VertexInfo;

public abstract class BaseGraphInfoSaver implements GraphInfoSaver {

public abstract Writer writeYaml(URI uri) throws IOException;

@Override
public void save(URI graphInfoUri, GraphInfo graphInfo) throws IOException {
Writer writer = writeYaml(graphInfoUri);
writer.write(graphInfo.dump(graphInfoUri));
writer.close();

for (VertexInfo vertexInfo : graphInfo.getVertexInfos()) {
URI saveUri = graphInfoUri.resolve(graphInfo.getStoreUri(vertexInfo));
save(saveUri, vertexInfo);
}
for (EdgeInfo edgeInfo : graphInfo.getEdgeInfos()) {
URI saveUri = graphInfoUri.resolve(graphInfo.getStoreUri(edgeInfo));
save(saveUri, edgeInfo);
}
}

@Override
public void save(URI vertexInfoUri, VertexInfo vertexInfo) throws IOException {
Writer vertexWriter = writeYaml(vertexInfoUri);
vertexWriter.write(vertexInfo.dump());
vertexWriter.close();
}

@Override
public void save(URI edgeInfoUri, EdgeInfo edgeInfo) throws IOException {
Writer edgeWriter = writeYaml(edgeInfoUri);
edgeWriter.write(edgeInfo.dump());
edgeWriter.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,15 @@
package org.apache.graphar.info.saver;

import java.io.IOException;
import java.net.URI;
import org.apache.graphar.info.EdgeInfo;
import org.apache.graphar.info.GraphInfo;
import org.apache.graphar.info.VertexInfo;

@FunctionalInterface
public interface GraphSaver {
void save(String path, GraphInfo graphInfo) throws IOException;
public interface GraphInfoSaver {
void save(URI graphInfoUri, GraphInfo graphInfo) throws IOException;

void save(URI vertexInfoUri, VertexInfo vertexInfo) throws IOException;

void save(URI edgeInfoUri, EdgeInfo edgeInfo) throws IOException;
}
Loading