gemini-code-assist[bot] commented on code in PR #38457:
URL: https://github.com/apache/beam/pull/38457#discussion_r3226094529
##########
sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/BatchElements.java:
##########
@@ -568,7 +571,11 @@ public void processElement(
try (BatchSizeEstimator.Stopwatch sw =
estimator.recordTime(targetBatch.size)) {
- receiver.outputWithTimestamp(targetBatch.elements,
targetWindow.maxTimestamp());
+ receiver.outputWindowedValue(
+ targetBatch.elements,
+ targetWindow.maxTimestamp(),
+ singleton(targetWindow),
+ PaneInfo.NO_FIRING);
Review Comment:

The `OutputReceiver` interface in the Apache Beam Java SDK does not
currently have an `outputWindowedValue` method. This method is available on
`DoFn.ProcessContext`, but using `ProcessContext` is generally deprecated in
favor of parameter injection. If the intention is to explicitly set the window
and pane info (which is necessary if the batch is being emitted for a window
other than the one currently in context), you may need to use `ProcessContext`
for this specific `DoFn`, or ensure that the `OutputReceiver` interface is
updated to support this method. As it stands, this code will likely fail to
compile.
##########
examples/java/src/main/java/org/apache/beam/examples/BatchElementsExample.java:
##########
@@ -0,0 +1,81 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.beam.examples;
+
+import java.util.List;
+import org.apache.beam.sdk.Pipeline;
+import org.apache.beam.sdk.options.PipelineOptions;
+import org.apache.beam.sdk.options.PipelineOptionsFactory;
+import org.apache.beam.sdk.transforms.BatchElements;
+import org.apache.beam.sdk.transforms.Create;
+import org.apache.beam.sdk.transforms.DoFn;
+import org.apache.beam.sdk.transforms.ParDo;
+import org.apache.beam.sdk.values.PCollection;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+// beam-playground:
+// name: BatchElements
+// description: Demonstration of BatchElements transform usage.
+// multifile: false
+// default_example: false
+// context_line: 47
+// categories:
+// - Core Transforms
+// complexity: BASIC
+// tags:
+// - transforms
+// - batch
+
+public class BatchElementsExample {
+ public static void main(String[] args) {
+ PipelineOptions options = PipelineOptionsFactory.create();
+
+ Pipeline pipeline = Pipeline.create(options);
+
+ // [START main_section]
+ // Create input
+
+ PCollection<String> inputs =
+ pipeline.apply(Create.of("apple", "strawberry", "orange", "peach",
"cherry", "pear"));
+
+ // Create Batch Config
+ BatchElements.BatchConfig config =
+
BatchElements.BatchConfig.builder().withMinBatchSize(2).withMaxBatchSize(4).build();
+ // Batch Elements
+ PCollection<List<String>> result =
inputs.apply(BatchElements.withConfig(config));
+ // [END main_section]
+ result.apply(ParDo.of(new LogOutput()));
+ pipeline.run();
+ }
+
+ static class LogOutput extends DoFn<List<String>, String> {
+ private static final Logger LOG = LoggerFactory.getLogger(LogOutput.class);
+
+ @ProcessElement
+ public void processElement(ProcessContext c) throws Exception {
+ List<String> batch = c.element();
+
+ LOG.info("Batch Contents: {}", batch);
+
+ for (String element : batch) {
+ c.output(element);
+ }
+ }
Review Comment:

In modern Beam, it is preferred to use parameter injection (e.g., `@Element`
and `OutputReceiver`) instead of the `ProcessContext` object. Additionally, the
`throws Exception` clause is unnecessary here as no checked exceptions are
thrown in the method body.
```java
public void processElement(@Element List<String> batch,
OutputReceiver<String> receiver) {
LOG.info("Batch Contents: {}", batch);
for (String element : batch) {
receiver.output(element);
}
}
```
##########
website/www/site/content/en/documentation/transforms/java/aggregation/batchelements.md:
##########
@@ -0,0 +1,30 @@
+---
+title: "BatchElements"
+---
+<!--
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+-->
+
+# BatchElements
+
+BatchElements transform groups individual elements into batches before
processing them downstream.
+The transform takes a `PCollection<T>` as input and produces a
`PCollection<List<T>>`, where each output element is a batch containing
multiple input elements.
+Batch sizes are chosen dynamically between the configured minimum and maximum
values by measuring the execution time of downstream operations.
+
+Batching is performed per window. Each emitted batch belongs to the same
window as its input elements
Review Comment:

The sentence is missing a trailing period.
```suggestion
Batching is performed per window. Each emitted batch belongs to the same
window as its input elements.
```
##########
examples/java/src/main/java/org/apache/beam/examples/BatchElementsExample.java:
##########
@@ -0,0 +1,81 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.beam.examples;
+
+import java.util.List;
+import org.apache.beam.sdk.Pipeline;
+import org.apache.beam.sdk.options.PipelineOptions;
+import org.apache.beam.sdk.options.PipelineOptionsFactory;
+import org.apache.beam.sdk.transforms.BatchElements;
+import org.apache.beam.sdk.transforms.Create;
+import org.apache.beam.sdk.transforms.DoFn;
+import org.apache.beam.sdk.transforms.ParDo;
+import org.apache.beam.sdk.values.PCollection;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+// beam-playground:
+// name: BatchElements
+// description: Demonstration of BatchElements transform usage.
+// multifile: false
+// default_example: false
+// context_line: 47
+// categories:
+// - Core Transforms
+// complexity: BASIC
+// tags:
+// - transforms
+// - batch
+
+public class BatchElementsExample {
+ public static void main(String[] args) {
+ PipelineOptions options = PipelineOptionsFactory.create();
Review Comment:

It is recommended to use
`PipelineOptionsFactory.fromArgs(args).withValidation().create()` instead of
`PipelineOptionsFactory.create()`. This allows users to pass command-line
arguments, such as `--runner`, to configure the pipeline execution when running
the example.
```suggestion
PipelineOptions options =
PipelineOptionsFactory.fromArgs(args).withValidation().create();
```
--
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]