-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#8 initial naive approach to transformation
- Loading branch information
1 parent
f7fcc8f
commit 61d14c6
Showing
11 changed files
with
208 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package io.amient.kafka.hadoop.api; | ||
|
||
import io.amient.kafka.hadoop.io.MsgMetadataWritable; | ||
import org.apache.hadoop.io.BytesWritable; | ||
|
||
import java.io.IOException; | ||
|
||
public interface Extractor { | ||
|
||
/** | ||
* TODO this could be used directly with configured Deserializer kafkaDeserializer; | ||
* @param key metadata associated with the message | ||
* @param value message payload byte buffer | ||
* @return deserialized object that can be processed for extraction of values | ||
* @throws IOException | ||
*/ | ||
Object deserialize(MsgMetadataWritable key, BytesWritable value) throws IOException; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/main/java/io/amient/kafka/hadoop/api/Transformation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright 2014 Michal Harish, [email protected] | ||
* | ||
* 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 io.amient.kafka.hadoop.api; | ||
|
||
import org.apache.hadoop.io.BytesWritable; | ||
|
||
import java.io.IOException; | ||
|
||
public interface Transformation { | ||
|
||
//TODO #8 besides transformation make also the OUTVAL generic | ||
BytesWritable transform(Object any) throws IOException; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
src/test/java/io/amient/kafka/hadoop/TransformationSystemTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package io.amient.kafka.hadoop; | ||
|
||
import io.amient.kafka.hadoop.api.TimestampExtractor; | ||
import io.amient.kafka.hadoop.api.Transformation; | ||
import io.amient.kafka.hadoop.io.KafkaInputFormat; | ||
import io.amient.kafka.hadoop.io.MsgMetadataWritable; | ||
import io.amient.kafka.hadoop.io.MultiOutputFormat; | ||
import io.amient.kafka.hadoop.testutils.SystemTestBase; | ||
import kafka.producer.KeyedMessage; | ||
import org.apache.hadoop.fs.Path; | ||
import org.apache.hadoop.io.BytesWritable; | ||
import org.codehaus.jackson.JsonNode; | ||
import org.codehaus.jackson.map.ObjectMapper; | ||
import org.codehaus.jackson.node.ObjectNode; | ||
import org.junit.Test; | ||
|
||
import java.io.IOException; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
public class TransformationSystemTest extends SystemTestBase { | ||
|
||
|
||
static public class TSVToJson implements Transformation, TimestampExtractor { | ||
private ObjectMapper mapper = new ObjectMapper(); | ||
|
||
@Override | ||
public Object deserialize(MsgMetadataWritable key, BytesWritable value) throws IOException { | ||
String[] tsv = new String(value.getBytes(), 0, value.getLength(), "UTF-8").split("\t"); | ||
ObjectNode json = mapper.createObjectNode(); | ||
json.put("category", tsv[0]); | ||
json.put("quantity", Double.parseDouble(tsv[1])); | ||
json.put("timestamp", Long.parseLong(tsv[2])); | ||
return json; | ||
} | ||
|
||
@Override | ||
public Long extractTimestamp(Object any) throws IOException { | ||
return ((JsonNode) any).get("timestamp").getLongValue(); | ||
} | ||
|
||
@Override | ||
public BytesWritable transform(Object any) throws IOException { | ||
JsonNode json = (JsonNode) any; | ||
return new BytesWritable(mapper.writeValueAsBytes(json)); | ||
} | ||
|
||
} | ||
|
||
/** | ||
* this test uses a custom schema transformation. the tsv file has | ||
* a `known` field mapping provided by the transformation implementation. | ||
* | ||
* @throws Exception | ||
*/ | ||
@Test | ||
public void canTransformTSVInputToJSON() throws Exception { | ||
//produce text data | ||
simpleProducer.send(new KeyedMessage<>("topic01", "key1", "A\t1.0\t1473246194481")); | ||
simpleProducer.send(new KeyedMessage<>("topic01", "key2", "B\t1.5\t1473246214844")); | ||
simpleProducer.send(new KeyedMessage<>("topic01", "key1", "C\t2.0\t1473246220528")); | ||
|
||
//configure inputs, timestamp extractor and the output path format | ||
KafkaInputFormat.configureKafkaTopics(conf, "topic01"); | ||
KafkaInputFormat.configureZkConnection(conf, zkConnect); | ||
HadoopJobMapper.configureExtractor(conf, TSVToJson.class); | ||
MultiOutputFormat.configurePathFormat(conf, "'{T}'"); | ||
|
||
//run the first job | ||
Path outDir = runSimpleJob("topic01", "canTransformTSVInputToJSON"); | ||
|
||
Path output1 = new Path(outDir, "topic01/topic01-0-0000000000000000000"); | ||
assertTrue(localFileSystem.exists(output1)); | ||
String json1 = readFullyAsString(output1, 1000); | ||
assertEquals("{\"category\":\"A\",\"quantity\":1.0,\"timestamp\":1473246194481}\n" + | ||
"{\"category\":\"C\",\"quantity\":2.0,\"timestamp\":1473246220528}\n", json1); | ||
|
||
Path output2 = new Path(outDir, "topic01/topic01-1-0000000000000000000"); | ||
assertTrue(localFileSystem.exists(output2)); | ||
String json2 = readFullyAsString(output2, 1000); | ||
assertEquals("{\"category\":\"B\",\"quantity\":1.5,\"timestamp\":1473246214844}\n", json2); | ||
|
||
} | ||
|
||
// /** | ||
// * this test uses a custom schema transformation. the tsv file has | ||
// * a pre-defined field mapping provided by the | ||
// * @throws Exception | ||
// */ | ||
// @Test | ||
// public void canTransfromJsonInputToParquet() throws Exception { | ||
// //TODO | ||
// } | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters