You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@metron.apache.org by cestella <gi...@git.apache.org> on 2016/09/27 01:21:01 UTC

[GitHub] incubator-metron pull request #277: METRON-455: Create stellar management fu...

GitHub user cestella opened a pull request:

    https://github.com/apache/incubator-metron/pull/277

    METRON-455: Create stellar management functions to put, get and list data in HDFS

    Create functions around reading, writing and removing local and HDFS files.
    
    Functions added:
    * Interacting with HDFS
      * `HDFS_LS`
      * `HDFS_RM`
      * `HDFS_GET`
      * `HDFS_PUT`
    * Interacting with the local filesystem
      * `FILE_LS`
      * `FILE_RM`
      * `FILE_GET`
      * `FILE_PUT`
    
    You can test these by deploying the management jar as per the instructions in `metron-management` and try them out in the Stellar REPL.

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

    $ git pull https://github.com/cestella/incubator-metron METRON-455

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

    https://github.com/apache/incubator-metron/pull/277.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 #277
    
----
commit e4cd0620c64bc67bde027caa224b99dbaf454636
Author: cstella <ce...@gmail.com>
Date:   2016-09-26T22:01:01Z

    Initial implementation

commit 58fda38fa5616e876254f5f535c86518bc805b48
Author: cstella <ce...@gmail.com>
Date:   2016-09-26T22:01:27Z

    Merge branch 'master' into METRON-455

commit 57897decf4a8eec03d6e32ec7fcaab54db8d8563
Author: cstella <ce...@gmail.com>
Date:   2016-09-27T01:15:52Z

    Added unit/integration tests.

----


---
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.
---

[GitHub] incubator-metron pull request #277: METRON-455: Create stellar management fu...

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

    https://github.com/apache/incubator-metron/pull/277#discussion_r80933040
  
    --- Diff: metron-platform/metron-management/src/main/java/org/apache/metron/management/FileSystemFunctions.java ---
    @@ -0,0 +1,402 @@
    +/**
    + * 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.management;
    +
    +import com.google.common.base.Splitter;
    +import com.google.common.collect.Iterables;
    +import com.jakewharton.fliptables.FlipTable;
    +import org.apache.commons.io.IOUtils;
    +import org.apache.hadoop.conf.Configuration;
    +import org.apache.hadoop.fs.*;
    +import org.apache.log4j.Logger;
    +import org.apache.metron.common.dsl.Context;
    +import org.apache.metron.common.dsl.ParseException;
    +import org.apache.metron.common.dsl.Stellar;
    +import org.apache.metron.common.dsl.StellarFunction;
    +import org.apache.metron.common.utils.ConversionUtils;
    +
    +import java.io.IOException;
    +import java.net.URI;
    +import java.text.DateFormat;
    +import java.util.*;
    +import java.util.function.Function;
    +
    +public class FileSystemFunctions {
    +  private static final Logger LOG = Logger.getLogger(FileSystemFunctions.class);
    +
    +  public interface FileSystemGetter {
    +    FileSystem getSystem() throws IOException ;
    +  }
    +
    +  public static enum FS_TYPE implements FileSystemGetter {
    +    LOCAL(() -> {
    +      FileSystem fs = new LocalFileSystem();
    +      fs.initialize(URI.create("file:///"), new Configuration());
    +      return fs;
    +    })
    +    ,HDFS(() -> FileSystem.get(new Configuration()))
    +    ;
    +    FileSystemGetter _func;
    +    FS_TYPE(FileSystemGetter func) {
    +      _func = func;
    +    }
    +
    +
    +    @Override
    +    public FileSystem getSystem() throws IOException {
    +      return _func.getSystem();
    +    }
    +  }
    +
    +  private abstract static class FileSystemFunction implements StellarFunction {
    +    protected FileSystem fs;
    +    private FileSystemGetter getter;
    +    FileSystemFunction(FileSystemGetter getter) {
    +      this.getter = getter;
    +    }
    +    @Override
    +    public void initialize(Context context) {
    +      try {
    +        fs = getter.getSystem();
    +      } catch (IOException e) {
    +        String message = "Unable to get FileSystem: " + e.getMessage();
    +        LOG.error(message, e);
    +        throw new IllegalStateException(message, e);
    +      }
    +    }
    +
    +    @Override
    +    public boolean isInitialized() {
    +      return fs != null;
    +    }
    +
    +  }
    +
    +  static class FileSystemGetList extends FileSystemFunction {
    +
    +    FileSystemGetList(FileSystemGetter getter) {
    +      super(getter);
    +    }
    +
    +    @Override
    +    public Object apply(List<Object> args, Context context) throws ParseException {
    +      String path = (String) args.get(0);
    +      if(path == null) {
    +        return null;
    +      }
    +      try(FSDataInputStream is = fs.open(new Path(path))) {
    +        return IOUtils.readLines(is);
    +      } catch (IOException e) {
    +        String message = "Unable to read " + path + ": " + e.getMessage();
    +        LOG.error(message, e);
    +        return null;
    +      }
    +    }
    +  }
    +
    +  static class FileSystemGet extends FileSystemFunction {
    +
    +    FileSystemGet(FileSystemGetter getter) {
    +      super(getter);
    +    }
    +
    +    @Override
    +    public Object apply(List<Object> args, Context context) throws ParseException {
    +      String path = (String) args.get(0);
    +      if(path == null) {
    +        return null;
    +      }
    +      try(FSDataInputStream is = fs.open(new Path(path))) {
    +        return IOUtils.toString(is);
    +      } catch (IOException e) {
    +        String message = "Unable to read " + path + ": " + e.getMessage();
    +        LOG.error(message, e);
    +        return null;
    +      }
    +    }
    +  }
    +
    +  static class FileSystemRm extends FileSystemFunction {
    +
    +    FileSystemRm(FileSystemGetter getter) {
    +      super(getter);
    +    }
    +
    +    @Override
    +    public Object apply(List<Object> args, Context context) throws ParseException {
    +
    +      String path = (String) args.get(0);
    +      if(path == null) {
    +        return false;
    +      }
    +
    +      boolean recursive = false;
    +      if(args.size() > 1) {
    +        recursive = ConversionUtils.convert(args.get(1), Boolean.class);
    +      }
    +
    +      try {
    +        fs.delete(new Path(path), recursive);
    +        return true;
    +      } catch (IOException e) {
    +        String message = "Unable to remove " + path + (recursive?" recursively":"") + ": " + e.getMessage();
    +        LOG.error(message, e);
    +        return false;
    +      }
    +    }
    +  }
    +
    +  static class FileSystemPut extends FileSystemFunction {
    +
    +    FileSystemPut(FileSystemGetter getter) {
    +      super(getter);
    +    }
    +
    +    @Override
    +    public Object apply(List<Object> args, Context context) throws ParseException {
    +      String content = (String)args.get(0);
    +      if(content == null) {
    +        return false;
    +      }
    +      String path = (String) args.get(1);
    +      if(path == null) {
    +        return false;
    +      }
    +
    +      try(FSDataOutputStream os = fs.create(new Path(path))) {
    +        os.writeBytes(content);
    +        os.flush();
    +        return true;
    +      } catch (IOException e) {
    +        String message = "Unable to write " + path + ": " + e.getMessage();
    +        LOG.error(message, e);
    +        return false;
    +      }
    +    }
    +  }
    +
    +  static class FileSystemLs extends FileSystemFunction {
    +    private static ThreadLocal<DateFormat> dateFormat = new ThreadLocal<DateFormat>() {
    +
    +      @Override
    +      protected DateFormat initialValue() {
    +        return DateFormat.getDateTimeInstance(
    +                DateFormat.DEFAULT,
    +                DateFormat.DEFAULT,
    +                Locale.getDefault()
    +                );
    +      }
    +    };
    +
    +    FileSystemLs(FileSystemGetter getter) {
    +      super(getter);
    +    }
    +
    +    @Override
    +    public Object apply(List<Object> args, Context context) throws ParseException {
    +
    +      String path = (String) args.get(0);
    --- End diff --
    
    Might a sensible default be better here when no args are passed? Perhaps the user's home dir or '/'?
    ```
    [Stellar]>>> HDFS_LS()
    [!] Unable to execute: Index: 0, Size: 0
    org.apache.metron.common.dsl.ParseException: Unable to execute: Index: 0, Size: 0
    ...
    Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
           	at java.util.LinkedList.checkElementIndex(LinkedList.java:555)
           	at java.util.LinkedList.get(LinkedList.java:476)
           	at org.apache.metron.management.FileSystemFunctions$FileSystemLs.apply(FileSystemFunctions.java:213)
    ```


---
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.
---

[GitHub] incubator-metron issue #277: METRON-455: Create stellar management functions...

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

    https://github.com/apache/incubator-metron/pull/277
  
    This is excellent... +1 pending the changes pass Travis


---
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.
---

[GitHub] incubator-metron pull request #277: METRON-455: Create stellar management fu...

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

    https://github.com/apache/incubator-metron/pull/277


---
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.
---

[GitHub] incubator-metron pull request #277: METRON-455: Create stellar management fu...

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

    https://github.com/apache/incubator-metron/pull/277#discussion_r80932548
  
    --- Diff: metron-platform/metron-management/README.md ---
    @@ -19,11 +19,72 @@ project.
     
     The functions are split roughly into a few sections:
     * Shell functions - Functions surrounding interacting with the shell in either a nicer way or a more functional way.
    +* File functions - Functions around interacting with local or HDFS files
     * Configuration functions - Functions surrounding pulling and pushing configs from zookeeper
     * Parser functions - Functions surrounding adding, viewing, and removing Parser functions.
     * Enrichment functions - Functions surrounding adding, viewing and removing Stellar enrichments as well as managing batch size and index names for the enrichment topology configuration
     * Threat Triage functions - Functions surrounding adding, viewing and removing threat triage functions.
     
    +### File Functions
    +
    +* Local Files
    +  * `FILE_LS`
    --- End diff --
    
    completely agreed.


---
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.
---

[GitHub] incubator-metron pull request #277: METRON-455: Create stellar management fu...

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

    https://github.com/apache/incubator-metron/pull/277#discussion_r80936127
  
    --- Diff: metron-platform/metron-management/src/main/java/org/apache/metron/management/FileSystemFunctions.java ---
    @@ -0,0 +1,402 @@
    +/**
    + * 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.management;
    +
    +import com.google.common.base.Splitter;
    +import com.google.common.collect.Iterables;
    +import com.jakewharton.fliptables.FlipTable;
    +import org.apache.commons.io.IOUtils;
    +import org.apache.hadoop.conf.Configuration;
    +import org.apache.hadoop.fs.*;
    +import org.apache.log4j.Logger;
    +import org.apache.metron.common.dsl.Context;
    +import org.apache.metron.common.dsl.ParseException;
    +import org.apache.metron.common.dsl.Stellar;
    +import org.apache.metron.common.dsl.StellarFunction;
    +import org.apache.metron.common.utils.ConversionUtils;
    +
    +import java.io.IOException;
    +import java.net.URI;
    +import java.text.DateFormat;
    +import java.util.*;
    +import java.util.function.Function;
    +
    +public class FileSystemFunctions {
    +  private static final Logger LOG = Logger.getLogger(FileSystemFunctions.class);
    +
    +  public interface FileSystemGetter {
    +    FileSystem getSystem() throws IOException ;
    +  }
    +
    +  public static enum FS_TYPE implements FileSystemGetter {
    +    LOCAL(() -> {
    +      FileSystem fs = new LocalFileSystem();
    +      fs.initialize(URI.create("file:///"), new Configuration());
    +      return fs;
    +    })
    +    ,HDFS(() -> FileSystem.get(new Configuration()))
    +    ;
    +    FileSystemGetter _func;
    +    FS_TYPE(FileSystemGetter func) {
    +      _func = func;
    +    }
    +
    +
    +    @Override
    +    public FileSystem getSystem() throws IOException {
    +      return _func.getSystem();
    +    }
    +  }
    +
    +  private abstract static class FileSystemFunction implements StellarFunction {
    +    protected FileSystem fs;
    +    private FileSystemGetter getter;
    +    FileSystemFunction(FileSystemGetter getter) {
    +      this.getter = getter;
    +    }
    +    @Override
    +    public void initialize(Context context) {
    +      try {
    +        fs = getter.getSystem();
    +      } catch (IOException e) {
    +        String message = "Unable to get FileSystem: " + e.getMessage();
    +        LOG.error(message, e);
    +        throw new IllegalStateException(message, e);
    +      }
    +    }
    +
    +    @Override
    +    public boolean isInitialized() {
    +      return fs != null;
    +    }
    +
    +  }
    +
    +  static class FileSystemGetList extends FileSystemFunction {
    +
    +    FileSystemGetList(FileSystemGetter getter) {
    +      super(getter);
    +    }
    +
    +    @Override
    +    public Object apply(List<Object> args, Context context) throws ParseException {
    +      String path = (String) args.get(0);
    +      if(path == null) {
    +        return null;
    +      }
    +      try(FSDataInputStream is = fs.open(new Path(path))) {
    +        return IOUtils.readLines(is);
    +      } catch (IOException e) {
    +        String message = "Unable to read " + path + ": " + e.getMessage();
    +        LOG.error(message, e);
    +        return null;
    +      }
    +    }
    +  }
    +
    +  static class FileSystemGet extends FileSystemFunction {
    +
    +    FileSystemGet(FileSystemGetter getter) {
    +      super(getter);
    +    }
    +
    +    @Override
    +    public Object apply(List<Object> args, Context context) throws ParseException {
    +      String path = (String) args.get(0);
    +      if(path == null) {
    +        return null;
    +      }
    +      try(FSDataInputStream is = fs.open(new Path(path))) {
    +        return IOUtils.toString(is);
    +      } catch (IOException e) {
    +        String message = "Unable to read " + path + ": " + e.getMessage();
    +        LOG.error(message, e);
    +        return null;
    +      }
    +    }
    +  }
    +
    +  static class FileSystemRm extends FileSystemFunction {
    +
    +    FileSystemRm(FileSystemGetter getter) {
    +      super(getter);
    +    }
    +
    +    @Override
    +    public Object apply(List<Object> args, Context context) throws ParseException {
    +
    +      String path = (String) args.get(0);
    +      if(path == null) {
    +        return false;
    +      }
    +
    +      boolean recursive = false;
    +      if(args.size() > 1) {
    +        recursive = ConversionUtils.convert(args.get(1), Boolean.class);
    +      }
    +
    +      try {
    +        fs.delete(new Path(path), recursive);
    +        return true;
    +      } catch (IOException e) {
    +        String message = "Unable to remove " + path + (recursive?" recursively":"") + ": " + e.getMessage();
    +        LOG.error(message, e);
    +        return false;
    +      }
    +    }
    +  }
    +
    +  static class FileSystemPut extends FileSystemFunction {
    +
    +    FileSystemPut(FileSystemGetter getter) {
    +      super(getter);
    +    }
    +
    +    @Override
    +    public Object apply(List<Object> args, Context context) throws ParseException {
    +      String content = (String)args.get(0);
    +      if(content == null) {
    +        return false;
    +      }
    +      String path = (String) args.get(1);
    +      if(path == null) {
    +        return false;
    +      }
    +
    +      try(FSDataOutputStream os = fs.create(new Path(path))) {
    +        os.writeBytes(content);
    +        os.flush();
    +        return true;
    +      } catch (IOException e) {
    +        String message = "Unable to write " + path + ": " + e.getMessage();
    +        LOG.error(message, e);
    +        return false;
    +      }
    +    }
    +  }
    +
    +  static class FileSystemLs extends FileSystemFunction {
    +    private static ThreadLocal<DateFormat> dateFormat = new ThreadLocal<DateFormat>() {
    +
    +      @Override
    +      protected DateFormat initialValue() {
    +        return DateFormat.getDateTimeInstance(
    +                DateFormat.DEFAULT,
    +                DateFormat.DEFAULT,
    +                Locale.getDefault()
    +                );
    +      }
    +    };
    +
    +    FileSystemLs(FileSystemGetter getter) {
    +      super(getter);
    +    }
    +
    +    @Override
    +    public Object apply(List<Object> args, Context context) throws ParseException {
    +
    +      String path = (String) args.get(0);
    --- End diff --
    
    agreed, homedir makes sense


---
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.
---