Skip to content

Commit 363e9ea

Browse files
committed
Adding the suggestions.
1 parent 456bd08 commit 363e9ea

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

marklogic-client-api/src/main/java/com/marklogic/client/example/cookbook/datamovement/BulkExportWithDataService.java

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.marklogic.client.query.*;
2929

3030
import java.io.*;
31+
import java.util.concurrent.atomic.AtomicInteger;
3132
import java.util.stream.Stream;
3233

3334
/*
@@ -46,9 +47,12 @@
4647

4748
public class BulkExportWithDataService {
4849

49-
private DatabaseClient dbClient = DatabaseClientFactory.newClient("localhost", 8012, new DatabaseClientFactory.DigestAuthContext("rest-writer", "x"));
50+
private static Util.ExampleProperties properties = getProperties();
51+
private DatabaseClient dbClient = DatabaseClientFactory.newClient(properties.host, 8012,
52+
new DatabaseClientFactory.DigestAuthContext(properties.writerUser, properties.writerPassword));
5053
private DatabaseClient dbModulesClient = DatabaseClientSingleton.getAdmin("java-unittest-modules");
5154
private DataMovementManager moveMgr = dbClient.newDataMovementManager();
55+
private int count = 100;
5256

5357
public static void main(String args[]) throws IOException {
5458
new BulkExportWithDataService().run();
@@ -62,7 +66,7 @@ private void run() throws IOException {
6266

6367
private void setup() throws IOException {
6468

65-
writeDocuments(100,"BulkExportWithDataService");
69+
writeDocuments(count,"BulkExportWithDataService");
6670
writeScriptFile("readJsonDocs.api");
6771
writeScriptFile("readJsonDocs.sjs");
6872
}
@@ -82,20 +86,22 @@ private void exportJson(){
8286
BulkExportServices bulkExportServices = BulkExportServices.on(dbClient);
8387

8488
StructuredQueryBuilder structuredQueryBuilder = new StructuredQueryBuilder();
89+
AtomicInteger docCount = new AtomicInteger();
8590
QueryBatcher queryBatcher = moveMgr.newQueryBatcher(structuredQueryBuilder.collection("BulkExportWithDataService"))
8691
.withBatchSize(3)
8792
.withThreadCount(3)
8893
.onQueryFailure(batch -> new InternalError("An exception occured in queryBatcher"))
8994
.onUrisReady(batch -> (bulkExportServices.readJsonDocs(Stream.of(batch.getItems())))
9095
.forEach(reader -> {
9196
try {
92-
int charValue = reader.read();
93-
String docContent = "";
94-
while(charValue!=-1) {
95-
docContent+=((char)charValue);
96-
charValue = reader.read();
97+
BufferedReader br = new BufferedReader(reader);
98+
StringBuilder out = new StringBuilder();
99+
String line;
100+
while ((line = br.readLine()) != null) {
101+
out.append(line);
102+
docCount.getAndIncrement();
97103
}
98-
System.out.println(docContent);
104+
System.out.println(out.toString());
99105
reader.close();
100106
}
101107
catch (IOException e) {
@@ -105,7 +111,8 @@ private void exportJson(){
105111
moveMgr.startJob(queryBatcher);
106112
queryBatcher.awaitCompletion();
107113
moveMgr.stopJob(queryBatcher);
108-
114+
if(docCount.get()!= count)
115+
throw new InternalError("Mismatch between the number of documents written and read from the database.");
109116
}
110117

111118
private void writeDocuments(int count, String collection) {
@@ -139,4 +146,13 @@ private void writeScriptFile(String fileName) throws IOException {
139146
(new StringHandle(out.toString())));
140147
modMgr.write(writeSet);
141148
}
149+
150+
private static Util.ExampleProperties getProperties() {
151+
try {
152+
return Util.loadProperties();
153+
} catch (IOException e) {
154+
e.printStackTrace();
155+
throw new RuntimeException(e);
156+
}
157+
}
142158
}

0 commit comments

Comments
 (0)