You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@nifi.apache.org by MikeThomsen <gi...@git.apache.org> on 2018/04/09 15:37:32 UTC

[GitHub] nifi pull request #2619: NIFI-5059 Updated MongoDBLookupService to be able t...

GitHub user MikeThomsen opened a pull request:

    https://github.com/apache/nifi/pull/2619

    NIFI-5059 Updated MongoDBLookupService to be able to detect record sc…

    …hemas or take one provided by the user.
    
    Thank you for submitting a contribution to Apache NiFi.
    
    In order to streamline the review of the contribution we ask you
    to ensure the following steps have been taken:
    
    ### For all changes:
    - [ ] Is there a JIRA ticket associated with this PR? Is it referenced 
         in the commit message?
    
    - [ ] Does your PR title start with NIFI-XXXX where XXXX is the JIRA number you are trying to resolve? Pay particular attention to the hyphen "-" character.
    
    - [ ] Has your PR been rebased against the latest commit within the target branch (typically master)?
    
    - [ ] Is your initial contribution a single, squashed commit?
    
    ### For code changes:
    - [ ] Have you ensured that the full suite of tests is executed via mvn -Pcontrib-check clean install at the root nifi folder?
    - [ ] Have you written or updated unit tests to verify your changes?
    - [ ] If adding new dependencies to the code, are these dependencies licensed in a way that is compatible for inclusion under [ASF 2.0](http://www.apache.org/legal/resolved.html#category-a)? 
    - [ ] If applicable, have you updated the LICENSE file, including the main LICENSE file under nifi-assembly?
    - [ ] If applicable, have you updated the NOTICE file, including the main NOTICE file found under nifi-assembly?
    - [ ] If adding new Properties, have you added .displayName in addition to .name (programmatic access) for each of the new properties?
    
    ### For documentation related changes:
    - [ ] Have you ensured that format looks appropriate for the output in which it is rendered?
    
    ### Note:
    Please ensure that once the PR is submitted, you check travis-ci for build issues and submit an update to your PR as soon as possible.


You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/MikeThomsen/nifi NIFI-5059

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/nifi/pull/2619.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #2619
    
----
commit 40cbd11915e15e87c5b568a2cd918e57126bb7b4
Author: Mike Thomsen <mi...@...>
Date:   2018-04-09T11:28:40Z

    NIFI-5059 Updated MongoDBLookupService to be able to detect record schemas or take one provided by the user.

----


---

[GitHub] nifi issue #2619: NIFI-5059 Updated MongoDBLookupService to be able to detec...

Posted by mattyb149 <gi...@git.apache.org>.
Github user mattyb149 commented on the issue:

    https://github.com/apache/nifi/pull/2619
  
    Agreed, I'll try to get this one in today then take a look at the ES one.


---

[GitHub] nifi pull request #2619: NIFI-5059 Updated MongoDBLookupService to be able t...

Posted by mattyb149 <gi...@git.apache.org>.
Github user mattyb149 commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/2619#discussion_r199518028
  
    --- Diff: nifi-nar-bundles/nifi-extension-utils/nifi-record-utils/nifi-avro-record-utils/src/main/java/org/apache/nifi/serialization/JsonInferenceSchemaRegistryService.java ---
    @@ -0,0 +1,103 @@
    +/*
    + * 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.nifi.serialization;
    +
    +import org.apache.nifi.annotation.lifecycle.OnEnabled;
    +import org.apache.nifi.components.AllowableValue;
    +import org.apache.nifi.components.PropertyDescriptor;
    +import org.apache.nifi.components.PropertyValue;
    +import org.apache.nifi.controller.ConfigurationContext;
    +import org.apache.nifi.schema.access.AvroSchemaTextStrategy;
    +import org.apache.nifi.schema.access.InferenceSchemaStrategy;
    +import org.apache.nifi.schema.access.JsonSchemaAccessStrategy;
    +import org.apache.nifi.schema.access.SchemaAccessStrategy;
    +import org.apache.nifi.schema.access.SchemaNamePropertyStrategy;
    +import org.apache.nifi.schema.access.SchemaNotFoundException;
    +import org.apache.nifi.schemaregistry.services.SchemaRegistry;
    +import org.apache.nifi.serialization.record.RecordSchema;
    +
    +import java.io.IOException;
    +import java.util.ArrayList;
    +import java.util.List;
    +import java.util.Map;
    +
    +import static org.apache.nifi.schema.access.SchemaAccessUtils.INFER_SCHEMA;
    +import static org.apache.nifi.schema.access.SchemaAccessUtils.SCHEMA_BRANCH_NAME;
    +import static org.apache.nifi.schema.access.SchemaAccessUtils.SCHEMA_NAME;
    +import static org.apache.nifi.schema.access.SchemaAccessUtils.SCHEMA_NAME_PROPERTY;
    +import static org.apache.nifi.schema.access.SchemaAccessUtils.SCHEMA_REGISTRY;
    +import static org.apache.nifi.schema.access.SchemaAccessUtils.SCHEMA_TEXT;
    +import static org.apache.nifi.schema.access.SchemaAccessUtils.SCHEMA_TEXT_PROPERTY;
    +import static org.apache.nifi.schema.access.SchemaAccessUtils.SCHEMA_VERSION;
    +
    +public class JsonInferenceSchemaRegistryService extends SchemaRegistryService {
    +    private String schemaAccess;
    +
    +    @OnEnabled
    +    public void onEnabled(ConfigurationContext context) {
    +        this.storeSchemaAccessStrategy(context);
    +        this.schemaAccess = context.getProperty(getSchemaAcessStrategyDescriptor()).getValue();
    +    }
    +
    +    @Override
    +    protected SchemaAccessStrategy getSchemaAccessStrategy(final String strategy, final SchemaRegistry schemaRegistry, final ConfigurationContext context) {
    --- End diff --
    
    Since this impl is specifically for JSON inference, perhaps it should override getDefaultSchemaAccessStrategy() to return the Infer one?


---

[GitHub] nifi pull request #2619: NIFI-5059 Updated MongoDBLookupService to be able t...

Posted by mattyb149 <gi...@git.apache.org>.
Github user mattyb149 commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/2619#discussion_r199519266
  
    --- Diff: nifi-nar-bundles/nifi-mongodb-bundle/nifi-mongodb-services/src/test/java/org/apache/nifi/mongodb/TestSchemaRegistry.java ---
    @@ -0,0 +1,49 @@
    +/*
    + * 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.nifi.mongodb;
    +
    +import org.apache.nifi.controller.AbstractControllerService;
    +import org.apache.nifi.schema.access.SchemaField;
    +import org.apache.nifi.schemaregistry.services.SchemaRegistry;
    +import org.apache.nifi.serialization.SimpleRecordSchema;
    +import org.apache.nifi.serialization.record.RecordField;
    +import org.apache.nifi.serialization.record.RecordFieldType;
    +import org.apache.nifi.serialization.record.RecordSchema;
    +import org.apache.nifi.serialization.record.SchemaIdentifier;
    +
    +import java.util.ArrayList;
    +import java.util.HashSet;
    +import java.util.List;
    +import java.util.Set;
    +
    +public class TestSchemaRegistry extends AbstractControllerService implements SchemaRegistry {
    --- End diff --
    
    Should this be called StubSchemaRegistry or MockSchemaRegistry? With Test at the front, I imagine it gets picked up by JUnit (although there are no @Test methods, but still)


---

[GitHub] nifi issue #2619: NIFI-5059 Updated MongoDBLookupService to be able to detec...

Posted by MikeThomsen <gi...@git.apache.org>.
Github user MikeThomsen commented on the issue:

    https://github.com/apache/nifi/pull/2619
  
    @mattyb149 @ijokarumawak do either of you have time to get this reviewed before 1.7.0 release vote starts?


---

[GitHub] nifi pull request #2619: NIFI-5059 Updated MongoDBLookupService to be able t...

Posted by MikeThomsen <gi...@git.apache.org>.
Github user MikeThomsen commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/2619#discussion_r193770298
  
    --- Diff: nifi-nar-bundles/nifi-extension-utils/nifi-record-utils/nifi-standard-record-utils/src/main/java/org/apache/nifi/schema/access/JsonSchemaAccessStrategy.java ---
    @@ -0,0 +1,36 @@
    +/*
    + * 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.nifi.schema.access;
    +
    +import org.apache.nifi.serialization.record.RecordSchema;
    +
    +import java.io.IOException;
    +import java.util.Map;
    +
    +public interface JsonSchemaAccessStrategy extends SchemaAccessStrategy {
    --- End diff --
    
    You're not missing anything...


---

[GitHub] nifi pull request #2619: NIFI-5059 Updated MongoDBLookupService to be able t...

Posted by bbende <gi...@git.apache.org>.
Github user bbende commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/2619#discussion_r193739859
  
    --- Diff: nifi-nar-bundles/nifi-extension-utils/nifi-record-utils/nifi-standard-record-utils/src/main/java/org/apache/nifi/schema/access/JsonSchemaAccessStrategy.java ---
    @@ -0,0 +1,36 @@
    +/*
    + * 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.nifi.schema.access;
    +
    +import org.apache.nifi.serialization.record.RecordSchema;
    +
    +import java.io.IOException;
    +import java.util.Map;
    +
    +public interface JsonSchemaAccessStrategy extends SchemaAccessStrategy {
    --- End diff --
    
    Ok but I'm confused because I'm not seeing an actual call that uses the new method...
    
    The MongoLookupService does this:
    ```
    
    private RecordSchema loadSchema(Map<String, Object> coordinates, Document doc) {
    +        Map<String, String> variables = coordinates.entrySet().stream()
    +            .collect(Collectors.toMap(
    +                e -> e.getKey(),
    +                e -> e.getValue().toString()
    +            ));
    +        ObjectMapper mapper = new ObjectMapper();
    +        try {
    +            byte[] bytes = mapper.writeValueAsBytes(doc);
    +            return getSchema(variables, new ByteArrayInputStream(bytes), null);
    +        } catch (Exception ex) {
    +            return null;
    +        }
    +    }
    
    So since we are reserializing the Doc here and putting the coordinates as variables, I'm not seeing where we call the new method, but I may be missing it.
    ```


---

[GitHub] nifi pull request #2619: NIFI-5059 Updated MongoDBLookupService to be able t...

Posted by mattyb149 <gi...@git.apache.org>.
Github user mattyb149 commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/2619#discussion_r192784028
  
    --- Diff: nifi-nar-bundles/nifi-mongodb-bundle/nifi-mongodb-services/src/main/java/org/apache/nifi/mongodb/MongoDBLookupService.java ---
    @@ -52,68 +54,125 @@
         "The query is limited to the first result (findOne in the Mongo documentation). If no \"Lookup Value Field\" is specified " +
         "then the entire MongoDB result document minus the _id field will be returned as a record."
     )
    -public class MongoDBLookupService extends MongoDBControllerService implements LookupService<Object> {
    +public class MongoDBLookupService extends SchemaRegistryService implements LookupService<Object> {
    +    public static final PropertyDescriptor CONTROLLER_SERVICE = new PropertyDescriptor.Builder()
    +        .name("mongo-lookup-client-service")
    +        .displayName("Client Service")
    +        .description("A MongoDB controller service to use with this lookup service.")
    +        .required(true)
    +        .identifiesControllerService(MongoDBControllerService.class)
    +        .build();
     
         public static final PropertyDescriptor LOOKUP_VALUE_FIELD = new PropertyDescriptor.Builder()
    -            .name("mongo-lookup-value-field")
    -            .displayName("Lookup Value Field")
    -            .description("The field whose value will be returned when the lookup key(s) match a record. If not specified then the entire " +
    -                    "MongoDB result document minus the _id field will be returned as a record.")
    -            .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
    -            .required(false)
    -            .build();
    +        .name("mongo-lookup-value-field")
    +        .displayName("Lookup Value Field")
    +        .description("The field whose value will be returned when the lookup key(s) match a record. If not specified then the entire " +
    +                "MongoDB result document minus the _id field will be returned as a record.")
    +        .addValidator(Validator.VALID)
    +        .required(false)
    +        .build();
    +    public static final PropertyDescriptor PROJECTION = new PropertyDescriptor.Builder()
    +        .name("mongo-lookup-projection")
    +        .displayName("Projection")
    +        .description("Specifies a projection for limiting which fields will be returned.")
    +        .required(false)
    +        .build();
     
         private String lookupValueField;
     
    -    private static final List<PropertyDescriptor> lookupDescriptors;
    -
    -    static {
    -        lookupDescriptors = new ArrayList<>();
    -        lookupDescriptors.addAll(descriptors);
    -        lookupDescriptors.add(LOOKUP_VALUE_FIELD);
    -    }
    -
         @Override
         public Optional<Object> lookup(Map<String, Object> coordinates) throws LookupFailureException {
    -        Map<String, Object> clean = new HashMap<>();
    -        clean.putAll(coordinates);
    +        Map<String, Object> clean = coordinates.entrySet().stream()
    +            .filter(e -> !schemaNameProperty.equals(String.format("${%s}", e.getKey())))
    +            .collect(Collectors.toMap(
    +                e -> e.getKey(),
    +                e -> e.getValue()
    +            ));
             Document query = new Document(clean);
     
             if (coordinates.size() == 0) {
                 throw new LookupFailureException("No keys were configured. Mongo query would return random documents.");
             }
     
             try {
    -            Document result = this.findOne(query);
    +            Document result = projection != null ? controllerService.findOne(query, projection) : controllerService.findOne(query);
     
                 if(result == null) {
                     return Optional.empty();
                 } else if (!StringUtils.isEmpty(lookupValueField)) {
                     return Optional.ofNullable(result.get(lookupValueField));
                 } else {
    -                final List<RecordField> fields = new ArrayList<>();
    +                RecordSchema schema = loadSchema(coordinates);
     
    -                for (String key : result.keySet()) {
    -                    if (key.equals("_id")) {
    -                        continue;
    -                    }
    -                    fields.add(new RecordField(key, RecordFieldType.STRING.getDataType()));
    -                }
    -
    -                final RecordSchema schema = new SimpleRecordSchema(fields);
    -                return Optional.ofNullable(new MapRecord(schema, result));
    +                RecordSchema toUse = schema != null ? schema : convertSchema(result);
    +                return Optional.ofNullable(new MapRecord(toUse, result));
                 }
             } catch (Exception ex) {
                 getLogger().error("Error during lookup {}", new Object[]{ query.toJson() }, ex);
                 throw new LookupFailureException(ex);
             }
         }
     
    -    @Override
    +    private RecordSchema loadSchema(Map<String, Object> coordinates) {
    +        Map<String, String> variables = coordinates.entrySet().stream()
    +            .collect(Collectors.toMap(
    +                e -> e.getKey(),
    +                e -> e.getValue().toString()
    +            ));
    +        try {
    +            return getSchema(variables, null);
    +        } catch (Exception ex) {
    +            return null;
    +        }
    +    }
    +
    +    private RecordSchema convertSchema(Map<String, Object> result) {
    +        List<RecordField> fields = new ArrayList<>();
    +        for (Map.Entry<String, Object> entry : result.entrySet()) {
    +
    +            RecordField field;
    +            if (entry.getValue() instanceof Integer) {
    +                field = new RecordField(entry.getKey(), RecordFieldType.INT.getDataType());
    +            } else if (entry.getValue() instanceof Long) {
    +                field = new RecordField(entry.getKey(), RecordFieldType.LONG.getDataType());
    +            } else if (entry.getValue() instanceof Boolean) {
    +                field = new RecordField(entry.getKey(), RecordFieldType.BOOLEAN.getDataType());
    +            } else if (entry.getValue() instanceof Double) {
    +                field = new RecordField(entry.getKey(), RecordFieldType.DOUBLE.getDataType());
    +            } else if (entry.getValue() instanceof Date) {
    +                field = new RecordField(entry.getKey(), RecordFieldType.DATE.getDataType());
    +            } else if (entry.getValue() instanceof List) {
    +                field = new RecordField(entry.getKey(), RecordFieldType.ARRAY.getDataType());
    +            } else if (entry.getValue() instanceof Map) {
    +                RecordSchema nestedSchema = convertSchema((Map)entry.getValue());
    +                RecordDataType rdt = new RecordDataType(nestedSchema);
    +                field = new RecordField(entry.getKey(), rdt);
    +            } else {
    +                field = new RecordField(entry.getKey(), RecordFieldType.STRING.getDataType());
    +            }
    +            fields.add(field);
    +        }
    +
    +        return new SimpleRecordSchema(fields);
    +    }
    +
    +    private volatile Document projection;
    +    private MongoDBControllerService controllerService;
    +    private String schemaNameProperty;
    +
         @OnEnabled
    -    public void onEnabled(final ConfigurationContext context) throws InitializationException, IOException, InterruptedException {
    +    public void onEnabled(final ConfigurationContext context) {
             this.lookupValueField = context.getProperty(LOOKUP_VALUE_FIELD).getValue();
    -        super.onEnabled(context);
    +        this.controllerService = context.getProperty(CONTROLLER_SERVICE).asControllerService(MongoDBControllerService.class);
    --- End diff --
    
    I get a runtime error here as it is expecting an interface not an impl class, I had to change this to `MongoDBClientService` to get past the runtime error.


---

[GitHub] nifi issue #2619: NIFI-5059 Updated MongoDBLookupService to be able to detec...

Posted by MikeThomsen <gi...@git.apache.org>.
Github user MikeThomsen commented on the issue:

    https://github.com/apache/nifi/pull/2619
  
    @mattyb149 Any chance you can take a look?


---

[GitHub] nifi pull request #2619: NIFI-5059 Updated MongoDBLookupService to be able t...

Posted by MikeThomsen <gi...@git.apache.org>.
Github user MikeThomsen commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/2619#discussion_r199600826
  
    --- Diff: nifi-nar-bundles/nifi-extension-utils/nifi-record-utils/nifi-avro-record-utils/src/main/java/org/apache/nifi/serialization/JsonInferenceSchemaRegistryService.java ---
    @@ -0,0 +1,103 @@
    +/*
    + * 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.nifi.serialization;
    +
    +import org.apache.nifi.annotation.lifecycle.OnEnabled;
    +import org.apache.nifi.components.AllowableValue;
    +import org.apache.nifi.components.PropertyDescriptor;
    +import org.apache.nifi.components.PropertyValue;
    +import org.apache.nifi.controller.ConfigurationContext;
    +import org.apache.nifi.schema.access.AvroSchemaTextStrategy;
    +import org.apache.nifi.schema.access.InferenceSchemaStrategy;
    +import org.apache.nifi.schema.access.JsonSchemaAccessStrategy;
    +import org.apache.nifi.schema.access.SchemaAccessStrategy;
    +import org.apache.nifi.schema.access.SchemaNamePropertyStrategy;
    +import org.apache.nifi.schema.access.SchemaNotFoundException;
    +import org.apache.nifi.schemaregistry.services.SchemaRegistry;
    +import org.apache.nifi.serialization.record.RecordSchema;
    +
    +import java.io.IOException;
    +import java.util.ArrayList;
    +import java.util.List;
    +import java.util.Map;
    +
    +import static org.apache.nifi.schema.access.SchemaAccessUtils.INFER_SCHEMA;
    +import static org.apache.nifi.schema.access.SchemaAccessUtils.SCHEMA_BRANCH_NAME;
    +import static org.apache.nifi.schema.access.SchemaAccessUtils.SCHEMA_NAME;
    +import static org.apache.nifi.schema.access.SchemaAccessUtils.SCHEMA_NAME_PROPERTY;
    +import static org.apache.nifi.schema.access.SchemaAccessUtils.SCHEMA_REGISTRY;
    +import static org.apache.nifi.schema.access.SchemaAccessUtils.SCHEMA_TEXT;
    +import static org.apache.nifi.schema.access.SchemaAccessUtils.SCHEMA_TEXT_PROPERTY;
    +import static org.apache.nifi.schema.access.SchemaAccessUtils.SCHEMA_VERSION;
    +
    +public class JsonInferenceSchemaRegistryService extends SchemaRegistryService {
    +    private String schemaAccess;
    +
    +    @OnEnabled
    +    public void onEnabled(ConfigurationContext context) {
    +        this.storeSchemaAccessStrategy(context);
    +        this.schemaAccess = context.getProperty(getSchemaAcessStrategyDescriptor()).getValue();
    +    }
    +
    +    @Override
    +    protected SchemaAccessStrategy getSchemaAccessStrategy(final String strategy, final SchemaRegistry schemaRegistry, final ConfigurationContext context) {
    --- End diff --
    
    Done. I think that probably is the right thing to do here.


---

[GitHub] nifi issue #2619: NIFI-5059 Updated MongoDBLookupService to be able to detec...

Posted by MikeThomsen <gi...@git.apache.org>.
Github user MikeThomsen commented on the issue:

    https://github.com/apache/nifi/pull/2619
  
    @mattyb149 can we close this one out? It's a good starting point for [this cleanup task](https://issues.apache.org/jira/browse/NIFI-5239).


---

[GitHub] nifi issue #2619: NIFI-5059 Updated MongoDBLookupService to be able to detec...

Posted by MikeThomsen <gi...@git.apache.org>.
Github user MikeThomsen commented on the issue:

    https://github.com/apache/nifi/pull/2619
  
    Accidentally rebased it a while and so I had to force push. Sorry about that.


---

[GitHub] nifi issue #2619: NIFI-5059 Updated MongoDBLookupService to be able to detec...

Posted by MikeThomsen <gi...@git.apache.org>.
Github user MikeThomsen commented on the issue:

    https://github.com/apache/nifi/pull/2619
  
    That was the original approach. I'm now leaning toward going back to that because it's feeling like "less is more" here.


---

[GitHub] nifi pull request #2619: NIFI-5059 Updated MongoDBLookupService to be able t...

Posted by mattyb149 <gi...@git.apache.org>.
Github user mattyb149 commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/2619#discussion_r192841468
  
    --- Diff: nifi-nar-bundles/nifi-mongodb-bundle/nifi-mongodb-services/src/main/java/org/apache/nifi/mongodb/MongoDBLookupService.java ---
    @@ -52,68 +54,125 @@
         "The query is limited to the first result (findOne in the Mongo documentation). If no \"Lookup Value Field\" is specified " +
         "then the entire MongoDB result document minus the _id field will be returned as a record."
     )
    -public class MongoDBLookupService extends MongoDBControllerService implements LookupService<Object> {
    +public class MongoDBLookupService extends SchemaRegistryService implements LookupService<Object> {
    +    public static final PropertyDescriptor CONTROLLER_SERVICE = new PropertyDescriptor.Builder()
    +        .name("mongo-lookup-client-service")
    +        .displayName("Client Service")
    +        .description("A MongoDB controller service to use with this lookup service.")
    +        .required(true)
    +        .identifiesControllerService(MongoDBControllerService.class)
    +        .build();
     
         public static final PropertyDescriptor LOOKUP_VALUE_FIELD = new PropertyDescriptor.Builder()
    -            .name("mongo-lookup-value-field")
    -            .displayName("Lookup Value Field")
    -            .description("The field whose value will be returned when the lookup key(s) match a record. If not specified then the entire " +
    -                    "MongoDB result document minus the _id field will be returned as a record.")
    -            .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
    -            .required(false)
    -            .build();
    +        .name("mongo-lookup-value-field")
    +        .displayName("Lookup Value Field")
    +        .description("The field whose value will be returned when the lookup key(s) match a record. If not specified then the entire " +
    +                "MongoDB result document minus the _id field will be returned as a record.")
    +        .addValidator(Validator.VALID)
    +        .required(false)
    +        .build();
    +    public static final PropertyDescriptor PROJECTION = new PropertyDescriptor.Builder()
    +        .name("mongo-lookup-projection")
    +        .displayName("Projection")
    +        .description("Specifies a projection for limiting which fields will be returned.")
    +        .required(false)
    +        .build();
     
         private String lookupValueField;
     
    -    private static final List<PropertyDescriptor> lookupDescriptors;
    -
    -    static {
    -        lookupDescriptors = new ArrayList<>();
    -        lookupDescriptors.addAll(descriptors);
    -        lookupDescriptors.add(LOOKUP_VALUE_FIELD);
    -    }
    -
         @Override
         public Optional<Object> lookup(Map<String, Object> coordinates) throws LookupFailureException {
    -        Map<String, Object> clean = new HashMap<>();
    -        clean.putAll(coordinates);
    +        Map<String, Object> clean = coordinates.entrySet().stream()
    +            .filter(e -> !schemaNameProperty.equals(String.format("${%s}", e.getKey())))
    +            .collect(Collectors.toMap(
    +                e -> e.getKey(),
    +                e -> e.getValue()
    +            ));
             Document query = new Document(clean);
     
             if (coordinates.size() == 0) {
                 throw new LookupFailureException("No keys were configured. Mongo query would return random documents.");
             }
     
             try {
    -            Document result = this.findOne(query);
    +            Document result = projection != null ? controllerService.findOne(query, projection) : controllerService.findOne(query);
     
                 if(result == null) {
                     return Optional.empty();
                 } else if (!StringUtils.isEmpty(lookupValueField)) {
                     return Optional.ofNullable(result.get(lookupValueField));
                 } else {
    -                final List<RecordField> fields = new ArrayList<>();
    +                RecordSchema schema = loadSchema(coordinates);
     
    -                for (String key : result.keySet()) {
    -                    if (key.equals("_id")) {
    -                        continue;
    -                    }
    -                    fields.add(new RecordField(key, RecordFieldType.STRING.getDataType()));
    -                }
    -
    -                final RecordSchema schema = new SimpleRecordSchema(fields);
    -                return Optional.ofNullable(new MapRecord(schema, result));
    +                RecordSchema toUse = schema != null ? schema : convertSchema(result);
    +                return Optional.ofNullable(new MapRecord(toUse, result));
                 }
             } catch (Exception ex) {
                 getLogger().error("Error during lookup {}", new Object[]{ query.toJson() }, ex);
                 throw new LookupFailureException(ex);
             }
         }
     
    -    @Override
    +    private RecordSchema loadSchema(Map<String, Object> coordinates) {
    +        Map<String, String> variables = coordinates.entrySet().stream()
    +            .collect(Collectors.toMap(
    +                e -> e.getKey(),
    +                e -> e.getValue().toString()
    +            ));
    +        try {
    +            return getSchema(variables, null);
    +        } catch (Exception ex) {
    +            return null;
    +        }
    +    }
    +
    +    private RecordSchema convertSchema(Map<String, Object> result) {
    +        List<RecordField> fields = new ArrayList<>();
    +        for (Map.Entry<String, Object> entry : result.entrySet()) {
    +
    +            RecordField field;
    +            if (entry.getValue() instanceof Integer) {
    +                field = new RecordField(entry.getKey(), RecordFieldType.INT.getDataType());
    +            } else if (entry.getValue() instanceof Long) {
    +                field = new RecordField(entry.getKey(), RecordFieldType.LONG.getDataType());
    +            } else if (entry.getValue() instanceof Boolean) {
    +                field = new RecordField(entry.getKey(), RecordFieldType.BOOLEAN.getDataType());
    +            } else if (entry.getValue() instanceof Double) {
    +                field = new RecordField(entry.getKey(), RecordFieldType.DOUBLE.getDataType());
    +            } else if (entry.getValue() instanceof Date) {
    +                field = new RecordField(entry.getKey(), RecordFieldType.DATE.getDataType());
    +            } else if (entry.getValue() instanceof List) {
    +                field = new RecordField(entry.getKey(), RecordFieldType.ARRAY.getDataType());
    +            } else if (entry.getValue() instanceof Map) {
    +                RecordSchema nestedSchema = convertSchema((Map)entry.getValue());
    +                RecordDataType rdt = new RecordDataType(nestedSchema);
    +                field = new RecordField(entry.getKey(), rdt);
    +            } else {
    +                field = new RecordField(entry.getKey(), RecordFieldType.STRING.getDataType());
    +            }
    +            fields.add(field);
    +        }
    +
    +        return new SimpleRecordSchema(fields);
    +    }
    +
    +    private volatile Document projection;
    +    private MongoDBControllerService controllerService;
    +    private String schemaNameProperty;
    +
         @OnEnabled
    -    public void onEnabled(final ConfigurationContext context) throws InitializationException, IOException, InterruptedException {
    +    public void onEnabled(final ConfigurationContext context) {
             this.lookupValueField = context.getProperty(LOOKUP_VALUE_FIELD).getValue();
    -        super.onEnabled(context);
    +        this.controllerService = context.getProperty(CONTROLLER_SERVICE).asControllerService(MongoDBControllerService.class);
    --- End diff --
    
    Looks like this one is still there, still getting the runtime errors


---

[GitHub] nifi pull request #2619: NIFI-5059 Updated MongoDBLookupService to be able t...

Posted by MikeThomsen <gi...@git.apache.org>.
Github user MikeThomsen commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/2619#discussion_r193738667
  
    --- Diff: nifi-nar-bundles/nifi-extension-utils/nifi-record-utils/nifi-avro-record-utils/src/main/java/org/apache/nifi/schema/access/SchemaAccessUtils.java ---
    @@ -176,6 +176,8 @@ public static SchemaAccessStrategy getSchemaAccessStrategy(final String allowabl
                 return new HortonworksAttributeSchemaReferenceStrategy(schemaRegistry);
             } else if (allowableValue.equalsIgnoreCase(CONFLUENT_ENCODED_SCHEMA.getValue())) {
                 return new ConfluentSchemaRegistryStrategy(schemaRegistry);
    +        } else if (allowableValue.equalsIgnoreCase(INFER_SCHEMA.getValue())) {
    --- End diff --
    
    Ok. I'll work on that.


---

[GitHub] nifi issue #2619: NIFI-5059 Updated MongoDBLookupService to be able to detec...

Posted by MikeThomsen <gi...@git.apache.org>.
Github user MikeThomsen commented on the issue:

    https://github.com/apache/nifi/pull/2619
  
    @mattyb149 can you review this?


---

[GitHub] nifi pull request #2619: NIFI-5059 Updated MongoDBLookupService to be able t...

Posted by mattyb149 <gi...@git.apache.org>.
Github user mattyb149 commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/2619#discussion_r193406221
  
    --- Diff: nifi-nar-bundles/nifi-mongodb-bundle/nifi-mongodb-services/src/main/java/org/apache/nifi/mongodb/MongoDBLookupService.java ---
    @@ -52,68 +54,125 @@
         "The query is limited to the first result (findOne in the Mongo documentation). If no \"Lookup Value Field\" is specified " +
         "then the entire MongoDB result document minus the _id field will be returned as a record."
     )
    -public class MongoDBLookupService extends MongoDBControllerService implements LookupService<Object> {
    +public class MongoDBLookupService extends SchemaRegistryService implements LookupService<Object> {
    +    public static final PropertyDescriptor CONTROLLER_SERVICE = new PropertyDescriptor.Builder()
    +        .name("mongo-lookup-client-service")
    +        .displayName("Client Service")
    +        .description("A MongoDB controller service to use with this lookup service.")
    +        .required(true)
    +        .identifiesControllerService(MongoDBClientService.class)
    +        .build();
     
         public static final PropertyDescriptor LOOKUP_VALUE_FIELD = new PropertyDescriptor.Builder()
    -            .name("mongo-lookup-value-field")
    -            .displayName("Lookup Value Field")
    -            .description("The field whose value will be returned when the lookup key(s) match a record. If not specified then the entire " +
    -                    "MongoDB result document minus the _id field will be returned as a record.")
    -            .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
    -            .required(false)
    -            .build();
    +        .name("mongo-lookup-value-field")
    +        .displayName("Lookup Value Field")
    +        .description("The field whose value will be returned when the lookup key(s) match a record. If not specified then the entire " +
    +                "MongoDB result document minus the _id field will be returned as a record.")
    +        .addValidator(Validator.VALID)
    +        .required(false)
    +        .build();
    +    public static final PropertyDescriptor PROJECTION = new PropertyDescriptor.Builder()
    +        .name("mongo-lookup-projection")
    +        .displayName("Projection")
    +        .description("Specifies a projection for limiting which fields will be returned.")
    +        .required(false)
    +        .build();
     
         private String lookupValueField;
     
    -    private static final List<PropertyDescriptor> lookupDescriptors;
    -
    -    static {
    -        lookupDescriptors = new ArrayList<>();
    -        lookupDescriptors.addAll(descriptors);
    -        lookupDescriptors.add(LOOKUP_VALUE_FIELD);
    -    }
    -
         @Override
         public Optional<Object> lookup(Map<String, Object> coordinates) throws LookupFailureException {
    -        Map<String, Object> clean = new HashMap<>();
    -        clean.putAll(coordinates);
    +        Map<String, Object> clean = coordinates.entrySet().stream()
    +            .filter(e -> !schemaNameProperty.equals(String.format("${%s}", e.getKey())))
    +            .collect(Collectors.toMap(
    +                e -> e.getKey(),
    +                e -> e.getValue()
    +            ));
             Document query = new Document(clean);
     
             if (coordinates.size() == 0) {
                 throw new LookupFailureException("No keys were configured. Mongo query would return random documents.");
             }
     
             try {
    -            Document result = this.findOne(query);
    +            Document result = projection != null ? controllerService.findOne(query, projection) : controllerService.findOne(query);
     
                 if(result == null) {
                     return Optional.empty();
                 } else if (!StringUtils.isEmpty(lookupValueField)) {
                     return Optional.ofNullable(result.get(lookupValueField));
                 } else {
    -                final List<RecordField> fields = new ArrayList<>();
    +                RecordSchema schema = loadSchema(coordinates);
     
    -                for (String key : result.keySet()) {
    -                    if (key.equals("_id")) {
    -                        continue;
    -                    }
    -                    fields.add(new RecordField(key, RecordFieldType.STRING.getDataType()));
    -                }
    -
    -                final RecordSchema schema = new SimpleRecordSchema(fields);
    -                return Optional.ofNullable(new MapRecord(schema, result));
    +                RecordSchema toUse = schema != null ? schema : convertSchema(result);
    --- End diff --
    
    One thing you could do is to override getSupportedPropertyDescriptors() and add your own property for Schema Access Strategy that only has the relevant ones, including your own strategy of "Infer Schema From Document" or something.


---

[GitHub] nifi pull request #2619: NIFI-5059 Updated MongoDBLookupService to be able t...

Posted by bbende <gi...@git.apache.org>.
Github user bbende commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/2619#discussion_r193735509
  
    --- Diff: nifi-nar-bundles/nifi-extension-utils/nifi-record-utils/nifi-avro-record-utils/src/main/java/org/apache/nifi/serialization/JsonInferenceSchemaRegistryService.java ---
    @@ -0,0 +1,77 @@
    +/*
    + * 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.nifi.serialization;
    +
    +import org.apache.nifi.annotation.lifecycle.OnEnabled;
    +import org.apache.nifi.components.AllowableValue;
    +import org.apache.nifi.components.PropertyDescriptor;
    +import org.apache.nifi.controller.ConfigurationContext;
    +import org.apache.nifi.schema.access.JsonSchemaAccessStrategy;
    +import org.apache.nifi.schema.access.SchemaNotFoundException;
    +import org.apache.nifi.serialization.record.RecordSchema;
    +
    +import java.io.IOException;
    +import java.util.ArrayList;
    +import java.util.List;
    +import java.util.Map;
    +
    +import static org.apache.nifi.schema.access.SchemaAccessUtils.INFER_SCHEMA;
    +import static org.apache.nifi.schema.access.SchemaAccessUtils.SCHEMA_BRANCH_NAME;
    +import static org.apache.nifi.schema.access.SchemaAccessUtils.SCHEMA_NAME;
    +import static org.apache.nifi.schema.access.SchemaAccessUtils.SCHEMA_NAME_PROPERTY;
    +import static org.apache.nifi.schema.access.SchemaAccessUtils.SCHEMA_REGISTRY;
    +import static org.apache.nifi.schema.access.SchemaAccessUtils.SCHEMA_TEXT;
    +import static org.apache.nifi.schema.access.SchemaAccessUtils.SCHEMA_TEXT_PROPERTY;
    +import static org.apache.nifi.schema.access.SchemaAccessUtils.SCHEMA_VERSION;
    +
    +public class JsonInferenceSchemaRegistryService extends SchemaRegistryService {
    --- End diff --
    
    I'm not totally sure about this, but I think if we take the approach mentioned in my other comments, we probably wouldn't need this class since the JSON readers would handle the logic for when schemaAccess is set to "JSON Inference", similar to how AvroReader handles when embedded schema is selected - https://github.com/apache/nifi/blob/master/nifi-nar-bundles/nifi-standard-services/nifi-record-serialization-services-bundle/nifi-record-serialization-services/src/main/java/org/apache/nifi/avro/AvroReader.java#L78


---

[GitHub] nifi issue #2619: NIFI-5059 Updated MongoDBLookupService to be able to detec...

Posted by mattyb149 <gi...@git.apache.org>.
Github user mattyb149 commented on the issue:

    https://github.com/apache/nifi/pull/2619
  
    +1 LGTM, one of the unit tests in Travis is failing but it's not the fault of this code. I ran the unit tests and some tests on a live NiFi instance with "Infer" and "Schema Text" strategies, all looked well. Thanks for the addition! Merging to master


---

[GitHub] nifi pull request #2619: NIFI-5059 Updated MongoDBLookupService to be able t...

Posted by bbende <gi...@git.apache.org>.
Github user bbende commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/2619#discussion_r193733115
  
    --- Diff: nifi-nar-bundles/nifi-extension-utils/nifi-record-utils/nifi-avro-record-utils/src/main/java/org/apache/nifi/schema/access/SchemaAccessUtils.java ---
    @@ -176,6 +176,8 @@ public static SchemaAccessStrategy getSchemaAccessStrategy(final String allowabl
                 return new HortonworksAttributeSchemaReferenceStrategy(schemaRegistry);
             } else if (allowableValue.equalsIgnoreCase(CONFLUENT_ENCODED_SCHEMA.getValue())) {
                 return new ConfluentSchemaRegistryStrategy(schemaRegistry);
    +        } else if (allowableValue.equalsIgnoreCase(INFER_SCHEMA.getValue())) {
    --- End diff --
    
    Since this inference only works when the content is JSON, I think this option should only be available when using a JSON related record reader, and not available in the default case. 
    
    This would be similar to how the AvroReader makes available the option for "Embedded Avro Schema" - https://github.com/apache/nifi/blob/master/nifi-nar-bundles/nifi-standard-services/nifi-record-serialization-services-bundle/nifi-record-serialization-services/src/main/java/org/apache/nifi/avro/AvroReader.java#L63
    



---

[GitHub] nifi issue #2619: NIFI-5059 Updated MongoDBLookupService to be able to detec...

Posted by mattyb149 <gi...@git.apache.org>.
Github user mattyb149 commented on the issue:

    https://github.com/apache/nifi/pull/2619
  
    I may have shot myself in the foot here by asking that this extend SchemaRegistryService, as that requires you supply some way to get to the schema. In this current form, how would I get to the code path where the Mongo document's schema is gleaned vs being provided from somewhere else?


---

[GitHub] nifi issue #2619: NIFI-5059 Updated MongoDBLookupService to be able to detec...

Posted by MikeThomsen <gi...@git.apache.org>.
Github user MikeThomsen commented on the issue:

    https://github.com/apache/nifi/pull/2619
  
    @mattyb149 once this and the ES one are merged, it would probably be a good time to discuss extracting the schema builder code into a utility class.


---

[GitHub] nifi pull request #2619: NIFI-5059 Updated MongoDBLookupService to be able t...

Posted by MikeThomsen <gi...@git.apache.org>.
Github user MikeThomsen commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/2619#discussion_r193737082
  
    --- Diff: nifi-nar-bundles/nifi-extension-utils/nifi-record-utils/nifi-standard-record-utils/src/main/java/org/apache/nifi/schema/access/JsonSchemaAccessStrategy.java ---
    @@ -0,0 +1,36 @@
    +/*
    + * 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.nifi.schema.access;
    +
    +import org.apache.nifi.serialization.record.RecordSchema;
    +
    +import java.io.IOException;
    +import java.util.Map;
    +
    +public interface JsonSchemaAccessStrategy extends SchemaAccessStrategy {
    --- End diff --
    
    The client APIs for the third party systems usually return a Map, not a String that we can just pass on. I didn't want to serialize the client's output and then deserialize it later.


---

[GitHub] nifi pull request #2619: NIFI-5059 Updated MongoDBLookupService to be able t...

Posted by MikeThomsen <gi...@git.apache.org>.
Github user MikeThomsen commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/2619#discussion_r189434124
  
    --- Diff: nifi-nar-bundles/nifi-mongodb-bundle/nifi-mongodb-services/src/main/java/org/apache/nifi/mongodb/MongoDBLookupService.java ---
    @@ -52,68 +54,125 @@
         "The query is limited to the first result (findOne in the Mongo documentation). If no \"Lookup Value Field\" is specified " +
         "then the entire MongoDB result document minus the _id field will be returned as a record."
     )
    -public class MongoDBLookupService extends MongoDBControllerService implements LookupService<Object> {
    +public class MongoDBLookupService extends SchemaRegistryService implements LookupService<Object> {
    +    public static final PropertyDescriptor CONTROLLER_SERVICE = new PropertyDescriptor.Builder()
    +        .name("mongo-lookup-client-service")
    +        .displayName("Client Service")
    +        .description("A MongoDB controller service to use with this lookup service.")
    +        .required(true)
    +        .identifiesControllerService(MongoDBControllerService.class)
    +        .build();
     
         public static final PropertyDescriptor LOOKUP_VALUE_FIELD = new PropertyDescriptor.Builder()
    -            .name("mongo-lookup-value-field")
    -            .displayName("Lookup Value Field")
    -            .description("The field whose value will be returned when the lookup key(s) match a record. If not specified then the entire " +
    -                    "MongoDB result document minus the _id field will be returned as a record.")
    -            .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
    -            .required(false)
    -            .build();
    +        .name("mongo-lookup-value-field")
    +        .displayName("Lookup Value Field")
    +        .description("The field whose value will be returned when the lookup key(s) match a record. If not specified then the entire " +
    +                "MongoDB result document minus the _id field will be returned as a record.")
    +        .addValidator(Validator.VALID)
    +        .required(false)
    +        .build();
    +    public static final PropertyDescriptor PROJECTION = new PropertyDescriptor.Builder()
    +        .name("mongo-lookup-projection")
    +        .displayName("Projection")
    +        .description("Specifies a projection for limiting which fields will be returned.")
    +        .required(false)
    +        .build();
     
         private String lookupValueField;
     
    -    private static final List<PropertyDescriptor> lookupDescriptors;
    -
    -    static {
    -        lookupDescriptors = new ArrayList<>();
    -        lookupDescriptors.addAll(descriptors);
    -        lookupDescriptors.add(LOOKUP_VALUE_FIELD);
    -    }
    -
         @Override
         public Optional<Object> lookup(Map<String, Object> coordinates) throws LookupFailureException {
    -        Map<String, Object> clean = new HashMap<>();
    -        clean.putAll(coordinates);
    +        Map<String, Object> clean = coordinates.entrySet().stream()
    +            .filter(e -> !schemaNameProperty.equals(String.format("${%s}", e.getKey())))
    +            .collect(Collectors.toMap(
    +                e -> e.getKey(),
    +                e -> e.getValue()
    +            ));
             Document query = new Document(clean);
     
             if (coordinates.size() == 0) {
                 throw new LookupFailureException("No keys were configured. Mongo query would return random documents.");
             }
     
             try {
    -            Document result = this.findOne(query);
    +            Document result = projection != null ? controllerService.findOne(query, projection) : controllerService.findOne(query);
     
                 if(result == null) {
                     return Optional.empty();
                 } else if (!StringUtils.isEmpty(lookupValueField)) {
                     return Optional.ofNullable(result.get(lookupValueField));
                 } else {
    -                final List<RecordField> fields = new ArrayList<>();
    +                RecordSchema schema = loadSchema(coordinates);
     
    -                for (String key : result.keySet()) {
    -                    if (key.equals("_id")) {
    -                        continue;
    -                    }
    -                    fields.add(new RecordField(key, RecordFieldType.STRING.getDataType()));
    -                }
    -
    -                final RecordSchema schema = new SimpleRecordSchema(fields);
    -                return Optional.ofNullable(new MapRecord(schema, result));
    +                RecordSchema toUse = schema != null ? schema : convertSchema(result);
    +                return Optional.ofNullable(new MapRecord(toUse, result));
                 }
             } catch (Exception ex) {
                 getLogger().error("Error during lookup {}", new Object[]{ query.toJson() }, ex);
                 throw new LookupFailureException(ex);
             }
         }
     
    -    @Override
    +    private RecordSchema loadSchema(Map<String, Object> coordinates) {
    +        Map<String, String> variables = coordinates.entrySet().stream()
    +            .collect(Collectors.toMap(
    +                e -> e.getKey(),
    +                e -> e.getValue().toString()
    +            ));
    +        try {
    +            return getSchema(variables, null);
    +        } catch (Exception ex) {
    +            return null;
    +        }
    +    }
    +
    +    private RecordSchema convertSchema(Map<String, Object> result) {
    +        List<RecordField> fields = new ArrayList<>();
    +        for (Map.Entry<String, Object> entry : result.entrySet()) {
    +
    +            RecordField field;
    +            if (entry.getValue() instanceof Integer) {
    --- End diff --
    
    At some point once this and the ES Lookup Service are merged, I'll refactor this into a helper method.


---

[GitHub] nifi pull request #2619: NIFI-5059 Updated MongoDBLookupService to be able t...

Posted by MikeThomsen <gi...@git.apache.org>.
Github user MikeThomsen commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/2619#discussion_r193740195
  
    --- Diff: nifi-nar-bundles/nifi-extension-utils/nifi-record-utils/nifi-avro-record-utils/src/main/java/org/apache/nifi/serialization/JsonInferenceSchemaRegistryService.java ---
    @@ -0,0 +1,77 @@
    +/*
    + * 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.nifi.serialization;
    +
    +import org.apache.nifi.annotation.lifecycle.OnEnabled;
    +import org.apache.nifi.components.AllowableValue;
    +import org.apache.nifi.components.PropertyDescriptor;
    +import org.apache.nifi.controller.ConfigurationContext;
    +import org.apache.nifi.schema.access.JsonSchemaAccessStrategy;
    +import org.apache.nifi.schema.access.SchemaNotFoundException;
    +import org.apache.nifi.serialization.record.RecordSchema;
    +
    +import java.io.IOException;
    +import java.util.ArrayList;
    +import java.util.List;
    +import java.util.Map;
    +
    +import static org.apache.nifi.schema.access.SchemaAccessUtils.INFER_SCHEMA;
    +import static org.apache.nifi.schema.access.SchemaAccessUtils.SCHEMA_BRANCH_NAME;
    +import static org.apache.nifi.schema.access.SchemaAccessUtils.SCHEMA_NAME;
    +import static org.apache.nifi.schema.access.SchemaAccessUtils.SCHEMA_NAME_PROPERTY;
    +import static org.apache.nifi.schema.access.SchemaAccessUtils.SCHEMA_REGISTRY;
    +import static org.apache.nifi.schema.access.SchemaAccessUtils.SCHEMA_TEXT;
    +import static org.apache.nifi.schema.access.SchemaAccessUtils.SCHEMA_TEXT_PROPERTY;
    +import static org.apache.nifi.schema.access.SchemaAccessUtils.SCHEMA_VERSION;
    +
    +public class JsonInferenceSchemaRegistryService extends SchemaRegistryService {
    --- End diff --
    
    Yeah, if this gets expanded into the readers I could definitely see that being the case. For now, this is limited to being used by LookupServices that need schema access + JSON help like this one, the ES one and possibly later something like a RethinkDBLookupService.


---

[GitHub] nifi pull request #2619: NIFI-5059 Updated MongoDBLookupService to be able t...

Posted by MikeThomsen <gi...@git.apache.org>.
Github user MikeThomsen commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/2619#discussion_r199597348
  
    --- Diff: nifi-nar-bundles/nifi-extension-utils/nifi-record-utils/nifi-avro-record-utils/src/main/java/org/apache/nifi/schema/access/SchemaAccessUtils.java ---
    @@ -50,7 +50,7 @@
             "The content of the FlowFile contains a reference to a schema in the Schema Registry service. The reference is encoded as a single "
                 + "'Magic Byte' followed by 4 bytes representing the identifier of the schema, as outlined at http://docs.confluent.io/current/schema-registry/docs/serializer-formatter.html. "
                 + "This is based on version 3.2.x of the Confluent Schema Registry.");
    -
    +    public static final AllowableValue INFER_SCHEMA = new AllowableValue("infer", "Infer from JSON");
    --- End diff --
    
    Probably. I'll go ahead and make that change.


---

[GitHub] nifi pull request #2619: NIFI-5059 Updated MongoDBLookupService to be able t...

Posted by asfgit <gi...@git.apache.org>.
Github user asfgit closed the pull request at:

    https://github.com/apache/nifi/pull/2619


---

[GitHub] nifi pull request #2619: NIFI-5059 Updated MongoDBLookupService to be able t...

Posted by mattyb149 <gi...@git.apache.org>.
Github user mattyb149 commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/2619#discussion_r199517358
  
    --- Diff: nifi-nar-bundles/nifi-extension-utils/nifi-record-utils/nifi-avro-record-utils/src/main/java/org/apache/nifi/schema/access/SchemaAccessUtils.java ---
    @@ -50,7 +50,7 @@
             "The content of the FlowFile contains a reference to a schema in the Schema Registry service. The reference is encoded as a single "
                 + "'Magic Byte' followed by 4 bytes representing the identifier of the schema, as outlined at http://docs.confluent.io/current/schema-registry/docs/serializer-formatter.html. "
                 + "This is based on version 3.2.x of the Confluent Schema Registry.");
    -
    +    public static final AllowableValue INFER_SCHEMA = new AllowableValue("infer", "Infer from JSON");
    --- End diff --
    
    Shouldn't this be "Infer from Result" or something? It could be used by other processors to infer the schema from whatever object is returned.


---

[GitHub] nifi issue #2619: NIFI-5059 Updated MongoDBLookupService to be able to detec...

Posted by mattyb149 <gi...@git.apache.org>.
Github user mattyb149 commented on the issue:

    https://github.com/apache/nifi/pull/2619
  
    Reviewing...


---

[GitHub] nifi pull request #2619: NIFI-5059 Updated MongoDBLookupService to be able t...

Posted by bbende <gi...@git.apache.org>.
Github user bbende commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/2619#discussion_r193734191
  
    --- Diff: nifi-nar-bundles/nifi-extension-utils/nifi-record-utils/nifi-standard-record-utils/src/main/java/org/apache/nifi/schema/access/JsonSchemaAccessStrategy.java ---
    @@ -0,0 +1,36 @@
    +/*
    + * 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.nifi.schema.access;
    +
    +import org.apache.nifi.serialization.record.RecordSchema;
    +
    +import java.io.IOException;
    +import java.util.Map;
    +
    +public interface JsonSchemaAccessStrategy extends SchemaAccessStrategy {
    --- End diff --
    
    Can this be done without introducing a new method to the interface?
    
    The original interface has:
    `getSchema(Map<String, String> variables, InputStream contentStream, RecordSchema readSchema`
    
    Since we know the content has to be json in this case, can't we read contentStream into the Map<String,Object> in the implementation of the access strategy, rather than requiring callers to do that first?


---

[GitHub] nifi issue #2619: NIFI-5059 Updated MongoDBLookupService to be able to detec...

Posted by MikeThomsen <gi...@git.apache.org>.
Github user MikeThomsen commented on the issue:

    https://github.com/apache/nifi/pull/2619
  
    @mattyb149 @ijokarumawak @bbende I built on the schema registry service to add a new option for NoSQL options like Mongo, ES, Solr, etc. to just throw JSON in `Map` form and say "you figure it out." Please take a look at the new schema code when you get a chance.


---

[GitHub] nifi issue #2619: NIFI-5059 Updated MongoDBLookupService to be able to detec...

Posted by MikeThomsen <gi...@git.apache.org>.
Github user MikeThomsen commented on the issue:

    https://github.com/apache/nifi/pull/2619
  
    @mattyb149 updated, but looks like Travis is busted at the moment (saying it can't find our repo)


---

[GitHub] nifi pull request #2619: NIFI-5059 Updated MongoDBLookupService to be able t...

Posted by mattyb149 <gi...@git.apache.org>.
Github user mattyb149 commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/2619#discussion_r192783725
  
    --- Diff: nifi-nar-bundles/nifi-mongodb-bundle/nifi-mongodb-services/src/main/java/org/apache/nifi/mongodb/MongoDBLookupService.java ---
    @@ -52,68 +54,125 @@
         "The query is limited to the first result (findOne in the Mongo documentation). If no \"Lookup Value Field\" is specified " +
         "then the entire MongoDB result document minus the _id field will be returned as a record."
     )
    -public class MongoDBLookupService extends MongoDBControllerService implements LookupService<Object> {
    +public class MongoDBLookupService extends SchemaRegistryService implements LookupService<Object> {
    +    public static final PropertyDescriptor CONTROLLER_SERVICE = new PropertyDescriptor.Builder()
    +        .name("mongo-lookup-client-service")
    +        .displayName("Client Service")
    +        .description("A MongoDB controller service to use with this lookup service.")
    +        .required(true)
    +        .identifiesControllerService(MongoDBControllerService.class)
    --- End diff --
    
    I believe this is supposed to be an interface not the impl class (see my other comment below), so I think you want `MongoDBClientService` here.


---

[GitHub] nifi issue #2619: NIFI-5059 Updated MongoDBLookupService to be able to detec...

Posted by zenfenan <gi...@git.apache.org>.
Github user zenfenan commented on the issue:

    https://github.com/apache/nifi/pull/2619
  
    @MikeThomsen I'm actually traveling with limited access to mails and internet. I'll try to take a look as soon as I can, if someone doesn't get to already.


---

[GitHub] nifi issue #2619: NIFI-5059 Updated MongoDBLookupService to be able to detec...

Posted by mattyb149 <gi...@git.apache.org>.
Github user mattyb149 commented on the issue:

    https://github.com/apache/nifi/pull/2619
  
    Reviewing...


---

[GitHub] nifi issue #2619: NIFI-5059 Updated MongoDBLookupService to be able to detec...

Posted by MikeThomsen <gi...@git.apache.org>.
Github user MikeThomsen commented on the issue:

    https://github.com/apache/nifi/pull/2619
  
    @mattyb149 can we close the loop on this?


---

[GitHub] nifi issue #2619: NIFI-5059 Updated MongoDBLookupService to be able to detec...

Posted by MikeThomsen <gi...@git.apache.org>.
Github user MikeThomsen commented on the issue:

    https://github.com/apache/nifi/pull/2619
  
    @mattyb149 Had to change the schema handling in MongoDBLookupService. Can you take a look?


---

[GitHub] nifi pull request #2619: NIFI-5059 Updated MongoDBLookupService to be able t...

Posted by MikeThomsen <gi...@git.apache.org>.
Github user MikeThomsen commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/2619#discussion_r199602436
  
    --- Diff: nifi-nar-bundles/nifi-mongodb-bundle/nifi-mongodb-services/src/test/java/org/apache/nifi/mongodb/TestSchemaRegistry.java ---
    @@ -0,0 +1,49 @@
    +/*
    + * 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.nifi.mongodb;
    +
    +import org.apache.nifi.controller.AbstractControllerService;
    +import org.apache.nifi.schema.access.SchemaField;
    +import org.apache.nifi.schemaregistry.services.SchemaRegistry;
    +import org.apache.nifi.serialization.SimpleRecordSchema;
    +import org.apache.nifi.serialization.record.RecordField;
    +import org.apache.nifi.serialization.record.RecordFieldType;
    +import org.apache.nifi.serialization.record.RecordSchema;
    +import org.apache.nifi.serialization.record.SchemaIdentifier;
    +
    +import java.util.ArrayList;
    +import java.util.HashSet;
    +import java.util.List;
    +import java.util.Set;
    +
    +public class TestSchemaRegistry extends AbstractControllerService implements SchemaRegistry {
    --- End diff --
    
    Yeah. Changed it to StubSchemaRegistry.


---

[GitHub] nifi pull request #2619: NIFI-5059 Updated MongoDBLookupService to be able t...

Posted by MikeThomsen <gi...@git.apache.org>.
Github user MikeThomsen commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/2619#discussion_r192802039
  
    --- Diff: nifi-nar-bundles/nifi-mongodb-bundle/nifi-mongodb-services/src/main/java/org/apache/nifi/mongodb/MongoDBLookupService.java ---
    @@ -52,68 +54,125 @@
         "The query is limited to the first result (findOne in the Mongo documentation). If no \"Lookup Value Field\" is specified " +
         "then the entire MongoDB result document minus the _id field will be returned as a record."
     )
    -public class MongoDBLookupService extends MongoDBControllerService implements LookupService<Object> {
    +public class MongoDBLookupService extends SchemaRegistryService implements LookupService<Object> {
    +    public static final PropertyDescriptor CONTROLLER_SERVICE = new PropertyDescriptor.Builder()
    --- End diff --
    
    I added to the property list.


---

[GitHub] nifi pull request #2619: NIFI-5059 Updated MongoDBLookupService to be able t...

Posted by MikeThomsen <gi...@git.apache.org>.
Github user MikeThomsen commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/2619#discussion_r192802068
  
    --- Diff: nifi-nar-bundles/nifi-mongodb-bundle/nifi-mongodb-services/src/main/java/org/apache/nifi/mongodb/MongoDBLookupService.java ---
    @@ -52,68 +54,125 @@
         "The query is limited to the first result (findOne in the Mongo documentation). If no \"Lookup Value Field\" is specified " +
         "then the entire MongoDB result document minus the _id field will be returned as a record."
     )
    -public class MongoDBLookupService extends MongoDBControllerService implements LookupService<Object> {
    +public class MongoDBLookupService extends SchemaRegistryService implements LookupService<Object> {
    +    public static final PropertyDescriptor CONTROLLER_SERVICE = new PropertyDescriptor.Builder()
    +        .name("mongo-lookup-client-service")
    +        .displayName("Client Service")
    +        .description("A MongoDB controller service to use with this lookup service.")
    +        .required(true)
    +        .identifiesControllerService(MongoDBControllerService.class)
    +        .build();
     
         public static final PropertyDescriptor LOOKUP_VALUE_FIELD = new PropertyDescriptor.Builder()
    -            .name("mongo-lookup-value-field")
    -            .displayName("Lookup Value Field")
    -            .description("The field whose value will be returned when the lookup key(s) match a record. If not specified then the entire " +
    -                    "MongoDB result document minus the _id field will be returned as a record.")
    -            .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
    -            .required(false)
    -            .build();
    +        .name("mongo-lookup-value-field")
    +        .displayName("Lookup Value Field")
    +        .description("The field whose value will be returned when the lookup key(s) match a record. If not specified then the entire " +
    +                "MongoDB result document minus the _id field will be returned as a record.")
    +        .addValidator(Validator.VALID)
    +        .required(false)
    +        .build();
    +    public static final PropertyDescriptor PROJECTION = new PropertyDescriptor.Builder()
    +        .name("mongo-lookup-projection")
    +        .displayName("Projection")
    +        .description("Specifies a projection for limiting which fields will be returned.")
    +        .required(false)
    +        .build();
     
         private String lookupValueField;
     
    -    private static final List<PropertyDescriptor> lookupDescriptors;
    -
    -    static {
    -        lookupDescriptors = new ArrayList<>();
    -        lookupDescriptors.addAll(descriptors);
    -        lookupDescriptors.add(LOOKUP_VALUE_FIELD);
    -    }
    -
         @Override
         public Optional<Object> lookup(Map<String, Object> coordinates) throws LookupFailureException {
    -        Map<String, Object> clean = new HashMap<>();
    -        clean.putAll(coordinates);
    +        Map<String, Object> clean = coordinates.entrySet().stream()
    +            .filter(e -> !schemaNameProperty.equals(String.format("${%s}", e.getKey())))
    +            .collect(Collectors.toMap(
    +                e -> e.getKey(),
    +                e -> e.getValue()
    +            ));
             Document query = new Document(clean);
     
             if (coordinates.size() == 0) {
                 throw new LookupFailureException("No keys were configured. Mongo query would return random documents.");
             }
     
             try {
    -            Document result = this.findOne(query);
    +            Document result = projection != null ? controllerService.findOne(query, projection) : controllerService.findOne(query);
     
                 if(result == null) {
                     return Optional.empty();
                 } else if (!StringUtils.isEmpty(lookupValueField)) {
                     return Optional.ofNullable(result.get(lookupValueField));
                 } else {
    -                final List<RecordField> fields = new ArrayList<>();
    +                RecordSchema schema = loadSchema(coordinates);
     
    -                for (String key : result.keySet()) {
    -                    if (key.equals("_id")) {
    -                        continue;
    -                    }
    -                    fields.add(new RecordField(key, RecordFieldType.STRING.getDataType()));
    -                }
    -
    -                final RecordSchema schema = new SimpleRecordSchema(fields);
    -                return Optional.ofNullable(new MapRecord(schema, result));
    +                RecordSchema toUse = schema != null ? schema : convertSchema(result);
    +                return Optional.ofNullable(new MapRecord(toUse, result));
                 }
             } catch (Exception ex) {
                 getLogger().error("Error during lookup {}", new Object[]{ query.toJson() }, ex);
                 throw new LookupFailureException(ex);
             }
         }
     
    -    @Override
    +    private RecordSchema loadSchema(Map<String, Object> coordinates) {
    +        Map<String, String> variables = coordinates.entrySet().stream()
    +            .collect(Collectors.toMap(
    +                e -> e.getKey(),
    +                e -> e.getValue().toString()
    +            ));
    +        try {
    +            return getSchema(variables, null);
    +        } catch (Exception ex) {
    +            return null;
    +        }
    +    }
    +
    +    private RecordSchema convertSchema(Map<String, Object> result) {
    +        List<RecordField> fields = new ArrayList<>();
    +        for (Map.Entry<String, Object> entry : result.entrySet()) {
    +
    +            RecordField field;
    +            if (entry.getValue() instanceof Integer) {
    +                field = new RecordField(entry.getKey(), RecordFieldType.INT.getDataType());
    +            } else if (entry.getValue() instanceof Long) {
    +                field = new RecordField(entry.getKey(), RecordFieldType.LONG.getDataType());
    +            } else if (entry.getValue() instanceof Boolean) {
    +                field = new RecordField(entry.getKey(), RecordFieldType.BOOLEAN.getDataType());
    +            } else if (entry.getValue() instanceof Double) {
    +                field = new RecordField(entry.getKey(), RecordFieldType.DOUBLE.getDataType());
    +            } else if (entry.getValue() instanceof Date) {
    +                field = new RecordField(entry.getKey(), RecordFieldType.DATE.getDataType());
    +            } else if (entry.getValue() instanceof List) {
    +                field = new RecordField(entry.getKey(), RecordFieldType.ARRAY.getDataType());
    +            } else if (entry.getValue() instanceof Map) {
    +                RecordSchema nestedSchema = convertSchema((Map)entry.getValue());
    +                RecordDataType rdt = new RecordDataType(nestedSchema);
    +                field = new RecordField(entry.getKey(), rdt);
    +            } else {
    +                field = new RecordField(entry.getKey(), RecordFieldType.STRING.getDataType());
    +            }
    +            fields.add(field);
    +        }
    +
    +        return new SimpleRecordSchema(fields);
    +    }
    +
    +    private volatile Document projection;
    +    private MongoDBControllerService controllerService;
    +    private String schemaNameProperty;
    +
         @OnEnabled
    -    public void onEnabled(final ConfigurationContext context) throws InitializationException, IOException, InterruptedException {
    +    public void onEnabled(final ConfigurationContext context) {
             this.lookupValueField = context.getProperty(LOOKUP_VALUE_FIELD).getValue();
    -        super.onEnabled(context);
    +        this.controllerService = context.getProperty(CONTROLLER_SERVICE).asControllerService(MongoDBControllerService.class);
    --- End diff --
    
    Fixed.


---

[GitHub] nifi issue #2619: NIFI-5059 Updated MongoDBLookupService to be able to detec...

Posted by bbende <gi...@git.apache.org>.
Github user bbende commented on the issue:

    https://github.com/apache/nifi/pull/2619
  
    I haven't gone too deep looking at this, but if the goal is to have a re-usable way to infer a schema from JSON across various NoSQL components, have we considered just putting some utility code in a JAR somewhere under nifi-nar-bundles/nifi-extension-utils rather than trying to hook into the SchemaAccessStrategy/SchemaRegistryService?
    
    I'm just on the fence about whether the schema access stuff makes sense here since that was designed for the readers/writers, and this is really coming from a different angle of already having some Map object in memory.
    



---

[GitHub] nifi pull request #2619: NIFI-5059 Updated MongoDBLookupService to be able t...

Posted by MikeThomsen <gi...@git.apache.org>.
Github user MikeThomsen commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/2619#discussion_r193381877
  
    --- Diff: nifi-nar-bundles/nifi-mongodb-bundle/nifi-mongodb-services/src/main/java/org/apache/nifi/mongodb/MongoDBLookupService.java ---
    @@ -52,68 +54,125 @@
         "The query is limited to the first result (findOne in the Mongo documentation). If no \"Lookup Value Field\" is specified " +
         "then the entire MongoDB result document minus the _id field will be returned as a record."
     )
    -public class MongoDBLookupService extends MongoDBControllerService implements LookupService<Object> {
    +public class MongoDBLookupService extends SchemaRegistryService implements LookupService<Object> {
    +    public static final PropertyDescriptor CONTROLLER_SERVICE = new PropertyDescriptor.Builder()
    +        .name("mongo-lookup-client-service")
    +        .displayName("Client Service")
    +        .description("A MongoDB controller service to use with this lookup service.")
    +        .required(true)
    +        .identifiesControllerService(MongoDBClientService.class)
    +        .build();
     
         public static final PropertyDescriptor LOOKUP_VALUE_FIELD = new PropertyDescriptor.Builder()
    -            .name("mongo-lookup-value-field")
    -            .displayName("Lookup Value Field")
    -            .description("The field whose value will be returned when the lookup key(s) match a record. If not specified then the entire " +
    -                    "MongoDB result document minus the _id field will be returned as a record.")
    -            .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
    -            .required(false)
    -            .build();
    +        .name("mongo-lookup-value-field")
    +        .displayName("Lookup Value Field")
    +        .description("The field whose value will be returned when the lookup key(s) match a record. If not specified then the entire " +
    +                "MongoDB result document minus the _id field will be returned as a record.")
    +        .addValidator(Validator.VALID)
    +        .required(false)
    +        .build();
    +    public static final PropertyDescriptor PROJECTION = new PropertyDescriptor.Builder()
    +        .name("mongo-lookup-projection")
    +        .displayName("Projection")
    +        .description("Specifies a projection for limiting which fields will be returned.")
    +        .required(false)
    +        .build();
     
         private String lookupValueField;
     
    -    private static final List<PropertyDescriptor> lookupDescriptors;
    -
    -    static {
    -        lookupDescriptors = new ArrayList<>();
    -        lookupDescriptors.addAll(descriptors);
    -        lookupDescriptors.add(LOOKUP_VALUE_FIELD);
    -    }
    -
         @Override
         public Optional<Object> lookup(Map<String, Object> coordinates) throws LookupFailureException {
    -        Map<String, Object> clean = new HashMap<>();
    -        clean.putAll(coordinates);
    +        Map<String, Object> clean = coordinates.entrySet().stream()
    +            .filter(e -> !schemaNameProperty.equals(String.format("${%s}", e.getKey())))
    +            .collect(Collectors.toMap(
    +                e -> e.getKey(),
    +                e -> e.getValue()
    +            ));
             Document query = new Document(clean);
     
             if (coordinates.size() == 0) {
                 throw new LookupFailureException("No keys were configured. Mongo query would return random documents.");
             }
     
             try {
    -            Document result = this.findOne(query);
    +            Document result = projection != null ? controllerService.findOne(query, projection) : controllerService.findOne(query);
     
                 if(result == null) {
                     return Optional.empty();
                 } else if (!StringUtils.isEmpty(lookupValueField)) {
                     return Optional.ofNullable(result.get(lookupValueField));
                 } else {
    -                final List<RecordField> fields = new ArrayList<>();
    +                RecordSchema schema = loadSchema(coordinates);
     
    -                for (String key : result.keySet()) {
    -                    if (key.equals("_id")) {
    -                        continue;
    -                    }
    -                    fields.add(new RecordField(key, RecordFieldType.STRING.getDataType()));
    -                }
    -
    -                final RecordSchema schema = new SimpleRecordSchema(fields);
    -                return Optional.ofNullable(new MapRecord(schema, result));
    +                RecordSchema toUse = schema != null ? schema : convertSchema(result);
    --- End diff --
    
    @mattyb149 I think the answer to your last question is here. If you specify `schema.name` in the coordinates, it'll get that from `loadSchema`. If not, it calls `convertSchema`. The rest of the lookup strategies don't make much sense in this case so I can back out the change to extended `SchemaRegistryService` if that makes sense.


---

[GitHub] nifi issue #2619: NIFI-5059 Updated MongoDBLookupService to be able to detec...

Posted by MikeThomsen <gi...@git.apache.org>.
Github user MikeThomsen commented on the issue:

    https://github.com/apache/nifi/pull/2619
  
    @mattyb149 made the changes you requested.


---

[GitHub] nifi issue #2619: NIFI-5059 Updated MongoDBLookupService to be able to detec...

Posted by MikeThomsen <gi...@git.apache.org>.
Github user MikeThomsen commented on the issue:

    https://github.com/apache/nifi/pull/2619
  
    @pvillard31 @mattyb149 I updated this to have a clean separation between the controller and lookup service code and subclassed the lookup service from SchemaRegistryService. Can one of you do a review sometime soon?


---

[GitHub] nifi pull request #2619: NIFI-5059 Updated MongoDBLookupService to be able t...

Posted by MikeThomsen <gi...@git.apache.org>.
Github user MikeThomsen commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/2619#discussion_r193480390
  
    --- Diff: nifi-nar-bundles/nifi-mongodb-bundle/nifi-mongodb-services/src/main/java/org/apache/nifi/mongodb/MongoDBLookupService.java ---
    @@ -52,68 +54,125 @@
         "The query is limited to the first result (findOne in the Mongo documentation). If no \"Lookup Value Field\" is specified " +
         "then the entire MongoDB result document minus the _id field will be returned as a record."
     )
    -public class MongoDBLookupService extends MongoDBControllerService implements LookupService<Object> {
    +public class MongoDBLookupService extends SchemaRegistryService implements LookupService<Object> {
    +    public static final PropertyDescriptor CONTROLLER_SERVICE = new PropertyDescriptor.Builder()
    +        .name("mongo-lookup-client-service")
    +        .displayName("Client Service")
    +        .description("A MongoDB controller service to use with this lookup service.")
    +        .required(true)
    +        .identifiesControllerService(MongoDBClientService.class)
    +        .build();
     
         public static final PropertyDescriptor LOOKUP_VALUE_FIELD = new PropertyDescriptor.Builder()
    -            .name("mongo-lookup-value-field")
    -            .displayName("Lookup Value Field")
    -            .description("The field whose value will be returned when the lookup key(s) match a record. If not specified then the entire " +
    -                    "MongoDB result document minus the _id field will be returned as a record.")
    -            .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
    -            .required(false)
    -            .build();
    +        .name("mongo-lookup-value-field")
    +        .displayName("Lookup Value Field")
    +        .description("The field whose value will be returned when the lookup key(s) match a record. If not specified then the entire " +
    +                "MongoDB result document minus the _id field will be returned as a record.")
    +        .addValidator(Validator.VALID)
    +        .required(false)
    +        .build();
    +    public static final PropertyDescriptor PROJECTION = new PropertyDescriptor.Builder()
    +        .name("mongo-lookup-projection")
    +        .displayName("Projection")
    +        .description("Specifies a projection for limiting which fields will be returned.")
    +        .required(false)
    +        .build();
     
         private String lookupValueField;
     
    -    private static final List<PropertyDescriptor> lookupDescriptors;
    -
    -    static {
    -        lookupDescriptors = new ArrayList<>();
    -        lookupDescriptors.addAll(descriptors);
    -        lookupDescriptors.add(LOOKUP_VALUE_FIELD);
    -    }
    -
         @Override
         public Optional<Object> lookup(Map<String, Object> coordinates) throws LookupFailureException {
    -        Map<String, Object> clean = new HashMap<>();
    -        clean.putAll(coordinates);
    +        Map<String, Object> clean = coordinates.entrySet().stream()
    +            .filter(e -> !schemaNameProperty.equals(String.format("${%s}", e.getKey())))
    +            .collect(Collectors.toMap(
    +                e -> e.getKey(),
    +                e -> e.getValue()
    +            ));
             Document query = new Document(clean);
     
             if (coordinates.size() == 0) {
                 throw new LookupFailureException("No keys were configured. Mongo query would return random documents.");
             }
     
             try {
    -            Document result = this.findOne(query);
    +            Document result = projection != null ? controllerService.findOne(query, projection) : controllerService.findOne(query);
     
                 if(result == null) {
                     return Optional.empty();
                 } else if (!StringUtils.isEmpty(lookupValueField)) {
                     return Optional.ofNullable(result.get(lookupValueField));
                 } else {
    -                final List<RecordField> fields = new ArrayList<>();
    +                RecordSchema schema = loadSchema(coordinates);
     
    -                for (String key : result.keySet()) {
    -                    if (key.equals("_id")) {
    -                        continue;
    -                    }
    -                    fields.add(new RecordField(key, RecordFieldType.STRING.getDataType()));
    -                }
    -
    -                final RecordSchema schema = new SimpleRecordSchema(fields);
    -                return Optional.ofNullable(new MapRecord(schema, result));
    +                RecordSchema toUse = schema != null ? schema : convertSchema(result);
    --- End diff --
    
    That's probably the right way to do it because we should have it blow up if it can't get the schema on the first pass instead of silently falling back onto the inference option. Once I get that worked out, I'll copy pasta it over the ES one as well.


---

[GitHub] nifi pull request #2619: NIFI-5059 Updated MongoDBLookupService to be able t...

Posted by mattyb149 <gi...@git.apache.org>.
Github user mattyb149 commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/2619#discussion_r192783531
  
    --- Diff: nifi-nar-bundles/nifi-mongodb-bundle/nifi-mongodb-services/src/main/java/org/apache/nifi/mongodb/MongoDBLookupService.java ---
    @@ -52,68 +54,125 @@
         "The query is limited to the first result (findOne in the Mongo documentation). If no \"Lookup Value Field\" is specified " +
         "then the entire MongoDB result document minus the _id field will be returned as a record."
     )
    -public class MongoDBLookupService extends MongoDBControllerService implements LookupService<Object> {
    +public class MongoDBLookupService extends SchemaRegistryService implements LookupService<Object> {
    +    public static final PropertyDescriptor CONTROLLER_SERVICE = new PropertyDescriptor.Builder()
    --- End diff --
    
    AFAICT this property is never added to the list of supported property descriptors, so I couldn't set it on the UI which causes an NPE when lookup() is called. Seems odd that for a required property that is not supported, setting it (in tests) would not complain. I haven't run the integration tests yet, just put the NARs into a live NiFi to try it out. 


---

[GitHub] nifi issue #2619: NIFI-5059 Updated MongoDBLookupService to be able to detec...

Posted by MikeThomsen <gi...@git.apache.org>.
Github user MikeThomsen commented on the issue:

    https://github.com/apache/nifi/pull/2619
  
    @zenfenan can you review? I think we're almost at close out point.


---