Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

NoSQL: An Overview to the Non-Relational Database

In this article we are going to see a complete overview regarding the development with NoSQL and its references to Non-relational Databases.

NoSQL is now a popular database in the database market. It has lots of new features to deal with large volume of data. NoSQL database is a different type of database compared to the traditional RDBMS. NoSQL is a not only SQL database for storage and retrieval of data other than the tabular representation. The term NOSQL was introduced by Carl Strozzi in 1998.

Here is an overview on NoSQL-the non-relational database. But, before moving further let us start with why NoSQL should be taken into consideration.

Why NoSQL is important?

NoSQL database is in need today. There are many reasons behind the development of NoSQL.

  • Schema is not static. Collections could be created without defining the structure. The structure of the documents could be easily changed by simply adding or deleting fields.
  • Large volumes of data: It can easily handle read/write cycles and users’ data.
  • Scalability: NoSQL do not require high performance servers; it can easily work on a cluster of them.
  • Availability: Easier replication of data in NoSQL is possible i.e. the failure of one node do not stop the operation.
  • Simple and easy to implement
  • Key-value access is faster.
  • Easy to distribute

Let us have a look at different NoSQL types.

NoSQL Types

  1. Key-value

    It is designed to handle massive load and scaling of huge amounts of data. Consists of a collection of key-value pairs. For Example: DynamoDB, Scalaris API’s- get(key): extract the value. put(key, value): create or update the value. delete(key): deleting the key and its associated value.

  2. Document-based

    Document based databases can handle complex objects easily. For example: MongoDB, CouchDB, etc.

  3. Column-based

    In column-based NoSQL, each storage block contains data from only one column. Here, the tables handle semi-structured data. For Example: BigTable, Cassandra, etc.

  4. Graph-based

    A graph based database focuses on modeling the structure of data and scales to its complexity. For Example: Neo4J, InfoGrid, etc.

Figure 1.Some well-known NoSQL databases in the market

Data Models

  1. Document Stores: Document stores and retrieves data
  2. Key-value Stores: key value pairs with each value having a unique key associated with it.
  3. Hierarchical Database: In this, the data is stored in the form of tree or parent-child relationship.
  4. Column-oriented: Google’s research paper on BigTable storage leads to the existence of Column-oriented databases.
  5. Graph Network: These are network databases that are used to store data. This data can be represented in the form of a Graph.

Users of NoSQL

Database

Used By

Cassandra

Reddit

Digg

Twitter

Dynama

Amazon.com

Project-voldemort

Linkedin

Here, I am going to demonstrate different NoSQL databases. Let us start withRedis. Let us first look some of the disadvantages of using NoSQL database:

Disadvantages

  • Lack of referential integrity constraints across partitions
  • Integrations with other applications that support SQL are not easy.
  • Do not fully support all relational features i.e. no join, groupby and order by.

Redis

It is an advanced key value store for building high performance applications. Building of scalable web applications in Redis is a simple task. When compared with other key value data stores, it has a rich set of data types.

Released in the year 2009 and right now considered as the most popular key-value store.

Reddis supports string as well as abstract data-types. The data-types are listed below:

  • Sets of strings.
  • List of strings
  • Hash Tables
  • Sorted sets of strings.

Now I am listing some benefits of Redis:

  • It can be used in caching, sessions, hit counts etc.
  • It supports most of the known data-types i.e. sets, hashes, list, etc.
  • Very fast.
It is commonly deployed on Amazon Web Services, Rackspace, or Heroku. Examples of commercial notable redis database as a service provider:

Provider

Characterstics

Available On

Morpheus-Redis as a service

Some features include performance monitoring, single click provisioning, support for strings, hashes, lists, etc.

Morpheus

Redis Cloud

High availability, automatic scaling, daily backups are some of its features.

Amazon EC2, Microsoft Azure, IBM SoftLayer, Google Cloud Platform, Heroku, Cloud Foundry, OpenShift, AppFog, AppHarbor

Amazon Web Services – ElastiCache

Persistence, high availability, performance monitoring.

Amazon EC2

Working with Redis on Linux

For installing Redis on Linux:

$sudo apt-get install redis-server

Initiate with the following command.

$redis-server

Now check the working of Redis by $redis-cli. After implementing this, redis prompt will open with 127.0.0.1 as machine’s IP address and port as 6379.

redis 127.0.0.1:6379>
redis 127.0.0.1:6379>ping

Now, let us implement the 5 data-types one by one:

Data-types

Here is the description of Redis data-types with syntax and examples.

Strings

This data-type is for string values. The syntax and example is given below:

redis 127.0.0.1:6379> COMMAND KEYNAME

For setting the key value:

redis 127.0.0.1:6379> SET welearn redis
OK

For retrieving the key value:

redis 127.0.0.1:6379> GET welearn
"redis"

Some basic String commands are given below:

S.No.

Command & Description

1

SET key value For setting the value at the specified key

2

GET key This command is for getting the value of a key.

3

GETRANGE key start end It gets a substring of the string stored at a key

4

GETSET key value It sets the string value of a key and return its old value

5

GETBIT key offset This returns the bit value at offset in the string value stored at key

6

MGET key1 [key2..] It gets the values of all the given keys

7

SETBIT key offset value This command sets or clears the bit at offset in the string value stored at key

8

SETEX key seconds value It sets the value with expiry of a key

9

SETNX key value It sets the value of a key, only if the key does not exist

10

SETRANGE key offset value It overwrite part of a string at key starting at the specified offset

11

STRLEN key It gets the length of the value stored in a key

12

MSET key value [key value ...] It sets multiple keys to multiple values

13

MSETNX key value [key value ...] It sets multiple keys to multiple values, only if none of the keys exist

14

PSETEX key milliseconds value It sets the value and expiration in milliseconds of a key

15

INCR key It increments the integer value of a key by one

16

INCRBY key increment It increments the integer value of a key by the given amount

17

INCRBYFLOAT key increment It increments the float value of a key by the given amount

18

DECR key It decrements the integer value of a key by one

19

DECRBY key decrement It decrements the integer value of a key by the given number

20

APPEND key value It appends a value to a key

Hashes

Hashes are maps between string fields and string values. More than 4 billion field-value pairs can be stored by hash. The syntax and example is given below:

redis 127.0.0.1:6379> COMMAND HASHNAME fields

For setting the hashname “welearn” and fields for hashname details:

redis 127.0.0.1:6379> HMSET welearn name "java" others "javascript" responsive “bootstrap for responsive websites”
OK

For getting the field values of hashname “ welearn”:

redis 127.0.0.1:6379> HGETALL welearn
1) "java"
2) "others"
3) "javascript"
4) “responsive”
5) “bootstrap for responsive websites”

Some basic Hash commands are given below with short description:

S.No.

Command & Description

1

HDEL key field2 [field2] It deletes one or more hash fields

2

HEXISTS key field It determines whether a hash field exists or not

3

HGET key field It gets the value of a hash field stored at specified key

4

HGETALL key This command gets all the fields and values stored in a hash at specified key

5

HINCRBY key field increment It increments the integer value of a hash field by the given number

6

HINCRBYFLOAT key field increment It increments the float value of a hash field by the given amount

7

HKEYS key It gets all the fields in a hash

8

HLEN key It gets the number of fields in a hash

9

HMGET key field1 [field2] It gets the values of all the given hash fields

10

HMSET key field1 value1 [field2 value2 ] It sets multiple hash fields to multiple values

11

HSET key field value It sets the string value of a hash field

12

HSETNX key field value It sets the value of a hash field, only if the field does not exist

13

HVALS key It gets all the values in a hash

14

HSCAN key cursor [MATCH pattern] [COUNT count] Incrementally iterate hash fields and associated values

Lists

List of strings in which the elements could be added in head or tail. The syntax and example is given below:

array12

For different values under list welearn:

redis 127.0.0.1:6379> LPUSH welearn java
(integer) 1
redis 127.0.0.1:6379> LPUSH welearn bootstrap
(integer) 2

For range from 0 to 5, here are the values.

redis 127.0.0.1:6379> LRANGE welearn 0 5
1) "bootstrap"
2) "java”

Some basic List commands are given below with short description:

S.No.

Command & Description

1

BLPOP key1 [key2 ] timeout It removes and gets the first element in a list, or block until one is available.

2

BRPOP key1 [key2 ] timeout It removes and gets the last element in a list, or block until one is available.

3

BRPOPLPUSH source destination timeout It pops a value from a list, push it to another list and return it; or block until one is available

4

LINDEX key index It gets an element from a list by its index

5

LINSERT key BEFORE|AFTER pivot value It inserts an element before or after another element in a list

6

LLEN key It gets the length of a list

7

LPOP key It removes and get the first element in a list

8

LPUSH key value1 [value2] It prepends one or multiple values to a list

9

LPUSHX key value It prepends a value to a list, only if the list exists

10

LRANGE key start stop It gets a range of elements from a list

11

LREM key count value It removes elements from a list

12

LSET key index value It sets the value of an element in a list by its index

13

LTRIM key start stop It trims a list to the specified range

14

RPOP key It removes and gets the last element in a list

15

RPOPLPUSH source destination It removes the last element in a list, append it to another list and return it

16

RPUSH key value1 [value2] It appends one or multiple values to a list

17

RPUSHX key value It appends a value to a list, only if the list exists

Sets

Unordered collection of string is known as Redi sets. The syntax and example is given below:

array12

For different values (and repetitive) under list welearn:

redis 127.0.0.1:6379> SADD welearn java
(integer) 1
redis 127.0.0.1:6379> SADD welearn bootstrap
(integer) 1
redis 127.0.0.1:6379> SADD welearn bootstrap
(integer) 0

For getting the unique value:

redis 127.0.0.1:6379> SMEMBERS welearn
1) "bootstrap"
2) "java"

Some basic Redis Sets commands are given below with description:

S.No.

Command & Description

1

SADD key member1 [member2] It adds one or more members to a set.

2

SCARD key It gets the number of members in a set.

3

SDIFF key1 [key2] It subtracts multiple sets.

4

SDIFFSTORE destination key1 [key2] It subtracts multiple sets and store the resulting set in a key

5

SINTER key1 [key2] It intersects multiple sets

6

SINTERSTORE destination key1 [key2] It intersects multiple sets and store the resulting set in a key

7

SISMEMBER key member It determines if a given value is a member of a set

8

SMEMBERS key It gets all the members in a set

9

SMOVE source destination member It moves a member from one set to another

10

SPOP key It removes and return a random member from a set

11

SRANDMEMBER key [count] It gets one or multiple random members from a set

12

SREM key member1 [member2] It removes one or more members from a set

13

SUNION key1 [key2] It adds multiple sets

14

SUNIONSTORE destination key1 [key2] It adds multiple sets and store the resulting set in a key

15

SSCAN key cursor [MATCH pattern] [COUNT count] Incrementally iterate Set elements

Sorted Sets

It is similar to sets with a score associated with every member for making the sorted set ordered. This order is from smallest to the greatest score with every member. The syntax and example is given below:

redis 127.0.0.1:6379> COMMAND KEY SCORE MEMBER

For setting the values with score:

redis 127.0.0.1:6379> ZADD welearn 1 java
(integer) 1
redis 127.0.0.1:6379> ZADD welearn 2 bootstrap
(integer) 1
redis 127.0.0.1:6379> ZADD welearn 2 bootstrap
(integer) 0
redis 127.0.0.1:6379> ZADD welearn 3 bootstrap
(integer) 0

For values within a range:

redis 127.0.0.1:6379> ZRANGE welearn 0 5 WITHSCORES
1) "java"
2) "1"
3) "bootstrap"
4) "3"

Some basic Sorted Set commands are given below:

S.No.

Command & Description

1

ZADD key score1 member1 [score2 member2] It adds one or more members to a sorted set, or update its score if it already exists

2

ZCARD key It gets the number of members in a sorted set

3

ZCOUNT key min max It counts the members in a sorted set with scores within the given values

4

ZINCRBY key increment member This command increments the score of a member in a sorted set

5

ZINTERSTORE destination numkeys key [key ...] It intersects multiple sorted sets and store the resulting sorted set in a new key.

6

ZLEXCOUNT key min max It counts the number of members in a sorted set between a given lexicographical range.

7

ZRANGE key start stop [WITHSCORES] It returns a range of members in a sorted set, by index.

8

ZRANGEBYLEX key min max [LIMIT offset count] It returns a range of members in a sorted set, by lexicographical range.

9

ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT] It returns a range of members in a sorted set, by score.

10

ZRANK key member It determines the index of a member in a sorted set.

11

ZREM key member [member ...] It removes one or more members from a sorted set.

12

ZREMRANGEBYLEX key min max It removes all members in a sorted set between the given lexicographical range.

13

ZREMRANGEBYRANK key start stop It removes all members in a sorted set within the given indexes

14

ZREMRANGEBYSCORE key min max It removes all members in a sorted set within the given scores

15

ZREVRANGE key start stop [WITHSCORES] It returns a range of members in a sorted set, by index, with scores ordered from high to low.

16

ZREVRANGEBYSCORE key max min [WITHSCORES] It returns a range of members in a sorted set, by score, with scores ordered from high to low.

17

ZREVRANK key member It determines the index of a member in a sorted set, with scores ordered from high to low.

18

ZSCORE key member It gets the score associated with the given member in a sorted set.

19

ZUNIONSTORE destination numkeys key [key ...] It adds multiple sorted sets and store the resulting sorted set in a new key.

20

ZSCAN key cursor [MATCH pattern] [COUNT count] It incrementally iterate sorted sets elements and associated scores.

Neo4j

It is an open source graph NoSQL database which was initially released in the year 2010 and implemented in Java.

Here are some benefits.

  • Easy to represent semi-structured data.
  • Joins are not needed to retrieve related data.
  • Retrieval of connected data is easy.

It has the following features:

  • Property Graph Data Mode
  • Its query language is Cypher Query Language (CQL).
  • UNIQUE constraints.
  • Exporting of query data to JSON and XLS format.
  • For java applications, it supports Cypher and Native Java API.

The query language of Neo4j is CQL with a declarative pattern-matching language.

Cyper Query Language Data-types

Data-type

Value

Boolean

boolean literals: true, false.

Byte

8-bit integers.

Short

16-bit integers.

Int

32-bit integers.

Long

64-bit integers.

Float

32-bit floating-point numbers.

Double

64-bit floating-point numbers.

Char

16-bit characters.

String

Strings.

CQL Commands

Some Neo4j commands are explained here:

CREATE

CREATE command is used for creating Nodes with Properties and without properties. It is also used for creating Relationships between Nodes with Properties and without Properties.

Syntax for creating a node with properties

Here,

  • node name is NodeName,
  • label of node to create is LabelName,
  • Name of the property assigned to newly created node is NameOfProperty1
  • Value of the property which is for the newly created node is ValueOfPoperty1
CREATE ( <NodeName>:<LabelName>
   {        
      <NameOfProperty1> : <ValueOfPoperty1>
      ........
      <Name of propertyn>:< Value of propertyn>
   }
)

Example:

CREATE (std:Student { stdname:Amit, stdsubject:"Java" })

Creating student node with node name as std and label as Student.

Here,

  • Name of property is stdname and stdsubject, and
  • Value of property is Amit and Java.

Syntax for creating a node without properties

CREATE (<NodeName>:<LabelName>)

Here,

  • node to be created is NodeName,
  • label for the node created is LabelName

Example

Creating student node with node name as std and label as Student

CREATE (std:Student)

MATCH

MATCH command is used for retrieving data about nodes, relationships, and properties from the database.

Syntax

Here,

  • node name is NodeName,
  • label of node to be created is LabelName,
MATCH ( : )

Example

Creating student node with node name as std and label as Student

MATCH (std:Student)

RETURN

RETURN command is used to return the properties of a node and associated relationships.

Syntax

Here,

  • node name is NodeName,
  • Name of the property assigned to newly created node is NameOfProperty1
RETURN 
.,
 ........
 < NodeName >.

Example

RETURN std.stdsubject

Here,

  • std is node name,
  • stdsubject is property name

CQL Functions

Let us start with Aggregation Functions:

Aggregation Functions

S.No.

Function

Description

1.

COUNT

It returns the number of rows returned by MATCH command.

2.

AVG

It returns the average value of all rows returned by MATCH command.

3.

MAX

It returns the maximum value from a set of rows returned by MATCH command.

4.

MIN

It returns the minimum value from a set of rows returned by MATCH command.

5.

SUM

It returns the summation value of all rows returned by MATCH command.

COUNT

It counts the rows present in the results returned by MATCH clause. Its syntax is given below:

Here value can be a node or a name of the property.

COUNT(< value >)

Example shows here the use of COUNT function.

MATCH (s:Student) 
RETURN s.stdname,s.stdsubject

MIN

This function finds the minimum value from the property name column of given rows. For input, it takes a set of rows and a property name of a Node or Relationship. Its syntax is given below:

MIN(< NameOfProperty > )

Example shows here the use of MAX function for returning the minimum student marks.

MATCH (s:Student) 
RETURN MIN(s.stdmarks)

MAX

This function finds the maximum value from the property name column of given rows. For input, it takes a set of rows and a property name of a Node or Relationship. Its syntax is given below:

MAX(< NameOfProperty > )

Example shows here the use of MAX function for returning the maximum student marks.

MATCH (s:Student) 
RETURN MAX(s.stdmarks)

AVG

This function finds the average value from the property name column of given rows. For input, it takes a set of rows and a property name of a Node or Relationship. Its syntax is given below:

AVG( )

Example shows here the use of AVG function for calculating the average of student marks.

MATCH (s:Student) 
RETURN AVG (s.stdmarks)

SUM

This function finds the Sum from the property name column of given rows. For input, it takes a set of rows and a property name of a Node or Relationship. Its syntax is given below:

SUM( )

Example shows here the use of SUM function for calculating the sum of student marks.

MATCH (s:Student) 
RETURN SUM (s.stdmarks)
Relationship Functions

S.No.

Function

Description

1.

STARTNODE

Start Node of a Relationship.

2.

ENDNODE

End Node of a Relationship.

3.

ID

The ID of a Relationship.

4.

TYPE

The TYPE of a Relationship in string representation.

String Functions

S.No.

Function

Description

1.

UPPER

For converting letters to uppercase.

2.

LOWER

For converting letters to lowercase.

3.

SUBSTRING

For getting a part of string.

Upper

This function converts the input string to Uppercase. Its syntax is given below:

UPPER(< String-to-be-converted >)

Example shows here the use of UPPER function for converting a lowercase (students name) string to uppercase.

MATCH (s:Student) 
RETURN UPPER (s.stdname)

Lower

This function converts the input string to Lowercase (student subjects). Its syntax is given below:

LOWER(< String-to-be-converted >)

Example shows here the use of LOWER function for converting a uppercase string in lowercase.

MATCH (s:Student) 
RETURN LOWER (s.stdsubject)

Substring

This function returns a substring. It takes the string, start, and end index as input. Its syntax is given below:

SUBSTRING(<The-Input-String>, <starting-Index> ,<ending-Index>)

Example shows here the use of SUBSTRING function for converting an uppercase string in lowercase.

MATCH (s:Student) 
RETURN SUBSTRING(e.stdname, 2, 4),LOWER (s.stdsubject)

Conclusion

This article is all about the NoSQL databases and their features. Here, we have focused on NoSQL with a deep insight into some popular databases; Redis and Neo4j. Neo4j is one of the best graph databases in the market, and Redis as a well-known key-value store. Just give NoSQL a try if you’ve worked on SQL for ages. NoSQL is the need for managing large chunks of data with flexibility.

Hope you have enjoyed the article.



Website: www.techalpine.com Have 16 years of experience as a technical architect and software consultant in enterprise application and product development. Have interest in new technology and innovation area along with technical...

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login