Last active
May 10, 2020 06:25
-
-
Save pluradj/f240ab883fde8136cbf48a9503b06d40 to your computer and use it in GitHub Desktop.
Updating the entire value of vertex property with List cardinality in Titan graph database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package pluradj.titan.tinkerpop3.example; | |
import static org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__.*; | |
import com.thinkaurelius.titan.core.Cardinality; | |
import com.thinkaurelius.titan.core.PropertyKey; | |
import com.thinkaurelius.titan.core.TitanFactory; | |
import com.thinkaurelius.titan.core.TitanGraph; | |
import com.thinkaurelius.titan.core.schema.TitanManagement; | |
import java.util.Arrays; | |
import java.util.ArrayList; | |
import java.util.List; | |
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource; | |
import org.apache.tinkerpop.gremlin.structure.T; | |
import org.apache.tinkerpop.gremlin.structure.Vertex; | |
import org.apache.tinkerpop.gremlin.structure.VertexProperty; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
// https://github.com/pluradj/titan-tp3-java-example | |
public class JavaExample { | |
private static final Logger LOGGER = | |
LoggerFactory.getLogger(JavaExample.class); | |
private static final String POST_KEYWORD = "post_keyword"; | |
public static void updateProperties(final Vertex nextVertex) { | |
final GraphTraversalSource g = nextVertex.graph().traversal(); | |
final List<String> keywords = Arrays.asList("three", "four", "five"); | |
if (keywords.size() > 0) { | |
nextVertex.graph().traversal().V(nextVertex).properties(POST_KEYWORD).drop().iterate(); | |
LOGGER.info("dropped = "+ g.V(nextVertex).properties(POST_KEYWORD).count().next() + " " + g.V(nextVertex).values(POST_KEYWORD).toList()); | |
for (String keyword : keywords) { | |
nextVertex.property(POST_KEYWORD, keyword); | |
} | |
LOGGER.info("added = "+ g.V(nextVertex).properties(POST_KEYWORD).count().next() + " " + g.V(nextVertex).values(POST_KEYWORD).toList()); | |
} | |
nextVertex.graph().tx().commit(); | |
} | |
public static void main(String[] args) { | |
String dataDir = "./data"; | |
TitanGraph graph = TitanFactory.open("berkeleyje:" + dataDir); | |
GraphTraversalSource g = graph.traversal(); | |
TitanManagement mgmt = graph.openManagement(); | |
PropertyKey name = mgmt.makePropertyKey(POST_KEYWORD).dataType(String.class).cardinality(Cardinality.LIST).make(); | |
mgmt.commit(); | |
final Vertex v = graph.addVertex(POST_KEYWORD, "one", POST_KEYWORD, "two"); | |
graph.tx().commit(); | |
LOGGER.info("start = "+ g.V(v).properties(POST_KEYWORD).count().next() + " " + g.V(v).values(POST_KEYWORD).toList()); | |
updateProperties(v); | |
LOGGER.info("done = "+ g.V(v).properties(POST_KEYWORD).count().next() + " " + g.V(v).values(POST_KEYWORD).toList()); | |
System.exit(0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://stackoverflow.com/questions/36540170/updating-the-entire-value-of-vertex-property-with-list-cardinality-in-titan-grap