将时间从Apache Log转换为Epoch时间

时间:2017-04-19 20:01:08

标签: java sql epoch grok

编辑:工作解决方案。原始问题如下。

private Timestamp extractTimestamp(Object timestamp) {
    try {
        return Timestamp.from(Instant.ofEpochMilli(Long.valueOf(String.valueOf(timestamp))));
    } catch(NumberFormatException e) {
        System.out.println("Not in Epoch format");
    }
    try {
        return Timestamp.from(Instant.parse(String.valueOf(timestamp)));
    } catch(DateTimeParseException e) {
        System.out.println("Not in UTC format");
    }
    try {
        return Timestamp.valueOf(String.valueOf(timestamp));
    } catch(IllegalArgumentException e) {
        System.out.println("Not in SQL format");
    }
    try {               
        SimpleDateFormat formatter = new SimpleDateFormat("dd/MMM/yyyy:hh:mm:ss Z");
        Date date = formatter.parse(String.valueOf(timestamp));
        return Timestamp.from(Instant.ofEpochMilli(date.getTime()));
    } catch(ParseException e) {
        System.out.println("Not in Apache Log format");
    }
    // Return current time if none found
    return Timestamp.from(Instant.now());
}

我正在尝试解析Apache访问日志中的时间戳,并将其转换为Epoch时间戳或SQL时间戳。我已经有代码将epoch转换为其他格式的SQL时间戳,因此我主要关注的是Epoch格式或任何其他易于转换的格式。我目前正在使用Grok模式,但我正在寻找一种更有效的方法来提取时间。

下面是我提取的日志和时间戳示例以及我当前的代码:

127.0.0.1 127.0.0.1 - - [04 / Nov / 2016:08:00:02 -0400]" GET / loc / ation" 200 163" - " " - " 26 163 37526

04 / Nov / 2016:08:00:02 -0400

private Timestamp extractTimestamp(Object timestamp) {
  try {
    return Timestamp.from(Instant.ofEpochMilli(Long.valueOf(String.valueOf(timestamp))));
  } catch(NumberFormatException e) {
    System.out.println("Not in Epoch format");
  }
  try {
    return Timestamp.from(Instant.parse(String.valueOf(timestamp)));
  } catch(DateTimeParseException e) {
    System.out.println("Not in UTC format");
  }
  try {
    return Timestamp.valueOf(String.valueOf(timestamp));
  } catch(IllegalArgumentException e) {
    System.out.println("Not in SQL Time format");
  }
  try {
    // Sample timestamp: 04/Nov/2016:08:00:02 -0400
    String apacheLogExpression = "%{NUMBER:day}/%{WORD:month}/%{NUMBER:year}:%{NUMBER:hour}:%{NUMBER:minute}:%{NUMBER:second}\\s%{GREEDYDATA:offset}";
    Grok compiledPattern = dictionary.compileExpression(apacheLogExpression);
    Map<String, String> values = compiledPattern.extractNamedGroups(String.valueOf(timestamp));
    System.out.println(values);
  } catch(Exception e) {
    System.out.println("Not in Apache Log format");
    e.printStackTrace();
  }
  // Return current time if none found
  return Timestamp.from(Instant.now());
}

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

    String logTime = "04/Nov/2016:08:00:02 -0400";
    SimpleDateFormat formatter = new SimpleDateFormat("dd/MMM/yyyy:hh:mm:ss Z");
    Date date = formatter.parse(logTime);
    System.out.println(date);

将打印Fri Nov 04 14:00:02 EET 2016并基本上获得java.util.Date对象