merrimanr commented on a change in pull request #1399: METRON-2073: Create 
in-memory use case for enrichment with map type and flatfile summarizer
URL: https://github.com/apache/metron/pull/1399#discussion_r287054079
 
 

 ##########
 File path: 
metron-platform/metron-enrichment/metron-enrichment-common/src/main/java/org/apache/metron/enrichment/stellar/EnrichmentObjectGet.java
 ##########
 @@ -0,0 +1,114 @@
+/**
+ * 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.metron.enrichment.stellar;
+
+import org.apache.metron.enrichment.cache.ObjectCache;
+import org.apache.metron.enrichment.cache.ObjectCacheConfig;
+import org.apache.metron.stellar.common.utils.ConversionUtils;
+import org.apache.metron.stellar.dsl.Context;
+import org.apache.metron.stellar.dsl.ParseException;
+import org.apache.metron.stellar.dsl.Stellar;
+import org.apache.metron.stellar.dsl.StellarFunction;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.lang.invoke.MethodHandles;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+
+import static 
org.apache.metron.enrichment.cache.ObjectCacheConfig.OBJECT_CACHE_EXPIRATION_KEY;
+import static 
org.apache.metron.enrichment.cache.ObjectCacheConfig.OBJECT_CACHE_MAX_FILE_SIZE_KEY;
+import static 
org.apache.metron.enrichment.cache.ObjectCacheConfig.OBJECT_CACHE_SIZE_KEY;
+import static 
org.apache.metron.enrichment.cache.ObjectCacheConfig.OBJECT_CACHE_TIME_UNIT_KEY;
+
+@Stellar(namespace="ENRICHMENT"
+        ,name="OBJECT_GET"
+        ,description="Retrieve and deserialize a serialized object from HDFS 
and stores it in the ObjectCache,  " +
+        "then returns the value associated with the indicator."
+        , params = {
+            "path - The path in HDFS to the serialized object" +
+            "indicator - The string indicator to look up"
+          }
+        , returns="Value associated with the indicator."
+)
+public class EnrichmentObjectGet implements StellarFunction {
+  private static final Logger LOG = 
LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
+  public final static String ENRICHMENT_OBJECT_GET_SETTINGS = 
"enrichment.object.get.settings";
+
+  private ObjectCache objectCache;
+
+  @Override
+  public Object apply(List<Object> args, Context context) throws 
ParseException {
+    if(args.size() != 2) {
+      throw new IllegalArgumentException("All parameters are mandatory, submit 
'hdfs path', 'indicator'");
+    }
+    if(!isInitialized()) {
+      return null;
+    }
+
+    String path = (String) args.get(0);
+    String indicator = (String) args.get(1);
+    if(path == null || indicator == null) {
+      return null;
+    }
+
+    Object value;
+    try {
+      Map cachedMap = (Map) objectCache.get(path);
+      LOG.debug("Looking up value from object at path '{}' using indicator 
{}", path, indicator);
+      value = cachedMap.get(indicator);
+    } catch(ClassCastException e) {
+      throw new ClassCastException(String.format("The object stored in HDFS at 
'%s' must be serialized in JSON format.", path));
+    }
+
+    return value;
+  }
+
+  @SuppressWarnings("unchecked")
+  @Override
+  public void initialize(Context context) {
+    Map<String, Object> config = (Map<String, Object>) 
context.getCapability(Context.Capabilities.GLOBAL_CONFIG, false)
+            .orElse(new HashMap<>());
+    ObjectCacheConfig objectCacheConfig = 
ObjectCacheConfig.fromGlobalConfig(config);
 
 Review comment:
   Here is how the process around cache settings works:
   
   1. Create a new `ObjectCacheConfig` object.  This config will be a mix of 
defaults and top-level object get global config settings, depending on what is 
set in the global config.
   2. Retrieve the nested enrichment object get settings using the 
`enrichment.object.get.settings` key.
   3. For each setting, using the setting in the nested object from the 
previous step if it is defined.  Otherwise fall back to the setting from step 1.
   
   Does that make sense?

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


With regards,
Apache Git Services

Reply via email to