Reader small image

You're reading from  Graph Data Processing with Cypher

Product typeBook
Published inDec 2022
PublisherPackt
ISBN-139781804611074
Edition1st Edition
Concepts
Right arrow
Author (1)
Ravindranatha Anthapu
Ravindranatha Anthapu
author image
Ravindranatha Anthapu

Ravindranatha Anthapu has more than 25 years of experience in working with W3C standards or building cutting-edge technologies like integrating speech with mobile applications in the 2000s. He is a technology enthusiast who worked from operating system device drivers to writing compilers for C language to modern web technologies, transitioning seamlessly and bringing experience from each of these domains and technologies to deliver successful solutions today. As a Principal Consultant at Neo4j today, Ravindranatha works with large enterprise customers to make sure they are able to leverage graph technologies effectively across various domains.
Read more about Ravindranatha Anthapu

Right arrow

Working with Lists and Maps

We have worked with lists and maps in earlier chapters in various queries. But we have not discussed how lists and maps can make Cypher queries more powerful. They are first-class types in Cypher, like string and integer, and can make it easy to build complex queries. This chapter discusses how we can handle both lists and maps as input and output. We will discuss how we can prepare lists from data, iterate lists to process data, handle nested maps, and return map projections.

The following aspects will be covered in this chapter:

  • Working with lists
  • Working with maps

We will be covering lists first. We will discuss in detail how lists work in Cypher and explore various ways we can manipulate and work with lists, both as input and output. We will also look at different types of lists and the functions available to work with them.

Let’s get onto working with lists.

Working with lists

Lists are the core data type in Cypher and because of this, there is extensive support for lists in Cypher. Lists hold elements in a sequence so that we can iterate the list in any order. They can hold any type of value. All of the elements in a list can be of the same type: whether integer, string, map, or list. It is also possible to mix and match different types in the same list.

We will take a look at different aspects of lists, such as using them as input and building lists for output or intermediate processing, along with using various built-in functions to process lists in this section.

Working with basic list capabilities

Cypher lists can be used to hold any type of data, including integers, strings, and so on. They hold the data in sequence like an array but are not limited to a single type. We can use indexing to access content or leverage built-in functions from Cypher to access data.

Let’s take a look at an example:

WITH [1,2,3,4...

Working with maps

In this section, we will take a look at working with maps in Cypher. Maps in Cypher represent key-value pairs. The keys must be strings and values can be any object. Maps can be defined in Cypher inline, where they are called literal maps, or they can be passed as parameters. Every node and relationship object can also be treated as a map in Cypher, so that we can access all the properties using dot (.) notation or index ([ ]) notation.

A map is like a JavaScript Object Notation (JSON) object. A sample JSON object looks like this:

{
  "firstName": "John",
  "lastName": "Smith",
  "isAlive": true,
  "age": 27
}

If we represented the same map in Cypher, it would look like this:

WITH {
  firstName: "John",
  lastName: "Smith",
  isAlive: true,
  age: 27
} as map
RETURN map

We can see from the code...

Summary

In this chapter, we have looked at how lists and maps are core data types in Cypher for data manipulation and processing data. We looked at creating basic lists, how they can store different data types, and how we can access data using operators and functions. Also, we looked at how maps work in Cypher and how to use them when we are returning data. We also looked at how LOAD CSV creates a map for each row to be processed. Also, we took a look at lists of maps that are provided by applications using the driver to be processed to ingest data similar to LOAD CSV.

In the next chapter, we will take a deep dive into advanced querying using WITH, FOREACH, CASE, and other Cypher clauses.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Graph Data Processing with Cypher
Published in: Dec 2022Publisher: PacktISBN-13: 9781804611074
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Ravindranatha Anthapu

Ravindranatha Anthapu has more than 25 years of experience in working with W3C standards or building cutting-edge technologies like integrating speech with mobile applications in the 2000s. He is a technology enthusiast who worked from operating system device drivers to writing compilers for C language to modern web technologies, transitioning seamlessly and bringing experience from each of these domains and technologies to deliver successful solutions today. As a Principal Consultant at Neo4j today, Ravindranatha works with large enterprise customers to make sure they are able to leverage graph technologies effectively across various domains.
Read more about Ravindranatha Anthapu