Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save chunni/1cae5389dd18ee48b277 to your computer and use it in GitHub Desktop.
Save chunni/1cae5389dd18ee48b277 to your computer and use it in GitHub Desktop.

Revisions

  1. chunni revised this gist Jul 11, 2014. 1 changed file with 0 additions and 3 deletions.
    3 changes: 0 additions & 3 deletions Using cron to perform incremental Map-Reduce in MongoDB
    Original file line number Diff line number Diff line change
    @@ -1,4 +1 @@
    Using cron to perform incremental Map-Reduce in MongoDB
    This is for the blog of:
    - [Perform incremental data aggregation with MongoDB's MapReduce and cron](http://meetfp.com/en/blog/mongo-mapreduce-cron)
    - [用MongoDB的增量MapReduce和cron实现增量计数](http://meetfp.com/zh/blog/mongo-mapreduce-cron)
  2. chunni revised this gist Jul 11, 2014. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions Using cron to perform incremental Map-Reduce in MongoDB
    Original file line number Diff line number Diff line change
    @@ -1 +1,4 @@
    Using cron to perform incremental Map-Reduce in MongoDB
    This is for the blog of:
    - [Perform incremental data aggregation with MongoDB's MapReduce and cron](http://meetfp.com/en/blog/mongo-mapreduce-cron)
    - [用MongoDB的增量MapReduce和cron实现增量计数](http://meetfp.com/zh/blog/mongo-mapreduce-cron)
  3. chunni revised this gist Jul 11, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Using cron to perform incremental Map-Reduce in MongoDB
    Original file line number Diff line number Diff line change
    @@ -1 +1 @@
    Using cron to perform incremental Map-Reduce in MongoDB
    Using cron to perform incremental Map-Reduce in MongoDB
  4. chunni renamed this gist Jul 11, 2014. 1 changed file with 0 additions and 0 deletions.
  5. chunni revised this gist Jul 11, 2014. 6 changed files with 1 addition and 0 deletions.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    1 change: 1 addition & 0 deletions Using cron to perform incremental Map-Reduce in MongoDB
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    Using cron to perform incremental Map-Reduce in MongoDB
  6. chunni revised this gist Jul 11, 2014. No changes.
  7. chunni created this gist Jul 11, 2014.
    1 change: 1 addition & 0 deletions cron_job
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    01 0 * * * /path/to/inc_tags_job.sh
    27 changes: 27 additions & 0 deletions inc_tags.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    //convert ISO data to ObjectId so that it can be compared with ObjectIds in db
    var ISODateToObjectId = function(date){
    // Convert date to hex seconds
    var hexSeconds = Math.floor(date.getTime()/1000).toString(16);

    // Create an ObjectId with the hex timestamp
    var id = ObjectId(hexSeconds + "0000000000000000");

    return id
    }

    var conn = new Mongo();
    var db = conn.getDB("your_db");

    var start = new Date();
    start.setDate(start.getDate() - 1)

    // The Map and the Reduce functions have been stored into system.js
    // and they can be used in the mapReduce function now
    db.yeedd.mapReduce( mapTags,
    reduceTags,
    {
    query: { _id: { $gt: ISODateToObjectId(start) } },
    out: { reduce: "calc_tags" }
    }
    );
    }
    3 changes: 3 additions & 0 deletions inc_tags.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    # execute javascript file with mongo shell.
    # please use your own file path and database path instead
    /usr/bin/mongo "localhost:27017/db" /path/to/inc_tags.js
    45 changes: 45 additions & 0 deletions init_tags.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    //The Map function, simply count tags
    var mapTags = function() {
    if (!this.tags) {
    return;
    }

    for (index in this.tags) {
    emit(this.tags[index], 1);
    }
    };

    //The Reduce function, accumulate the count
    var reduceTags = function(previous, current) {
    var count = 0;

    for (index in current) {
    count += current[index];
    }

    return count;
    }

    var conn = new Mongo();
    var db = conn.getDB("your_db");

    //save the functions
    db.system.js.save(
    { _id: "mapTags",
    value : mapTags
    }
    )

    db.system.js.save(
    { _id: "reduceTags",
    value : reduceTags
    }
    )
    //execute the Map-Reduce,
    //the results will be stored in the calc_tags collection
    db.yeedd.mapReduce( mapTags,
    reduceTags,
    {
    out: "calc_tags"
    }
    )
    3 changes: 3 additions & 0 deletions init_tags.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    # execute javascript file with mongo shell.
    # please use your own file path and database path instead
    /usr/bin/mongo "localhost:27017/db" /path/to/init_tags.js