kafka-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ewe...@apache.org
Subject kafka git commit: KAFKA-4783: Add ByteArrayConverter (KIP-128)
Date Wed, 15 Mar 2017 00:20:33 GMT
Repository: kafka
Updated Branches:
  refs/heads/trunk 9e4548df3 -> 52a15d7c0


KAFKA-4783: Add ByteArrayConverter (KIP-128)

Author: Ewen Cheslack-Postava <me@ewencp.org>

Reviewers: Guozhang Wang <wangguoz@gmail.com>

Closes #2599 from ewencp/kafka-4783-byte-array-converter


Project: http://git-wip-us.apache.org/repos/asf/kafka/repo
Commit: http://git-wip-us.apache.org/repos/asf/kafka/commit/52a15d7c
Tree: http://git-wip-us.apache.org/repos/asf/kafka/tree/52a15d7c
Diff: http://git-wip-us.apache.org/repos/asf/kafka/diff/52a15d7c

Branch: refs/heads/trunk
Commit: 52a15d7c0b88da11409954321463b8b57b133a23
Parents: 9e4548d
Author: Ewen Cheslack-Postava <me@ewencp.org>
Authored: Tue Mar 14 17:20:49 2017 -0700
Committer: Ewen Cheslack-Postava <me@ewencp.org>
Committed: Tue Mar 14 17:20:49 2017 -0700

----------------------------------------------------------------------
 checkstyle/import-control.xml                   |  4 +
 .../connect/converters/ByteArrayConverter.java  | 52 +++++++++++
 .../converters/ByteArrayConverterTest.java      | 90 ++++++++++++++++++++
 3 files changed, 146 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/kafka/blob/52a15d7c/checkstyle/import-control.xml
----------------------------------------------------------------------
diff --git a/checkstyle/import-control.xml b/checkstyle/import-control.xml
index fa98593..80747e1 100644
--- a/checkstyle/import-control.xml
+++ b/checkstyle/import-control.xml
@@ -221,6 +221,10 @@
       <allow pkg="org.apache.kafka.connect.storage" />
     </subpackage>
 
+    <subpackage name="converters">
+      <allow pkg="org.apache.kafka.connect.storage" />
+    </subpackage>
+    
     <subpackage name="runtime">
       <allow pkg="org.apache.kafka.connect" />
       <allow pkg="org.reflections"/>

http://git-wip-us.apache.org/repos/asf/kafka/blob/52a15d7c/connect/runtime/src/main/java/org/apache/kafka/connect/converters/ByteArrayConverter.java
----------------------------------------------------------------------
diff --git a/connect/runtime/src/main/java/org/apache/kafka/connect/converters/ByteArrayConverter.java
b/connect/runtime/src/main/java/org/apache/kafka/connect/converters/ByteArrayConverter.java
new file mode 100644
index 0000000..f592624
--- /dev/null
+++ b/connect/runtime/src/main/java/org/apache/kafka/connect/converters/ByteArrayConverter.java
@@ -0,0 +1,52 @@
+/**
+ * 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.kafka.connect.converters;
+
+import org.apache.kafka.connect.data.Schema;
+import org.apache.kafka.connect.data.SchemaAndValue;
+import org.apache.kafka.connect.errors.DataException;
+import org.apache.kafka.connect.storage.Converter;
+
+import java.util.Map;
+
+/**
+ * Pass-through converter for raw byte data.
+ */
+public class ByteArrayConverter implements Converter {
+
+    @Override
+    public void configure(Map<String, ?> configs, boolean isKey) {
+    }
+
+    @Override
+    public byte[] fromConnectData(String topic, Schema schema, Object value) {
+        if (schema != null && schema.type() != Schema.Type.BYTES)
+            throw new DataException("Invalid schema type for ByteArrayConverter: " + schema.type().toString());
+
+        if (value != null && !(value instanceof byte[]))
+            throw new DataException("ByteArrayConverter is not compatible with objects of
type " + value.getClass());
+
+        return (byte[]) value;
+    }
+
+    @Override
+    public SchemaAndValue toConnectData(String topic, byte[] value) {
+        return new SchemaAndValue(Schema.OPTIONAL_BYTES_SCHEMA, value);
+    }
+
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/kafka/blob/52a15d7c/connect/runtime/src/test/java/org/apache/kafka/connect/converters/ByteArrayConverterTest.java
----------------------------------------------------------------------
diff --git a/connect/runtime/src/test/java/org/apache/kafka/connect/converters/ByteArrayConverterTest.java
b/connect/runtime/src/test/java/org/apache/kafka/connect/converters/ByteArrayConverterTest.java
new file mode 100644
index 0000000..e0d533f
--- /dev/null
+++ b/connect/runtime/src/test/java/org/apache/kafka/connect/converters/ByteArrayConverterTest.java
@@ -0,0 +1,90 @@
+/**
+ * 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.kafka.connect.converters;
+
+import org.apache.kafka.connect.data.Schema;
+import org.apache.kafka.connect.data.SchemaAndValue;
+import org.apache.kafka.connect.errors.DataException;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.nio.charset.StandardCharsets;
+import java.util.Arrays;
+import java.util.Collections;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+public class ByteArrayConverterTest {
+    private static final String TOPIC = "topic";
+    private static final byte[] SAMPLE_BYTES = "sample string".getBytes(StandardCharsets.UTF_8);
+
+    private ByteArrayConverter converter = new ByteArrayConverter();
+
+    @Before
+    public void setUp() {
+        converter.configure(Collections.<String, String>emptyMap(), false);
+    }
+
+    @Test
+    public void testFromConnect() {
+        assertArrayEquals(
+                SAMPLE_BYTES,
+                converter.fromConnectData(TOPIC, Schema.BYTES_SCHEMA, SAMPLE_BYTES)
+        );
+    }
+
+    @Test
+    public void testFromConnectSchemaless() {
+        assertArrayEquals(
+                SAMPLE_BYTES,
+                converter.fromConnectData(TOPIC, null, SAMPLE_BYTES)
+        );
+    }
+
+    @Test(expected = DataException.class)
+    public void testFromConnectBadSchema() {
+        converter.fromConnectData(TOPIC, Schema.INT32_SCHEMA, SAMPLE_BYTES);
+    }
+
+    @Test(expected = DataException.class)
+    public void testFromConnectInvalidValue() {
+        converter.fromConnectData(TOPIC, Schema.BYTES_SCHEMA, 12);
+    }
+
+    @Test
+    public void testFromConnectNull() {
+        assertNull(converter.fromConnectData(TOPIC, Schema.BYTES_SCHEMA, null));
+    }
+
+    @Test
+    public void testToConnect() {
+        SchemaAndValue data = converter.toConnectData(TOPIC, SAMPLE_BYTES);
+        assertEquals(Schema.OPTIONAL_BYTES_SCHEMA, data.schema());
+        assertTrue(Arrays.equals(SAMPLE_BYTES, (byte[]) data.value()));
+    }
+
+    @Test
+    public void testToConnectNull() {
+        SchemaAndValue data = converter.toConnectData(TOPIC, null);
+        assertEquals(Schema.OPTIONAL_BYTES_SCHEMA, data.schema());
+        assertNull(data.value());
+    }
+}


Mime
View raw message