Wednesday, June 14, 2017

Packaging executable jar and war files

Packaging executable jar and war files

Once spring-boot-maven-plugin has been included in your pom.xml it will automatically attempt to rewrite archives to make them executable using the spring-boot:repackage goal. You should configure your project to build a jar or war (as appropriate) using the usual packaging element:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <!-- ... -->
    <packaging>jar</packaging>
    <!-- ... -->
</project>
Your existing archive will be enhanced by Spring Boot during the package phase. The main class that you want to launch can either be specified using a configuration option, or by adding a Main-Class attribute to the manifest in the usual way. If you don’t specify a main class the plugin will search for a class with apublic static void main(String[] args) method.
To build and run a project artifact, you can type the following:
$ mvn package
$ java -jar target/mymodule-0.0.1-SNAPSHOT.jar
To build a war file that is both executable and deployable into an external container you need to mark the embedded container dependencies as “provided”, e.g:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <!-- ... -->
    <packaging>war</packaging>
    <!-- ... -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <!-- ... -->
    </dependencies>
</project>

Resource Link: http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#build-tool-plugins-maven-packaging

Tuesday, June 13, 2017

How to POST JSON data with Curl from Terminal/Command line to Test Spring REST?


Common Options

-#, --progress-bar Make curl display a simple progress bar instead of the more informational standard meter.
-b, --cookie <name=data> Supply cookie with request. If no =, then specifies the cookie file to use (see -c).
-c, --cookie-jar <file name> File to save response cookies to.
-d, --data <data> Send specified data in POST request. Details provided below.
-f, --fail Fail silently (don't output HTML error form if returned).
-F, --form <name=content> Submit form data.
-H, --header <header> Headers to supply with request.
-i, --include Include HTTP headers in the output.
-I, --head Fetch headers only.
-k, --insecure Allow insecure connections to succeed.
-L, --location Follow redirects.
-o, --output <file> Write output to . Can use --create-dirs in conjunction with this to create any directories specified in the -o path.
-O, --remote-name Write output to file named like the remote file (only writes to current directory).
-s, --silent Silent (quiet) mode. Use with -S to force it to show errors.
-v, --verbose Provide more information (useful for debugging).
-w, --write-out <format> Make curl display information on stdout after a completed transfer. See man page for more details on available variables. Convenient way to force curl to append a newline to output: -w "\n" (can add to ~/.curlrc).
-X, --request The request method to use.

POST

When sending data via a POST or PUT request, two common formats (specified via the Content-Type header) are:
  • application/json
  • application/x-www-form-urlencoded
Many APIs will accept both formats, so if you're using curl at the command line, it can be a bit easier to use the form urlencoded format instead of json because
  • the json format requires a bunch of extra quoting
  • curl will send form urlencoded by default, so for json the Content-Type header must be explicitly set
This gist provides examples for using both formats, including how to use sample data files in either format with your curlrequests.

curl usage

For sending data with POST and PUT requests, these are common curl options:
  • request type
    • -X POST
    • -X PUT
  • content type header
  • -H "Content-Type: application/x-www-form-urlencoded"
  • -H "Content-Type: application/json"
  • data
    • form urlencoded: -d "param1=value1&param2=value2" or -d @data.txt
    • json: -d '{"key1":"value1", "key2":"value2"}' or -d @data.json

Examples

POST application/x-www-form-urlencoded

application/x-www-form-urlencoded is the default:
curl -d "param1=value1&param2=value2" -X POST http://localhost:3000/data
explicit:
curl -d "param1=value1&param2=value2" -H "Content-Type: application/x-www-form-urlencoded" -X POST http://localhost:3000/data
with a data file
curl -d "@data.txt" -X POST http://localhost:3000/data

POST application/json

curl -d '{"key1":"value1", "key2":"value2"}' -H "Content-Type: application/json" -X POST http://localhost:3000/data
with a data file
curl -d "@data.json" -X POST http://localhost:3000/data
{
"key1":"value1",
"key2":"value2"
}
param1=value1&param2=value2
{
"name": "postdemo",
"version": "1.0.0",
"scripts": {
"start": "node server.js"
},
"dependencies": {
"body-parser": "^1.15.0",
"express": "^4.13.4"
}
}
var app = require('express')();
var bodyParser = require('body-parser');
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
app.post('/data', function (req, res) {
console.log(req.body);
res.end();
});
app.listen(3000);
fdsfl

Resource Link:
1. https://gist.github.com/subfuzion/08c5d85437d5d4f00e58
2. https://stackoverflow.com/a/7173011

Thursday, June 8, 2017

Job Search in Stackoverflow

MongoDB commands step by step

MongoDB commands:

sudo service mongodb start
sudo service mongodb stop
sudo service mongodb restart

sudo systemctl start mongodb
sudo systemctl status mongodb
sudo systemctl stop mongodb

To use MongoDB run the following command.
$ mongo

To show the database name, number of collection and documents in the database. 
$ db.stats()

If you want to create a database with name <sys>, then use DATABASE statement would be as follows − 
> use sys
switched to db sys

> db.stats()
{
"db" : "sys",
"collections" : 1,
"objects" : 3,
"avgObjSize" : 216.66666666666666,
"dataSize" : 650,
"storageSize" : 16384,
"numExtents" : 0,
"indexes" : 1,
"indexSize" : 16384,
"ok" : 1
}
> db
sys
> show dbs
admin  0.000GB
local  0.000GB
sys    0.000GB
user   0.000GB
users  0.000GB

> db.games.insert({"name":"football"})
WriteResult({ "nInserted" : 1 })

In MongoDB default database is test. If you didn't create any database, then collections will be stored in test database.

> db.dropDatabase()
This will delete the selected database. If you have not selected any database, then it will delete default 'test' database.

If you want to delete new database <sys>, then dropDatabase() command would be as follows 
> use sys
switched to db sys

Drop Database command:

> db.dropDatabase()
> { "dropped" : "sys", "ok" : 1 }

> show dbs
admin  0.000GB
local  0.000GB
user   0.000GB
users  0.000GB

Table creation command:

> db.createCollection("mycol", { capped : true, autoIndexId : true, size :
... 6142800, max : 10000 } )
{
"note" : "the autoIndexId option is deprecated and will be removed in a future release",
"ok" : 1
}

Drop table/collections command:

> db.mycollection.drop()
true

Show all tables:

> show collections
api_flows
games
mycol

Insert data into mycol table.
> db.mycol.insert({id:'7df78ad8902c',title: 'MongoDB Overview', description: 'MongoDB is no sql database',by: 'tutorials point',url: 'http://www.tutorialspoint.com',tags: ['mongodb', 'database', 'NoSQL'],likes: 100})

> db.mycol.insert({id:'dskfl456ldjk34',title: 'DynamoDB Overview', description: 'DynamoDB is no sql database',by: 'AWS',url: 'http://www.aws.com',tags: ['dynamodb', 'database', 'NoSQL'],likes: 230})
WriteResult({ "nInserted" : 1 })
>

Show data from mycol table into structured way:

> db.mycol.find().pretty()
{
"_id" : ObjectId("5938e67a8e351ecde46646a6"),
"id" : "7df78ad8902c",
"title" : "MongoDB Overview",
"description" : "MongoDB is no sql database",
"by" : "tutorials point",
"url" : "http://www.tutorialspoint.com",
"tags" : [
"mongodb",
"database",
"NoSQL"
],
"likes" : 100
}

Show data from mycol table into non-structured way:

> db.mycol.find()
{ "_id" : ObjectId("5938e67a8e351ecde46646a6"), "id" : "7df78ad8902c", "title" : "MongoDB Overview", "description" : "MongoDB is no sql database", "by" : "tutorials point", "url" : "http://www.tutorialspoint.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 }
>

Select last one from query using findOne():

> db.mycol.findOne()
{
"_id" : ObjectId("5938e67a8e351ecde46646a6"),
"id" : "7df78ad8902c",
"title" : "MongoDB Overview",
"description" : "MongoDB is no sql database",
"by" : "tutorials point",
"url" : "http://www.tutorialspoint.com",
"tags" : [
"mongodb",
"database",
"NoSQL"
],
"likes" : 100
}

AND Query expression in MongoDB:

> db.mycol.find({$and:[{"by":"tutorials point"},{"title": "MongoDB Overview"}]}).pretty()
{
"_id" : ObjectId("5938e67a8e351ecde46646a6"),
"id" : "7df78ad8902c",
"title" : "MongoDB Overview",
"description" : "MongoDB is no sql database",
"by" : "tutorials point",
"url" : "http://www.tutorialspoint.com",
"tags" : [
"mongodb",
"database",
"NoSQL"
],
"likes" : 100
}
>

Using AND & OR get expression together in MongoDB:

> db.mycol.find({"likes": {$gt:10}, $or: [{"by": "tutorials point"},
...    {"title": "MongoDB Overview"}]}).pretty()
{
"_id" : ObjectId("5938e67a8e351ecde46646a6"),
"id" : "7df78ad8902c",
"title" : "MongoDB Overview",
"description" : "MongoDB is no sql database",
"by" : "tutorials point",
"url" : "http://www.tutorialspoint.com",
"tags" : [
"mongodb",
"database",
"NoSQL"
],
"likes" : 100
}
>

Resource Link: https://www.tutorialspoint.com/mongodb/mongodb_create_database.htm

RDBMS Where Clause Equivalents in MongoDB

RDBMS Where Clause Equivalents in MongoDB:


To query the document on the basis of some condition, you can use following operations.

Operation
Syntax
Example
RDBMS Equivalent
Equality {<key>:<value>} db.mycol.find({"by":"tutorials point"}).pretty() where by = 'tutorials point'
Less Than {<key>:{$lt:<value>}} db.mycol.find({"likes":{$lt:50}}).pretty() where likes < 50
Less Than Equals {<key>:{$lte:<value>}} db.mycol.find({"likes":{$lte:50}}).pretty() where likes <= 50
Greater Than {<key>:{$gt:<value>}} db.mycol.find({"likes":{$gt:50}}).pretty() where likes > 50
Greater Than Equals {<key>:{$gte:<value>}} db.mycol.find({"likes":{$gte:50}}).pretty() where likes >= 50
Not Equals {<key>:{$ne:<value>}} db.mycol.find({"likes":{$ne:50}}).pretty() where likes != 50

ResourceLink: https://www.tutorialspoint.com/mongodb/mongodb_query_document.htm

Why and Where to use MongoDB?

Why Use MongoDB?
1. Document Oriented Storage − Data is stored in the form of JSON style documents.
2. Index on any attribute
3. Replication and high availability
4. Auto-sharding
5. Rich queries
6. Fast in-place updates
7. Professional support by MongoDB

Where to Use MongoDB?
1. Big Data
2. Content Management and Delivery
3. Mobile and Social Infrastructure
4. User Data Management
5. Data Hub