Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* Copyright (C) 2016 Hurence ([email protected])
*
* Licensed 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.
*/
/*
Copyright 2016 Hurence
Licensed 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 com.hurence.logisland.serializer;
import com.hurence.logisland.record.*;
import org.apache.avro.JsonProperties;
import org.apache.avro.Schema;
import org.apache.avro.generic.GenericData;
import org.apache.avro.generic.GenericDatumReader;
import org.apache.avro.generic.GenericDatumWriter;
import org.apache.avro.generic.GenericRecord;
import org.apache.avro.io.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class AvroSerializer implements RecordSerializer {
private final Schema schema;
private Logger logger = LoggerFactory.getLogger(AvroSerializer.class);
public AvroSerializer(final Schema schema) {
this.schema = schema;
}
public AvroSerializer(final String strSchema) {
final Schema.Parser parser = new Schema.Parser();
try {
schema = parser.parse(strSchema);
} catch (Exception e) {
throw new RecordSerializationException("unable to create serializer", e);
}
}
public AvroSerializer(final InputStream inputStream) {
assert inputStream != null;
final Schema.Parser parser = new Schema.Parser();
try {
schema = parser.parse(inputStream);
} catch (IOException e) {
throw new RecordSerializationException("unable to create serializer", e);
}
}
protected static final byte MAGIC_BYTE = 0x0;
protected static final int idSize = 4;
@Override
public void serialize(final OutputStream out, final Record record) throws RecordSerializationException {
try {
/**
* convert the logIsland Event to an Avro GenericRecord
*/
GenericRecord eventRecord = new GenericData.Record(schema);
for (Map.Entry entry : record.getFieldsEntrySet()) {
// retrieve event field
String key = entry.getKey();
Field field = entry.getValue();
Object value = field.getRawValue();
// dump event field as record attribute
eventRecord.put(key, value);
}
/**
*
*/
DatumWriter datumWriter = new GenericDatumWriter<>(schema);
Encoder encoder = EncoderFactory.get().binaryEncoder(out, null);
datumWriter.write(eventRecord, encoder);
encoder.flush();
out.flush();
} catch (IOException | RuntimeException e) {
// avro serialization can throw AvroRuntimeException, NullPointerException,
// ClassCastException, etc
throw new RecordSerializationException("Error serializing Avro message", e);
}
}
public static List