chenboat commented on code in PR #11210: URL: https://github.com/apache/pinot/pull/11210#discussion_r1287762730
########## 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 { + private static final Logger _logger = LoggerFactory.getLogger(JsonLogTransformer.class); + + private final boolean _continueOnError; + private final JsonLogTransformerConfig _transformerConfig; + private final DataType _indexableExtrasFieldType; + private final DataType _unindexableExtrasFieldType; + + private Map<String, Object> _schemaTree; + + /** + * Validates the schema against the given transformer's configuration. + */ + public static void validateSchema(@Nonnull Schema schema, @Nonnull JsonLogTransformerConfig transformerConfig) { + validateSchemaFieldNames(schema.getPhysicalColumnNames(), transformerConfig); + + String indexableExtrasFieldName = transformerConfig.getIndexableExtrasField(); + getAndValidateExtrasFieldType(schema, indexableExtrasFieldName); + String unindexableExtrasFieldName = transformerConfig.getUnindexableExtrasField(); + if (null != unindexableExtrasFieldName) { + getAndValidateExtrasFieldType(schema, indexableExtrasFieldName); + } + + validateSchemaAndCreateTree(schema); + } + + /** + * Validates that none of the schema fields have names that conflict with the transformer's configuration. + */ + private static void validateSchemaFieldNames(Set<String> schemaFields, JsonLogTransformerConfig transformerConfig) { + // Validate that none of the columns in the schema end with unindexableFieldSuffix + String unindexableFieldSuffix = transformerConfig.getUnindexableFieldSuffix(); + if (null != unindexableFieldSuffix) { + for (String field : schemaFields) { + Preconditions.checkState(!field.endsWith(unindexableFieldSuffix), "Field '%s' has no-index suffix '%s'", field, + unindexableFieldSuffix); + } + } + + // Validate that none of the columns in the schema end overlap with the fields in fieldPathsToDrop + Set<String> fieldPathsToDrop = transformerConfig.getFieldPathsToDrop(); + if (null != fieldPathsToDrop) { + Set<String> fieldIntersection = new HashSet<>(schemaFields); + fieldIntersection.retainAll(fieldPathsToDrop); + Preconditions.checkState(fieldIntersection.isEmpty(), "Fields in schema overlap with fieldPathsToDrop"); + } + } + + /** + * @return The field type for the given extras field + */ + private static DataType getAndValidateExtrasFieldType(Schema schema, @Nonnull String extrasFieldName) { + FieldSpec fieldSpec = schema.getFieldSpecFor(extrasFieldName); + Preconditions.checkState(null != fieldSpec, "Field '%s' doesn't exist in schema", extrasFieldName); + DataType fieldDataType = fieldSpec.getDataType(); + Preconditions.checkState(DataType.JSON == fieldDataType || DataType.STRING == fieldDataType, + "Field '%s' has unsupported type %s", fieldDataType.toString()); + return fieldDataType; + } + + /** + * Validates the schema with a JsonLogTransformerConfig instance and creates a tree representing the fields in the + * schema to be used when transforming input records. For instance, the field "a.b" in the schema would be + * un-flattened into "{a: b: null}" in the tree, allowing us to more easily process records containing the latter. + * @throws IllegalArgumentException if schema validation fails Review Comment: Can you add javadoc (and some examples) on when the schema validation will fail? -- 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