private static void printDocument(String id) { // get a single document Document record = mainColl.findById(new HValue(id)); System.out.print("Single record\n\t"); System.out.println(record); // print individual fields System.out.println( "Id : " + record.getIdString() + " - first name : " + record.getString("first_name")); }
@Test public void queryDocuments() throws Exception { // get a single document Document record = mainColl.findById(new HValue("mdupont")); assertEquals("mdupont", record.getIdString()); assertEquals("Maxime", record.getString("first_name")); assertEquals("Dupont", record.getString("last_name")); // get a single document with project record = mainColl.findById(new HValue("mdupont"), "last_name"); assertEquals("mdupont", record.getIdString()); assertEquals(null, record.getString("first_name")); assertEquals("Dupont", record.getString("last_name")); // get single document and map it to the bean User user = mainColl.findById(new HValue("alehmann")).toJavaBean(User.class); assertEquals("alehmann", user.getId()); assertEquals("Andrew", user.getFirstName()); assertEquals("Lehmann", user.getLastName()); // all records in the table DocumentStream rs = mainColl.find(); Iterator<? extends Document> itrs = rs.iterator(); Document readRecord; Set<Document> readRecords = Sets.newHashSet(); while (itrs.hasNext()) { readRecord = itrs.next(); readRecords.add(readRecord); // System.out.println("\t" + readRecord); } assertEquals(5, readRecords.size()); rs.close(); // all records in the table with projection int cnt = 0; try (DocumentStream documentStream = mainColl.find("first_name", "last_name")) { for (Document doc : documentStream) { cnt++; assertEquals(3, doc.size()); // System.out.println("\t" + doc); } } assertEquals(5, cnt); // all records and use a POJO // it is interesting to see how you can ignore unknown attributes with the JSON Annotations cnt = 0; try (DocumentStream documentStream = mainColl.find()) { for (Document doc : documentStream) { cnt++; User u = doc.toJavaBean(User.class); assertTrue( ImmutableList.<String>of("Doe", "Simon", "Smith", "Dupont", "Lehmann") .contains(u.getLastName())); // System.out.println("\t" + doc.toJavaBean(User.class)); } } assertEquals(5, cnt); // find with condition // Condition equals a string QueryCondition condition = new HQueryCondition().is("last_name", QueryCondition.Op.EQUAL, "Doe").build(); // System.out.println("\n\nCondition: " + condition); cnt = 0; try (DocumentStream documentStream = mainColl.find(condition)) { ((HDocumentStream) documentStream).explain(); for (Document doc : documentStream) { cnt++; assertEquals("Doe", doc.getString("last_name")); // System.out.println("\t" + doc); } } assertEquals(1, cnt); // find with condition and projection // Condition equals a string condition = new HQueryCondition().is("last_name", QueryCondition.Op.EQUAL, "Doe").build(); // System.out.println("\n\nCondition: " + condition); cnt = 0; try (DocumentStream documentStream = mainColl.find(condition, "last_name")) { for (Document doc : documentStream) { cnt++; assertEquals("Doe", doc.getString("last_name")); // System.out.println("\t" + doc); } } assertEquals(1, cnt); // Condition as date range condition = new HQueryCondition() .and() .is("dob", QueryCondition.Op.GREATER_OR_EQUAL, ODate.parse("1980-01-01")) .is("dob", QueryCondition.Op.LESS, ODate.parse("1981-01-01")) .close() .build(); // System.out.println("\n\nCondition: " + condition); cnt = 0; try (DocumentStream documentStream = mainColl.find(condition)) { for (Document doc : documentStream) { cnt++; assertTrue(doc.getDate("dob").compareTo(ODate.parse("1980-01-01")) > 0); assertTrue(doc.getDate("dob").compareTo(ODate.parse("1981-01-01")) < 0); // System.out.println("\t" + doc); } } assertEquals(2, cnt); // Condition in sub document condition = new HQueryCondition().is("address.zip", QueryCondition.Op.EQUAL, 95109).build(); // System.out.println("\n\nCondition: " + condition); cnt = 0; try (DocumentStream documentStream = mainColl.find(condition)) { for (Document doc : documentStream) { cnt++; assertEquals(95109, doc.getInt("address.zip")); // System.out.println("\t" + doc); } } assertEquals(1, cnt); // Contains a specific value in an array condition = new HQueryCondition().is("interests[]", QueryCondition.Op.EQUAL, "sports").build(); // System.out.println("\n\nCondition: " + condition); cnt = 0; try (DocumentStream documentStream = mainColl.find(condition)) { for (Document doc : documentStream) { cnt++; assertTrue(doc.getList("interests").contains("sports")); // System.out.println("\t" + doc); } } assertEquals(2, cnt); // Contains a value at a specific index condition = new HQueryCondition().is("interests[0]", QueryCondition.Op.EQUAL, "sports").build(); // System.out.println("\n\nCondition: " + condition); cnt = 0; try (DocumentStream documentStream = mainColl.find(condition, "first_name", "last_name", "interests")) { for (Document doc : documentStream) { cnt++; assertEquals("sports", doc.getString("interests[0]")); // System.out.println("\t" + doc); } } assertEquals(1, cnt); }