Creating the Database and Other Database Operations
Once the Session is established you can start creating a database. The first step in this case is to create a database where you will store your data in the documents. A database must be created with lowercase characters (a-z), or digits (0-9), or any of the _$()+-/ characters and it must end with a slash in the URL. The name of the database has to start with characters. Also, a / character in a database name must be escaped when used in a URL. You can now create a database named "employee":
String dbname = "employee"; dbSession.createDatabase(dbname);
Here dbSession is the Session object which we created earlier and createDatabase is the method used to create database. The parameter for the createDatabase method is the name of the database (in String).
You can also retrieve the list of all the databases that are present in the CouchDB server. The following code shows how:
List <String> listofdb = dbSession.getDatabaseNames();
Here listofdb is the String list which will hold the result (that are the names of databases present) obtained by calling the getDatabaseNames method.
Here dbSession is the Session object which created earlier. Parameters for the deleteDatabase method will be the name of the database (in String).
Once you have created a database named "employee", you need to enter data to the database. With CouchDB, data is in the form of documents, so your next step will be to create documents for "employee" database.
Documents stored in a CouchDB have a document id -- DocID. DocIDs are case-sensitive string identifiers that uniquely identify a document. Documents can be created in two ways:
Now consider that you are having data of employee's in an organization:
Here EmpNO, Name, Group, Designation, Language are the field names. You can create a document for each of the employee (i.e. for each row in Table 1) providing information for each field as shown in Table 1. You will create all the documents inside the database "employee". In this case, I provide the DocID and it will be same as EmpNo field for each employee.
The following code illustrations the process of creating a document for the first employee (for first row data) only, similarly documents can be created for other employees also:
In this code, the dbSession.getDatabase(dbname) method will get the database object for the "employee" database. The doc.setId("1") method is used to set the DocID for the document, in this case, it create a document with DocID as 1. The doc.put("EmpNO","1") method is used to enter data to the document (i.e. enter field and its values), first parameter of the method is Field Name and second parameter is Value, so EmpNO is the field name and 1 is the value for the field. Once all the fields and its values are entered in the document, you save the document using the db.saveDocument(doc) method.
If you have data in text file, then you can write a code to read file and put each row data as a document in CouchDB database. If you do not want to give DocID for each document and want CouchDB to assign DocID, then just comment out the line doc.setId("1") in the above example.
You can also retrieve the total number of documents in a particular database. The following code shows the retrieval of total number of documents in the "employee" database:
The result:
Here the db.getDocumentCount() method is used to retrieve the number of documents in database.
If necessary, you can also delete a particular document from the database. The following code shows how to delete a document whose id is "1" from the "employee" database:
Here we will first retrieve the document which we want to delete by using the db.getDocument("1") method, which takes DocID as input parameter. Then we will delete the retrieved document by using the db.deleteDocument(d) method.
String dbname = "employee";
dbSession.deleteDatabase(dbname);
Inserting Documents (Data) Into the Database and Other Document Operations
String dbname = "employee";
Database db = dbSession.getDatabase(dbname);
Document doc = new Document();
doc.setId("1");
doc.put("EmpNO", "1");
doc.put("Name", "Mike");
doc.put("Group", "J2EECOE");
doc.put("Designation", "Manager");
doc.put("Language", "Java");
db.saveDocument(doc);
String dbname = "employee";
Database db = dbSession.getDatabase(dbname);
int count = db.getDocumentCount();
System.out.println("Total Documents: " + count);
Total Documents: 9
String dbname = "employee";
Database db = dbSession.getDatabase(dbname);
Document d = db.getDocument("1");
db.deleteDocument(d);


