You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2022/06/06 10:11:17 UTC

[GitHub] [pulsar] eolivelli commented on a diff in pull request #15903: [feature][functions]Create a built-in Function implementing the most common basic transformations

eolivelli commented on code in PR #15903:
URL: https://github.com/apache/pulsar/pull/15903#discussion_r890004881


##########
pulsar-functions/transforms/src/main/java/org/apache/pulsar/functions/transforms/MergeKeyValueStep.java:
##########
@@ -0,0 +1,86 @@
+/**
+ * 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.pulsar.functions.transforms;
+
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.stream.Collectors;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.avro.generic.GenericData;
+import org.apache.avro.generic.GenericRecord;
+import org.apache.pulsar.client.api.Schema;
+import org.apache.pulsar.common.schema.SchemaType;
+
+@Slf4j
+public class MergeKeyValueStep implements TransformStep {
+
+    private final Map<org.apache.avro.Schema, Map<org.apache.avro.Schema, org.apache.avro.Schema>> schemaCache =
+            new ConcurrentHashMap<>();
+
+    @Override
+    public void process(TransformContext transformContext) {
+        Schema<?> keySchema = transformContext.getKeySchema();
+        if (keySchema == null) {

Review Comment:
   what does it mean ?
   are we assuming that the Key is byte[] or String when we are not using KeyValue Schema ?
   



##########
pulsar-functions/transforms/src/main/java/org/apache/pulsar/functions/transforms/CastStep.java:
##########
@@ -0,0 +1,53 @@
+/**
+ * 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.pulsar.functions.transforms;
+
+import lombok.extern.slf4j.Slf4j;
+import org.apache.pulsar.client.api.Schema;
+import org.apache.pulsar.common.schema.SchemaType;
+
+@Slf4j
+public class CastStep implements TransformStep {

Review Comment:
   the purpose if this Step is not clear to me.
   what does this step do ?



##########
pulsar-functions/transforms/src/main/java/org/apache/pulsar/functions/transforms/TransformContext.java:
##########
@@ -0,0 +1,131 @@
+/**
+ * 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.pulsar.functions.transforms;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.util.Map;
+import lombok.Data;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.avro.generic.GenericDatumWriter;
+import org.apache.avro.generic.GenericRecord;
+import org.apache.avro.io.BinaryEncoder;
+import org.apache.avro.io.EncoderFactory;
+import org.apache.pulsar.client.api.Schema;
+import org.apache.pulsar.client.api.TypedMessageBuilder;
+import org.apache.pulsar.client.api.schema.GenericObject;
+import org.apache.pulsar.client.api.schema.KeyValueSchema;
+import org.apache.pulsar.common.schema.KeyValue;
+import org.apache.pulsar.common.schema.KeyValueEncodingType;
+import org.apache.pulsar.common.schema.SchemaType;
+import org.apache.pulsar.functions.api.Context;
+import org.apache.pulsar.functions.api.Record;
+
+@Slf4j
+@Data
+public class TransformContext {
+    private Context context;
+    private Schema<?> keySchema;
+    private Object keyObject;
+    private boolean keyModified;
+    private Schema<?> valueSchema;
+    private Object valueObject;
+    private boolean valueModified;
+    private KeyValueEncodingType keyValueEncodingType;
+    private String key;
+    private Map<String, String> properties;
+    private String outputTopic;

Review Comment:
   final?



##########
pulsar-functions/transforms/src/main/java/org/apache/pulsar/functions/transforms/TransformContext.java:
##########
@@ -0,0 +1,131 @@
+/**
+ * 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.pulsar.functions.transforms;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.util.Map;
+import lombok.Data;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.avro.generic.GenericDatumWriter;
+import org.apache.avro.generic.GenericRecord;
+import org.apache.avro.io.BinaryEncoder;
+import org.apache.avro.io.EncoderFactory;
+import org.apache.pulsar.client.api.Schema;
+import org.apache.pulsar.client.api.TypedMessageBuilder;
+import org.apache.pulsar.client.api.schema.GenericObject;
+import org.apache.pulsar.client.api.schema.KeyValueSchema;
+import org.apache.pulsar.common.schema.KeyValue;
+import org.apache.pulsar.common.schema.KeyValueEncodingType;
+import org.apache.pulsar.common.schema.SchemaType;
+import org.apache.pulsar.functions.api.Context;
+import org.apache.pulsar.functions.api.Record;
+
+@Slf4j
+@Data
+public class TransformContext {
+    private Context context;
+    private Schema<?> keySchema;
+    private Object keyObject;
+    private boolean keyModified;
+    private Schema<?> valueSchema;
+    private Object valueObject;
+    private boolean valueModified;
+    private KeyValueEncodingType keyValueEncodingType;
+    private String key;
+    private Map<String, String> properties;
+    private String outputTopic;
+
+    public TransformContext(Context context, Object value) {
+        Record<?> currentRecord = context.getCurrentRecord();
+        this.context = context;
+        this.outputTopic = context.getOutputTopic();
+        Schema<?> schema = currentRecord.getSchema();
+        //TODO: should we make a copy ?

Review Comment:
   no need to perform a copy, but maybe it is better to wrap to Collections.unmodifiableMap() if we are not supposed to change the contents



##########
pulsar-functions/transforms/src/main/java/org/apache/pulsar/functions/transforms/CastStep.java:
##########
@@ -0,0 +1,53 @@
+/**
+ * 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.pulsar.functions.transforms;
+
+import lombok.extern.slf4j.Slf4j;
+import org.apache.pulsar.client.api.Schema;
+import org.apache.pulsar.common.schema.SchemaType;
+
+@Slf4j
+public class CastStep implements TransformStep {
+
+    private final SchemaType keySchemaType;
+    private final SchemaType valueSchemaType;
+
+    public CastStep(SchemaType keySchemaType, SchemaType valueSchemaType) {
+        this.keySchemaType = keySchemaType;
+        this.valueSchemaType = valueSchemaType;
+    }
+
+    @Override
+    public void process(TransformContext transformContext) {
+        if (transformContext.getKeySchema() != null) {
+            Object outputKeyObject = transformContext.getKeyObject();
+            Schema<?> outputSchema = transformContext.getKeySchema();
+            if (keySchemaType == SchemaType.STRING) {
+                outputSchema = Schema.STRING;
+                outputKeyObject = outputKeyObject.toString();
+            }
+            transformContext.setKeySchema(outputSchema);
+            transformContext.setKeyObject(outputKeyObject);
+        }
+        if (valueSchemaType == SchemaType.STRING) {

Review Comment:
   else ?
   we should throw a IllegalStateException here if we are not supporting other types



##########
pulsar-functions/transforms/src/main/java/org/apache/pulsar/functions/transforms/TransformContext.java:
##########
@@ -0,0 +1,131 @@
+/**
+ * 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.pulsar.functions.transforms;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.util.Map;
+import lombok.Data;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.avro.generic.GenericDatumWriter;
+import org.apache.avro.generic.GenericRecord;
+import org.apache.avro.io.BinaryEncoder;
+import org.apache.avro.io.EncoderFactory;
+import org.apache.pulsar.client.api.Schema;
+import org.apache.pulsar.client.api.TypedMessageBuilder;
+import org.apache.pulsar.client.api.schema.GenericObject;
+import org.apache.pulsar.client.api.schema.KeyValueSchema;
+import org.apache.pulsar.common.schema.KeyValue;
+import org.apache.pulsar.common.schema.KeyValueEncodingType;
+import org.apache.pulsar.common.schema.SchemaType;
+import org.apache.pulsar.functions.api.Context;
+import org.apache.pulsar.functions.api.Record;
+
+@Slf4j
+@Data
+public class TransformContext {
+    private Context context;

Review Comment:
   final?



##########
pulsar-functions/transforms/src/main/java/org/apache/pulsar/functions/transforms/TransformFunction.java:
##########
@@ -0,0 +1,169 @@
+/**
+ * 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.pulsar.functions.transforms;
+
+import com.google.gson.Gson;
+import com.google.gson.reflect.TypeToken;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.pulsar.client.api.schema.GenericObject;
+import org.apache.pulsar.common.schema.SchemaType;
+import org.apache.pulsar.functions.api.Context;
+import org.apache.pulsar.functions.api.Function;
+import org.apache.pulsar.functions.api.Record;
+
+@Slf4j

Review Comment:
   please add javadoc



##########
pulsar-functions/transforms/src/main/java/org/apache/pulsar/functions/transforms/TransformContext.java:
##########
@@ -0,0 +1,131 @@
+/**
+ * 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.pulsar.functions.transforms;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.util.Map;
+import lombok.Data;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.avro.generic.GenericDatumWriter;
+import org.apache.avro.generic.GenericRecord;
+import org.apache.avro.io.BinaryEncoder;
+import org.apache.avro.io.EncoderFactory;
+import org.apache.pulsar.client.api.Schema;
+import org.apache.pulsar.client.api.TypedMessageBuilder;
+import org.apache.pulsar.client.api.schema.GenericObject;
+import org.apache.pulsar.client.api.schema.KeyValueSchema;
+import org.apache.pulsar.common.schema.KeyValue;
+import org.apache.pulsar.common.schema.KeyValueEncodingType;
+import org.apache.pulsar.common.schema.SchemaType;
+import org.apache.pulsar.functions.api.Context;
+import org.apache.pulsar.functions.api.Record;
+
+@Slf4j
+@Data
+public class TransformContext {
+    private Context context;
+    private Schema<?> keySchema;
+    private Object keyObject;
+    private boolean keyModified;
+    private Schema<?> valueSchema;
+    private Object valueObject;
+    private boolean valueModified;
+    private KeyValueEncodingType keyValueEncodingType;
+    private String key;
+    private Map<String, String> properties;
+    private String outputTopic;
+
+    public TransformContext(Context context, Object value) {
+        Record<?> currentRecord = context.getCurrentRecord();
+        this.context = context;
+        this.outputTopic = context.getOutputTopic();
+        Schema<?> schema = currentRecord.getSchema();
+        //TODO: should we make a copy ?
+        this.properties = currentRecord.getProperties();
+        if (schema instanceof KeyValueSchema && value instanceof KeyValue) {
+            KeyValueSchema kvSchema = (KeyValueSchema) schema;
+            KeyValue kv = (KeyValue) value;
+            this.keySchema = kvSchema.getKeySchema();
+            this.keyObject = this.keySchema.getSchemaInfo().getType() == SchemaType.AVRO
+                    ? ((org.apache.pulsar.client.api.schema.GenericRecord) kv.getKey()).getNativeObject()
+                    : kv.getKey();
+            this.valueSchema = kvSchema.getValueSchema();
+            this.valueObject = this.valueSchema.getSchemaInfo().getType() == SchemaType.AVRO
+                    ? ((org.apache.pulsar.client.api.schema.GenericRecord) kv.getValue()).getNativeObject()
+                    : kv.getValue();
+            this.keyValueEncodingType = kvSchema.getKeyValueEncodingType();
+        } else {
+            this.valueSchema = schema;
+            this.valueObject = value;
+            this.key = currentRecord.getKey().orElse(null);
+        }
+    }
+
+    public void send() throws IOException {
+        if (keyModified && keySchema != null && keySchema.getSchemaInfo().getType() == SchemaType.AVRO) {
+            GenericRecord genericRecord = (GenericRecord) keyObject;
+            keySchema = Schema.NATIVE_AVRO(genericRecord.getSchema());
+            keyObject = serializeGenericRecord(genericRecord);
+        }
+        if (valueModified && valueSchema != null && valueSchema.getSchemaInfo().getType() == SchemaType.AVRO) {
+            GenericRecord genericRecord = (GenericRecord) valueObject;
+            valueSchema = Schema.NATIVE_AVRO(genericRecord.getSchema());
+            valueObject = serializeGenericRecord(genericRecord);
+        }
+
+        Schema outputSchema;
+        Object outputObject;
+        GenericObject recordValue = (GenericObject) context.getCurrentRecord().getValue();
+        if (keySchema != null) {
+            outputSchema = Schema.KeyValue(keySchema, valueSchema, keyValueEncodingType);
+            Object outputKeyObject = !keyModified && keySchema.getSchemaInfo().getType().isStruct()
+                    ? ((KeyValue) recordValue.getNativeObject()).getKey()
+                    : keyObject;
+            Object outputValueObject = !valueModified && valueSchema.getSchemaInfo().getType().isStruct()
+                    ? ((KeyValue) recordValue.getNativeObject()).getValue()
+                    : valueObject;
+            outputObject = new KeyValue(outputKeyObject, outputValueObject);
+        } else {
+            outputSchema = valueSchema;
+            outputObject = !valueModified && valueSchema.getSchemaInfo().getType().isStruct()
+                    ? recordValue
+                    : valueObject;
+        }
+
+        if (log.isDebugEnabled()) {
+            log.debug("output {} schema {}", outputObject, outputSchema);
+        }
+        TypedMessageBuilder<?> message = context.newOutputMessage(outputTopic, outputSchema)
+                .properties(properties)
+                .value(outputObject);
+        if (key != null) {

Review Comment:
   if we are using KeyValue Schema we should not set the Key this way.



##########
pulsar-functions/transforms/src/main/java/org/apache/pulsar/functions/transforms/UnwrapKeyValueStep.java:
##########
@@ -0,0 +1,42 @@
+/**
+ * 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.pulsar.functions.transforms;
+
+public class UnwrapKeyValueStep implements TransformStep {
+
+    private final boolean unwrapKey;
+
+    public UnwrapKeyValueStep(boolean unwrapKey) {
+        this.unwrapKey = unwrapKey;
+    }
+
+    @Override
+    public void process(TransformContext transformContext) throws Exception {
+        if (transformContext.getKeySchema() != null) {
+            if (unwrapKey) {
+                transformContext.setValueSchema(transformContext.getKeySchema());
+                transformContext.setValueObject(transformContext.getKeyObject());
+            }
+            // TODO: can we avoid making the conversion to NATIVE_AVRO ?

Review Comment:
   can you please clarify ?



##########
pulsar-functions/transforms/src/main/java/org/apache/pulsar/functions/transforms/CastStep.java:
##########
@@ -0,0 +1,53 @@
+/**
+ * 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.pulsar.functions.transforms;
+
+import lombok.extern.slf4j.Slf4j;
+import org.apache.pulsar.client.api.Schema;
+import org.apache.pulsar.common.schema.SchemaType;
+
+@Slf4j
+public class CastStep implements TransformStep {
+
+    private final SchemaType keySchemaType;
+    private final SchemaType valueSchemaType;
+
+    public CastStep(SchemaType keySchemaType, SchemaType valueSchemaType) {
+        this.keySchemaType = keySchemaType;
+        this.valueSchemaType = valueSchemaType;
+    }
+
+    @Override
+    public void process(TransformContext transformContext) {
+        if (transformContext.getKeySchema() != null) {
+            Object outputKeyObject = transformContext.getKeyObject();
+            Schema<?> outputSchema = transformContext.getKeySchema();
+            if (keySchemaType == SchemaType.STRING) {
+                outputSchema = Schema.STRING;
+                outputKeyObject = outputKeyObject.toString();
+            }

Review Comment:
   else ?
   we should throw a IllegalStateException here if we are not supporting other types



-- 
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-unsubscribe@pulsar.apache.org

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