|
| 1 | +/* |
| 2 | + * Copyright 2020 MarkLogic Corporation |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.marklogic.client.example.cookbook.datamovement; |
| 17 | + |
| 18 | +import com.marklogic.client.DatabaseClient; |
| 19 | +import com.marklogic.client.datamovement.DataMovementManager; |
| 20 | +import com.marklogic.client.datamovement.QueryBatch; |
| 21 | +import com.marklogic.client.datamovement.QueryBatchListener; |
| 22 | +import com.marklogic.client.datamovement.QueryBatcher; |
| 23 | +import com.marklogic.client.document.DocumentWriteSet; |
| 24 | +import com.marklogic.client.document.JSONDocumentManager; |
| 25 | +import com.marklogic.client.document.TextDocumentManager; |
| 26 | +import com.marklogic.client.io.DocumentMetadataHandle; |
| 27 | +import com.marklogic.client.io.StringHandle; |
| 28 | +import com.marklogic.client.example.cookbook.Util; |
| 29 | +import com.marklogic.client.query.*; |
| 30 | + |
| 31 | +import java.io.*; |
| 32 | +import java.util.ArrayList; |
| 33 | +import java.util.Arrays; |
| 34 | +import java.util.List; |
| 35 | +import java.util.stream.Stream; |
| 36 | + |
| 37 | + |
| 38 | +public class BulkExportWithDataService implements QueryBatchListener { |
| 39 | + |
| 40 | + private DatabaseClient dbClient = DatabaseClientSingleton.getAdmin("java-unittest"); |
| 41 | + private DatabaseClient dbModulesClient = DatabaseClientSingleton.getAdmin("java-unittest-modules"); |
| 42 | + private DataMovementManager moveMgr = dbClient.newDataMovementManager(); |
| 43 | + private List<String> urisList = new ArrayList<>(); |
| 44 | + |
| 45 | + @Override |
| 46 | + public void processEvent(QueryBatch batch) { |
| 47 | + } |
| 48 | + |
| 49 | + public static void main(String args[]) throws IOException { |
| 50 | + new BulkExportWithDataService().run(); |
| 51 | + } |
| 52 | + |
| 53 | + private void run() throws IOException { |
| 54 | + setup(); |
| 55 | + exportJson(); |
| 56 | + tearDown(); |
| 57 | + } |
| 58 | + |
| 59 | + private void setup() throws IOException { |
| 60 | + |
| 61 | + writeDocuments(100,"BulkExportWithDataService"); |
| 62 | + writeScriptFile("readJsonDocs.api"); |
| 63 | + writeScriptFile("readJsonDocs.sjs"); |
| 64 | + } |
| 65 | + |
| 66 | + private void tearDown(){ |
| 67 | + |
| 68 | + QueryManager queryMgr = dbClient.newQueryManager(); |
| 69 | + QueryManager queryMgrModules = dbModulesClient.newQueryManager(); |
| 70 | + DeleteQueryDefinition deletedef = queryMgr.newDeleteDefinition(); |
| 71 | + deletedef.setCollections("BulkExportWithDataService"); |
| 72 | + queryMgr.delete(deletedef); |
| 73 | + queryMgrModules.delete(deletedef); |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | + 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 | + }; |
| 87 | + |
| 88 | + StructuredQueryBuilder structuredQueryBuilder = new StructuredQueryBuilder(); |
| 89 | + structuredQueryBuilder.directory(1,"/example/cookbook/bulkExport/"); |
| 90 | + QueryBatcher queryBatcher = moveMgr.newQueryBatcher(structuredQueryBuilder.collection("BulkExportWithDataService")) |
| 91 | + .withBatchSize(3) |
| 92 | + .withThreadCount(3) |
| 93 | + .onQueryFailure(batch -> new InternalError("An exception occured in queryBatcher")) |
| 94 | + .onUrisReady(batch -> bulkExportServices.readJsonDocs(Stream.of(batch.getItems()))); |
| 95 | + moveMgr.startJob(queryBatcher); |
| 96 | + queryBatcher.awaitCompletion(); |
| 97 | + moveMgr.stopJob(queryBatcher); |
| 98 | + |
| 99 | + } |
| 100 | + |
| 101 | + private void writeDocuments(int count, String collection) { |
| 102 | + JSONDocumentManager manager = dbClient.newJSONDocumentManager(); |
| 103 | + DocumentMetadataHandle metadata = new DocumentMetadataHandle().withCollections(collection); |
| 104 | + |
| 105 | + for(int i=1;i<=count;i++) { |
| 106 | + StringHandle data = new StringHandle("{\"docNum\":"+i+", \"docName\":\"doc"+i+"\"}"); |
| 107 | + String docId = "/example/cookbook/bulkExport/"+i+".json"; |
| 108 | + manager.write(docId, metadata, data); |
| 109 | + urisList.add(docId); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + private void writeScriptFile(String fileName) throws IOException { |
| 114 | + TextDocumentManager modMgr = dbModulesClient.newTextDocumentManager(); |
| 115 | + DocumentWriteSet writeSet = modMgr.newWriteSet(); |
| 116 | + 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); |
| 119 | + |
| 120 | + InputStream in = (Util.openStream("scripts"+ File.separator+"bulkExport"+File.separator+fileName)); |
| 121 | + BufferedReader reader = new BufferedReader(new InputStreamReader(in)); |
| 122 | + StringBuilder out = new StringBuilder(); |
| 123 | + String line; |
| 124 | + while ((line = reader.readLine()) != null) { |
| 125 | + out.append(line); |
| 126 | + } |
| 127 | + reader.close(); |
| 128 | + |
| 129 | + writeSet.add("/example/cookbook/bulkExport/"+fileName, metadata, |
| 130 | + (new StringHandle(out.toString()))); |
| 131 | + modMgr.write(writeSet); |
| 132 | + } |
| 133 | +} |
0 commit comments