venerdì 28 dicembre 2012

MongoDB - MapReduce - Max e Min nel periodo

Utilizzo la piccola app del Meteo per fare qualche esperimento con le Map/Reduce
Devo ottenere il valore massimo e minino dei valori giornalieri, mensili e annuali da una collection contentente i logger raccolti ogni 5 minuti.

la collection sorgente (Meteolog):

{ "_id" : ObjectId("50d5a8a01e05acfe08c086fb"), "className" : "it.marcoberri.mbmeteo.model.Meteolog" , "n" : 1643, "time" : ISODate("2012-12-09T00:33:00Z"), "interval" : 5, "indoorHumidity" : 65, "indo orTemperature" : 19.5, "outdoorHumidity" : 72, "outdoorTemperature" : -0.6, "absolutePressure" : 997 .2, "wind" : 0, "gust" : 0.3, "direction" : "SE", "relativePressure" : 1025, "dewpoint" : -5, "windc hill" : -0.6, "hourRainfall" : 0, "dayRainfall" : 6.9, "weekRainfall" : 12.3, "monthRainfall" : 12.3 , "totalRainfall" : 12.3, "windLevel" : 0, "gustLevel" : 1 }

{ "_id" : ObjectId("50d5a8a01e05acfe08c086fc"), "className" : "it.marcoberri.mbmeteo.model.Meteolog" , "n" : 1644, "time" : ISODate("2012-12-09T00:38:00Z"), "interval" : 5, "indoorHumidity" : 65, "indo orTemperature" : 19.5, "outdoorHumidity" : 71, "outdoorTemperature" : -0.8, "absolutePressure" : 997 .3, "wind" : 0.3, "gust" : 0.7, "direction" : "S", "relativePressure" : 1025.1, "dewpoint" : -5.4, " windchill" : -0.8, "hourRainfall" : 0, "dayRainfall" : 6.9, "weekRainfall" : 12.3, "monthRainfall" : 12.3, "totalRainfall" : 12.3, "windLevel" : 1, "gustLevel" : 1 }
etc.


la funzionalità di map sul singolo giorno (variabile time_short)

map = function(){
    var fields =['outdoorTemperature','outdoorHumidity','absolutePressure','relativePressure','wind','gust','dewpoint','windchill',

'hourRainfall','dayRainfall','weekRainfall','monthRainfall','totalRainfall','windLevel','gustLevel'];

 var objs = {};

 for( var f=0; f<fields.length;f++) {
  var k = fields[f];
  objs[k] = { "min" : this[k], "max" : this[k]};
 }

 var time_short = this.time.getFullYear() + "-" + this.time.getMonth() + "-" + this.time.getDate();
 emit(time_short,objs)
}



La funzionalità di reduce:

reduce = function(key, values){
   var fields =['outdoorTemperature','outdoorHumidity','absolutePressure','relativePressure','wind','gust','dewpoint','windchill',

'hourRainfall','dayRainfall','weekRainfall','monthRainfall','totalRainfall','windLevel','gustLevel'];
   var res = values[0];
    for ( var i=1; i<values.length; i++ ) {
 for( var f=0; f<fields.length;f++) {
  var k = fields[f];
  var v = values[i][k];
         if ( v.min < res[k].min  )
            res[k].min = v.min;
         if ( v.max  > res[k].max  )
                   res[k].max = v.max;
 }
    }
    return res;
}


Usiamo le funzionalità:

db.Meteolog.mapReduce(map,reduce,{out:{merge:"reduceMaxMinDay"}});
db.reduceMaxMinDay.find();

result:

{ "_id" : "2012-11-23", "value" : { "outdoorTemperature" : { "min" : -2.3, "max" : 5.6 }, "outdoorHu midity" : { "min" : 79, "max" : 90 }, "absolutePressure" : { "min" : 996.8, "max" : 999 }, "relative Pressure" : { "min" : 1024.6, "max" : 1026.8 }, "wind" : { "min" : 0, "max" : 0.7 }, "gust" : { "min " : 0, "max" : 1 }, "dewpoint" : { "min" : -4.7, "max" : 2.4 }, "windchill" : { "min" : -2.3, "max" : 5.6 }, "hourRainfall" : { "min" : 0, "max" : 0 }, "dayRainfall" : { "min" : 0, "max" : 0 }, "weekR ainfall" : { "min" : 0, "max" : 0 }, "monthRainfall" : { "min" : 4.2, "max" : 4.2 }, "totalRainfall" : { "min" : 4.2, "max" : 4.2 }, "windLevel" : { "min" : 0, "max" : 1 }, "gustLevel" : { "min" : 0, "max" : 1 } } }

{ "_id" : "2012-11-9", "value" : { "outdoorTemperature" : { "min" : -2.3, "max" : 13.4 }, "outdoorHu midity" : { "min" : 26, "max" : 75 }, "absolutePressure" : { "min" : 992.9, "max" : 999.4 }, "relati vePressure" : { "min" : 1020.7, "max" : 1027.2 }, "wind" : { "min" : 0, "max" : 1 }, "gust" : { "min " : 0, "max" : 1.7 }, "dewpoint" : { "min" : -7.3, "max" : 0.3 }, "windchill" : { "min" : -2.3, "max " : 13.4 }, "hourRainfall" : { "min" : 0, "max" : 8.1 }, "dayRainfall" : { "min" : 0, "max" : 12.3 } , "weekRainfall" : { "min" : 0, "max" : 20.4 }, "monthRainfall" : { "min" : 0, "max" : 20.4 }, "tota lRainfall" : { "min" : 0, "max" : 20.4 }, "windLevel" : { "min" : 0, "max" : 1 }, "gustLevel" : { "m in" : 0, "max" : 2 } } } etc.

per modificare il periodo di calcolo basta modficare la variabile che permette il raggruppamento delle info:
es: mesile --> var time_short = this.time.getFullYear() + "-" + this.time.getMonth();
es: annulale -->  var time_short = this.time.getFullYear();

giovedì 27 dicembre 2012

MBMeteo

Dopo aver preso una piccola stazione meteo salta fuori la app fatta in java.






  • JFreeChart
  • Quartz
  • Morphia

  • ExtJs
  • Bootstrap

  • MongoDB
  • Jetty
il Sito: http://mbmeteo.marcoberri.it/
Sorgenti: http://code.google.com/p/mbmeteo/

domenica 11 novembre 2012

NetBeans 7.2 + Google App Engine

1) Donwload appengine sdk.

2) unzip file


3) Open NetBeans -- tools - plugins

4) add new Url http://kenai.com/downloads/nbappengine/NetBeans7.2/updates.xml


5) install plug-in

6) add new server


7) Start new project (Web Application) with Server Google App Engine.

martedì 25 settembre 2012

Import Geonames DB on MongoDB with NodeJs and Query


Continuo a pensare che le prestazioni di MongoDB siano veramente buone, ancora meglio se unite ad un linguaggio di scripting come NodeJs.

Ecco un esempio di utilizzo su moli di dati consistenti (testato con server MongoDB in locale quindi con poche risorse di sistema).

Resources:

Installare i seguenti moduli

MongoDB ODM Mongoose
npm install mongoose

Modulo per leggere file di grosse dimensioni
npm install carrier

Lib per il calcolo delle query
npm install moment

Il solito file da 1,2 GB di Geonames che uso per i test di una certa consistenza.


Procedura di import,
definiamo la struttura della collection, poi si prosegue con l'inserimento delle info.


var mongoose = require('mongoose');
var databaseName = 'geonames';
var connection_string = 'mongodb://localhost/'+databaseName;
var db = mongoose.connect(connection_string);

var schema_geonames = mongoose.Schema({ 
     geonameid :  Number, 
     name  :  String, 
     asciiname   :  String,
     alternatenames  :  String,
     loc  :       [Number],
     feature_class   :  String,
     feature_code :  String,
     country_code   :  String,
     alternatenames  :  String,
     cc2  :  String,
     admin1         :  String,
     admin2  :  String,
     admin3  :  String,
     admin4  :  String,
     population :  String,
     elevation :  Number,
     dem         :  String,
     timezone :  String,
     modification_date :  String
       });

schema_geonames.index ({
       loc : "2d",
       asciiname : 1,
       name : 1
});


var Geonames = db.model('Geonames', schema_geonames);

var carrier = require('carrier');
var fs = require('fs');
var filename = '/path/of/file/allCountries.txt';
var inStream = fs.createReadStream(filename, {flags:'r'});

carrier.carry(inStream)
 .on('line', 
 
  function(line) {
      
   var fields =  line.split('\t');
   
   var geonames = new Geonames({
     geonameid  :  fields[0], 
     name   :  fields[1],       
     asciiname    :  fields[2],
     alternatenames   :  fields[3],
     loc   :       [fields[4],fields[5]],
     feature_class    :  fields[6],
     feature_code  :  fields[7],
     country_code    :  fields[8],
     alternatenames   :  fields[9],
     cc2   :  fields[10],
     admin1          :  fields[11],
     admin2   :  fields[12],
     admin3   :  fields[13],
     admin4   :  fields[14],
     population  :  fields[15],
     elevation  :  fields[16],
     dem   :  fields[17],
     timezone  :  fields[18],
     modification_date :  fields[19]
   });
   
   
   
   geonames.save(function (err) {
    if (err)
     console.log('Error save geonames');
   });  

      
  } 
 )
 
 .on('end',
 
  function(){
        console.log('end');   
        process.exit(1);
   }
 );
db.geonames.find().count(); 8309522 Dopo aver aspettato che a procedura di import abbia concluso possiamo eseguire qualche semplice query es di $near:


GeonamesModel.find({ feature_class: 'P', loc : { $near : [45.32306, 8.41533] }} ,{},{ limit: 20 }, function(err, docs){
 
      if (err) {
          console.log("error in finding near", err);
          throw err;
      }
      
      console.log('docs.length : ' , docs.length);
      
      docs.forEach(function(doc) 
       { 
        console.log( doc.asciiname + ' (' + doc.admin1 + ')');
       }
      )

  })

Risultato: 
docs.length : 20 
Vercelli (VC) 45.32306,8.41533 
Larizzate (VC) 45.3,8.38333 
Caresanablot (VC) 45.35736,8.39203 
Torrione (VC) 45.31667,8.46667 
Borgo Vercelli (VC) 45.35786,8.46303 
Scavarda (NO) 45.33218,8.47477 
Asigliano Vercellese (VC) 45.26146,8.40853 
Villata (VC) 45.38776,8.43263 
Prarolo (VC) 45.28206,8.47814 
Desana (VC) 45.26966,8.35973 
Quinto Vercellese (VC) 45.37966,8.36173 
Lignana (VC) 45.28606,8.34393 
Oldenico (VC) 45.40276,8.38103 
Sali Vercellese (VC) 45.30986,8.32893 
Pertengo (VC) 45.23556,8.41754 
Casalvolone (NO) 45.40096,8.46463 
Pezzana (VC) 45.26206,8.48504 
Costanzana (VC) 45.23816,8.36813 
Collobiano (VC) 45.39686,8.34833 
Stroppiana (VC) 45.23026,8.45384 
Tot time 205ms

[Sorgenti completi]

venerdì 14 settembre 2012

MBFastTrack - Online la Beta

Online la Beta di MBFastTrack. 

Applicativo realizzato in Java con Spring, MongoDB e Bootstrap.

E' una beta e prendetela come tale ancora pieni di bachi... ;)





martedì 4 settembre 2012

PHP - Problemi con EOL

Per qualche strano motivo il testo contenuto in una variabile continua ad andare a capo, questa cosa non è gradita alla funzione javascript a cui passo il valore, dopo varie ricerche e prove riesco a trovare una soluzione.



 $html = '' . $var1 . '' . $var2;
 $html = str_replace("\\r\\n", "", addslashes(trim($html)));
 $html = str_replace(PHP_EOL,"",$html);
 $html = str_replace(array("\r", "\n"), '', $html);


il PHP alle volte è veramente ostico.

mercoledì 13 giugno 2012

MongoDB - decode failed on shell




PRIMARY> show dbs;
Wed Jun 13 10:45:05 decode failed. probably invalid utf-8 string [pJ@??]
Wed Jun 13 10:45:05 why: TypeError: malformed UTF-8 character sequence at offset 3
Wed Jun 13 10:45:05 Error: invalid utf8 shell/utils.js:1237


AAAAAAA!!!!!!!

/etc/init.d/mongodb stop
/etc/init.d/mongodb start

PRIMARY> show dbs;
admin 0.203125GB
db0 0.203125GB
db1 0.453125GB
db2 0.203125GB
db3 0.203125G
...
...
..


OK!!!!