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 [email protected] or file a JIRA ticket
with INFRA.
---

Reply via email to