1616package com .marklogic .client .example .cookbook .datamovement ;
1717
1818import com .marklogic .client .DatabaseClient ;
19+ import com .marklogic .client .DatabaseClientFactory ;
1920import com .marklogic .client .datamovement .DataMovementManager ;
20- import com .marklogic .client .datamovement .QueryBatch ;
21- import com .marklogic .client .datamovement .QueryBatchListener ;
2221import com .marklogic .client .datamovement .QueryBatcher ;
2322import com .marklogic .client .document .DocumentWriteSet ;
2423import com .marklogic .client .document .JSONDocumentManager ;
2928import com .marklogic .client .query .*;
3029
3130import java .io .*;
32- import java .util .ArrayList ;
33- import java .util .Arrays ;
34- import java .util .List ;
3531import java .util .stream .Stream ;
3632
33+ /*
34+ * For custom task approach, the Gradle build file should include the below plugin and task -
35+ *
36+ * plugins {
37+ * id 'com.marklogic.ml-development-tools' version '5.1.0'
38+ * }
39+ * task generateBulkExportServices(type: com.marklogic.client.tools.gradle.EndpointProxiesGenTask) {
40+ * serviceDeclarationFile = 'marklogic-client-api/src/main/resources/scripts/bulkExport/service.json'
41+ * }
42+ *
43+ * URL of the product documentation at http://docs.marklogic.com/guide/java/DataServices#id_44346
44+ *
45+ * */
3746
38- public class BulkExportWithDataService implements QueryBatchListener {
47+ public class BulkExportWithDataService {
3948
40- private DatabaseClient dbClient = DatabaseClientSingleton . getAdmin ( "java-unittest" );
49+ private DatabaseClient dbClient = DatabaseClientFactory . newClient ( "localhost" , 8012 , new DatabaseClientFactory . DigestAuthContext ( "rest-writer" , "x" ) );
4150 private DatabaseClient dbModulesClient = DatabaseClientSingleton .getAdmin ("java-unittest-modules" );
4251 private DataMovementManager moveMgr = dbClient .newDataMovementManager ();
43- private List <String > urisList = new ArrayList <>();
44-
45- @ Override
46- public void processEvent (QueryBatch batch ) {
47- }
4852
4953 public static void main (String args []) throws IOException {
5054 new BulkExportWithDataService ().run ();
@@ -75,23 +79,29 @@ private void tearDown(){
7579
7680
7781 private void exportJson (){
78- BulkExportServices bulkExportServices = uris -> {
79- List <Object > list = Arrays .asList (uris .toArray ());
80- for (Object i :list ) {
81- if (!(urisList .contains (i )))
82- throw new InternalError ("urisList does not contain " +i .toString ());
83- urisList .remove (i );
84- }
85- return null ;
86- };
82+ BulkExportServices bulkExportServices = BulkExportServices .on (dbClient );
8783
8884 StructuredQueryBuilder structuredQueryBuilder = new StructuredQueryBuilder ();
89- structuredQueryBuilder .directory (1 ,"/example/cookbook/bulkExport/" );
9085 QueryBatcher queryBatcher = moveMgr .newQueryBatcher (structuredQueryBuilder .collection ("BulkExportWithDataService" ))
9186 .withBatchSize (3 )
9287 .withThreadCount (3 )
9388 .onQueryFailure (batch -> new InternalError ("An exception occured in queryBatcher" ))
94- .onUrisReady (batch -> bulkExportServices .readJsonDocs (Stream .of (batch .getItems ())));
89+ .onUrisReady (batch -> (bulkExportServices .readJsonDocs (Stream .of (batch .getItems ())))
90+ .forEach (reader -> {
91+ try {
92+ int charValue = reader .read ();
93+ String docContent = "" ;
94+ while (charValue !=-1 ) {
95+ docContent +=((char )charValue );
96+ charValue = reader .read ();
97+ }
98+ System .out .println (docContent );
99+ reader .close ();
100+ }
101+ catch (IOException e ) {
102+ e .printStackTrace ();
103+ }
104+ }));
95105 moveMgr .startJob (queryBatcher );
96106 queryBatcher .awaitCompletion ();
97107 moveMgr .stopJob (queryBatcher );
@@ -106,16 +116,15 @@ private void writeDocuments(int count, String collection) {
106116 StringHandle data = new StringHandle ("{\" docNum\" :" +i +", \" docName\" :\" doc" +i +"\" }" );
107117 String docId = "/example/cookbook/bulkExport/" +i +".json" ;
108118 manager .write (docId , metadata , data );
109- urisList .add (docId );
110119 }
111120 }
112121
113122 private void writeScriptFile (String fileName ) throws IOException {
114123 TextDocumentManager modMgr = dbModulesClient .newTextDocumentManager ();
115124 DocumentWriteSet writeSet = modMgr .newWriteSet ();
116125 DocumentMetadataHandle metadata = new DocumentMetadataHandle ().withCollections ("BulkExportWithDataService" );
117- metadata .getPermissions ().add ("rest-writer" , DocumentMetadataHandle .Capability .UPDATE , DocumentMetadataHandle . Capability . READ );
118- metadata .getPermissions ().add ("rest-reader" , DocumentMetadataHandle .Capability .READ );
126+ metadata .getPermissions ().add ("rest-writer" , DocumentMetadataHandle .Capability .UPDATE );
127+ metadata .getPermissions ().add ("rest-reader" , DocumentMetadataHandle .Capability .READ , DocumentMetadataHandle . Capability . EXECUTE );
119128
120129 InputStream in = (Util .openStream ("scripts" + File .separator +"bulkExport" +File .separator +fileName ));
121130 BufferedReader reader = new BufferedReader (new InputStreamReader (in ));
0 commit comments