Monday, 24 April 2017

Homework-5.1-BLOG-MONGODB

Finding the most frequent author of comments on your blog
In this assignment you will use the aggregation framework to find the most frequent author of comments on your blog. We will be using a data set similar to ones we've used before.
Start by downloading the handout zip file for this problem. Then import into your blog database as follows:
mongoimport --drop -d blog -c posts posts.json
Now use the aggregation framework to calculate the author with the greatest number of comments.
To help you verify your work before submitting, the author with the fewest comments is Mariela Sherer and she commented 387 times.
Please choose your answer below for the most prolific comment author:

ANSWER:
Elizabet Kleine

db.posts.aggregate([{$unwind:"$comments"},{$group:{_id:"$co
mments.author",count:{$sum:1}}},{$sort:{count:-1}}])

Tuesday, 18 April 2017

System Error 1326 has occurred & Logon failure: unknown user name or bad password using command prompt

Using the windows command prompt

Unable to login into remote machine & following error was displayed in command prompt

System Error 1326 has occurred 
Logon failure: unknown user name or bad password

For solving the above issue

net use \\10.201.81.99\e$ /USER:domainname\username password

Sunday, 16 April 2017

M102-Homework-4.1-

In this chapter's homework we will create a replica set and add some data to it.
  1. Download the replication.js handout.
2. We will create a three member replica set. Pick a root working directory to work in. Go to that directory in a console window.
Given we will have three members in the set, and three mongod processes, create three data directories:
mkdir 1
mkdir 2
mkdir 3
3. We will now start a single mongod as a standalone server. Given that we will have three mongod processes on our single test server, we will explicitly specify the port numbers (this wouldn't be necessary if we had three real machines or three virtual machines). We'll also use the --smallfiles parameter and --oplogSize so the files are small given we have a lot of server processes running on our test PC.
# starting as a standalone server for problem 1:
mongod --dbpath 1 --port 27001 --smallfiles --oplogSize 50
Note: for all mongod startups in the homework this chapter, you can optionally use --logPath, --logappend, and --fork. Or, since this is just an exercise on a local PC, you could simply have a separate terminal window for all and forgo those settings. Run "mongod --help" for more info on those.
4. In a separate terminal window (cmd.exe on Windows), run the mongo shell with the replication.js file:
mongo --port 27001 --shell replication.js
Then run in the shell:
homework.init()
This will load a small amount of test data into the database.
Now run:
homework.a()
and enter the result. This will simply confirm all the above happened ok.
Answer:
5001


// replication.js

homework = { } 

var d = db.getSisterDB("replication");

homework.init = function() { 
    var rsConf; try { rsConf = rs.conf(); } catch (e) { rsConf = null; }
    if( rs.Conf != null ) { 
 print("at this stage of the homework the mongod should NOT be using --replSet");
 return;
    }
    if( d.foo.count() != 0 ) { 
 print("expected replication.foo collection to be empty to init. can't init. :-(");
 return;
    }
    for( var i = 0; i < 5000; i++ ) d.foo.insert({x:i,y:Math.random()});
    var result = db.getLastError();
    if( result ) print("Something is wrong? : " + result);
    else print("ok");
}

homework.a = function() { 
    return d.foo.stats().count + d.foo.stats().nindexes;
}

homework.b = function() { 
    if( !db.isMaster().ismaster ) {
 print("something is wrong. try again. initiate?");
 return;
    }
    if( rs.status().members.length != 1 ) { 
 print("only supposed to be one member of the set at this point. try again?");
 return;
    }
    return rs.status().members.length + d.foo.count() + 1;
}

homework.c = function() { 
    var s = rs.status();
    if( s.members.length != 3 ) { 
 print("something is wrong i don't see 3 members");
 return;
    }
    var x = 0;
    s.members.forEach( function(m){x+=m.state} );
    if( x > 7 ) { 
 print("something isn't right yet. did you wait for the new members to finish synchronizing?");
    }
    return x;
}

homework.d = function() {
    var s = rs.status();
    if( s.members.length != 2 ) {
 print("something is wrong i don't see 2 members");
 return;
    }
    var x = 0;
    s.members.forEach( function(m){x+=m._id+m.state} );
    if( x > 9 ) {
 print("something isn't right yet. did you wait for a new election to finish? want to see a primary and a secondary");
    }
    return x;
}

Tuesday, 11 April 2017

M101J-Homework-3.2,3.3- Updating the java BlogPostDAO

package course;

import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.Sorts;
import com.mongodb.client.model.Updates;
import org.bson.Document;
import org.bson.conversions.Bson;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class BlogPostDAO {
    MongoCollection<Document> postsCollection;

    public BlogPostDAO(final MongoDatabase blogDatabase) {
        postsCollection = blogDatabase.getCollection("posts");
    }

    // Return a single post corresponding to a permalink    public Document findByPermalink(String permalink) {

        // XXX HW 3.2,  Work Here        Document post = null;
        Bson filter = Filters.eq("permalink",permalink);
        List<Document> list = postsCollection.find(filter).into(new ArrayList<Document>());
        if (list.size() > 0)
            post = list.get(0);


        return post;
    }

    // Return a list of posts in descending order. Limit determines    // how many posts are returned.    public List<Document> findByDateDescending(int limit) {

        // XXX HW 3.2,  Work Here        // Return a list of DBObjects, each one a post from the posts collection        List<Document> posts = null;
           posts = postsCollection.find().sort(Sorts.descending("date")).limit(limit).into(new ArrayList<Document>());

        return posts;
    }


    public String addPost(String title, String body, List tags, String username) {

        System.out.println("inserting blog entry " + title + " " + body);

        String permalink = title.replaceAll("\\s", "_"); // whitespace becomes _        permalink = permalink.replaceAll("\\W", ""); // get rid of non alphanumeric        permalink = permalink.toLowerCase();


        // XXX HW 3.2, Work Here        // Remember that a valid post has the following keys:        // author, body, permalink, tags, comments, date, title        //        // A few hints:        // - Don't forget to create an empty list of comments        // - for the value of the date key, today's datetime is fine.        // - tags are already in list form that implements suitable interface.        // - we created the permalink for you above.
        // Build the post object and insert it        Document post = new Document();
        post.append("title",title).append("author",username).append("body",body).append("permalink",permalink).append("tags",tags).append("comments",new ArrayList<String>()). append("date",new Date());
        postsCollection.insertOne(post);

        return permalink;
    }




    // White space to protect the innocent







    // Append a comment to a blog post    public void addPostComment(final String name, final String email, final String body,
                               final String permalink) {

        // XXX HW 3.3, Work Here        // Hints:        // - email is optional and may come in NULL. Check for that.        // - best solution uses an update command to the database and a suitable        //   operator to append the comment on to any existing list of comments        if (permalink != null) {

            Document post = null;
            Bson filter = Filters.eq("permalink",permalink);
            List<Document> list = postsCollection.find(filter).into(new ArrayList<Document>());
            if (list.size() > 0)
                post = list.get(0);
            if (post != null) {
                Bson comments = Updates.push("comments",new Document("author",name).append("body",body).append("email",email));
                postsCollection.updateOne(post,comments);
            }

        }
    }
}

M101J-Homework3.1-Mongo DB using pull in java

Download the students.json file from the Download Handout link and import it into your local Mongo instance with this command:
mongoimport --drop -d school -c students students.json
This dataset holds the same type of data as last week's grade collection, but it's modeled differently. You might want to start by inspecting it in the Mongo shell.
Write a program in the language of your choice that will remove the lowest homeworkscore for each student. Since there is a single  document for each student containing an array of scores, you will need to update the scores array and remove the homework.
Remember, just remove a homework score. Don't remove a quiz or an exam!
Hint/spoiler: With the new schema, this problem is a lot harder and that is sort of the point. One way is to find the lowest homework in code and then update the scores array with the low homework pruned.
To confirm you are on the right track, here are some queries to run after you process the data with the correct answer shown:
Let us count the number of students we have:
use school
db.students.count()
The answer will be 200.
Let's see what Tamika Schildgen's record looks like once you have removed the lowest score:
db.students.find( { _id : 137 } ).pretty( )
This should be the output:
{
    "_id" : 137,
    "name" : "Tamika Schildgen",
    "scores" : [
        {
            "type" : "exam",
            "score" : 4.433956226109692
        },
        {
            "type" : "quiz",
            "score" : 65.50313785402548
        },
        {
            "type" : "homework",
            "score" : 89.5950384993947
        }
    ]
}
To verify that you have completed this task correctly, provide the identity (in the form of their _id) of the student with the highest average in the class with following query that uses the aggregation framework. The answer will appear in the _id field of the resulting document.
db.students.aggregate( [
  { '$unwind': '$scores' },
  {
    '$group':
    {
      '_id': '$_id',
      'average': { $avg: '$scores.score' }
    }
  },
  { '$sort': { 'average' : -1 } },
  { '$limit': 1 } ] )

Answer: 13

For solution the following code need to wirte

import com.mongodb.client.model.Filters;
import com.mongodb.client.model.Updates;
import org.bson.Document;
import org.bson.conversions.Bson;

import java.util.ArrayList;
import java.util.List;

/** * Created by DGuntha on 4/11/2017. */public class Homework31 {


    public static void main(String[] args) {
        MongoClient client = new MongoClient();
        MongoDatabase database = client.getDatabase("school");
        MongoCollection<Document> collection = database.getCollection("students");
        Bson filters = Filters.eq("scores.type","homework");
        List<Document> list =  collection.find(filters).into(new ArrayList<Document>());

        for (Document document : list) {
            List<Document> scoreList = (List<Document>)  document.get("scores");

            Document hw1 =  scoreList.get(2);
            double hw1Score = hw1.getDouble("score");

            Document hw2 =  scoreList.get(3);
            double hw2Score = hw2.getDouble("score");

            if (hw1Score > hw2Score) {

                Bson d = Updates.pull("scores",new Document("score",hw2Score));
                collection.updateOne(document,d);
            } else {
                //BasicDBObject update = new BasicDBObject("scores.score", hw1Score);
                //collection.update(doc, new BasicDBObject("$pull",update));                Bson d = Updates.pull("scores",new Document("score",hw1Score));
                collection.updateOne(document,d);
            }
        }
    }
}
Then use the aggregation then you get answer is 13
{ "_id" : 13, "average" : 91.98315917172745 }

Wednesday, 5 April 2017

M101J-HomeWork2.1-mongodb java

We are now going to test that you can run a Maven-based project that depends on the MongoDB Java driver, Spark, and Freemarker. Download spark-freemarker-mongodb.zip, uncompress it, cd to the spark-freemarker-mongodb directory, and run Maven as follows:
mvn compile exec:java -Dexec.mainClass=course.homework.MongoDBSparkFreemarkerStyle
Like the previous homework, it requires Maven to be installed correctly, your mongod server to be running, and that you have run mongorestore properly from HW 1.1.
If all is working correctly, there should be two lines towards the bottom that say:
== Spark has ignited ...
>> Listening on 0.0.0.0:4567
Next, open a tab in your web browser and navigate to http://localhost:4567. There should be a single line of text that starts with:
THE ANSWER IS:
Type the number that appears after the colon into the box below, no spaces or punctuation.
Answer: 2805

Creating a scheduler in windows and run the batch file.

Step for creating the windows schedulers:

My scenario of using Scheduler task in windows for testing Login to application & check application is accessible for every 2 hours . 



    1. Open the Control Panel. Select the System & Secuirty.
    2. Select the Administration Tools
    3. Double Click on the Task Scheduler shortcut
    4. On open of Task Scheduler, on Right hand side you will find the action menu.
    5. Select the Create Task as highlighted with green box.
    6. Create Task dialog is opened, enter the name details. 
    7. Click on the Tiggers tab as highlighted in green colour. Click on New.. button as highlighted with Orange colour.
    8.  Select the Begin the tasks. In my case i selected On a schedule option.
    9. In settings, i selected daily every one day as per my case
    10. Repeat task every 1 hour as default. As per my case it should be 2 hours. So i changed to 2 hours. For a duration of should be changed to Indefinitely. Press OK button.
    11. Click on the Action tab as highlighted in green colour. Click on New.. button as highlighted with Orange color.
    12. New Action dialog will open, click on Browse.. button for selecting the batch file.
    13. Enter the start in textbox. Path of the batch file. If your code need to run in particular location. Press Ok button
    14.  Press Ok button.
    15. Then you can run onDemand also & check works as expected.
       All the best for creating the scheduler for windows using batch file run.

Running jar file with lib/libraries in command prompt (or) running in bat file

Running Jar file in Command Prompt using lib folder

Make sure java is installed. To check is installed use command
java -version

Running jar file using command prompt or terminal
java -cp xxxx.jar;lib/*;. com.easycode.StartupClassname

where xxxx.jar is the jar file name
StartupClassname is main method present class.

Once successfully running the jar file command prompt. If you want create bat file follow below steps:

Use below code in notepad

@echo ON
SET DEVELOPMENT_HOME=%cd%
cd /d %DEVELOPMENT_HOME%
call java -cp xxxx.jar;lib/*;. com.easycode.StartupClassname

====================================================
Jar file running in the command prompt
Jar using lib folder running the command prompt
cmd run the jar file
lib add to jar in command prompt