metron-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cestella <...@git.apache.org>
Subject [GitHub] incubator-metron pull request #445: METRON-706: Add Stellar transformations ...
Date Wed, 08 Feb 2017 14:26:40 GMT
Github user cestella commented on a diff in the pull request:

    https://github.com/apache/incubator-metron/pull/445#discussion_r100074335
  
    --- Diff: metron-platform/metron-data-management/src/main/java/org/apache/metron/dataloads/extractor/TransformFilterExtractorDecorator.java
---
    @@ -0,0 +1,236 @@
    +/**
    + * 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.dataloads.extractor;
    +
    +import com.fasterxml.jackson.core.type.TypeReference;
    +import org.apache.commons.lang.StringUtils;
    +import org.apache.curator.framework.CuratorFramework;
    +import org.apache.log4j.Logger;
    +import org.apache.metron.common.configuration.ConfigurationsUtils;
    +import org.apache.metron.common.dsl.Context;
    +import org.apache.metron.common.dsl.MapVariableResolver;
    +import org.apache.metron.common.dsl.StellarFunctions;
    +import org.apache.metron.common.stellar.StellarPredicateProcessor;
    +import org.apache.metron.common.stellar.StellarProcessor;
    +import org.apache.metron.common.utils.ConversionUtils;
    +import org.apache.metron.common.utils.JSONUtils;
    +import org.apache.metron.enrichment.lookup.LookupKV;
    +
    +import java.io.ByteArrayInputStream;
    +import java.io.IOException;
    +import java.util.*;
    +
    +import static org.apache.metron.dataloads.extractor.TransformFilterExtractorDecorator.ExtractorOptions.*;
    +
    +public class TransformFilterExtractorDecorator extends ExtractorDecorator {
    +  private static final Logger LOG = Logger.getLogger(TransformFilterExtractorDecorator.class);
    +
    +  protected enum ExtractorOptions {
    +    VALUE_TRANSFORM("value_transform"),
    +    VALUE_FILTER("value_filter"),
    +    INDICATOR_TRANSFORM("indicator_transform"),
    +    INDICATOR_FILTER("indicator_filter"),
    +    ZK_QUORUM("zk_quorum"),
    +    INDICATOR("indicator");
    +
    +    private String key;
    +
    +    ExtractorOptions(String key) {
    +      this.key = key;
    +    }
    +
    +    @Override
    +    public String toString() {
    +      return key;
    +    }
    +
    +    public boolean existsIn(Map<String, Object> config) {
    +      return config.containsKey(key);
    +    }
    +  }
    +
    +  private Optional<CuratorFramework> zkClient;
    +  private Map<String, String> valueTransforms;
    +  private Map<String, String> indicatorTransforms;
    +  private String valueFilter;
    +  private String indicatorFilter;
    +  private Context stellarContext;
    +  private StellarProcessor transformProcessor;
    +  private StellarPredicateProcessor filterProcessor;
    +  private Map<String, Object> globalConfig;
    +
    +  public TransformFilterExtractorDecorator(Extractor decoratedExtractor) {
    +    super(decoratedExtractor);
    +    this.zkClient = Optional.empty();
    +    this.valueTransforms = new LinkedHashMap<>();
    +    this.indicatorTransforms = new LinkedHashMap<>();
    +    this.valueFilter = "";
    +    this.indicatorFilter = "";
    +    this.transformProcessor = new StellarProcessor();
    +    this.filterProcessor = new StellarPredicateProcessor();
    +  }
    +
    +  @Override
    +  public void initialize(Map<String, Object> config) {
    +    super.initialize(config);
    +    if (VALUE_TRANSFORM.existsIn(config)) {
    +      this.valueTransforms = getTransforms(config, VALUE_TRANSFORM.toString());
    +    }
    +    if (INDICATOR_TRANSFORM.existsIn(config)) {
    +      this.indicatorTransforms = getTransforms(config, INDICATOR_TRANSFORM.toString());
    +    }
    +    if (VALUE_FILTER.existsIn(config)) {
    +      this.valueFilter = getFilter(config, VALUE_FILTER.toString());
    +    }
    +    if (INDICATOR_FILTER.existsIn(config)) {
    +      this.indicatorFilter = getFilter(config, INDICATOR_FILTER.toString());
    +    }
    +    String zkClientUrl = "";
    +    if (ZK_QUORUM.existsIn(config)) {
    +      zkClientUrl = ConversionUtils.convert(config.get(ZK_QUORUM.toString()), String.class);
    +    }
    +    zkClient = setupClient(zkClient, zkClientUrl);
    +    this.globalConfig = getGlobalConfig(zkClient);
    +    this.stellarContext = createContext(zkClient);
    +    StellarFunctions.initialize(stellarContext);
    +    this.transformProcessor = new StellarProcessor();
    +    this.filterProcessor = new StellarPredicateProcessor();
    +  }
    +
    +  private String getFilter(Map<String, Object> config, String valueFilter) {
    +    return ConversionUtils.convertOrFail(config.get(valueFilter), String.class);
    +  }
    +
    +  /**
    +   * Get a map of the transformations from the config of the specified type
    +   * @param config main config map
    +   * @param type the transformation type to get from config
    +   * @return map of transformations.
    +   */
    +  private Map<String, String> getTransforms(Map<String, Object> config, String
type) {
    +    Map<Object, Object> transformsConfig = ConversionUtils.convertOrFail(config.get(type),
Map.class);
    +    Map<String, String> transforms = new LinkedHashMap<>();
    +    for (Map.Entry<Object, Object> e : transformsConfig.entrySet()) {
    +      String key = ConversionUtils.convertOrFail(e.getKey(), String.class);
    +      String val = ConversionUtils.convertOrFail(e.getValue(), String.class);
    +      transforms.put(key, val);
    +    }
    +    return transforms;
    +  }
    +
    +  /**
    +   * Creates a Zookeeper client if it doesn't exist and a url for zk is provided.
    +   * @param zookeeperUrl The Zookeeper URL.
    +   */
    +  private Optional<CuratorFramework> setupClient(Optional<CuratorFramework>
zkClient, String zookeeperUrl) {
    +    // can only create client if we have a valid zookeeper URL
    +    if (!zkClient.isPresent()) {
    +      if (StringUtils.isNotBlank(zookeeperUrl)) {
    +        CuratorFramework client = ConfigurationsUtils.getClient(zookeeperUrl);
    +        client.start();
    +        return Optional.of(client);
    +      } else {
    +        LOG.warn("Unable to setup zookeeper client - zk_quorum url not provided. **This
will limit some Stellar functionality**");
    +        return Optional.empty();
    +      }
    +    } else {
    +      return zkClient;
    +    }
    +  }
    +
    +  private Map<String, Object> getGlobalConfig(Optional<CuratorFramework>
zkClient) {
    +    if (zkClient.isPresent()) {
    +      try {
    +        return JSONUtils.INSTANCE.load(
    +                new ByteArrayInputStream(ConfigurationsUtils.readGlobalConfigBytesFromZookeeper(zkClient.get())),
    +                new TypeReference<Map<String, Object>>() {
    +                });
    +      } catch (Exception e) {
    +        LOG.warn("Exception thrown while attempting to get global config from Zookeeper.",
e);
    +      }
    +    }
    +    return new LinkedHashMap<>();
    +  }
    +
    +  private Context createContext(Optional<CuratorFramework> zkClient) {
    +    Context.Builder builder = new Context.Builder();
    +    if (zkClient.isPresent()) {
    +      builder.with(Context.Capabilities.ZOOKEEPER_CLIENT, zkClient::get)
    +              .with(Context.Capabilities.GLOBAL_CONFIG, () -> globalConfig);
    --- End diff --
    
    You might want an empty global config even if the `zkClient` isn't present.  Not sure,
just a thought.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message