Beispiel #1
0
        public ChoAvroReader(Stream inStream, ChoAvroRecordConfiguration configuration = null)
        {
            ChoGuard.ArgumentNotNull(inStream, "Stream");

            Configuration = configuration;
            Init();

            if (inStream is MemoryStream)
            {
                _sr = new Lazy <StreamReader>(() => new StreamReader(inStream));
            }
            else
            {
                _sr = new Lazy <StreamReader>(() =>
                {
                    if (Configuration.DetectEncodingFromByteOrderMarks == null)
                    {
                        return(new StreamReader(inStream, Configuration.GetEncoding(inStream), false, Configuration.BufferSize));
                    }
                    else
                    {
                        return(new StreamReader(inStream, Encoding.Default, Configuration.DetectEncodingFromByteOrderMarks.Value, Configuration.BufferSize));
                    }
                });
            }
            //_closeStreamOnDispose = true;
        }
        internal void Validate(ChoAvroRecordConfiguration config)
        {
            try
            {
                if (FieldName.IsNullOrWhiteSpace())
                {
                    FieldName = Name;
                }

                if (Size != null && Size.Value <= 0)
                {
                    throw new ChoRecordConfigurationException("Size must be > 0.");
                }
                if (ErrorMode == null)
                {
                    ErrorMode = config.ErrorMode; // config.ErrorMode;
                }
                if (IgnoreFieldValueMode == null)
                {
                    IgnoreFieldValueMode = config.IgnoreFieldValueMode;
                }
                if (QuoteField == null)
                {
                    QuoteField = config.QuoteAllFields;
                }
                if (NullValue == null)
                {
                    NullValue = config.NullValue;
                }
            }
            catch (Exception ex)
            {
                throw new ChoRecordConfigurationException("Invalid configuration found at '{0}' field.".FormatString(Name), ex);
            }
        }
Beispiel #3
0
        protected ChoAvroReader(IAvroReader <Dictionary <string, object> > avroReader, ChoAvroRecordConfiguration configuration = null)
        {
            ChoGuard.ArgumentNotNull(avroReader, "AvroReader");

            Configuration = configuration;
            Init();

            _avroReader = avroReader;
        }
Beispiel #4
0
        public ChoAvroReader(IAvroReader <T> avroReader, ChoAvroRecordConfiguration configuration = null)
        {
            ChoGuard.ArgumentNotNull(avroReader, "AvroReader");

            Configuration = configuration;
            Init();

            _avroReader = avroReader;
        }
Beispiel #5
0
        public ChoAvroWriter(StreamWriter streamWriter, ChoAvroRecordConfiguration configuration = null)
        {
            ChoGuard.ArgumentNotNull(streamWriter, "StreamWriter");

            Configuration = configuration;
            Init();

            _streamWriter = new Lazy <StreamWriter>(() => streamWriter);
        }
Beispiel #6
0
        public ChoAvroReader(string filePath, ChoAvroRecordConfiguration configuration = null)
        {
            ChoGuard.ArgumentNotNullOrEmpty(filePath, "FilePath");

            Configuration = configuration;

            Init();

            _sr = new Lazy <StreamReader>(() => new StreamReader(filePath, Configuration.GetEncoding(filePath), false, Configuration.BufferSize));
            _closeStreamOnDispose = true;
        }
Beispiel #7
0
        public ChoAvroWriter(string filePath, ChoAvroRecordConfiguration configuration = null)
        {
            ChoGuard.ArgumentNotNullOrEmpty(filePath, "FilePath");

            Configuration = configuration;

            Init();

            _streamWriter         = new Lazy <StreamWriter>(() => new StreamWriter(filePath, Configuration.Append, Configuration.Encoding, Configuration.BufferSize));
            _closeStreamOnDispose = true;
        }
Beispiel #8
0
        public ChoAvroRecordReader(Type recordType, ChoAvroRecordConfiguration configuration) : base(recordType, false)
        {
            ChoGuard.ArgumentNotNull(configuration, "Configuration");
            Configuration = configuration;

            _callbackRecordFieldRead     = ChoMetadataObjectCache.CreateMetadataObject <IChoNotifyRecordFieldRead>(recordType);
            _callbackFileRead            = ChoMetadataObjectCache.CreateMetadataObject <IChoNotifyFileRead>(recordType);
            _callbackRecordRead          = ChoMetadataObjectCache.CreateMetadataObject <IChoNotifyRecordRead>(recordType);
            _callbackRecordSeriablizable = ChoMetadataObjectCache.CreateMetadataObject <IChoRecordFieldSerializable>(recordType);
            System.Threading.Thread.CurrentThread.CurrentCulture = Configuration.Culture;
        }
Beispiel #9
0
        private void Init()
        {
            var recordType = typeof(T).GetUnderlyingType();

            if (Configuration == null)
            {
                Configuration = new ChoAvroRecordConfiguration(recordType);
            }

            _writer              = new ChoAvroRecordWriter(recordType, Configuration);
            _writer.RowsWritten += NotifyRowsWritten;
        }
Beispiel #10
0
        public ChoAvroWriter(Stream inStream, ChoAvroRecordConfiguration configuration = null)
        {
            ChoGuard.ArgumentNotNull(inStream, "Stream");

            Configuration = configuration;
            Init();

            if (inStream is MemoryStream)
            {
                _streamWriter = new Lazy <StreamWriter>(() => new StreamWriter(inStream));
            }
            else
            {
                _streamWriter = new Lazy <StreamWriter>(() => new StreamWriter(inStream, Configuration.Encoding, Configuration.BufferSize));
            }
            //_closeStreamOnDispose = true;
        }
Beispiel #11
0
        public ChoAvroRecordWriter(Type recordType, ChoAvroRecordConfiguration configuration) : base(recordType)
        {
            ChoGuard.ArgumentNotNull(configuration, "Configuration");
            Configuration = configuration;

            _callbackFileHeaderArrange = ChoMetadataObjectCache.CreateMetadataObject <IChoNotifyFileHeaderArrange>(recordType);
            _callbackRecordWrite       = ChoMetadataObjectCache.CreateMetadataObject <IChoNotifyRecordWrite>(recordType);
            _callbackFileWrite         = ChoMetadataObjectCache.CreateMetadataObject <IChoNotifyFileWrite>(recordType);
            _callbackRecordFieldWrite  = ChoMetadataObjectCache.CreateMetadataObject <IChoNotifyRecordFieldWrite>(recordType);
            System.Threading.Thread.CurrentThread.CurrentCulture = Configuration.Culture;

            _recBuffer = new Lazy <List <object> >(() =>
            {
                if (Writer != null)
                {
                    var b = Writer.Context.ContainsKey("RecBuffer") ? Writer.Context.RecBuffer : null;
                    if (b == null)
                    {
                        Writer.Context.RecBuffer = new List <object>();
                    }

                    return(Writer.Context.RecBuffer);
                }
                else
                {
                    return(new List <object>());
                }
            }, true);

            BeginWrite = new Lazy <bool>(() =>
            {
                if (_sw != null)
                {
                    return(RaiseBeginWrite(_sw));
                }
                if (_avroWriter != null)
                {
                    return(RaiseBeginWrite(_avroWriter));
                }

                return(false);
            });
            //Configuration.Validate();
        }
Beispiel #12
0
        private void Init()
        {
            _enumerator = new Lazy <IEnumerator <T> >(() => GetEnumerator());

            var recordType = typeof(T).GetUnderlyingType();

            if (Configuration == null)
            {
                Configuration = new ChoAvroRecordConfiguration(recordType);
            }
            else
            {
                Configuration.RecordType = recordType;
            }
            Configuration.IsDynamicObject = Configuration.RecordType.IsDynamicType();

            if (!ChoETLFrxBootstrap.IsSandboxEnvironment)
            {
                _prevCultureInfo = System.Threading.Thread.CurrentThread.CurrentCulture;
                System.Threading.Thread.CurrentThread.CurrentCulture = Configuration.Culture;
            }
        }
Beispiel #13
0
 public ChoAvroReader(ChoAvroRecordConfiguration configuration = null)
 {
     Configuration = configuration;
     Init();
 }
Beispiel #14
0
        static void SerializeAndDeserializeDynamicTest()
        {
            string path = "AvroSampleReflection.avro";
            //SerializeDynamicSampleFile(path);

            var dict = new Dictionary <string, object>();

            dict.Add("1", 3);
            dict.Add("2", new Location {
                Room = 243, Floor = 1
            });

            ChoAvroRecordConfiguration config = null;
            AvroSerializerSettings     sett1  = null;

            using (var w = new ChoAvroWriter(path)
                           .WithAvroSerializer(AvroSerializer.Create <Dictionary <string, object> >(new AvroSerializerSettings()
            {
                Resolver = new ChoAvroPublicMemberContractResolver()
            }))
                           .Configure(c => c.KnownTypes = new List <Type> {
                typeof(Location), typeof(string), typeof(int)
            })
                           //.Configure(c => c.UseAvroSerializer = true)
                           //.Configure(c => c.AvroSerializerSettings.Resolver = new AvroDataContractResolverEx())
                   )
            {
                sett1  = w.Configuration.AvroSerializerSettings;
                config = w.Configuration;

                w.Write(dict);
                w.Write(dict);
                w.Write(dict);
            }
            //var sett = new AvroSerializerSettings();
            //sett.Resolver = new ChoAvroPublicMemberContractResolver(); // false) { Configuration = config };
            //sett.KnownTypes = new List<Type> { typeof(Location), typeof(string), typeof(int) };
            //var avroSerializer = AvroSerializer.Create<Dictionary<string, object>>(sett1);
            //using (var r = new StreamReader(path))
            //{
            //    var rec = avroSerializer.Deserialize(r.BaseStream);
            //    var rec2 = avroSerializer.Deserialize(r.BaseStream);
            //    var rec3 = avroSerializer.Deserialize(r.BaseStream);
            //    Console.WriteLine(rec.Dump());
            //    Console.WriteLine(rec2.Dump());
            //    Console.WriteLine(rec3.Dump());
            //    //var rec4 = avroSerializer.Deserialize(r);
            //}

            StringBuilder json = new StringBuilder();

            using (var r = new ChoAvroReader(path)
                           .Configure(c => c.KnownTypes = new List <Type> {
                typeof(Location), typeof(string), typeof(int)
            })
                           .Configure(c => c.UseAvroSerializer = true)
                           //.Configure(c => c.AvroSerializerSettings = sett1)
                           .Configure(c => c.NestedColumnSeparator = '_')
                   )
            {
                //var dt = r.AsDataTable();
                //Console.WriteLine(dt.Dump());
                //return;
                //foreach (var rec in r)
                //{
                //    Console.WriteLine(rec.Dump());
                //}
                //return;
                using (var w = new ChoJSONWriter(json)
                               .Configure(c => c.TurnOnAutoDiscoverJsonConverters = true)
                       )
                {
                    w.Write(r);
                }
            }
            Console.WriteLine(json.ToString());
        }