воскресенье, 22 июля 2012 г.

OrientDB - Java sample 1

1. The first step it is need to create a simple POJO. An empty constructor is mandatory.


public class Organization {
    String  name;
    String  description;


    public Organization() {
    }

    public Organization(String name, String description) {
        this.name = name;
        this.description = description;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

}

2. Next step, we open  a database and save POJO

    public void createNewOrg() {

//Open the database in a thread-safe way i.e. in every thread we must create new db object this way
//A database mydb must exist.
        OObjectDatabaseTx db = OObjectDatabasePool.global().acquire("remote:localhost/mydb", "admin", "admin");
        try {

            //Register our class in OrientDB Entity manager to work with POJO's
            db.getEntityManager().registerEntityClass(Organization.class);

            // create a new proxied object and fill its properties
            Organization org = db.newInstance(Organization.class);
            org.setName("MyOrg");
            org.setDescription("This is my good org.");
            db.save(org);
        } finally {

    //ALWAYS CLOSE TO RELEASE CONNECTION POOL RESOURCES
            db.close();
        }

    }

3. Next step we demonstrate a simple query


   public void browseAllOrgs() {
        // Open a database
        OObjectDatabaseTx db = OObjectDatabasePool.global().acquire("remote:localhost/mydb", "admin", "admin");
        try {
            //Register the class to work with it with Entity Manager
            db.getEntityManager().registerEntityClass(Organization.class);

            //Select all objects of class Organization and print their properties
            for (Organization orgItem : db.browseClass(Organization.class)) {
                System.out.print(orgItem.getName() + ' ');
                System.out.println(orgItem.getDescription());
            }

            System.out.println("-------------------");

            //Select all objects of class Organization were description contains word good
            // and print their properties
            List result = db.query(
                    new OSQLSynchQuery("select * from Organization where description like '%good%'"));

            for (Organization org : result) {
                System.out.print(org.getName() + ' ');
                System.out.println(org.getDescription());
            }
        } finally {
            //ALWAYS CLOSE
            db.close();
        }

    }

That's all.

PS. OObjectDatabasePool.global().acquire reuses the database connection to avoid to create it every time. db.close() doesn't close the database but release it to the owner pool. It could be reused in the future.

PPS. It is not necessary to create table (class) type of Organization before run this sample. New table (class) with name Organization  will be created during the first run.

PPPS. If you add a new property (field) to class Organization then at next run a new object  will appear in database with this new field. Older objects will remain unchanged.

пятница, 13 июля 2012 г.

Maven - add custom jar to maven project

...based on (http://blog.dub.podval.org/2010/01/maven-in-project-repository.html)

In this example I will add custom log4j-1.2.1234.jar to my project

1. Create folder with name lib in project root (where pom.xml is placed)

2. Add next structure to pom.xml

<repositories>
    < repository>
         < id > lib < /id>
        < name > lib < /name>
         < releases>
            < enabled > true < /enabled >
             < checksumPolicy > ignore < /checksumPolicy>
         < /releases>
         < snapshots>
             < enabled > false < /enabled>
         < /snapshots >
         < url > file://${project.basedir}/lib < /url>
     < /repository>
  < /repositories>



3. Then install necessary jar (log4j-1.2.1234.jar) to local maven repo:

mvn install:install-file -Dfile=log4j-1.2.1234.jar -DgroupId=myrepo -DartifactId=log4j -Dversion=1.2.1234 -Dpackaging=jar

4. Then go to $user_home/.m2/repository/myrepo/log4j/1.2.1234/ and copy all it contents to {$project root}/lib/myrepo/log4j/1.2.1234/

5. Add new dependency jar to project:


          < dependency >
             < groupId > myrepo < /groupId >
             < artifactId > log4j < /artifactId>
            < version > 1.2.1234 < /version>
         < /dependency >


That's all.


PS. Every time you compile this project on new machine jar's will be added to local maven repo of this new machine

воскресенье, 8 июля 2012 г.

OrientDB - create new table (class or object)


First way



1. To create new object or class simple call:
orientdb>  create class person

2. Then we need to define what properties of object person we need to store
create property person.firstname STRING
create property person.lastname STRING
create property person.middlename STRING
create property person.age INTEGER
create property person.birthdate DATE
create property person.country STRING
create property person.city STRING
create property person.address STRING
create property person.email STRING
create property person.phone STRING
create property person.ssn STRING

3. Then if we need that some field of object (class) must be unique then we define index
orientdb> create index person.ssn UNIQUE

 or

orientdb>CREATE INDEX persons ON person (ssn, email, pnone) UNIQUE

Second way


1. To dynamically create new object it is possible to point out property names and values directly

orientdb> create class group
orientdb> insert into group (name, description) values ('employee','Internal personnel')

orientdb> browse class group

---+---------+--------------------+--------------------
  #| RID     |name                |description
---+---------+--------------------+--------------------
  0|     #9:0|employee            |Internal personnel
---+---------+--------------------+--------------------

2. To make name of group unique we must define property name and index
orientdb> create property group.name STRING
orientdb> create index group.name UNIQUE

OrientDB - how to create new database

1. Download graph version of database from http://code.google.com/p/orient/downloads/list  and unzip it.

2. Run bin/server.bat  (server will generate new long password for user root)

3. Open file config/orientdb-server-config.xml with text editor and copy the password value of user root

4. Run bin/console.bat to access console (server is still running)

5. Run command in console (root password must be from orientdb-server-config.xml) :

orientdb> create database remote:localhost/mydb root 9E2B4BDEC420D87672DFFE5BBCA03C808A80D2618CE246F16102BE9D244072AA local graph

Database created successfully.

Current database is: remote:localhost/mydb


6. Now database is created with admin:admin (user:password) credentials. To change password

orientdb> update ouser set password = 'Secret13' where name = 'admin'

Updated 1 record(s) in 0,019000 sec(s).


7. Now disconnect from new database

orientdb> disconnect

Disconnecting from the database [mydb]...OK



8. Connect to new database using admin account

orientdb> connect remote:127.0.0.1/mydb admin Secret13
Connecting to database [remote:127.0.0.1/mydb] with user 'admin'...OK


That's all.