An Introduction to APOC in Neo4j
Posted By : Pavan Kumar | 31-Oct-2018
APOC:-
Awesome Procedures on
Areas of APOC:-
Here are some areas from where it contains procedures are
- Graph Algorithm
- Metadata
- Manual Indexes and relationship indexes
- Integrated with other databases like Cassandra
,MongoDB and relation databases - Import and export
- Date and Time functions
- XML loading and JSON loading from Apis and files.
- String and text functions
- Support data as
Json or HashMap in a Node.
Steps to Use APOC:-
- Download the latest binary jar from https://github.com/neo4j-contrib/neo4j-apoc-procedures/releases/3.2.0.4
- put this jar into your plugins folder inside Neo4j
- Restart Neo4j.
After restarting your server you will see a lot of procedures.
to check all
dbms.procedures();
for help in APOC use following Query
apoc.help("apoc");
Loading data from Web and Other APIs:-
With the help of APOC you can load the data from web url and APis.
In data If the result is a JSON object, then it will be returned as a singular map. and if it is an array, then it will be turned into a stream of maps.
Suppose that we want to load data from any URL then we have to use following query to load data into our neo4j database.
WITH "Your Specified URL" AS url
CALL apoc.load.json(url) YIELD value
UNWIND value.items AS item
RETURN item.title, item.owner, item.creation_date, keys(item)
We will cover more on this in next part
Creating Map:-
Currently, you can't create a map from raw data in Cypher, only properties are supported. but with the help of apoc we can do this.
to create a map from pairs of data you have to use following query in neo4J
WITH [["Fruit","Apple"],["Vegetable","Ladyfinger"],["Sports","Cricket"],
["Animal","Loin"]] as pairs
CALL apoc.map.fromPairs(pairs) YIELD value as map
RETURN map
OutPut of the following query:-
map
{Fruit:"Apple", Vegetable:"Vegetable", Sports:"Cricket", Animal:"Loin"}
Creating Dynamic Keys in map:-
By simply using cypher we can't add dynamic keys in map in neo4j
Let's take an example
If We will execute this query without using apoc it will not add dynamic key
WITH "a" as dynamicKey, "b" as dynamicValue
RETURN { dynamicKey: dynamicValue } AS map
Output
map
{"dynamicKey":"b"}
But, If we use apoc then it will add dynamic key as "a":"b"
Query to add dynamic keys
WITH "a" as dynamicKey, "b" as dynamicValue
RETURN apoc.map.fromValues([dynamicKey, dynamicValue]) AS map
Output:-
map
{"a":"b"}
Usage of APOC in Date/Time:-
apoc is also used in date time conversion.
to parse the date we have to use following query
apoc.date.parse('2015/03/25 03:15:59',['ms'/'s'], ['yyyy/MM/dd HH:mm:ss'])
To get the system timezone display format string we have to use following query using apoc.
apoc.date.systemTimezone()
Thanks
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Pavan Kumar
Pavan is a bright Java developer. He is a learner by heart and has a passion and profile to adapt various technologies.