This repository has been archived by the owner on Dec 5, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use deserialisation scheme for messages
- Loading branch information
Showing
4 changed files
with
62 additions
and
11 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
46 changes: 46 additions & 0 deletions
46
src/main/java/uk/co/samstokes/storm/scheme/JSONScheme.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,46 @@ | ||
package uk.co.samstokes.storm.scheme; | ||
|
||
import java.io.UnsupportedEncodingException; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import org.json.simple.JSONValue; | ||
|
||
import backtype.storm.spout.Scheme; | ||
|
||
import backtype.storm.tuple.Fields; | ||
|
||
|
||
public class JSONScheme implements Scheme { | ||
private static final long serialVersionUID = -7734176307841199017L; | ||
|
||
private final String encoding; | ||
|
||
|
||
public JSONScheme(String encoding) { | ||
this.encoding = encoding; | ||
} | ||
public JSONScheme() { | ||
this("UTF-8"); | ||
} | ||
|
||
|
||
@Override | ||
public List<Object> deserialize(byte[] bytes) { | ||
final String chars; | ||
try { | ||
chars = new String(bytes, encoding); | ||
} catch (UnsupportedEncodingException e) { | ||
throw new RuntimeException(e); | ||
} | ||
final Object json = JSONValue.parse(chars); | ||
return Collections.singletonList(json); | ||
} | ||
|
||
|
||
@Override | ||
public Fields getOutputFields() { | ||
return new Fields("object"); | ||
} | ||
} |
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