NoSQL Zone is brought to you in partnership with:

I've been in the world of software development for 14+ years working with most mainstream programming languages at some point. Chad is a DZone MVB and is not an employee of DZone and has posted 38 posts at DZone. You can read more from them at their website. View Full User Profile

5-Minute Beginner's Guide: Installing / Testing Redis and Node.js on OS X

05.22.2012
| 6720 views |
  • submit to reddit

Redis is an open source, advanced key-value store. Node.js is a platform built on Chrome’s JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.

Today I’m going to show you how quickly you can get Redis and Node.js running on OS X. In addition, I’ll show you two quick demos you can then try out with Node.js.

Note: I’m using OS X version 10.7.3 and I have the latest XCode installed (available on the App Store). Much of this information is available at the respective websites, I just wanted to put it all in one place for beginners.

Step one, go grab the OS X package from the Nodejs website and download and install it. The package is pre-built and is very simple to install.

Step Two: Once the above is done go to the Redis website and download the latest Redis package. Once the tar file was downloaded I double clicked it to extract it, you can also issue the follow command line from the terminal:

$ tar xzf redis-2.x.x.tar.gz

From the terminal go into the new folder that was created when you extracted Redis:

$ cd redis-2.x.x

Still within the Redis folder simply run the make command now:

$ make

Test everything out:

$ make test

Time to start Redis:

$ src/redis-server

Let’s start with a very simple program. The program will simply set a value and then fetch it back.

First, create a new folder and inside that folder create a new text file called app.js. Inside the app.js file add the following code:

var redis = require("redis"),
    client = redis.createClient();
 
client.on("error", function (err) {
    console.log("Error " + err);
});
// Set a value
client.set("string key", "Hello World", redis.print);
// Get the value back
client.get("string key", function (err, reply) {
    console.log(reply.toString());
});
// Clean quit (waits for client to finish)
client.quit();

Now, let’s install node_redis locally via NPM:

Go back into the terminal and navigate the folder where you placed the app.js file and enter this command from within that folder:

$ npm install redis

Note: You can also install a non-blocking C library version like this:

$ npm install hiredis redis

Run the Node.js program like so:

$ node app.js

Output will look similar to this:

Reply: OKHello World

Lets build a more complex program now. Modify the app.js file and replace the previous code with the code below (taken from the node_redis project on Github):

var redis = require("redis"),
    client = redis.createClient();
 
client.on("error", function (err) {
    console.log("Error " + err);
});
 
client.set("string key", "string val", redis.print);
client.hset("hash key", "hashtest 1", "some value", redis.print);
client.hset(["hash key", "hashtest 2", "some other value"], redis.print);
client.hkeys("hash key", function (err, replies) {
    console.log(replies.length + " replies:");
    replies.forEach(function (reply, i) {
        console.log("    " + i + ": " + reply);
    });
    client.quit();
});

Run the Node.js program now like so:

$ node app.js

Output will look similar to this:

Reply: OKReply: 1Reply: 12 replies:    0: hashtest 1    1: hashtest 2

In the terminal and the Redis folder you can safely shutdown Redis with the following command:

$ src/redis-cli shutdown
Published at DZone with permission of Chad Lung, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Herry Johnson replied on Tue, 2012/06/12 - 2:01pm

If you can read the backslash from the XML into a String that contains the backslash it will work when you construct a File. If you can't read the backslash from the XML you need to do something about that, probably at the source of the XML, i.e. use forward slashes. You don't need to double backslashes in Java code. You only need to do that in Java String literals, property files, and any other file formats that require it.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.