use of com.tinkerpop.blueprints.util.wrappers.batch.BatchGraph in project blueprints by tinkerpop.
the class GraphSONReader method inputGraph.
/**
* Input the JSON stream data into the graph.
* More control over how data is streamed is provided by this method.
*
* @param inputGraph the graph to populate with the JSON data
* @param jsonInputStream an InputStream of JSON data
* @param bufferSize the amount of elements to hold in memory before committing a transactions (only valid for TransactionalGraphs)
* @throws IOException thrown when the JSON data is not correctly formatted
*/
public static void inputGraph(final Graph inputGraph, final InputStream jsonInputStream, int bufferSize, final Set<String> edgePropertyKeys, final Set<String> vertexPropertyKeys) throws IOException {
if (jsonInputStream == null) {
throw new IllegalArgumentException("InputStream must not be null");
}
final JsonParser jp = jsonFactory.createJsonParser(jsonInputStream);
// if this is a transactional graph then we're buffering
final BatchGraph graph = BatchGraph.wrap(inputGraph, bufferSize);
final ElementFactory elementFactory = new GraphElementFactory(graph);
GraphSONUtility graphson = new GraphSONUtility(GraphSONMode.NORMAL, elementFactory, vertexPropertyKeys, edgePropertyKeys);
while (jp.nextToken() != JsonToken.END_OBJECT) {
final String fieldname = jp.getCurrentName() == null ? "" : jp.getCurrentName();
if (fieldname.equals(GraphSONTokens.MODE)) {
jp.nextToken();
final GraphSONMode mode = GraphSONMode.valueOf(jp.getText());
graphson = new GraphSONUtility(mode, elementFactory, vertexPropertyKeys, edgePropertyKeys);
} else if (fieldname.equals(GraphSONTokens.VERTICES)) {
jp.nextToken();
while (jp.nextToken() != JsonToken.END_ARRAY) {
final JsonNode node = jp.readValueAsTree();
graphson.vertexFromJson(node);
}
} else if (fieldname.equals(GraphSONTokens.EDGES)) {
jp.nextToken();
while (jp.nextToken() != JsonToken.END_ARRAY) {
final JsonNode node = jp.readValueAsTree();
final Vertex inV = graph.getVertex(GraphSONUtility.getTypedValueFromJsonNode(node.get(GraphSONTokens._IN_V)));
final Vertex outV = graph.getVertex(GraphSONUtility.getTypedValueFromJsonNode(node.get(GraphSONTokens._OUT_V)));
graphson.edgeFromJson(node, outV, inV);
}
}
}
jp.close();
graph.commit();
;
}
use of com.tinkerpop.blueprints.util.wrappers.batch.BatchGraph in project blueprints by tinkerpop.
the class GraphMLReader method inputGraph.
/**
* Input the GraphML stream data into the graph.
* More control over how data is streamed is provided by this method.
*
* @param inputGraph the graph to populate with the GraphML data
* @param graphMLInputStream an InputStream of GraphML data
* @param bufferSize the amount of elements to hold in memory before committing a transactions (only valid for TransactionalGraphs)
* @param vertexIdKey if the id of a vertex is a <data/> property, fetch it from the data property.
* @param edgeIdKey if the id of an edge is a <data/> property, fetch it from the data property.
* @param edgeLabelKey if the label of an edge is a <data/> property, fetch it from the data property.
* @throws IOException thrown when the GraphML data is not correctly formatted
*/
public static void inputGraph(final Graph inputGraph, final InputStream graphMLInputStream, int bufferSize, String vertexIdKey, String edgeIdKey, String edgeLabelKey) throws IOException {
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
try {
XMLStreamReader reader = inputFactory.createXMLStreamReader(graphMLInputStream);
final BatchGraph graph = BatchGraph.wrap(inputGraph, bufferSize);
Map<String, String> keyIdMap = new HashMap<String, String>();
Map<String, String> keyTypesMaps = new HashMap<String, String>();
// <Mapped ID String, ID Object>
// <Default ID String, Mapped ID String>
Map<String, String> vertexMappedIdMap = new HashMap<String, String>();
// Buffered Vertex Data
String vertexId = null;
Map<String, Object> vertexProps = null;
boolean inVertex = false;
// Buffered Edge Data
String edgeId = null;
String edgeLabel = null;
//[0] = outVertex , [1] = inVertex
Vertex[] edgeEndVertices = null;
Map<String, Object> edgeProps = null;
boolean inEdge = false;
while (reader.hasNext()) {
Integer eventType = reader.next();
if (eventType.equals(XMLEvent.START_ELEMENT)) {
String elementName = reader.getName().getLocalPart();
if (elementName.equals(GraphMLTokens.KEY)) {
String id = reader.getAttributeValue(null, GraphMLTokens.ID);
String attributeName = reader.getAttributeValue(null, GraphMLTokens.ATTR_NAME);
String attributeType = reader.getAttributeValue(null, GraphMLTokens.ATTR_TYPE);
keyIdMap.put(id, attributeName);
keyTypesMaps.put(id, attributeType);
} else if (elementName.equals(GraphMLTokens.NODE)) {
vertexId = reader.getAttributeValue(null, GraphMLTokens.ID);
if (vertexIdKey != null)
vertexMappedIdMap.put(vertexId, vertexId);
inVertex = true;
vertexProps = new HashMap<String, Object>();
} else if (elementName.equals(GraphMLTokens.EDGE)) {
edgeId = reader.getAttributeValue(null, GraphMLTokens.ID);
edgeLabel = reader.getAttributeValue(null, GraphMLTokens.LABEL);
edgeLabel = edgeLabel == null ? GraphMLTokens._DEFAULT : edgeLabel;
String[] vertexIds = new String[2];
vertexIds[0] = reader.getAttributeValue(null, GraphMLTokens.SOURCE);
vertexIds[1] = reader.getAttributeValue(null, GraphMLTokens.TARGET);
edgeEndVertices = new Vertex[2];
for (int i = 0; i < 2; i++) {
//i=0 => outVertex, i=1 => inVertex
if (vertexIdKey == null) {
edgeEndVertices[i] = graph.getVertex(vertexIds[i]);
} else {
edgeEndVertices[i] = graph.getVertex(vertexMappedIdMap.get(vertexIds[i]));
}
if (null == edgeEndVertices[i]) {
edgeEndVertices[i] = graph.addVertex(vertexIds[i]);
if (vertexIdKey != null)
// Default to standard ID system (in case no mapped
// ID is found later)
vertexMappedIdMap.put(vertexIds[i], vertexIds[i]);
}
}
inEdge = true;
edgeProps = new HashMap<String, Object>();
} else if (elementName.equals(GraphMLTokens.DATA)) {
String key = reader.getAttributeValue(null, GraphMLTokens.KEY);
String attributeName = keyIdMap.get(key);
if (attributeName != null) {
String value = reader.getElementText();
if (inVertex == true) {
if ((vertexIdKey != null) && (key.equals(vertexIdKey))) {
// Should occur at most once per Vertex
// Assumes single ID prop per Vertex
vertexMappedIdMap.put(vertexId, value);
vertexId = value;
} else
vertexProps.put(attributeName, typeCastValue(key, value, keyTypesMaps));
} else if (inEdge == true) {
if ((edgeLabelKey != null) && (key.equals(edgeLabelKey)))
edgeLabel = value;
else if ((edgeIdKey != null) && (key.equals(edgeIdKey)))
edgeId = value;
else
edgeProps.put(attributeName, typeCastValue(key, value, keyTypesMaps));
}
}
}
} else if (eventType.equals(XMLEvent.END_ELEMENT)) {
String elementName = reader.getName().getLocalPart();
if (elementName.equals(GraphMLTokens.NODE)) {
Vertex currentVertex = graph.getVertex(vertexId);
if (currentVertex == null) {
currentVertex = graph.addVertex(vertexId);
}
for (Entry<String, Object> prop : vertexProps.entrySet()) {
currentVertex.setProperty(prop.getKey(), prop.getValue());
}
vertexId = null;
vertexProps = null;
inVertex = false;
} else if (elementName.equals(GraphMLTokens.EDGE)) {
Edge currentEdge = graph.addEdge(edgeId, edgeEndVertices[0], edgeEndVertices[1], edgeLabel);
for (Entry<String, Object> prop : edgeProps.entrySet()) {
currentEdge.setProperty(prop.getKey(), prop.getValue());
}
edgeId = null;
edgeLabel = null;
edgeEndVertices = null;
edgeProps = null;
inEdge = false;
}
}
}
reader.close();
graph.commit();
;
} catch (XMLStreamException xse) {
throw new IOException(xse);
}
}
use of com.tinkerpop.blueprints.util.wrappers.batch.BatchGraph in project blueprints by tinkerpop.
the class GMLReader method inputGraph.
/**
* Load the GML file into the Graph.
*
* @param inputGraph to receive the data
* @param inputStream GML file
* @param defaultEdgeLabel default edge label to be used if not defined in the data
* @param vertexIdKey if the id of a vertex is a <data/> property, fetch it from the data property.
* @param edgeIdKey if the id of an edge is a <data/> property, fetch it from the data property.
* @param edgeLabelKey if the label of an edge is a <data/> property, fetch it from the data property.
* @throws IOException thrown if the data is not valid
*/
public static void inputGraph(final Graph inputGraph, final InputStream inputStream, final int bufferSize, final String defaultEdgeLabel, final String vertexIdKey, final String edgeIdKey, final String edgeLabelKey) throws IOException {
final BatchGraph graph = BatchGraph.wrap(inputGraph, bufferSize);
final Reader r = new BufferedReader(new InputStreamReader(inputStream, Charset.forName("ISO-8859-1")));
final StreamTokenizer st = new StreamTokenizer(r);
try {
st.commentChar(GMLTokens.COMMENT_CHAR);
st.ordinaryChar('[');
st.ordinaryChar(']');
final String stringCharacters = "/\\(){}<>!£$%^&*-+=,.?:;@_`|~";
for (int i = 0; i < stringCharacters.length(); i++) {
st.wordChars(stringCharacters.charAt(i), stringCharacters.charAt(i));
}
new GMLParser(graph, defaultEdgeLabel, vertexIdKey, edgeIdKey, edgeLabelKey).parse(st);
graph.commit();
} catch (IOException e) {
throw new IOException("GML malformed line number " + st.lineno() + ": ", e);
} finally {
r.close();
}
}
use of com.tinkerpop.blueprints.util.wrappers.batch.BatchGraph in project orientdb by orientechnologies.
the class OGraphSONReader method inputGraph.
/**
* Input the JSON stream data into the graph. More control over how data is streamed is provided by this method.
*
* @param jsonInputStream
* an InputStream of JSON data
* @param bufferSize
* the amount of elements to hold in memory before committing a transactions (only valid for TransactionalGraphs)
* @throws IOException
* thrown when the JSON data is not correctly formatted
*/
public void inputGraph(final InputStream jsonInputStream, int bufferSize, final Set<String> edgePropertyKeys, final Set<String> vertexPropertyKeys) throws IOException {
final JsonParser jp = jsonFactory.createJsonParser(jsonInputStream);
// if this is a transactional localGraph then we're buffering
final BatchGraph batchGraph = BatchGraph.wrap(graph, bufferSize);
final ElementFactory elementFactory = new GraphElementFactory(batchGraph);
OGraphSONUtility graphson = new OGraphSONUtility(GraphSONMode.NORMAL, elementFactory, vertexPropertyKeys, edgePropertyKeys);
long importedVertices = 0;
long importedEdges = 0;
while (jp.nextToken() != JsonToken.END_OBJECT) {
final String fieldname = jp.getCurrentName() == null ? "" : jp.getCurrentName();
if (fieldname.equals(GraphSONTokens.MODE)) {
jp.nextToken();
final GraphSONMode mode = GraphSONMode.valueOf(jp.getText());
graphson = new OGraphSONUtility(mode, elementFactory, vertexPropertyKeys, edgePropertyKeys);
} else if (fieldname.equals(GraphSONTokens.VERTICES)) {
jp.nextToken();
while (jp.nextToken() != JsonToken.END_ARRAY) {
final JsonNode node = jp.readValueAsTree();
graphson.vertexFromJson(node);
importedVertices++;
printStatus(jp, importedVertices, importedEdges);
}
} else if (fieldname.equals(GraphSONTokens.EDGES)) {
jp.nextToken();
while (jp.nextToken() != JsonToken.END_ARRAY) {
final JsonNode node = jp.readValueAsTree();
final Vertex inV = batchGraph.getVertex(OGraphSONUtility.getTypedValueFromJsonNode(node.get(GraphSONTokens._IN_V)));
final Vertex outV = batchGraph.getVertex(OGraphSONUtility.getTypedValueFromJsonNode(node.get(GraphSONTokens._OUT_V)));
graphson.edgeFromJson(node, outV, inV);
importedEdges++;
printStatus(jp, importedVertices, importedEdges);
}
}
}
jp.close();
batchGraph.commit();
}
Aggregations