Search in sources :

Example 1 with ValueSerializer

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;
}
Also used : HttpURLConnection(java.net.HttpURLConnection) ValueSerializer(cz.o2.proxima.scheme.ValueSerializer)

Example 2 with ValueSerializer

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;
            }
        }
    };
}
Also used : SerializationException(cz.o2.proxima.scheme.SerializationException) Schema(org.apache.avro.Schema) IOException(java.io.IOException) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) SerializationException(cz.o2.proxima.scheme.SerializationException) SpecificRecord(org.apache.avro.specific.SpecificRecord) ValueSerializer(cz.o2.proxima.scheme.ValueSerializer)

Example 3 with ValueSerializer

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())));
}
Also used : Event(cz.o2.proxima.scheme.avro.test.Event) URI(java.net.URI) Test(org.junit.Test)

Example 4 with ValueSerializer

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());
}
Also used : Event(cz.o2.proxima.scheme.avro.test.Event) URI(java.net.URI) Test(org.junit.Test)

Example 5 with ValueSerializer

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());
}
Also used : ValueSerializer(cz.o2.proxima.scheme.ValueSerializer) URI(java.net.URI) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)10 URI (java.net.URI)9 ValueSerializer (cz.o2.proxima.scheme.ValueSerializer)8 Event (cz.o2.proxima.scheme.proto.test.Scheme.Event)5 ByteString (com.google.protobuf.ByteString)4 Event (cz.o2.proxima.scheme.avro.test.Event)3 SerializationException (cz.o2.proxima.scheme.SerializationException)2 IOException (java.io.IOException)2 Method (java.lang.reflect.Method)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 AbstractMessage (com.google.protobuf.AbstractMessage)1 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 JsonFormat (com.google.protobuf.util.JsonFormat)1 OnlineAttributeWriter (cz.o2.proxima.direct.core.OnlineAttributeWriter)1 EntityDescriptor (cz.o2.proxima.repository.EntityDescriptor)1 Repository (cz.o2.proxima.repository.Repository)1 StructureValue (cz.o2.proxima.scheme.AttributeValueAccessors.StructureValue)1 TransactionProtoSerializer (cz.o2.proxima.scheme.proto.ProtoSerializerFactory.TransactionProtoSerializer)1 StreamElement (cz.o2.proxima.storage.StreamElement)1 Builder (cz.o2.proxima.storage.proto.Serialization.JsonElement.Builder)1