OrientDB – Create Cluster

OrientDB – Create Cluster ”; Previous Next Cluster is an important concept in OrientDB which is used to store records, documents, or vertices. In simple words, cluster is a place where a group of records are stored. By default, OrientDB will create one cluster per class. All the records of a class are stored in the same cluster, which has the same name as the class. You can create up to 32,767(2^15-1) clusters in a database. The CREATE class is a command used to create a cluster with a specific name. Once the cluster is created, you can use the cluster to save records by specifying the name during the creation of any data model. If you want to add a new cluster to a class, use Alter Class command and ADDCLUSTER command. The following statement is the basic syntax of Create Cluster command. CREATE CLUSTER <cluster> [ID <cluster-id>] Where <cluster> defines the name of the cluster you want to create and <cluster-id> defines the numeric ID you want to use for the cluster. The following table provides the list of Cluster selection strategies. Sr.No. Strategy & Description 1 Default Selects the cluster using the class property default ClusterId. 2 Round-robin Selects the next cluster in a circular order. It is restarting once complete. 3 Balanced Selects the smallest cluster. Allows the class to have all underlying clusters balanced on size. When adding a new cluster to an existing class, it fills the new cluster first. Example Let us take an example to create a cluster named sales. orientdb> CREATE CLUSTER sales If the above query is executed successfully, you will get the following output. Cluster created correctly with id #12 Print Page Previous Next Advertisements ”;

OrientDB – Alter Cluster

OrientDB – Alter Cluster ”; Previous Next Alter Cluster command is to update attributes on an existing cluster. In this chapter you can learn how to add or modify the attributes of a cluster. The following statement is the basic syntax of Alter Cluster command. ALTER CLUSTER <cluster> <attribute-name> <attribute-value> Following are the details about the options in the above syntax. <cluster> − Defines the cluster name. <attribute-name> − Defines the attribute you want to change. <attribute-value> − Defines the value you want to set for this attribute. The following tabular format provides the list of supported attributes you can use along with Alter cluster command. Name Type Description NAME String Changes the cluster name. STATUS String Changes the cluster status. Allowed values are ONLINE and OFFLINE. By default, clusters are online. COMPRESSION String Defines the compression type to use. Allowed values are NOTHING, SNAPPY, GZIP, and any other compression types registered in the OCompressionFactory class. USE_WAL Boolean Defines whether it uses the Journal when OrientDB operates against the cluster RECORD_GROW_FACTO R Integer Defines the grow factor to save more space on record creation. You may find this useful when you update the record with additional information. RECORD_OVERFLOW_GR OW_FACTOR Integer Defines grow factor on updates. When it reaches the size limit, is uses this setting to get more space, (factor > 1). CONFLICTSTRATEGY String Defines the strategy it uses to handle conflicts in the event that OrientDB MVCC finds an update or a delete operation it executes against an old record. The following table provides the list of Conflict strategies. Sr.No. Strategy & Description 1 Version Throws an exception when versions are different. This is the default setting. 2 Content In the event that the versions are different, it checks for changes in the content, otherwise it uses the highest version to avoid throwing an exception. 3 Automerge Merges the changes. Example Try the following example queries to learn Alter cluster command. Execute the following command to change the name of a cluster from Employee to Employee2. orientdb {db = demo}> ALTER CLUSTER Employee NAME Employee2 If the above command is executed successfully, you will get the following output. Cluster updated successfully Execute the following command to change the name of a cluster from Employee2 to Employee using cluster ID. orientdb {db = demo}> ALTER CLUSTER 12 NAME Employee If the above command is executed successfully, you will get the following output. Cluster updated successfully Execute the following command to change the cluster conflict strategy to automerge. orientdb {db = demo}> ALTER CLUSTER V CONFICTSTRATEGY automerge If the above command is executed successfully, you will get the following output. Cluster updated successfully Print Page Previous Next Advertisements ”;

OrientDB – Truncate Class

OrientDB – Truncate Class ”; Previous Next Truncate class will delete all records of clusters defined as part of class. In OrientDB, every class has an associated cluster with the same name. If you want to also remove all records from the class hierarchy, you need to use the POLYMORPHIC keyword. The following statement is the basic syntax of Truncate Class command. TRUNCATE CLASS <class> [ POLYMORPHIC ] [ UNSAFE ] Following are the details about the options in the above syntax. <class> − Defines the class you want to truncate. POLYMORPHIC − Defines whether the command also truncates the hierarchy. UNSAFE − Defines the command forces truncation on vertex or edge class. Example The following query to truncate a class Profile. orientdb> TRUNCATE CLASS Profile If the above query is executed successfully, you will get the following output. Class truncated successfully Print Page Previous Next Advertisements ”;

OrientDB – Alter Class

OrientDB – Alter Class ”; Previous Next Class and Property in OrientDB are used to build a schema with the respective attributes such as class name, super-class, cluster, number of clusters, Abstract, etc. If you want to modify or update any attribute of existing classes in the schema then you have to use Alter Class command. The following statement is the basic syntax of the Alter Class Command. ALTER CLASS <class> <attribute-name> <attribute-value> Following are the details about the options in the above syntax. <class> − Defines the class name. <attribute-name> − Defines the attribute you want to change. <attribute-value> − Defines the value you want to set for the attribute. The following table defines the list of attributes that support Alter Class command. Attribute Type Description NAME String Changes the class name. SHORTNAME String Defines a short name, (that is, an alias), for the class. Use NULL to remove a short name assignment. SUPERCLASS String Defines a super-class for the class. To add a new class, you can use the syntax +<class>, to remove it use -<class>. OVERSIZE Decimal number Defines the oversize factor. ADDCLUSTER String Adds a cluster to the class. If the cluster doesn”t exist, it creates a physical cluster. Adding clusters to a class is also useful in storing records in distributed servers. REMOVECLUSTER String Removes a cluster from a class. It does not delete the cluster, only removes it from the class. STRICTMODE – Enables or disables strict mode. When in strict mode, you work in schema-full mode and cannot add new properties to a record if they are part of the class” schema definition. CLUSTERSELECTION – Defines the selection strategy in choosing which cluster it uses for new records. CUSTOM – Defines custom properties. Property names and values must follow the syntax <propertyname>=<value> without spaces between the name and value. ABSTRACT Boolean Converts class to an abstract class or the opposite. Example Let us try few examples that will update or modify the attributes of the existing class. The following query is used to define a super-class ‘Person’ for an existing class ‘Employee’. orientdb> ALTER CLASS Employee SUPERCLASS Person If the above query is executed successfully, you will get the following output. Class altered successfully The following query is used to add a super-class ‘Person’ for an existing class ‘Employee’. orientdb> ALTER CLASS Employee SUPERCLASS +Person If the above query is executed successfully, you will get the following output. Class altered successfully Print Page Previous Next Advertisements ”;

OrientDB – Drop Class

OrientDB – Drop Class ”; Previous Next The Drop Class command removes a class from the schema. It is important to pay attention and maintain a consistent schema. For example, avoid to remove classes that are super-classes of others. The associated cluster won”t be deleted. The following statement is the basic syntax of Drop Class command. DROP CLASS <class> Drop a class with the class name. Example Try the following query to Drop a class Employee. Orientdb> DROP CLASS Employee If the above query is executed successfully, you will get the following output. Class dropped successfully Print Page Previous Next Advertisements ”;

OrientDB – Truncate Record

OrientDB – Truncate Record ”; Previous Next Truncate Record command is used to delete the values of a particular record. The following statement is the basic syntax of the Truncate command. TRUNCATE RECORD <rid>* Where <rid>* indicates the Record ID to truncate. You can use multiple Rids separated by comma to truncate multiple records. It returns the number of records truncated. Example Let us consider the same Customer table that we have used in the previous chapter. Sr.No. Name Age 1 Satish 25 2 Krishna 26 3 Kiran 29 4 Javeed 21 5 Raja 28 Try the following query to truncate the record having Record ID #11:4. Orientdb {db = demo}> TRUNCATE RECORD #11:4 If the above query is executed successfully, you will get the following output. Truncated 1 record(s) in 0.008000 sec(s). To check the record of Customer table you can use the following query. Orientdb {db = demo}> SELECT FROM Customer If the above query is executed successfully, you will get the following output. —-+—–+——–+—-+——-+—- # |@RID |@CLASS |id |name |age —-+—–+——–+—-+——-+—- 0 |#11:0|Customer|1 |satish |25 1 |#11:1|Customer|2 |krishna|26 2 |#11:2|Customer|3 |kiran |29 3 |#11:3|Customer|4 |javeed |21 —-+—–+——–+—-+——-+—- Print Page Previous Next Advertisements ”;

OrientDB – Delete Record

OrientDB – Delete Record ”; Previous Next Delete Record command is used to delete one or more records completely from the database. The following statement is the basic syntax of the Delete command. DELETE FROM <Class>|cluster:<cluster>|index:<index> [LOCK <default|record>] [RETURN <returning>] [WHERE <Condition>*] [LIMIT <MaxRecords>] [TIMEOUT <timeout>] Following are the details about the options in the above syntax. LOCK − Specifies how to lock the records between load and update. We have two options to specify Default and Record. RETURN − Specifies an expression to return instead of the number of records. LIMIT − Defines the maximum number of records to update. TIMEOUT − Defines the time you want to allow the update run before it times out. Note − Don’t use DELETE to remove Vertices or Edges because it effects the integrity of the graph. Example Let us consider the Customer table. Sr.No. Name Age 1 Satish 25 2 Krishna 26 3 Kiran 29 4 Javeed 21 Try the following query to delete the record having id = 4. orientdb {db = demo}> DELETE FROM Customer WHERE id = 4 If the above query is executed successfully, you will get the following output. Delete 1 record(s) in 0.008000 sec(s). To check the record of Customer table you can use the following query. Orientdb {db = demo}> SELECT FROM Customer If the above query is executed successfully, you will get the following output. —-+—–+——–+—-+——-+—- # |@RID |@CLASS |id |name |age —-+—–+——–+—-+——-+—- 0 |#11:0|Customer|1 |satish |25 1 |#11:1|Customer|2 |krishna|26 2 |#11:2|Customer|3 |kiran |29 —-+—–+——–+—-+——-+—- Print Page Previous Next Advertisements ”;

OrientDB – Create Class

OrientDB – Create Class ”; Previous Next OrientDB supports multi-model feature and provides different ways in approaching and understanding the basic concepts of a database. However, we can easily access these models from the perspective of Document database API. Like RDBMS, OrientDB also uses the Record as an element of storage but it uses the Document type. Documents are stored in the form of Key/Value pairs. We are storing fields and properties as key/value pairs which belong to a concepts class. Class is a type of data model and the concept is drawn from the Object-oriented programming paradigm. Based on the traditional document database model, data is stored in the form of collection, while in the relational database model data it is stored in tables. OrientDB follows the Document API along with OPPS paradigm. As a concept, class in OrientDB has the closest relationship with the table in relational databases, but (unlike tables) classes can be schema-less, schema-full or mixed. Classes can inherit from other classes, creating trees of classes. Each class has its own cluster or clusters, (created by default, if none are defined). The following statement is the basic syntax of the Create Class Command. CREATE CLASS <class> [EXTENDS <super-class>] [CLUSTER <cluster-id>*] [CLUSTERS <total-cluster-number>] [ABSTRACT] Following are the details about the options in the above syntax. <class> − Defines the name of the class you want to create. <super-class> − Defines the super-class you want to extend with this class. <total-cluster-number> − Defines the total number of clusters used in this class. Default is 1. ABSTARCT − Defines the class is abstract. This is optional. Example As discussed, class is a concept related to table. Therefore here we will create a table Account. However, while creating class we cannot define fields i.e., properties based on OOPS paradigm. The following command is to create a class named Account. orientdb> CREATE CLASS Account If the above command is executed successfully, you will get the following output. Class created successfully You can use the following command to create a class Car which extends to class Vehicle. orientdb> CREATE CLASS Car EXTENDS Vehicle If the above command is executed successfully, you will get the following output. Class created successfully You can use the following command to create a class Person as abstract. orientdb> CREATE CLASS Person ABSTRACT If the above command is executed successfully, you will get the following output. Class created successfully Note − Without having properties, the class is useless and unable to build real object. In the further chapters, you can learn how to create properties for a particular class. Print Page Previous Next Advertisements ”;

OrientDB – Update Record

OrientDB – Update Record ”; Previous Next Update Record command is used to modify the value of a particular record. SET is the basic command to update a particular field value. The following statement is the basic syntax of the Update command. UPDATE <class>|cluster:<cluster>|<recordID> [SET|INCREMENT|ADD|REMOVE|PUT <field-name> = <field-value>[,]*] |[CONTENT| MERGE <JSON>] [UPSERT] [RETURN <returning> [<returning-expression>]] [WHERE <conditions>] [LOCK default|record] [LIMIT <max-records>] [TIMEOUT <timeout>] Following are the details about the options in the above syntax. SET − Defines the field to update. INCREMENT − Increments the specified field value by the given value. ADD − Adds the new item in the collection fields. REMOVE − Removes an item from the collection field. PUT − Puts an entry into map field. CONTENT − Replaces the record content with JSON document content. MERGE − Merges the record content with a JSON document. LOCK − Specifies how to lock the records between load and update. We have two options to specify Default and Record. UPSERT − Updates a record if it exists or inserts a new record if it doesn’t. It helps in executing a single query in the place of executing two queries. RETURN − Specifies an expression to return instead of the number of records. LIMIT − Defines the maximum number of records to update. TIMEOUT − Defines the time you want to allow the update run before it times out. Example Let us consider the same Customer table that we have used in the previous chapter. Sr.No. Name Age 1 Satish 25 2 Krishna 26 3 Kiran 29 4 Javeed 21 5 Raja 29 Try the following query to update the age of a customer ‘Raja’. Orientdb {db = demo}> UPDATE Customer SET age = 28 WHERE name = ”Raja” If the above query is executed successfully, you will get the following output. Updated 1 record(s) in 0.008000 sec(s). To check the record of Customer table you can use the following query. orientdb {db = demo}> SELECT FROM Customer If the above query is executed successfully, you will get the following output. —-+—–+——–+—-+——-+—- # |@RID |@CLASS |id |name |age —-+—–+——–+—-+——-+—- 0 |#11:0|Customer|1 |satish |25 1 |#11:1|Customer|2 |krishna|26 2 |#11:2|Customer|3 |kiran |29 3 |#11:3|Customer|4 |javeed |21 4 |#11:4|Customer|5 |raja |28 —-+—–+——–+—-+——-+—- Print Page Previous Next Advertisements ”;

OrientDB – Drop Database

OrientDB – Drop Database ”; Previous Next Similar to RDBMS, OrientDB provides the feature to drop a database. Drop database refers to removing a database completely. The following statement is the basic syntax of the Drop database command. DROP DATABASE [<database-name> <server-username> <server-user-password>] Following are the details about the options in the above syntax. <database-name> − Database name you want to drop. <server-username> − Username of the database who has the privilege to drop a database. <server-user-password> − Password of the particular user. Example There are two ways to drop a database, one is drop a currently open database and second is drop a particular database by providing the particular name. In this example, we will use the same database named ‘demo’ that we created in an earlier chapter. You can use the following command to drop a database demo. orientdb {db = demo}> DROP DATABASE If this command is successfully executed, you will get the following output. Database ”demo” deleted successfully OR You can use another command to drop a database as follows. orientdb> DROP DATABASE PLOCAL:/opt/orientdb/databases/demo admin admin If this command is successfully executed, you will get the following output. Database ”demo” deleted successfully Print Page Previous Next Advertisements ”;