You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@druid.apache.org by GitBox <gi...@apache.org> on 2018/11/12 21:37:52 UTC

[GitHub] njhartwell commented on a change in pull request #5440: Adding ParserSpec for Influx Line Protocol

njhartwell commented on a change in pull request #5440: Adding ParserSpec for Influx Line Protocol
URL: https://github.com/apache/incubator-druid/pull/5440#discussion_r232820551
 
 

 ##########
 File path: extensions-contrib/influx-extensions/src/main/java/io/druid/data/input/influx/InfluxParser.java
 ##########
 @@ -0,0 +1,173 @@
+/*
+ * Licensed to Metamarkets Group Inc. (Metamarkets) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. Metamarkets 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 io.druid.data.input.influx;
+
+import com.google.common.collect.ImmutableList;
+import io.druid.java.util.common.parsers.ParseException;
+import io.druid.java.util.common.parsers.Parser;
+import org.antlr.v4.runtime.ANTLRInputStream;
+import org.antlr.v4.runtime.CharStream;
+import org.antlr.v4.runtime.CommonTokenStream;
+import org.antlr.v4.runtime.TokenStream;
+
+import javax.annotation.Nullable;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+public class InfluxParser implements Parser<String, Object>
+{
+  public static final String TIMESTAMP_KEY = "__ts";
+  private static final String MEASUREMENT_KEY = "measurement";
+  private final Set<String> measurementWhitelist;
+
+  public InfluxParser(Set<String> measurementWhitelist)
+  {
+    this.measurementWhitelist = measurementWhitelist;
+  }
+
+  @Override
+  public void startFileFromBeginning()
+  {
+  }
+
+  @Nullable
+  @Override
+  public Map<String, Object> parseToMap(String input)
+  {
+    CharStream charStream = new ANTLRInputStream(input);
+    InfluxLineProtocolLexer lexer = new InfluxLineProtocolLexer(charStream);
+    TokenStream tokenStream = new CommonTokenStream(lexer);
+    InfluxLineProtocolParser parser = new InfluxLineProtocolParser(tokenStream);
+
+    List<InfluxLineProtocolParser.LineContext> lines = parser.lines().line();
+    if (parser.getNumberOfSyntaxErrors() != 0) {
+      throw new ParseException("Unable to parse line.");
+    }
+    if (lines.size() != 1) {
+      throw new ParseException("Multiple lines present; unable to parse more than one per record.");
+    }
+
+    Map<String, Object> out = new LinkedHashMap<>();
+
+    InfluxLineProtocolParser.LineContext line = lines.get(0);
+    String measurement = parseIdentifier(line.identifier());
+
+    if (!checkWhitelist(measurement)) {
+      throw new ParseException("Metric not whitelisted.");
+    }
+
+    out.put(MEASUREMENT_KEY, measurement);
+    if (line.tag_set() != null) {
+      line.tag_set().tag_pair().forEach(t -> parseTag(t, out));
+    }
+
+    line.field_set().field_pair().forEach(t -> parseField(t, out));
+
+    if (line.timestamp() != null) {
+      String timestamp = line.timestamp().getText();
+      parseTimestamp(timestamp, out);
+    }
+    return out;
+  }
+
+  private void parseTag(InfluxLineProtocolParser.Tag_pairContext tag, Map<String, Object> out)
+  {
+    String key = parseIdentifier(tag.identifier(0));
+    String value = parseIdentifier(tag.identifier(1));
+    out.put(key, value);
+  }
+
+  private void parseField(InfluxLineProtocolParser.Field_pairContext field, Map<String, Object> out)
+  {
+    String key = parseIdentifier(field.identifier());
+    InfluxLineProtocolParser.Field_valueContext valueContext = field.field_value();
+    Object value;
+    if (valueContext.NUMBER() != null) {
+      value = parseNumber(valueContext.NUMBER().getText());
+    } else if (valueContext.BOOLEAN() != null) {
+      value = parseBool(valueContext.BOOLEAN().getText());
+    } else {
+      value = parseQuotedString(valueContext.QUOTED_STRING().getText());
+    }
+    out.put(key, value);
+  }
+
+  private Object parseQuotedString(String text)
+  {
+    return text.substring(1, text.length() - 1).replaceAll("\\\\\"", "\"");
 
 Review comment:
   Yes, 4 slashes is a syntax error. The first four are read into the java string as two backlashes, and the fifth is paired with the double quote, so that the string passed to the regex library is `\\"` which matches the string `\"`, i.e. an escaped quote.

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


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org