public void runExample() { LoadExampleData.load(); List<Customer> list2 = Customer.find.query().select("name").fetch("contacts").findList(); PathProperties pathProperties = PathProperties.parse("(id,version,name,contacts(id,email,version))"); Query<Customer> query = Customer.find.query(); pathProperties.apply(query); List<Customer> list = query.findList(); JsonContext jsonContext = Customer.find.db().json(); String jsonString = jsonContext.toJson(list); System.out.println(jsonString); for (Customer customer : list2) { customer.getName(); List<Contact> contacts = customer.getContacts(); if (contacts != null) { for (Contact contact : contacts) { System.out.println("contact: " + contact.getFirstName() + " " + contact.getLastName()); } } } }
public void runExampleUsingOrders() { LoadExampleData.load(); PathProperties pathProperties = PathProperties.parse("(id,status,orderDate,customer(id,name),details(*,product(sku)))"); Query<Order> query = Ebean.createQuery(Order.class); pathProperties.apply(query); List<Order> orders = query.where().gt("id", 1).findList(); String rawJson = Ebean.json().toJson(orders); System.out.println(rawJson); }
@Test public void testCreateGenerator() throws Exception { EbeanServer server = Ebean.getDefaultServer(); StringWriter writer = new StringWriter(); JsonContext json = server.json(); JsonGenerator generator = json.createGenerator(writer); Customer customer = new Customer(); customer.setId(1); customer.setName("Jim"); // we can use the generator before and after our json.toJson() call // ... confirming we are not closing the generator generator.writeStartArray(); json.toJson(customer, generator, PathProperties.parse("id,name")); generator.writeEndArray(); generator.close(); String jsonString = writer.toString(); assertTrue(jsonString, jsonString.startsWith("[")); assertTrue(jsonString, jsonString.endsWith("]")); assertTrue(jsonString, jsonString.contains("{\"id\":1,\"name\":\"Jim\"}")); }
@Ignore @Test public void testBulkUpdate() throws IOException { ResetBasicData.reset(); EbeanServer server = Ebean.getServer(null); JsonContext jsonContext = server.json(); StringWriter writer = new StringWriter(); JsonGenerator generator = jsonContext.createGenerator(writer); List<Customer> customers = Ebean.find(Customer.class).findList(); for (Customer customer : customers) { customer.setName(customer.getName() + "esMod"); PathProperties updateProps = PathProperties.parse("name"); generator.writeStartObject(); generator.writeFieldName("update"); generator.writeStartObject(); generator.writeStringField("_id", customer.getId().toString()); generator.writeStringField("_type", "customer"); generator.writeStringField("_index", "customer"); generator.writeEndObject(); generator.writeEndObject(); generator.writeRaw("\n"); generator.writeStartObject(); generator.writeFieldName("doc"); jsonContext.toJson(customer, generator, updateProps); generator.writeEndObject(); generator.writeRaw("\n"); } generator.close(); String json = writer.toString(); post("http://localhost:9200/_bulk", json); // curl -s -XPOST localhost:9200/_bulk // { "update" : {"_id" : "1", "_type" : "type1", "_index" : "index1"} } // { "doc" : {"field2" : "value2"} } }
@Ignore @Test public void test() throws IOException { ResetBasicData.reset(); EbeanServer server = Ebean.getServer(null); JsonContext jsonContext = server.json(); List<Customer> customers = Ebean.find(Customer.class).findList(); PathProperties paths = PathProperties.parse("name, status, anniversary"); for (Customer customer : customers) { String json = jsonContext.toJson(customer, paths); put("http://localhost:9200/customer/customer/" + customer.getId(), json); } }