chenboat commented on code in PR #11210:
URL: https://github.com/apache/pinot/pull/11210#discussion_r1287759588


##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/recordtransformer/JsonLogTransformer.java:
##########
@@ -0,0 +1,534 @@
+/**
+ * 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.pinot.segment.local.recordtransformer;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.google.common.base.Preconditions;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+import org.apache.pinot.spi.config.table.TableConfig;
+import org.apache.pinot.spi.config.table.ingestion.JsonLogTransformerConfig;
+import org.apache.pinot.spi.data.FieldSpec;
+import org.apache.pinot.spi.data.FieldSpec.DataType;
+import org.apache.pinot.spi.data.Schema;
+import org.apache.pinot.spi.data.readers.GenericRow;
+import org.apache.pinot.spi.stream.StreamDataDecoderImpl;
+import org.apache.pinot.spi.utils.JsonUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * This transformer transforms a record representing a JSON log event such 
that it can be stored in a table. JSON log
+ * events typically have a user-defined schema, so it is impractical to store 
each field in its own table column. At the
+ * same time, most (if not all) fields are important to the user, so we should 
not drop any field unnecessarily. Thus,
+ * this transformer primarily takes record-fields that don't exist in the 
schema and stores them in a type of catchall
+ * field.
+ * <p>
+ * For example, consider this log event:
+ * <pre>
+ * {
+ *   "timestamp": 1687786535928,
+ *   "hostname": "host1",
+ *   "level": "INFO",
+ *   "message": "Started processing job1",
+ *   "tags": {
+ *     "platform": "data",
+ *     "service": "serializer",
+ *     "params": {
+ *       "queueLength": 5,
+ *       "timeout": 299,
+ *       "userData_noIndex": {
+ *         "nth": 99
+ *       }
+ *     }
+ *   }
+ * }
+ * </pre>
+ * And let's say the table's schema contains these fields:
+ * <ul>
+ *   <li>timestamp</li>
+ *   <li>hostname</li>
+ *   <li>level</li>
+ *   <li>message</li>
+ *   <li>tags.platform</li>
+ *   <li>tags.service</li>
+ *   <li>indexableExtras</li>
+ *   <li>unindexableExtras</li>
+ * </ul>
+ * <p>
+ * Without this transformer, the entire "tags" field would be dropped when 
storing the record in the table. However,
+ * with this transformer, the record would be transformed into the following:
+ * <pre>
+ * {
+ *   "timestamp": 1687786535928,
+ *   "hostname": "host1",
+ *   "level": "INFO",
+ *   "message": "Started processing job1",
+ *   "tags.platform": "data",
+ *   "tags.service": "serializer",
+ *   "indexableExtras": {
+ *     "tags": {
+ *       "params": {
+ *         "queueLength": 5,
+ *         "timeout": 299
+ *       }
+ *     }
+ *   },
+ *   "unindexableExtras": {
+ *     "tags": {
+ *       "userData_noIndex": {
+ *         "nth": 99
+ *       }
+ *     }
+ *   }
+ * }
+ * </pre>
+ * Notice that the transformer:
+ * <ul>
+ *   <li>Flattens nested fields which exist in the schema, like 
"tags.platform"</li>
+ *   <li>Moves fields which don't exist in the schema into the 
"indexableExtras" field</li>
+ *   <li>Moves fields which don't exist in the schema and have the suffix 
"_noIndex" into the "unindexableExtras"
+ *   field</li>
+ * </ul>
+ * <p>
+ * The "unindexableExtras" field allows the transformer to separate fields 
which don't need indexing (because they are
+ * only retrieved, not searched) from those that do. The transformer also has 
other configuration options specified in
+ * {@link JsonLogTransformerConfig}.
+ * <p>
+ * One notable complication that this class handles is adding nested fields to 
the "extras" fields. E.g., consider
+ * this record
+ * <pre>
+ * {
+ *   a: {
+ *     b: {
+ *       c: 0,
+ *       d: 1
+ *     }
+ *   }
+ * }
+ * </pre>
+ * Assume "$.a.b.c" exists in the schema but "$.a.b.d" doesn't. This class 
processes the record recursively from the
+ * root node to the children, so it would only know that "$.a.b.d" doesn't 
exist when it gets to "d". At this point we
+ * need to add "d" and all of its parents to the indexableExtrasField. To do 
so efficiently, the class builds this
+ * branch starting from the leaf and attaches it to parent nodes as we return 
from each recursive call.
+ */
+public class JsonLogTransformer implements RecordTransformer {

Review Comment:
   No 2 looks like a good one. Should we also add json to the class name and 
make it JsonSchemaAdaptionTransformer?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org
For additional commands, e-mail: commits-h...@pinot.apache.org

Reply via email to