Created
May 10, 2016 11:45
-
-
Save rhulha/10e1489d12429b548bb4f5301953a809 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.marklogic.client.*; | |
import com.marklogic.client.document.*; | |
import com.marklogic.client.io.*; | |
import com.marklogic.client.query.*; | |
public class TestMarkLogic { | |
public static void main(String[] args) { | |
DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8000, "Admin", "Admin", DatabaseClientFactory.Authentication.DIGEST); | |
JSONDocumentManager docMgr = client.newJSONDocumentManager(); | |
// CREATE | |
docMgr.write("/afternoon-drink", new StringHandle("{name: \"Iced Mocha\", size: \"Grandé\", tasty: true}")); | |
StringHandle metadata = docMgr.readMetadata("/afternoon-drink", new StringHandle().withFormat(Format.JSON)); | |
System.out.println(metadata); | |
DocumentDescriptor docDesc = docMgr.create(docMgr.newDocumentUriTemplate("json"), new StringHandle("{name: \"Iced Mocha\", size: \"Grandé\", tasty: true}")); | |
// READ | |
String doc = docMgr.read("/afternoon-drink", new StringHandle()).get(); | |
System.out.println(doc); | |
// UPDATE | |
DocumentPatchBuilder pb = docMgr.newPatchBuilder(); | |
//pb.replaceFragment("/", new ObjectMapper().createObjectNode().put("name", "Hot Mocha")); | |
pb.replaceValue("/name", "Hot Mocha"); | |
docMgr.patch("/afternoon-drink", pb.build()); | |
doc = docMgr.read("/afternoon-drink", new StringHandle()).get(); | |
System.out.println(doc); | |
// SEARCH | |
QueryManager queryMgr = client.newQueryManager(); | |
StringQueryDefinition query = queryMgr.newStringDefinition(); | |
query.setCriteria("mocha"); | |
MatchDocumentSummary[] results = queryMgr.search(query, new SearchHandle()).getMatchResults(); | |
for (MatchDocumentSummary mds : results) { | |
String uri = mds.getUri(); | |
System.out.println(uri); | |
} | |
client.release(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment