bhrgv-bolla commented on issue #6513: Ability to auto populate timestamp on 
event ingestion. 
URL: 
https://github.com/apache/incubator-druid/issues/6513#issuecomment-435460269
 
 
   Yes, timestamp on ingestion. I made a hack for our use case. 
   
   This is a slightly modified string input row parser.
   
   ```java
   
   /*
    * 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 com.example.druid.parsers;
   
   import com.fasterxml.jackson.annotation.JsonCreator;
   import com.fasterxml.jackson.annotation.JsonProperty;
   import com.google.common.base.Charsets;
   import com.google.common.base.Preconditions;
   import com.google.common.base.Strings;
   import com.google.common.collect.Iterators;
   import com.google.common.collect.Maps;
   import io.druid.data.input.ByteBufferInputRowParser;
   import io.druid.data.input.InputRow;
   import io.druid.data.input.impl.MapInputRowParser;
   import io.druid.data.input.impl.ParseSpec;
   import io.druid.java.util.common.collect.Utils;
   import io.druid.java.util.common.parsers.ParseException;
   import io.druid.java.util.common.parsers.Parser;
   import org.joda.time.DateTime;
   
   import javax.annotation.Nullable;
   import java.nio.ByteBuffer;
   import java.nio.CharBuffer;
   import java.nio.charset.Charset;
   import java.nio.charset.CoderResult;
   import java.nio.charset.CodingErrorAction;
   import java.util.List;
   import java.util.Map;
   
   /**
    * Adds a timestamp column to the current time.
    * User needs to specify timestamp column in timestamp spec.
    */
   public class ManagedStringInputRowParser implements ByteBufferInputRowParser 
{
       private static final Charset DEFAULT_CHARSET = Charsets.UTF_8;
   
       private final ParseSpec parseSpec;
       private final MapInputRowParser mapParser;
       private final Charset charset;
   
       private Parser<String, Object> parser;
       private CharBuffer chars;
       private final Boolean autoTimeStamp;
   
       @JsonProperty
       public String getType() {
           return "managed";
       }
   
       @JsonCreator
       public ManagedStringInputRowParser(
               @JsonProperty("parseSpec") ParseSpec parseSpec,
               @JsonProperty("encoding") String encoding,
               @JsonProperty("autoTimestamp") Boolean autoTimeStamp
       ) {
           this.parseSpec = Preconditions.checkNotNull(parseSpec, "parseSpec");
           this.mapParser = new MapInputRowParser(parseSpec);
   
           //Enabled by default.
           if(autoTimeStamp == null) this.autoTimeStamp = true;
           else this.autoTimeStamp = autoTimeStamp;
   
           if (encoding != null) {
               this.charset = Charset.forName(encoding);
           } else {
               this.charset = DEFAULT_CHARSET;
           }
       }
   
       @Deprecated
       public ManagedStringInputRowParser(ParseSpec parseSpec) {
           this(parseSpec, null, false);
       }
   
       public List<InputRow> parseBatch(ByteBuffer input) {
           return Utils.nullableListOf(parseMap(buildStringKeyMap(input)));
       }
   
       @JsonProperty
       public ParseSpec getParseSpec() {
           return parseSpec;
       }
   
       @JsonProperty
       public String getEncoding() {
           return charset.name();
       }
   
       public ManagedStringInputRowParser withParseSpec(ParseSpec parseSpec) {
           return new ManagedStringInputRowParser(parseSpec, getEncoding(), 
true);
       }
   
       private Map<String, Object> buildStringKeyMap(ByteBuffer input) {
           int payloadSize = input.remaining();
   
           if (chars == null || chars.remaining() < payloadSize) {
               chars = CharBuffer.allocate(payloadSize);
           }
   
           final CoderResult coderResult = charset.newDecoder()
                   .onMalformedInput(CodingErrorAction.REPLACE)
                   .onUnmappableCharacter(CodingErrorAction.REPLACE)
                   .decode(input, chars, true);
   
           Map<String, Object> theMap;
           if (coderResult.isUnderflow()) {
               chars.flip();
               try {
                   theMap = parseString(chars.toString());
                   theMap = Maps.newHashMap(theMap); //ObjectFlatnner causing 
issues.
                   //Always a timestamp column need to exist
                   if(getParseSpec() == null || 
getParseSpec().getTimestampSpec() == null || 
Strings.isNullOrEmpty(getParseSpec().getTimestampSpec().getTimestampColumn())) {
                       throw new IllegalStateException("Couldn't find the 
timestamp column name : "+ getParseSpec());
                   }
                   String timestampColumn = 
getParseSpec().getTimestampSpec().getTimestampColumn();
                   if(theMap != null && this.autoTimeStamp) 
theMap.put(timestampColumn, DateTime.now().toDateTimeISO().toString()); //TODO 
review Managed timestamp.
               } finally {
                   chars.clear();
               }
           } else {
               throw new ParseException("Failed with CoderResult[%s]", 
coderResult);
           }
           return theMap;
       }
   
       public void initializeParser() {
           if (parser == null) {
               // parser should be created when it is really used to avoid 
unnecessary initialization of the underlying
               // parseSpec.
               parser = parseSpec.makeParser();
           }
       }
   
       public void startFileFromBeginning() {
           initializeParser();
           parser.startFileFromBeginning();
       }
   
       @Nullable
       public InputRow parse(@Nullable String input) {
           return parseMap(parseString(input));
       }
   
       @Nullable
       private Map<String, Object> parseString(@Nullable String inputString) {
           initializeParser();
           return parser.parseToMap(inputString);
       }
   
       @Nullable
       private InputRow parseMap(@Nullable Map<String, Object> theMap) {
           // If a header is present in the data (and with proper 
configurations), a null is returned
           if (theMap == null) {
               return null;
           }
           return 
Iterators.getOnlyElement(mapParser.parseBatch(theMap).iterator());
       }
   }
   
   ```
   

----------------------------------------------------------------
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:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to