Node.JS平台上的資料庫Redis,MongoDB,HBASE,MySQL

來源:互聯網
上載者:User

 

一. MongoDB:

    因為10gen是的贊助商之一,所以MongoDB有著良好的Node.JS支援。

    a. 基本支援:,在Node.JS對MongoDB的支援有兩種常用的組件mongodb, mongoose.下面分別介紹。

     (1)基於mongodb的支援。這個for Node.JS的驅動是基於事件驅動的,所以用法基本上都是非同步回調函方式。下載驅動組件$npm install -gd mongodb

     在testdb.js加入如下代碼:     

  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的支援。

  下載驅動組件$npm install -gd mongoose

     測試代碼如下:

     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 支援:

    由於MongoDB的BJSON資料存貯模式,天生的對JSON格式資料支援。在些條件下建立的REST支援最方便不過。

  1.-rest啟動參數對網頁管理的支援:

  2.快速搭建基於Node.JS,支援REST Web Service方式,JSON的格式MongoDB訪問平台:

    首先安裝mongodb 驅動:$npm install mongodb -gd.

    安裝mongodb的rest 組件:$npm install mongodb-rest -gd

    直接運行mongodb-rest.(在運行之前,找到這個組件目錄(在nodejs全域模組目錄如:/usr/local/nodejs/lib/node_modules/mongodb-rest下,找到config.json.修改其中的mongodb的伺服器位址連接埠為正確的資料,,不然會報unauthorized錯誤)

   如果說不能運行,且報說createServer錯誤,就要去修改組件目錄下的server.js把其中的var app = module.exports.app = express.createServer();改為var app = module.exports.app = express();

  代碼有點老了:)不過用來學習還是不錯的。

    此時就可以通過REST web Service方式來訪問mongoDB 了,我們可以用curl測試一下:

  $$ curl -d '{ "A1" : 201 }' -H "Content-Tyson" http://localhost:3000/test/example 

 

 

二,Redis資料庫:

   Redis的優缺點及應用情境在這裡就不提了,可以尋找我的別的日誌瞭解一下。如果用在複雜些的緩衝情境,用它是沒錯的,至少從目前技術角度來看。

 1.安裝Redis 資料庫。到官網下載安裝包。

http://redis.io/ 這裡應該是2.6.10版本了。

$tar -xzvf redis-2.6.10.tar.gz

$cd redis-2.6.10

$make

$make install

ok現在你可以用redis內建的用戶端來測試了

先運行Redis 伺服器

$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,一切正常

安裝Node.JS的Redis驅動模組:

$sudo npm install redis -gd

安裝完後可以寫代碼進行測試了:

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();

});

三,MySQL資料庫

四,HBase資料庫:

 

 

---待續-----

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.