viernes, 10 de julio de 2015

GIT

Git tutorial:
http://www.vogella.com/tutorials/Git/article.html

Just to know
  • Bare Repository: A remote repository on a server typically does not require a working tree. A Git repository without a working tree is called a bare repository. are used on servers to share changes coming from different developers
  • No-bare Repository: Allow you to create new changes through modification of files and to create new versions in the repository
  • Always need to initialize a git repository before call any commands

Creating Bare repository (shared/server repository)
  • git --bare init

Creating Non-bare repository (local?developer repository)
  • git init

Global Configuration (applies to all repositories)
  • Configure your user and email for Git
  • # configure the user which will be used by Git
  • # this should be not an acronym but your full name
  • git config --global user.name "Firstname Lastname" 
  • # configure the email address 
  • git config --global user.email "your.email@example.org"
  • Configures Git to push only the active branch ( for older versions )
  • # set default so that only the current branch is pushed
  • git config --global push.default simple 

Adding remote repository
  • Inside the non-bare repository type the following command:
  • git remote add origin /home/etc/repo.git
  • git remote add origin http://bla.com/github/bla

List the remote repository (in the non bare repository)
  • git remote -v

Pushing from the non-bare (developer) repository to bare (shared) repository
  • git add .
  • git commit -m "init commit"
  • git push origin master
origin=>the name of the repo
master=>the default branch


Cloning from the bare repository to non-bare repository
  • Initialize a git non bare repository
  • $ git init
  • Initialized empty Git repository in /home/Damian Ciocca/clone.git3/.git/
  • $ git remote add origin /home/etc/repo.git
  • $ git clone /home/Damian\ Ciocca/repo.git/ repo
  • Cloning into 'repo'...
  • done.

Pulling from the bare repository to non-bare repository
  • git pull orifin master

Another commands
  • git log
  • git status -s
  • git remote -v

miércoles, 8 de julio de 2015

Elasticsearch (Lucene)

Elasticsearch Basic Concepts Link:

Elasticsearch excellent tutorial:

Just to know:
  • First way to talk to the Elasticsearh is over the port 9300, using the native Elasticsearch transport protocol
  • The second way to talk to the Elasticsearh is over port 9200 using a RESTful API.
  • You can even talk to Elasticsearch from the command line by using the curl command
  • A document belongs to a type, and those types live inside an index. We can draw some (rough) parallels to a traditional relational database: 
  • Relational DB ⇒ Databases ⇒ Tables ⇒ Rows ⇒ Columns
  • Elasticsearch ⇒ Indices ⇒ Types ⇒ Documents ⇒ Fields 
  • An Elasticsearch cluster can contain multiple indices (databases), which in turn contain multiple types(tables). These types hold multiple documents (rows), and each document has multiple fields (columns). (https://www.elastic.co/guide/en/elasticsearch/guide/current/_indexing_employee_documents.html)
  • To identify unequivocally to one document, we need to know is: a index, type and id.

A request to Elasticsearch consists of the same parts as any HTTP request:
  • curl -X<VERB> '<PROTOCOL>://<HOST>/<PATH>?<QUERY_STRING>' -d '<BODY>'
  • Note:
VERB The appropriate HTTP method or verb: GET, POST, PUT, HEAD, or DELETE.
QUERY_STRING Any optional query-string parameters (for example ?pretty will pretty-print the JSON response to make it easier to read.)
BODY A JSON-encoded request body (if the request needs one.)

CURL Example:
  •  curl -i -X GET localhost:9200/_count?pretty
  • Note:
-i allows to see the http headers

Creation document example using REST API:

PUT
  • curl -i -X PUT localhost:9200/megacorp/employee/1 -d '{
    "first_name" : "John",
    "last_name" :  "Smith",
    "age" :        25,
    "about" :      "I love to go rock climbing",
    "interests": [ "sports", "music" ]}'
  • Note:
The path /megacorp/employee/1 contains three pieces of information: 
megacorp => The index (database) name
employee => The type name (table)
1 => The ID of this particular employee (row)
-d (....)  => The Json document (row)
  • curl -i -X PUT localhost:9200/megacorp/employee/1/_create -d '{}'
HTTP/1.1 409 ConflictContent-Type: application/json; charset=UTF-8
Content-Length: 110
{"error":"DocumentAlreadyExistsException[[megacorp][3] [employee][40]: document already exists]","status":409}
  • Note:
In that case you can see that we can use _create to help us in order to prohibit create an existing document.
POST
If our data doesn’t have a natural ID, we can let Elasticsearch autogenerate one for us. Here we show how to use POST instead of use PUT to autogenerate the ID.
  • curl -i -X PUT localhost:9200/megacorp/employee/4 -d '{
    "first_name" : "John",
    "last_name" :  "Smith",
    "age" :        25,
    "about" :      "I love to go rock climbing",
    "interests": [ "sports", "music" ]}'
HTTP/1.1 201 CreatedContent-Type: application/json; charset=UTF-8Content-Length: 78{"_index":"megacorp","_type":"employee","_id":"4","_version":1,"created":true}
  • curl -i -X POST localhost:9200/megacorp/employee/ -d '{
    "first_name" : "John",
    "last_name" :  "Smith",
    "age" :        25,
    "about" :      "I love to go rock climbing",
    "interests": [ "sports", "music" ]}'
HTTP/1.1 201 CreatedContent-Type: application/json; charset=UTF-8Content-Length: 97{"_index":"megacorp","_type":"employee","_id":"AU5uz7mdzFHxYLzn_JYw","_version":1,"created":true}
Note that the he response is similar to what we saw before, except that the _id field has been generated for us 
Remember that the combination of _index, _type, and _id uniquely identifies a document. So the easiest way to ensure that our document is new is by letting Elasticsearch autogenerate a new unique_id, using the POST version of the index request.

Retrieve document example using REST API (simple mode)

GET
  • $ curl -i -X GET localhost:9200/megacorp/employee/3?pretty
HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
Content-Length: 290

{
  "_index" : "megacorp",
  "_type" : "employee",
  "_id" : "3",
  "_version" : 1,
  "found" : true,
  "_source":{
    "first_name" :  "Douglas",
    "last_name" :   "Fir",
    "age" :         35,
    "about":        "I like to build cabinets",
    "interests":  [ "forestry" ]
}
}
  • $ curl -i -X GET localhost:9200/megacorp/employee/12?pretty
HTTP/1.1 404 Not Found
Content-Type: application/json; charset=UTF-8
Content-Length: 92

{
  "_index" : "megacorp",
  "_type" : "employee",
  "_id" : "12",
  "found" : false
}

Retrieve part of a document example using REST API 

GET
  • $ curl -i -X GET localhost:9200/megacorp/employee/4?_source=first_name,age
HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
Content-Length: 117

{"_index":"megacorp",
 "_type":"employee",
 "_id":"4",
 "_version":1,
 "found":true,
 "_source":{"first_name":"John","age":25}
}

Retrieve just a document without any metadata example using REST API 

GET
  • $ curl -i -X GET localhost:9200/megacorp/employee/4/_source
HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
Content-Length: 168
{
    "first_name" : "John",
    "last_name" :  "Smith",
    "age" :        25,
    "about" :      "I love to go rock climbing",
    "interests": [ "sports", "music" ]}

Retrieve document example using REST API (DSL mode)

GET
  • curl -i -X GET localhost:9200/megacorp/employee/_search -d '{
    "query" : {
        "match" : {
            "last_name" : "Smith"
                  }
              } 
   }'


Check if any document exists:

HEAD (because this action does not return the body, just HTTP headers)
  • $ curl -i -X HEAD localhost:9200/megacorp/employee/4
HTTP/1.1 200 OK
Content-Type: text/plain; charset=UTF-8
Content-Length: 0
  • $ curl -i -X HEAD localhost:9200/megacorp/employee/121
HTTP/1.1 404 NOT FOUND
Content-Type: text/plain; charset=UTF-8
Content-Length: 0

Update a document

PUT (twice)
  • $ curl -i -X PUT localhost:9200/megacorp/employee/40 -d '{
>     "first_name" : "John",
>     "last_name" :  "Smith",
>     "age" :        25,
>     "about" :      "I love to go rock climbing",
>     "interests": [ "sports", "music" ]}'

HTTP/1.1 201 Created
Content-Type: application/json; charset=UTF-8
Content-Length: 79
{"_index":"megacorp","_type":"employee","_id":"40","_version":1,"created":true}

  • $ curl -i -X PUT localhost:9200/megacorp/employee/40 -d '{
>     "first_name" : "John",
>     "last_name" :  "Smith",
>     "age" :        25,
>     "about" :      "I love to go rock climbing",
>     "interests": [ "sports", "music" ]}'

HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
Content-Length: 80
{"_index":"megacorp","_type":"employee","_id":"40","_version":2,"created":false}



















lunes, 6 de julio de 2015

Couchbase (NoSQL) & Java SDK

Couchbase Basic Concepts Link
https://es.wikipedia.org/wiki/CouchDB
https://en.wikipedia.org/wiki/Couchbase_Server
http://docs.couchbase.com/admin/admin/Misc/admin-basics.html
http://docs.couchbase.com/admin/admin/Views/views-intro.html

Couchbase example (install, create data and query data) 
http://tugdualgrall.blogspot.com.ar/2012/07/couchbase-101-install-store-and-query.html

Couchbase querying
http://docs.couchbase.com/admin/admin/Views/views-querySample.html
http://hardlifeofapo.com/basic-couchbase-querying-for-sql-people/

Couchbase & Java
http://docs.couchbase.com/developer/java-2.1/java-intro.html
http://docs.couchbase.com/developer/java-2.1/documents-basics.html
http://www.javacodegeeks.com/2013/01/couchbase-101-create-views-mapreduce-from-your-java-application.html
https://dzone.com/articles/hello-world-couchbase-and-java

Just to know:
  • 8092 is the Couch API REST port used to access data (where 8091 is the port for the Admin console)
  • default is the bucket in which the document is stored
  • A bucket is used by Couchbase to store data. It could be compared to a “database” in RDBMS world.
  • Couchbase can store any type of data, but when you need to manipulate some data with a structure the best way is to use JSON Documents. 
  • Access document directly from its ID: Use the REST API in port 8092)
  • Search your data with queries: Use Views (written in javascripts)

Quickly creation (via REST API / port 8092)

You can use the REST API to create the documents or buckets (databases)

Bucket (database) creation:
  • curl -X PUT 'http://localhost:8092/defaultDB
Document creation:
  • curl -X PUT 'http://localhost:8092/defaultDB/10' -d '{"name":"My Name 1"}'
In the couchbase server you will find the following new json document with the ID = 10:

{
"name": "My Name 1"

}

lunes, 29 de junio de 2015

Eclipse & XML

Para poder integrar con eclipse XML tenemos que instalar el plung llamado WTP (Web Tool Plugin)

https://eclipse.org/webtools/

o bien buscar en

https://marketplace.eclipse.org/

Acceso directo: https://marketplace.eclipse.org/content/eclipse-wtpxml-search

Mediante el Marketplace de Eclipse, podemos descargar dicho plugin:





Nota: La versión de eclipse Luna es la 4.4.x







miércoles, 1 de abril de 2015

Semaphores in java

Please, see this tutorial

http://tutorials.jenkov.com/java-concurrency/semaphores.html

Example of Semaphore as a lock:

public class Semaphore {

private static final Logger LOG = LoggerFactory.getLogger(Semaphore.class);
private boolean signal = true; // It means that the first threads could get the light green

/**

*/
public Semaphore() {

super();
}

/**

*/
public synchronized void release() {

this.signal = true;
this.notifyAll();
}

/**

*/
public synchronized void take() {

while(this.signal == false) {
try {
wait();
} catch (InterruptedException e) {
LOG.error(e.getMessage(), e);
}
}
this.signal = false;
}
}