[ 
https://issues.apache.org/jira/browse/BEAM-6134?focusedWorklogId=170225&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-170225
 ]

ASF GitHub Bot logged work on BEAM-6134:
----------------------------------------

                Author: ASF GitHub Bot
            Created on: 28/Nov/18 13:44
            Start Date: 28/Nov/18 13:44
    Worklog Time Spent: 10m 
      Work Description: jbonofre closed pull request #7148: BEAM-6134: 
MongoDbIO add support for projection
URL: https://github.com/apache/beam/pull/7148
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git 
a/sdks/java/io/mongodb/src/main/java/org/apache/beam/sdk/io/mongodb/MongoDbIO.java
 
b/sdks/java/io/mongodb/src/main/java/org/apache/beam/sdk/io/mongodb/MongoDbIO.java
index b5cba4b4f5f0..9d6e05bfb209 100644
--- 
a/sdks/java/io/mongodb/src/main/java/org/apache/beam/sdk/io/mongodb/MongoDbIO.java
+++ 
b/sdks/java/io/mongodb/src/main/java/org/apache/beam/sdk/io/mongodb/MongoDbIO.java
@@ -18,6 +18,7 @@
 package org.apache.beam.sdk.io.mongodb;
 
 import static com.google.common.base.Preconditions.checkArgument;
+import static com.mongodb.client.model.Projections.include;
 
 import com.google.auto.value.AutoValue;
 import com.google.common.annotations.VisibleForTesting;
@@ -29,6 +30,7 @@
 import com.mongodb.client.MongoCursor;
 import com.mongodb.client.MongoDatabase;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.List;
 import javax.annotation.Nullable;
 import org.apache.beam.sdk.annotations.Experimental;
@@ -135,6 +137,9 @@ private MongoDbIO() {}
     @Nullable
     abstract String filter();
 
+    @Nullable
+    abstract List<String> projection();
+
     abstract int numSplits();
 
     abstract Builder builder();
@@ -153,6 +158,8 @@ private MongoDbIO() {}
 
       abstract Builder setFilter(String filter);
 
+      abstract Builder setProjection(List<String> fieldNames);
+
       abstract Builder setNumSplits(int numSplits);
 
       abstract Read build();
@@ -226,6 +233,12 @@ public Read withFilter(String filter) {
       return builder().setFilter(filter).build();
     }
 
+    /** Sets a projection on the documents in a collection. */
+    public Read withProjection(final String... fieldNames) {
+      checkArgument(fieldNames.length > 0, "projection can not be null");
+      return builder().setProjection(Arrays.asList(fieldNames)).build();
+    }
+
     /** Sets the user defined number of splits. */
     public Read withNumSplits(int numSplits) {
       checkArgument(numSplits >= 0, "invalid num_splits: must be >= 0, but was 
%s", numSplits);
@@ -249,6 +262,10 @@ public void populateDisplayData(DisplayData.Builder 
builder) {
       builder.add(DisplayData.item("database", database()));
       builder.add(DisplayData.item("collection", collection()));
       builder.addIfNotNull(DisplayData.item("filter", filter()));
+      if (projection() != null) {
+        builder.addIfNotNull(
+            DisplayData.item("projection", 
Arrays.toString(projection().toArray())));
+      }
       builder.add(DisplayData.item("numSplit", numSplits()));
     }
   }
@@ -450,10 +467,18 @@ public boolean start() {
       MongoCollection<Document> mongoCollection = 
mongoDatabase.getCollection(spec.collection());
 
       if (spec.filter() == null) {
-        cursor = mongoCollection.find().iterator();
+        if (spec.projection() == null) {
+          cursor = mongoCollection.find().iterator();
+        } else {
+          cursor = 
mongoCollection.find().projection(include(spec.projection())).iterator();
+        }
       } else {
         Document bson = Document.parse(spec.filter());
-        cursor = mongoCollection.find(bson).iterator();
+        if (spec.projection() == null) {
+          cursor = mongoCollection.find(bson).iterator();
+        } else {
+          cursor = 
mongoCollection.find(bson).projection(include(spec.projection())).iterator();
+        }
       }
 
       return advance();
diff --git 
a/sdks/java/io/mongodb/src/test/java/org/apache/beam/sdk/io/mongodb/MongoDbIOTest.java
 
b/sdks/java/io/mongodb/src/test/java/org/apache/beam/sdk/io/mongodb/MongoDbIOTest.java
index 3d586284d248..09d8d3a14969 100644
--- 
a/sdks/java/io/mongodb/src/test/java/org/apache/beam/sdk/io/mongodb/MongoDbIOTest.java
+++ 
b/sdks/java/io/mongodb/src/test/java/org/apache/beam/sdk/io/mongodb/MongoDbIOTest.java
@@ -45,6 +45,7 @@
 import org.apache.beam.sdk.testing.TestPipeline;
 import org.apache.beam.sdk.transforms.Count;
 import org.apache.beam.sdk.transforms.Create;
+import org.apache.beam.sdk.transforms.Filter;
 import org.apache.beam.sdk.transforms.MapElements;
 import org.apache.beam.sdk.transforms.SimpleFunction;
 import org.apache.beam.sdk.values.KV;
@@ -130,11 +131,24 @@ public void setup() throws Exception {
       "Galilei",
       "Maxwell"
     };
+    String[] country = {
+      "Germany",
+      "England",
+      "Poland",
+      "France",
+      "France",
+      "England",
+      "England",
+      "Denmark",
+      "Florence",
+      "Scotland"
+    };
     for (int i = 1; i <= 1000; i++) {
       int index = i % scientists.length;
       Document document = new Document();
       document.append("_id", i);
       document.append("scientist", scientists[index]);
+      document.append("country", country[index]);
       collection.insertOne(document);
     }
   }
@@ -252,6 +266,55 @@ public void testReadWithFilter() throws Exception {
     pipeline.run();
   }
 
+  @Test
+  public void testReadWithFilterAndProjection() throws Exception {
+
+    PCollection<Document> output =
+        pipeline.apply(
+            MongoDbIO.read()
+                .withUri("mongodb://localhost:" + port)
+                .withDatabase(DATABASE)
+                .withCollection(COLLECTION)
+                .withFilter("{\"scientist\":\"Einstein\"}")
+                .withProjection("country", "scientist"));
+
+    PAssert.thatSingleton(
+            output
+                .apply(
+                    "Map Scientist",
+                    Filter.by(
+                        (Document doc) ->
+                            doc.get("country") != null && doc.get("scientist") 
!= null))
+                .apply("Count", Count.globally()))
+        .isEqualTo(100L);
+
+    pipeline.run();
+  }
+
+  @Test
+  public void testReadWithProjection() throws Exception {
+
+    PCollection<Document> output =
+        pipeline.apply(
+            MongoDbIO.read()
+                .withUri("mongodb://localhost:" + port)
+                .withDatabase(DATABASE)
+                .withCollection(COLLECTION)
+                .withProjection("country"));
+
+    PAssert.thatSingleton(
+            output
+                .apply(
+                    "Map Scientist",
+                    Filter.by(
+                        (Document doc) ->
+                            doc.get("country") != null && doc.get("scientist") 
== null))
+                .apply("Count", Count.globally()))
+        .isEqualTo(1000L);
+
+    pipeline.run();
+  }
+
   @Test
   public void testWrite() throws Exception {
 


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


Issue Time Tracking
-------------------

            Worklog Id:     (was: 170225)
            Time Spent: 40m  (was: 0.5h)
    Remaining Estimate: 23h 20m  (was: 23.5h)

> MongoDbIO add support for projection
> ------------------------------------
>
>                 Key: BEAM-6134
>                 URL: https://issues.apache.org/jira/browse/BEAM-6134
>             Project: Beam
>          Issue Type: Improvement
>          Components: io-java-mongodb
>    Affects Versions: 2.8.0
>            Reporter: Chaim
>            Assignee: Jean-Baptiste Onofré
>            Priority: Major
>              Labels: easyfix
>             Fix For: 2.10.0
>
>   Original Estimate: 24h
>          Time Spent: 40m
>  Remaining Estimate: 23h 20m
>
> MongoDbIO.read should have support for Projection, so that we can limit the 
> document size that is retrieved, for example:
>  
> pipeline.apply(tableName + " read", MongoDbIO.read()
>  .withUri("mongodb://" + srcMongodbManagment.getMongodbConnection())
>  .withProjection("time")
>  .withDatabase(options.getDBName())
>  .withKeepAlive(true)
>  .withCollection(tableName))
>  



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to