Esempio n. 1
0
        public StreamWriter(Stream stream, bool leaveOpen, IAvroSerializer <T> serializer, Codec codec)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            if (serializer == null)
            {
                throw new ArgumentNullException("serializer");
            }

            if (codec == null)
            {
                throw new ArgumentNullException("codec");
            }

            this.codec           = codec;
            this.serializer      = serializer;
            this.resultStream    = stream;
            this.encoder         = new BinaryEncoder(this.resultStream, leaveOpen);
            this.isHeaderWritten = false;
            this.locker          = new object();

            var syncMarker = new byte[16];

            new Random().NextBytes(syncMarker);
            this.header = new ObjectContainerHeader(syncMarker)
            {
                CodecName = this.codec.Name, Schema = this.serializer.WriterSchema.ToString()
            };
        }
        /// <summary>
        /// Retrieve Avro Hearder from a stream
        /// </summary>
        /// <param name="avroStream">The stream that contains the avro header</param>
        /// <returns>Avro Header Object</returns>
        public static ObjectContainerHeader GetAvroHeader <TSchema>(Stream avroStream)
        {
            ObjectContainerHeader avroHeader = null;

            avroStream.Seek(0, SeekOrigin.Begin);
            using (var decoder = new BinaryDecoder(avroStream, true))
            {
                avroHeader = ObjectContainerHeader.Read(decoder);
            }
            avroStream.Dispose();
            return(avroHeader);
        }
        /// <summary>
        /// Creates Avro Hearder Stream without zero block.
        /// </summary>
        /// <param name="dataList">the list of input data</param>
        /// <param name="schema">The writer schema.</param>
        /// <param name="settings">The serializer settings.</param>
        /// <param name="codec">The codec.</param>
        /// <returns> A writer.</returns>
        /// <exception cref="System.ArgumentNullException">Thrown when any argument is null.</exception>
        /// <returns>The first Tuple element is the stream with header, the second tuple element is the avro header</returns>
        public static Tuple <Stream, ObjectContainerHeader> CreateAvroHeaderStream <TSchema>(TSchema t, string schema, AvroSerializerSettings settings, Codec codec, int syncNumberOfObjects)
        {
            ObjectContainerHeader avroHeader = null;
            var stream = new MemoryStream();

            using (var avroWriter = (StreamWriter <TSchema>)AvroContainer.CreateWriter <TSchema>(stream, true, settings, codec, false, null))
            {
                avroWriter.WriteHeader();
                avroHeader = avroWriter.Header;
            }
            stream.Seek(0, SeekOrigin.Begin);
            return(new Tuple <Stream, ObjectContainerHeader>(stream, avroHeader));
        }
        /// <summary>
        /// Creates Avro Blocks stream.
        /// </summary>
        /// <param name="dataList">the list of input data</param>
        /// <param name="schema">The writer schema.</param>
        /// <param name="settings">The serializer settings.</param>
        /// <param name="codec">The codec.</param>
        /// <returns> A writer.</returns>
        /// <exception cref="System.ArgumentNullException">Thrown when any argument is null.</exception>
        /// <returns>The first Tuple element is the stream with header and blocks, the second tuple element is the avro header</returns>
        public static Tuple <Stream, ObjectContainerHeader> CreateAvroHeaderAndBlockStream <TSchema>(List <TSchema> dataList, string schema, AvroSerializerSettings settings, Codec codec, int syncNumberOfObjects)
        {
            ObjectContainerHeader avroHeader = null;
            var stream = new MemoryStream();

            using (var avroWriter = (StreamWriter <TSchema>)AvroContainer.CreateWriter <TSchema>(stream, true, settings, codec, false, null))
            {
                avroHeader = avroWriter.Header;

                using (var sequentialWriter = new SequentialWriter <TSchema>(avroWriter, syncNumberOfObjects))
                {
                    dataList.ToList().ForEach(sequentialWriter.Write);
                }
            }
            stream.Seek(0, SeekOrigin.Begin);
            return(new Tuple <Stream, ObjectContainerHeader>(stream, avroHeader));
        }
Esempio n. 5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="StreamReader{T}"/> class for Avro records.
        /// </summary>
        /// <param name="readerSchema">The reader schema.</param>
        /// <param name="stream">The input stream.</param>
        /// <param name="leaveOpen">If set to <c>true</c> leaves the input stream open.</param>
        /// <param name="codecFactory">The codec factory.</param>
        public StreamReader(string readerSchema, Stream stream, bool leaveOpen, CodecFactory codecFactory)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            if (codecFactory == null)
            {
                throw new ArgumentNullException("codecFactory");
            }

            this.stream     = stream;
            this.decoder    = new BinaryDecoder(stream, leaveOpen);
            this.header     = ObjectContainerHeader.Read(this.decoder);
            this.codec      = codecFactory.Create(this.header.CodecName);
            this.serializer = (IAvroSerializer <T>)AvroSerializer.CreateGenericDeserializerOnly(this.header.Schema, readerSchema ?? this.header.Schema);
        }
Esempio n. 6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="StreamReader{T}"/> class for static types.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <param name="leaveOpen">If set to <c>true</c> leaves the input stream open.</param>
        /// <param name="settings">The settings.</param>
        /// <param name="codecFactory">The codec factory.</param>
        public StreamReader(Stream stream, bool leaveOpen, AvroSerializerSettings settings, CodecFactory codecFactory)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }

            if (codecFactory == null)
            {
                throw new ArgumentNullException("codecFactory");
            }

            this.stream     = stream;
            this.decoder    = new BinaryDecoder(stream, leaveOpen);
            this.header     = ObjectContainerHeader.Read(this.decoder);
            this.codec      = codecFactory.Create(this.header.CodecName);
            this.serializer = AvroSerializer.CreateDeserializerOnly <T>(this.header.Schema, settings);
        }