redisgraph v0.1.0 RedisGraph View Source
Provides the components to construct and interact with Graph entities in a RedisGraph database.
This library uses Redix to communicate with a redisgraph server.
To launch redisgraph locally with Docker, use
docker run -p 6379:6379 -it --rm redislabs/redisgraph
Here is a simple example:
alias RedisGraph.{Node, Edge, Graph, QueryResult}
# Create a connection using Redix
{:ok, conn} = Redix.start_link("redis://localhost:6379")
# Create a graph
graph = Graph.new(%{
name: "social"
})
# Create a node
john = Node.new(%{
label: "person",
properties: %{
name: "John Doe",
age: 33,
gender: "male",
status: "single"
}
})
# Add the node to the graph
# The graph and node are returned
# The node may be modified if no alias has been set
# For this reason, nodes should always be added to the graph
# before creating edges between them.
{graph, john} = Graph.add_node(graph, john)
# Create a second node
japan = Node.new(%{
label: "country",
properties: %{
name: "Japan"
}
})
# Add the second node
{graph, japan} = Graph.add_node(graph, japan)
# Create an edge connecting the two nodes
edge = Edge.new(%{
src_node: john,
dest_node: japan,
relation: "visited"
})
# Add the edge to the graph
# If the nodes are not present, an {:error, error} is returned
{:ok, graph} = Graph.add_edge(graph, edge)
# Commit the graph to the database
{:ok, commit_result} = RedisGraph.commit(conn, graph)
# Print the transaction statistics
IO.inspect(commit_result.statistics)
# Create a query to fetch some data
query = "MATCH (p:person)-[v:visited]->(c:country) RETURN p.name, p.age, v.purpose, c.name"
# Execute the query
{:ok, query_result} = RedisGraph.query(conn, graph.name, query)
# Pretty print the results using the Scribe lib
IO.puts(QueryResult.pretty_print(query_result))
which gives the following results:
# Commit result statistics
%{
"Labels added" => nil,
"Nodes created" => "2",
"Nodes deleted" => nil,
"Properties set" => "5",
"Query internal execution time" => "0.228669",
"Relationships created" => "1",
"Relationships deleted" => nil
}
# Query result pretty-printed
+----------------+-------------+-----------------+--------------+
| "p.name" | "p.age" | "v.purpose" | "c.name" |
+----------------+-------------+-----------------+--------------+
| "John Doe" | 33 | nil | "Japan" |
+----------------+-------------+-----------------+--------------+
Link to this section Summary
Functions
Execute a procedure call against the graph specified.
Execute arbitrary command against the database.
Commit a RedisGraph.Graph to the database using CREATE.
Delete a graph from the database.
Fetch the execution plan for a query on a graph.
Fetch the raw response of the db.labels() procedure call against the specified graph.
Merge a pattern into the graph.
Fetch the raw response of the db.propertyKeys() procedure call against the specified graph.
Query on a graph in the database.
Fetch the raw response of the db.relationshipTypes() procedure call against the specified graph.
Link to this section Types
Link to this section Functions
Execute a procedure call against the graph specified.
Returns the raw result of the procedure call.
command(conn, c)
View Sourcecommand(connection(), [String.t()]) :: {:ok, RedisGraph.QueryResult.t()} | {:error, any()}
Execute arbitrary command against the database.
https://oss.redislabs.com/redisgraph/commands/
Query commands will be a list of strings. They
will begin with either GRAPH.QUERY,
GRAPH.EXPLAIN, or GRAPH.DELETE.
The next element will be the name of the graph.
The third element will be the query command.
Optionally pass the last element --compact
for compact results.
Example:
[
"GRAPH.QUERY",
"imdb",
"MATCH (a:actor)-[:act]->(m:movie {title:'straight outta compton'})",
"--compact"
]
commit(conn, graph)
View Sourcecommit(connection(), RedisGraph.Graph.t()) :: {:ok, RedisGraph.QueryResult.t()} | {:error, any()}
Commit a RedisGraph.Graph to the database using CREATE.
Returns a RedisGraph.QueryResult which contains query
statistics related to entities created.
delete(conn, name)
View Sourcedelete(connection(), String.t()) :: {:ok, RedisGraph.QueryResult.t()} | {:error, any()}
Delete a graph from the database.
Returns a RedisGraph.QueryResult with statistic for
query execution time.
execution_plan(conn, name, q)
View Sourceexecution_plan(connection(), String.t(), String.t()) :: {:ok, RedisGraph.QueryResult.t()} | {:error, any()}
Fetch the execution plan for a query on a graph.
Returns a raw result containing the query plan.
labels(conn, name)
View Sourcelabels(connection(), String.t()) :: {:ok, list()} | {:error, any()}
Fetch the raw response of the db.labels() procedure call against the specified graph.
merge(conn, name, pattern)
View Sourcemerge(connection(), String.t(), String.t()) :: {:ok, RedisGraph.QueryResult.t()} | {:error, any()}
Merge a pattern into the graph.
Creates or updates the graph pattern into the graph specified
using the MERGE command.
Returns a RedisGraph.QueryResult with statistics for
entities created.
property_keys(conn, name)
View Sourceproperty_keys(connection(), String.t()) :: {:ok, list()} | {:error, any()}
Fetch the raw response of the db.propertyKeys() procedure call against the specified graph.
query(conn, name, q)
View Sourcequery(connection(), String.t(), String.t()) :: {:ok, RedisGraph.QueryResult.t()} | {:error, any()}
Query on a graph in the database.
Returns a RedisGraph.QueryResult containing the result set
and metadata associated with the query.
relationship_types(conn, name)
View Sourcerelationship_types(connection(), String.t()) :: {:ok, list()} | {:error, any()}
Fetch the raw response of the db.relationshipTypes() procedure call against the specified graph.