Abacn commented on code in PR #41923:
URL: https://github.com/apache/beam/pull/41923#discussion_r4792850217


##########
sdks/java/io/elasticsearch/src/main/java/org/apache/beam/sdk/io/elasticsearch/ElasticsearchIO.java:
##########
@@ -1893,47 +1939,84 @@ public void serialize(DocumentMetadata value, 
JsonGenerator gen, SerializerProvi
       }
     }
 
+    private static boolean containsLineBreak(String document) {
+      return document.indexOf('\n') >= 0 || document.indexOf('\r') >= 0;
+    }
+
+    private static String validateAndCompactDocument(String document) throws 
IOException {
+      boolean needsCompaction = containsLineBreak(document);
+      try (JsonParser parser = 
OBJECT_MAPPER.getFactory().createParser(document)) {
+        if (parser.nextToken() != JsonToken.START_OBJECT) {
+          throw new IOException("Document must be a single JSON object");
+        }
+        String result = document;
+        if (needsCompaction) {
+          StringWriter writer = new StringWriter(document.length());

Review Comment:
   Now an element will serialize twice (one to validate it's a single json 
object), arguably this won't affect performance from baseline. Currently, if 
incoming element has newline, e.g., pretty-formatted json, it's not supported 
and trigger 400 error:
   
   ```
   org.apache.beam.sdk.Pipeline$PipelineExecutionException: 
org.elasticsearch.client.ResponseException: method [POST], host 
[http://localhost:38769], URI [/beam1/_bulk], status line [HTTP/1.1 400 Bad 
Request]
   
{"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"Malformed
 action/metadata line [3], expected START_OBJECT but found 
[VALUE_STRING]"}],"type":"illegal_argument_exception","reason":"Malformed 
action/metadata line [3], expected START_OBJECT but found 
[VALUE_STRING]"},"status":400}
   ```
   
   therefore no actual performance impact for use cases used to work.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to