Redis, MongoDB, hbase, and MySQL databases on the node. js Platform

Source: Internet
Author: User
Tags install mongodb mongodb driver mongodb server

 

I. MongoDB:

Because 10gen is one of the sponsors, MongoDB has good support for node. js.

A. Basic Support: node. js provides two common components for MongoDB. Mongoose.

(1) MongoDB-based support. This for node. js driver is event-driven, so its usage is basically asynchronous callback. Download the driver $ NPM install-Gd MongoDB

Add the following in testdb. JS:Code:

VaR MongoDB = require ('mongodb ');
VaR Server = new MongoDB. Server ("127.0.0.1", 27017 ,{});
VaR connection = new MongoDB. dB ('test', server, {safe: true}). Open (function (error, client ){
If (error) Throw error;

VaR collection = client. Collection ('users ');
VaR test = collection. Find ({}, {limit: 10}). toarray (function (ERR, Docs ){
Console. dir (DOCS );
});

// Insert Section
VaR doc1 = {ID: 33, name: 'liuyang1 '};
VaR doc2 = {ID: 44, name: 'liuyang2 '};
Collection. insert ([doc1, doc2], function (ERR, result ){
Console. Log (result );
});

Console. Log ('this is a test result... \ n ');
});

(2) Mongoose-based support.

Download the driver $ NPM install-Gd mongoose

The test code is as follows:

VaR mongoose = require ('mongoose ');
VaR conn = mongoose. Connect ('mongodb: // localhost/test ');

VaR DB = mongoose. connection;
DB. On ('error', console. Error. BIND (console, 'Connection error :'));
DB. Once ('open', function callback (ERR ){
If (! Err) {console. Log ('opened ');}
});

VaR schema = mongoose. Schema;

VaR person = new schema ({
ID: Number
, Name: {type: string}

});
VaR result = mongoose. Model ('users', person );
VaR personcollection = mongoose. Model ('users ');
VaR one = new personcollection ();
One. Title = "zyq ";
One. Age = 38;
One. Save (function (ERR ){
If (ERR ){
Console. Log ('failed !!!! ');
} Else {
Console. Log ('successful !!!! ');
} // End of if Block
});

// Test find
Personcollection. Find (function (ERR, persons ){
If (! Err ){
Console. Log (persons );
}
});

 

B. MongoDB rest support:

Because of MongoDB's bjson data storage mode, apsaradb for MongoDB naturally supports JSON-format data. The rest Support created under some conditions is the most convenient.

1.-Rest startup parameters support webpage management:

2. quickly build a MongoDB access platform based on node. js that supports the rest web service mode and JSON format:

First install the MongoDB DRIVER: $ NPM install MongoDB-GD.

Install the MongoDB rest component: $ NPM install MongoDB-rest-Gd

Run MongoDB-rest directly. (before running, find the component directory (in the nodejs global module directory, for example,/usr/local/nodejs/lib/node_modules/MongoDB-rest, find config. JSON. modify the MongoDB server address and port to the correct data, otherwise the unauthorized error will be reported)

If a createserver error is reported, you must modify the server in the component directory. JS sets var APP = module. exports. APP = Express. createserver (); changed to VaR APP = module. exports. APP = Express ();

The Code is a bit old :) but it is good for learning.

Now we can access MongoDB through rest web service. We can test it with cURL:

$ Curl-d '{"A1": 201}'-h "content-Tyson" http: // localhost: 3000/test/Example

Ii. redis database:

The advantages and disadvantages and application scenarios of redis are not mentioned here. You can find other logs for more information. If it is used in Complex cache scenarios, it is correct to use it, at least from the current technical perspective.

1. Install the redis database. Download the installation package from the official website.

Http://redis.io/Here it should be 2.6.10.

$ Tar-xzvf redis-2.6.10.tar.gz

$ Redis-2.6.10 CD

$ Make

$ Make install

OK. Now you can use the client that comes with redis to test it.

Run the redis server first

$ Redis-Server

$ Redis-cli

> Redis 127.0.0.1: 6379> set zyq 'is a good man'

Redis 127.0.0.1: 6379> Get zyq

Is a good man 'OK, everything works

Install the node. js redis driver module:

$ Sudo NPM install redis-Gd

After installation, you can write code for testing:

VaR redis = require ("redis ");

VaR client = redis. createclient ();

Client. On ("error", function (ERR) {console. Log ("error" + ERR );});

Client. Set ("skey", "this is string", redis. Print );

Client. hset ("hashkey", "hashtest a", "ABC", redis. Print );

Client. hset (["hashkey", "hashtest B", "Some otest"], redis. Print );

Client. hkeys ("hashkey", function (ERR, replies ){

Console. Log (replies. Length + "replies :");

Replies. foreach (function (reply, I ){

Console. Log ("" + I + ":" + reply );

});

Client. Quit ();

});

Iii. MySQL database

Iv. hbase database:

 

 

--- to be continued -----

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.