private static void createDocuments() throws IOException { Set<Document> documents = Sets.newHashSet(); // Create a new document (simple format) Document document = new HDocument() .set("_id", "jdoe") .set("first_name", "John") .set("last_name", "Doe") .set("dob", ODate.parse("1970-06-23")); // save document into the table mainColl.insertOrReplace(document); documents.add(document); printDocument("jdoe"); // create a new document without _id document = new HDocument() .set("first_name", "David") .set("last_name", "Simon") .set("dob", ODate.parse("1980-10-13")); document.setId(new HValue("dsimon")); mainColl.insert(new HValue("dsimon"), document); documents.add(((HDocument) document).shallowCopy().setId(new HValue("dsimon"))); printDocument("dsimon"); // create a new document from a simple bean // look at the User class to see how you can use JSON Annotation to drive the format of the // document User user = new User(); user.setId("alehmann"); user.setFirstName("Andrew"); user.setLastName("Lehmann"); user.setDob(ODate.parse("1980-10-13")); user.addInterest("html"); user.addInterest("css"); user.addInterest("js"); document = Json.newDocument(user); // save document into the table mainColl.insertOrReplace(document); documents.add(HValue.initFromDocument(document)); printDocument("alehmann"); // try to insert the same document ID try { mainColl.insert(new HValue("dsimon"), document); } catch (DocumentExistsException dee) { System.out.println("Exception during insert : " + dee.getMessage()); } // Create more complex Record document = new HDocument() .set("_id", "mdupont") .set("first_name", "Maxime") .set("last_name", "Dupont") .set("dob", ODate.parse("1982-02-03")) .set("interests", Arrays.asList("sports", "movies", "electronics")) .set("address.line", "1223 Broadway") .set("address.city", "San Jose") .set("address.zip", 95109); mainColl.insert(document); documents.add(document); printDocument("mdupont"); // Another way to create sub document // Create the sub document as document and use it to set the value Document addressRecord = new HDocument() .set("line", "100 Main Street") .set("city", "San Francisco") .set("zip", 94105); document = new HDocument() .set("_id", "rsmith") .set("first_name", "Robert") .set("last_name", "Smith") .set("dob", ODate.parse("1982-02-03")) .set("interests", Arrays.asList("electronics", "music", "sports")) .set("address", addressRecord); mainColl.insert(document); documents.add(document); printDocument("rsmith"); mainColl.flush(); // flush to the server }