Coding Basics
To use XCC, there are several basic things you need to do in your Java code:
Import the needed libraries.
Set up the
ContentSource
object to authenticate against MarkLogic Server.Create a new
Session
object.Add a
Request
to the session object.Submit the request and get back a
ResultSequence
object from MarkLogic Server.Do something with the results (print them out, for example).
Close the session.
The following are Java code samples that illustrate these basic design patterns:
package com.marklogic.xcc.examples; import com.marklogic.xcc.ContentSource; import com.marklogic.xcc.ContentSourceFactory; import com.marklogic.xcc.Session; import com.marklogic.xcc.Request; import com.marklogic.xcc.ResultSequence; URI uri = new URI("xcc://user:pass@localhost:8000/mycontent"); ContentSource contentSource = ContentSourceFactory.newContentSource (uri); Session session = contentSource.newSession(); Request request = session.newAdhocQuery ("\"Hello World\""); ResultSequence rs = session.submitRequest (request); System.out.println (rs.asString()); session.close();
Note
Session
objects are not thread safe. A Session
object should not be used concurrently by multiple threads.