use of cz.o2.proxima.scheme.ValueSerializer in project proxima-platform by O2-Czech-Republic.
the class OpenTsdbConnectionFactory method openConnection.
@Override
public HttpURLConnection openConnection(URI base, StreamElement elem) throws IOException {
if (!elem.getParsed().isPresent()) {
return null;
}
HttpURLConnection conn = createConnection(base);
@SuppressWarnings("unchecked") ValueSerializer valueSerializer = elem.getAttributeDescriptor().getValueSerializer();
@SuppressWarnings("unchecked") String data = "{\"metric\": \"" + elem.getKey() + "\"," + "\"timestamp\": " + elem.getStamp() + "," + "\"value\": " + valueSerializer.asJsonValue(elem.getParsed().get()) + "," + "\"tags\": {\"entity\": \"" + elem.getEntityDescriptor().getName() + "\"," + "\"attribute\": \"" + elem.getAttribute() + "\"}" + "}";
conn.setDoOutput(true);
conn.getOutputStream().write(data.getBytes(StandardCharsets.UTF_8));
return conn;
}
use of cz.o2.proxima.scheme.ValueSerializer in project proxima-platform by O2-Czech-Republic.
the class AvroSerializerFactory method createSerializer.
private static <M extends SpecificRecord> ValueSerializer<M> createSerializer(URI uri) {
return new ValueSerializer<M>() {
private static final long serialVersionUID = 1L;
final String avroClassName = uri.getSchemeSpecificPart();
transient M defaultInstance = null;
transient AvroSerializer<M> avroSerializer = null;
@Override
public Optional<M> deserialize(byte[] input) {
if (avroSerializer == null) {
avroSerializer = new AvroSerializer<>(getAvroSchemaForClass(avroClassName));
}
try {
return Optional.of(avroSerializer.deserialize(input));
} catch (IOException ex) {
log.warn("Unable to deserialize avro payload", ex);
return Optional.empty();
}
}
@Override
public byte[] serialize(M value) {
if (avroSerializer == null) {
avroSerializer = new AvroSerializer<>(getAvroSchemaForClass(avroClassName));
}
try {
return avroSerializer.serialize(value);
} catch (IOException ex) {
throw new SerializationException("Unable to serialize avro object", ex);
}
}
@SuppressWarnings("unchecked")
@Override
public M getDefault() {
if (defaultInstance == null) {
defaultInstance = Classpath.newInstance((Class<M>) Classpath.findClass(avroClassName, SpecificRecord.class));
}
return defaultInstance;
}
private Schema getAvroSchemaForClass(String avroClassName) {
try {
Class<? extends SpecificRecord> avroClass = Classpath.findClass(avroClassName, SpecificRecord.class);
Method method = avroClass.getMethod("getSchema");
return (Schema) method.invoke(Classpath.newInstance(avroClass));
} catch (IllegalAccessException | IllegalArgumentException | SecurityException | InvocationTargetException | NoSuchMethodException ex) {
throw new IllegalArgumentException("Cannot get schema from class " + avroClassName, ex);
}
}
@Override
public boolean isUsable() {
try {
return deserialize(serialize(getDefault())).isPresent();
} catch (Exception ex) {
log.warn("Exception during (de)serialization of default value for " + "class {}. Please consider making all fields optional, otherwise " + "you might encounter unexpected behavior.", avroClassName, ex);
}
try {
return getDefault() != null;
} catch (Exception ex) {
log.warn("Error getting default value for {}", avroClassName, ex);
return false;
}
}
};
}
use of cz.o2.proxima.scheme.ValueSerializer in project proxima-platform by O2-Czech-Republic.
the class AvroSerializerFactoryTest method testEqualsAfterSerializeAndDeserialize.
@Test
public void testEqualsAfterSerializeAndDeserialize() throws URISyntaxException {
Event event = Event.newBuilder().setGatewayId("gateway").setPayload(ByteBuffer.wrap("my-payload".getBytes())).build();
ValueSerializer<Event> serializer = factory.getValueSerializer(new URI("avro:" + event.getClass().getName()));
byte[] bytes = serializer.serialize(event);
Optional<Event> deserialized = serializer.deserialize(bytes);
assertTrue(deserialized.isPresent());
assertEquals(event, deserialized.get());
assertEquals(Event.class.getName(), factory.getClassName(new URI("avro:" + Event.class.getName())));
}
use of cz.o2.proxima.scheme.ValueSerializer in project proxima-platform by O2-Czech-Republic.
the class AvroSerializerFactoryTest method testIsUsable.
@Test
public void testIsUsable() throws URISyntaxException {
ValueSerializer<Event> serializer = factory.getValueSerializer(new URI("avro:" + Event.class.getName()));
assertTrue(serializer.isUsable());
}
use of cz.o2.proxima.scheme.ValueSerializer in project proxima-platform by O2-Czech-Republic.
the class SchemaRegistrySerializerFactoryTest method testWithInvalidUri.
@Test
public void testWithInvalidUri() throws Exception {
ValueSerializer s = factory.getValueSerializer(new URI("schema-registry:not-valid"));
assertFalse(s.isUsable());
}
Aggregations