Beispiel #1
0
        public void SchemaDefaultValues_RecordWithInvalidFields()
        {
            const string DefaultSchema =
                @"{""type"":""record"",""name"":""SomeRecord"",""fields"":[{""name"":""Field1"",""type"":""int""}, {""name"":""Field2"", ""type"":""string""}]}";
            var     tempSerializer = AvroSerializer.CreateGeneric(DefaultSchema);
            dynamic defaultValue   = new AvroRecord(tempSerializer.WriterSchema);

            defaultValue.Field1 = Utilities.GetRandom <int>(false);
            defaultValue.Field2 = "some string " + Utilities.GetRandom <int>(false);
            var defaultValueJson = @"{""Field1"":" + defaultValue.Field1 + @", ""Field2"":""" + defaultValue.Field2 +
                                   @""", ""WRONGFIELD"":""WRONGVALUE""}";

            Assert.ThrowsException <SerializationException>(() =>
            {
                RoundTripSerializationWithDefaultsAndCheck <AvroRecord>(
                    DefaultSchema,
                    defaultValue,
                    defaultValueJson,
                    (Action <dynamic, dynamic>)((expected, actual) =>
                {
                    Assert.AreEqual(expected.Field1, actual.Field1);
                    Assert.AreEqual(expected.Field2, actual.Field2);
                }));
            });
        }
        public override IEnumerable <IRow> Extract(IUnstructuredReader input, IUpdatableRow output)
        {
            if (input.Length == 0)
            {
                yield break;
            }

            var serializer = AvroSerializer.CreateGeneric(avroSchema);

            using (var genericReader = AvroContainer.CreateGenericReader(input.BaseStream))
            {
                using (var reader = new SequentialReader <dynamic>(genericReader))
                {
                    foreach (var obj in reader.Objects)
                    {
                        foreach (var column in output.Schema)
                        {
                            output.Set(column.Name, obj[column.Name]);
                        }

                        yield return(output.AsReadOnly());
                    }
                }
            }
        }
Beispiel #3
0
        public void SchemaDefaultValues_RecordWithIntFieldHavingDefaultValue()
        {
            const string WriterSchema = @"{
                                              ""type"":""record"",
                                              ""name"":""Microsoft.Hadoop.Avro.Tests.SimpleIntClass"",
                                              ""fields"":
                                              [
                                                  {
                                                      ""name"":""IntField"",
                                                      ""type"":""int"",
                                                      ""default"": 0
                                                  }
                                              ]
                                          }";

            var serializer   = AvroSerializer.CreateGeneric(WriterSchema);
            var deserializer = AvroSerializer.CreateGenericDeserializerOnly(WriterSchema, WriterSchema);

            using (var stream = new MemoryStream())
            {
                dynamic expected = new AvroRecord(serializer.ReaderSchema);
                expected.IntField = 1;

                serializer.Serialize(stream, expected);
                stream.Seek(0, SeekOrigin.Begin);

                dynamic actual = deserializer.Deserialize(stream);
                Assert.Equal(expected.IntField, actual.IntField);
            }
        }
Beispiel #4
0
        public void AvroRecord_CreateUnionOfAvroRecordAndNull()
        {
            const string Schema =
                @"[
                             ""null"",
                             {
                                ""type"":""record"",
                                ""name"":""Microsoft.Hadoop.Avro.Tests.SimpleIntClass"",
                                ""fields"":
                                        [
                                            { ""name"":""IntField"", ""type"":""int"" },
                                        ]
                             }
                        ]";

            var serializer  = AvroSerializer.CreateGeneric(Schema);
            var unionSchema = (UnionSchema)serializer.WriterSchema;
            var expected    = new AvroRecord(unionSchema.Schemas[1]);

            expected["IntField"] = Utilities.GetRandom <int>(false);

            serializer.Serialize(this.stream, null);
            serializer.Serialize(this.stream, expected);

            this.stream.Seek(0, SeekOrigin.Begin);
            var actual1 = serializer.Deserialize(this.stream);

            Assert.Null(actual1);

            var actual2 = (AvroRecord)serializer.Deserialize(this.stream);

            Assert.NotNull(actual2);
            Assert.Equal(expected["IntField"], actual2["IntField"]);
        }
        public void GenericSerializer_SerializeRecordWithUnion()
        {
            const string StringSchema = @"{
                 ""name"":""Category"",
                 ""namespace"":""ApacheAvro.Types"",
                 ""type"":""record"",
                 ""fields"":
                           [
                                {""name"":""CategoryName"", ""type"":""string""},
                                {""name"":""Description"", ""type"":[""string"",""null""]},
                                {""name"":""Picture"", ""type"":[""bytes"", ""null""]},
                                {""name"":""Id"", ""type"":[""int"", ""null""]}
                           ]
             }";

            var serializer = AvroSerializer.CreateGeneric(StringSchema);

            using (var stream = new MemoryStream())
            {
                dynamic expected = new AvroRecord(serializer.WriterSchema);
                expected.CategoryName = "Test";
                expected.Description  = "Test";
                expected.Picture      = null;
                expected.Id           = 1;

                serializer.Serialize(stream, expected);
                stream.Seek(0, SeekOrigin.Begin);

                var actual = serializer.Deserialize(stream) as AvroRecord;
                Assert.IsTrue(ShallowlyEqual(expected, actual));
            }
        }
        public void GenericSerializer_SerializeUsingDifferentReaderWriterType()
        {
            const string StringSchema = @"{
                             ""type"":""record"",
                             ""name"":""Microsoft.Hadoop.Avro.Tests.SimpleIntClass"",
                             ""fields"":
                                       [
                                           {
                                               ""name"":""IntField"",
                                               ""type"":""int""
                                           }
                                       ]
                          }";
            var          serializer   = AvroSerializer.CreateGeneric(StringSchema);
            var          deserializer = AvroSerializer.CreateGenericDeserializerOnly(StringSchema, StringSchema);

            using (var stream = new MemoryStream())
            {
                dynamic expected = new AvroRecord(serializer.WriterSchema);
                expected.IntField = 5;

                serializer.Serialize(stream, expected);
                stream.Position = 0;

                var actual = deserializer.Deserialize(stream) as AvroRecord;
                Assert.IsTrue(expected["IntField"].Equals(actual["IntField"]));
            }
        }
        public void GenericSerializer_SerializeEvolvedRecordWithPromotedField()
        {
            const string WriterSchema = @"{
                             ""type"":""record"",
                             ""name"":""Microsoft.Hadoop.Avro.Tests.SimpleIntClass"",
                             ""fields"":
                                       [
                                           {""name"":""IntField"", ""type"":""int""}
                                       ]
                          }";

            const string ReaderSchema = @"{
                            ""type"":""record"",
                            ""name"":""Microsoft.Hadoop.Avro.Tests.SimpleIntClass"",
                            ""fields"":
                                    [
                                        {""name"":""IntField"",""type"":""long""}
                                    ]
                        }";

            var serializer   = AvroSerializer.CreateGeneric(WriterSchema);
            var deserializer = AvroSerializer.CreateGenericDeserializerOnly(WriterSchema, ReaderSchema);

            using (var stream = new MemoryStream())
            {
                dynamic expected = new AvroRecord(serializer.ReaderSchema);
                expected.IntField = 1;

                serializer.Serialize(stream, expected);
                stream.Seek(0, SeekOrigin.Begin);

                dynamic actual = deserializer.Deserialize(stream);
                Assert.AreEqual(expected.IntField, actual.IntField);
            }
        }
        public void GenericSerializer_SerializeEnum()
        {
            const string Schema = "{" +
                                  "\"type\":\"enum\"," +
                                  "\"name\":\"Microsoft.Hadoop.Avro.Tests.TestEnum\"," +
                                  "\"symbols\":" +
                                  "[" +
                                  "\"EnumValue3\"," +
                                  "\"EnumValue2\"," +
                                  "\"EnumValue1\"" +
                                  "]}";
            var serializer = AvroSerializer.CreateGeneric(Schema);

            using (var stream = new MemoryStream())
            {
                dynamic expected = new AvroEnum(serializer.WriterSchema);
                expected.IntegerValue = 0;

                serializer.Serialize(stream, expected);
                stream.Seek(0, SeekOrigin.Begin);

                dynamic actual = serializer.Deserialize(stream);
                Assert.AreEqual(expected.IntegerValue, actual.IntegerValue);
                Assert.AreEqual(expected.Value, actual.Value);
            }
        }
        public void ReadAvroAsError()
        {
            const string Schema = @"{
""type"":""record"",
""name"":""OData.Error"",
""fields"":
    [
        { ""name"":""ErrorCode"", ""type"":""string"" },
        { ""name"":""Message""  , ""type"":""string"" },

    ]
}";
            var          stream = new MemoryStream();

            using (var writer = AvroContainer.CreateGenericWriter(Schema, stream, /*leave open*/ true, Codec.Null))
                using (var seqWriter = new SequentialWriter <object>(writer, 24))
                {
                    RecordSchema parameterSchema = (RecordSchema)AvroSerializer.CreateGeneric(Schema).WriterSchema;
                    AvroRecord   ar = new AvroRecord(parameterSchema);
                    ar["ErrorCode"] = "e1";
                    ar["Message"]   = "m1";

                    seqWriter.Write(ar);
                    seqWriter.Flush();
                }

            stream.Flush();
            stream.Seek(0, SeekOrigin.Begin);
            var error = this.CreateODataInputContext(stream).ReadError();

            Assert.AreEqual("e1", error.ErrorCode);
            Assert.AreEqual("m1", error.Message);
        }
        public void GenericSerializer_SerializeUnionWithMapAndNull()
        {
            const string StringSchema = @"{
                             ""type"":""record"",
                             ""name"":""Microsoft.Hadoop.Avro.Tests.SimpleIntClass"",
                             ""fields"":
                                       [
                                           {""name"":""LongMap"", ""type"": [""null"", {""type"":""map"", ""values"":""long""}]},
                                       ]
                          }";
            var          serializer   = AvroSerializer.CreateGeneric(StringSchema);

            using (var stream = new MemoryStream())
            {
                dynamic expected = new AvroRecord(serializer.WriterSchema);
                expected.LongMap = new Dictionary <string, long> {
                    { "test", 1 }
                };

                serializer.Serialize(stream, expected);
                stream.Position = 0;

                dynamic actual = serializer.Deserialize(stream);
                Utilities.DictionaryEquals <string, long>(expected.LongMap as IDictionary <string, long>, actual.LongMap as IDictionary <string, long>);
            }
        }
        public void GenericSerializer_SerializeRecursiveRecord()
        {
            const string StringSchema = @"{
                      ""type"":""record"",
                      ""name"":""Microsoft.Hadoop.Avro.Tests.Recursive"",
                      ""fields"":[
                                     {""name"":""IntField"",""type"":""int""},
                                     {""name"":""RecursiveField"",""type"":[
                                                                               ""null"",
                                                                               ""Microsoft.Hadoop.Avro.Tests.Recursive""
                                                                           ]
                                     }
                                 ]}";

            var serializer = AvroSerializer.CreateGeneric(StringSchema);

            using (var stream = new MemoryStream())
            {
                dynamic expected = new AvroRecord(serializer.WriterSchema);
                expected.IntField                      = 5;
                expected.RecursiveField                = new AvroRecord(((serializer.ReaderSchema as RecordSchema).GetField("RecursiveField").TypeSchema as UnionSchema).Schemas[1]);
                expected.RecursiveField.IntField       = 3;
                expected.RecursiveField.RecursiveField = null;

                serializer.Serialize(stream, expected);
                stream.Position = 0;

                var actual = serializer.Deserialize(stream) as AvroRecord;
                Assert.IsTrue(ShallowlyEqual(expected, actual));
            }
        }
Beispiel #12
0
        static void Main(string[] args)
        {
            var schemaString = "{\"type\": \"record\", " +
                               "\"name\": \"kevent\"," +
                               "\"fields\": [" +
                               "{\"name\": \"timestamp\", \"type\": \"long\"}," +
                               "{\"name\": \"payload\", \"type\": \"bytes\"}" +
                               "]}";
            var avroRecord = new AvroRecord(TypeSchema.Create(schemaString));

            avroRecord["timestamp"] = DateTime.UtcNow.Ticks;
            avroRecord["payload"]   = Enumerable.Range(0, 10).Select(x => (byte)x).ToArray();
            var totalMilliseconds = DateTime.Now.Subtract(new DateTime(1970, 01, 01)).TotalMilliseconds;
            var dateTime          = new DateTime(1970, 01, 01).Add(TimeSpan.FromMilliseconds(1504007964797L));
            var avroSerializer    = AvroSerializer.CreateGeneric(schemaString);

            //byte[] bytes;
            //using (var memoryStream = new MemoryStream())
            //{
            //    avroSerializer.Serialize(memoryStream, avroRecord);
            //    bytes = memoryStream.ToArray();
            //    var base64String = Convert.ToBase64String(bytes);
            //}
            try
            {
                var bytes = Convert.FromBase64String("AAAAACn6oeLdxVcUUy+TAh26uvgGwg==");
                using (var memoryStream = new MemoryStream(bytes))
                {
                    var deserialize = avroSerializer.Deserialize(memoryStream);
                }
            }
            catch (Exception e)
            {
            }
        }
Beispiel #13
0
 public void GenericSerializer_CreateWithNullSchema()
 {
     Assert.Throws <ArgumentNullException>(() =>
     {
         AvroSerializer.CreateGeneric(null);
     }
                                           );
 }
        public void GenericSerializer_SerializeNestedRecordWithReferences()
        {
            const string StringSchema =
                @"{
                  ""name"":""MultiOrderProperties"",
                  ""namespace"":""ApacheAvro.Types"",
                  ""type"":""record"",
                  ""fields"":
                  [
                      {
                          ""name"":""Orders1"",
                          ""type"":
                          {
                              ""name"":""Order"",
                              ""namespace"":""ApacheAvro.Types"",
                              ""type"":""record"",
                              ""fields"":
                              [
                                  {""name"":""CustomerId"", ""type"":""string""},
                                  {""name"":""EmployeeId"", ""type"":""int""},
                              ]
                          }
                      },
                      {""name"":""Orders2"", ""type"":""Order""},
                      {""name"":""Orders3"", ""type"":""Order""},
                      {""name"":""Orders4"", ""type"":""Order""},
                  ]
            }";

            var serializer   = AvroSerializer.CreateGeneric(StringSchema);
            var recordSchema = serializer.ReaderSchema as RecordSchema;

            Assert.IsNotNull(recordSchema);
            using (var stream = new MemoryStream())
            {
                dynamic expected = new AvroRecord(serializer.ReaderSchema);
                expected.Orders1            = new AvroRecord(recordSchema.GetField("Orders1").TypeSchema);
                expected.Orders1.CustomerId = "1";
                expected.Orders1.EmployeeId = 1;
                expected.Orders2            = new AvroRecord(recordSchema.GetField("Orders2").TypeSchema);
                expected.Orders2.CustomerId = "1";
                expected.Orders2.EmployeeId = 2;
                expected.Orders3            = new AvroRecord(recordSchema.GetField("Orders3").TypeSchema);
                expected.Orders3.CustomerId = "1";
                expected.Orders3.EmployeeId = 3;
                expected.Orders4            = new AvroRecord(recordSchema.GetField("Orders4").TypeSchema);
                expected.Orders4.CustomerId = "1";
                expected.Orders4.EmployeeId = 4;

                serializer.Serialize(stream, expected);
                stream.Seek(0, SeekOrigin.Begin);

                var actual = serializer.Deserialize(stream) as AvroRecord;
                Assert.IsTrue(ShallowlyEqual(expected, actual));
            }
        }
        public void SequentialGenericWritingReading_RecursiveRecord()
        {
            const string StringSchema = @"{
                                ""type"":""record"",
                                ""name"":""Microsoft.Hadoop.Avro.Tests.Recursive"",
                                ""fields"":[
                                    {""name"":""IntField"",""type"":""int""},
                                    {""name"":""RecursiveField"",""type"":[
                                                                            ""null"",
                                                                            ""Microsoft.Hadoop.Avro.Tests.Recursive""
                                                                        ]
                                    }
                            ]}";

            using (var stream = new MemoryStream())
            {
                var serializer = AvroSerializer.CreateGeneric(StringSchema);
                using (var streamWriter = AvroContainer.CreateGenericWriter(StringSchema, stream, Codec.Null))
                {
                    using (var writer = new SequentialWriter <object>(streamWriter, 24))
                    {
                        var expected = new List <AvroRecord>();
                        var random   = new Random(93);
                        for (int i = 0; i < 10; i++)
                        {
                            dynamic record = new AvroRecord(serializer.WriterSchema);
                            record.IntField       = random.Next();
                            record.RecursiveField =
                                new AvroRecord(
                                    ((serializer.ReaderSchema as RecordSchema).GetField("RecursiveField").TypeSchema as UnionSchema).Schemas[1]);
                            record.RecursiveField.IntField       = random.Next();
                            record.RecursiveField.RecursiveField = null;
                            expected.Add(record);
                        }

                        expected.ForEach(writer.Write);
                        writer.Flush();

                        stream.Seek(0, SeekOrigin.Begin);

                        var streamReader = AvroContainer.CreateReader <Recursive>(stream, true, this.dataContractSettings, new CodecFactory());
                        using (var reader = new SequentialReader <Recursive>(streamReader))
                        {
                            var j = 0;
                            foreach (var avroRecord in reader.Objects)
                            {
                                Assert.Equal(expected[j]["IntField"], avroRecord.IntField);
                                Assert.Equal(((dynamic)expected[j]["RecursiveField"])["IntField"], avroRecord.RecursiveField.IntField);
                                Assert.Equal(
                                    ((dynamic)expected[j++]["RecursiveField"])["RecursiveField"], avroRecord.RecursiveField.RecursiveField);
                            }
                        }
                    }
                }
            }
        }
Beispiel #16
0
 private static object SerializeRoundTrip(string schema, object obj)
 {
     using (var stream = new MemoryStream())
     {
         var serializer = AvroSerializer.CreateGeneric(schema);
         serializer.Serialize(stream, obj);
         stream.Seek(0, SeekOrigin.Begin);
         return(serializer.Deserialize(stream));
     }
 }
Beispiel #17
0
        public AvroTestKafkaModelDeserializer()
        {
            var schemaString = "{\"type\": \"record\", " +
                               "\"name\": \"kevent\"," +
                               "\"fields\": [" +
                               "{\"name\": \"timestamp\", \"type\": \"long\"}," +
                               "{\"name\": \"payload\", \"type\": \"bytes\"}" +
                               "]}";

            avroSerializer = AvroSerializer.CreateGeneric(schemaString);
        }
Beispiel #18
0
        public void AvroRecord_CreateMapOfArrayOfAvroRecords()
        {
            const string Schema      = @"
                {
                    ""type""  :""map"",
                    ""values"":
                        {
                            ""type"" :""array"",
                            ""items"":
                            {
                                ""type"":""record"",
                                ""name"":""Microsoft.Hadoop.Avro.Tests.SimpleIntClass"",
                                ""fields"":
                                    [
                                        { ""name"":""IntField"", ""type"":""int"" },
                                    ]
                            }
                        }
                }";
            var          serializer  = AvroSerializer.CreateGeneric(Schema);
            var          mapSchema   = (MapSchema)serializer.WriterSchema;
            var          arraySchema = (ArraySchema)mapSchema.ValueSchema;

            var expected = new Dictionary <string, AvroRecord[]>();

            for (int i = 0; i < 5; i++)
            {
                var key   = Utilities.GetRandom <string>(false) + i;
                var value = new AvroRecord[10];
                for (int j = 0; j < 10; j++)
                {
                    var record = new AvroRecord(arraySchema.ItemSchema);
                    record["IntField"] = j;
                    value[j]           = record;
                }
                expected.Add(key, value);
            }

            serializer.Serialize(this.stream, expected);
            this.stream.Seek(0, SeekOrigin.Begin);
            dynamic actual = serializer.Deserialize(this.stream);

            foreach (var expectedKeyValuePair in expected)
            {
                Assert.True(actual.ContainsKey(expectedKeyValuePair.Key));
                dynamic actualValue = actual[expectedKeyValuePair.Key];
                for (int i = 0; i < 10; i++)
                {
                    Assert.Equal(expectedKeyValuePair.Value[i]["IntField"], actualValue[i].IntField);
                }
            }
        }
        public void GenericSerializer_SerializeSimpleRecord()
        {
            const string StringSchema = @"{
                             ""type"":""record"",
                             ""name"":""Microsoft.Hadoop.Avro.Tests.SimpleIntClass"",
                             ""fields"":
                                       [
                                           { ""name"":""IntField"", ""type"":""int"" },
                                           { ""name"":""MyGuid"", ""type"": {""type"":""fixed"", ""size"":4, ""name"": ""q"" } },
                                           { ""name"":""Arr"", ""type"": {""type"":""array"", ""items"":""int""}},
                                           { ""name"":""LongField"", ""type"": ""long""},
                                           {""name"":""LongMap"", ""type"": {""type"":""map"", ""values"":""long""}},
                                           { ""name"":""DoubleField"", ""type"": ""double""},
                                           { ""name"":""FloatField"", ""type"": ""float""},
                                           { ""name"":""BooleanField"", ""type"": ""boolean""},
                                           { ""name"":""BytesField"", ""type"": ""bytes""},
                                       ]
                          }";
            var          serializer   = AvroSerializer.CreateGeneric(StringSchema);

            using (var stream = new MemoryStream())
            {
                dynamic expected = new AvroRecord(serializer.WriterSchema);
                expected.IntField  = 5;
                expected.MyGuid    = new byte[] { 1, 2, 3, 4 };
                expected.Arr       = new int[] { 1, 2, 5 };
                expected.LongField = (long)14;
                expected.LongMap   = new Dictionary <string, long> {
                    { "test", 1 }
                };
                expected.DoubleField  = (double)3;
                expected.FloatField   = (float)4;
                expected.BooleanField = true;
                expected.BytesField   = new byte[3] {
                    4, 5, 6
                };

                serializer.Serialize(stream, expected);
                stream.Position = 0;

                dynamic actual = serializer.Deserialize(stream);
                Assert.AreEqual(expected.IntField, actual.IntField);
                CollectionAssert.AreEqual(expected.MyGuid, actual.MyGuid);
                CollectionAssert.AreEqual(expected.Arr, actual.Arr);
                Assert.AreEqual(expected.LongField, actual.LongField);
                Utilities.DictionaryEquals(expected.LongMap as IDictionary <string, long>, actual.LongMap as IDictionary <string, long>);
                Assert.AreEqual(expected.DoubleField, actual.DoubleField);
                Assert.AreEqual(expected.FloatField, actual.FloatField);
                Assert.AreEqual(expected.BooleanField, actual.BooleanField);
                CollectionAssert.AreEqual(expected.BytesField, actual.BytesField);
            }
        }
        public void GenericSerializer_UnionOfNullAndArrayWithInvalidArrayType()
        {
            const string Schema = @"[""null"", { ""type"" :""array"", ""items"":""int"" } ]";

            var arrayOfDoubles = Utilities.GetRandom <double[]>(false);

            var serializer = AvroSerializer.CreateGeneric(Schema);

            using (var memoryStream = new MemoryStream())
            {
                serializer.Serialize(memoryStream, arrayOfDoubles);
            }
        }
        public void GenericSerializer_UnionOfNullAndMapWithInvalidMapValueType()
        {
            const string Schema = @"[""null"", { ""type"" :""map"", ""values"":""int"" } ]";

            var dictionaryOfDoubles = Utilities.GetRandom <Dictionary <string, double> >(false);

            var serializer = AvroSerializer.CreateGeneric(Schema);

            using (var memoryStream = new MemoryStream())
            {
                serializer.Serialize(memoryStream, dictionaryOfDoubles);
            }
        }
        public void GenericSerializer_UnionOfNullAndArrayWithInvalidType()
        {
            const string Schema = @"[""null"", { ""type"" :""array"", ""items"":""int"" } ]";

            var dictionaryOfDoubles = Utilities.GetRandom <Dictionary <string, double> >(false);

            var serializer = AvroSerializer.CreateGeneric(Schema);

            using (var memoryStream = new MemoryStream())
            {
                Assert.ThrowsException <SerializationException>(() => serializer.Serialize(memoryStream, dictionaryOfDoubles));
            }
        }
        public void GenericSerializer_UnionOfNullAndMapWithInvalidType()
        {
            const string Schema = @"[""null"", { ""type"" :""map"", ""values"":""int"" } ]";

            var arrayOfDoubles = Utilities.GetRandom <double[]>(false);

            var serializer = AvroSerializer.CreateGeneric(Schema);

            using (var memoryStream = new MemoryStream())
            {
                Assert.ThrowsException <SerializationException>(() => serializer.Serialize(memoryStream, arrayOfDoubles));
            }
        }
        public void SchemaDefaultValues_Enum()
        {
            const string DefaultSchema  = @"{ ""type"": ""enum"",""name"": ""Suit"",""symbols"" : [""SPADES"", ""HEARTS"", ""DIAMONDS"", ""CLUBS""]}";
            var          tempSerializer = AvroSerializer.CreateGeneric(DefaultSchema);
            var          defaultValue   = new AvroEnum(tempSerializer.WriterSchema);

            defaultValue.Value = "DIAMONDS";
            const string DefaultValueJson = @"""DIAMONDS""";

            RoundTripSerializationWithDefaultsAndCheck(
                DefaultSchema,
                defaultValue,
                DefaultValueJson,
                (expected, actual) => Assert.AreEqual(expected.Value, actual.Value));
        }
Beispiel #25
0
        public void AvroRecord_SerializeRecordWithSByteFields()
        {
            var schema     = AvroSerializer.Create <ClassWithSByteFields>().WriterSchema;
            var expected   = ClassWithSByteFields.Create();
            var serializer = AvroSerializer.CreateGeneric(schema.ToString());

            using (var stream = new MemoryStream())
            {
                serializer.Serialize(stream, expected.ToAvroRecord(schema));
                stream.Seek(0, SeekOrigin.Begin);
                var result = serializer.Deserialize(stream);
                var actual = ClassWithSByteFields.Create((AvroRecord)result);
                Assert.Equal(expected, actual);
            }
        }
        public void SequentialGenericWritingReading_SimpleRecord()
        {
            const string StringSchema = @"{
                             ""type"":""record"",
                             ""name"":""Microsoft.Hadoop.Avro.Tests.ClassOfInt"",
                             ""fields"":
                                       [
                                           {
                                               ""name"":""PrimitiveInt"",
                                               ""type"":""int""
                                           }
                                       ]
                          }";

            using (var stream = new MemoryStream())
            {
                var serializer = AvroSerializer.CreateGeneric(StringSchema);
                using (var streamWriter = AvroContainer.CreateGenericWriter(StringSchema, stream, Codec.Null))
                {
                    using (var writer = new SequentialWriter <object>(streamWriter, 24))
                    {
                        var expected = new List <AvroRecord>();
                        var random   = new Random(113);
                        for (int i = 0; i < 10; i++)
                        {
                            dynamic record = new AvroRecord(serializer.WriterSchema);
                            record.PrimitiveInt = random.Next();
                            expected.Add(record);
                        }

                        expected.ForEach(writer.Write);
                        writer.Flush();

                        stream.Seek(0, SeekOrigin.Begin);

                        var streamReader = AvroContainer.CreateReader <ClassOfInt>(stream, true, this.dataContractSettings, new CodecFactory());
                        using (var reader = new SequentialReader <ClassOfInt>(streamReader))
                        {
                            var j = 0;
                            foreach (var avroRecord in reader.Objects)
                            {
                                Assert.Equal(expected[j++]["PrimitiveInt"], avroRecord.PrimitiveInt);
                            }
                        }
                    }
                }
            }
        }
Beispiel #27
0
 /// <summary>
 /// Creates a writer of <see cref="Microsoft.Hadoop.Avro.AvroRecord"/> or primitive type.
 /// </summary>
 /// <param name="schema">The writer schema.</param>
 /// <param name="stream">The stream that will contain the resulting Avro object container.</param>
 /// <param name="leaveOpen">If set to <c>true</c> the <paramref name="stream"/> is left open.</param>
 /// <param name="codec">The codec.</param>
 /// <returns> A writer.</returns>
 /// <exception cref="System.ArgumentNullException">Thrown when any argument is null.</exception>
 public static IAvroWriter <object> CreateGenericWriter(string schema, Stream stream, bool leaveOpen, Codec codec)
 {
     if (string.IsNullOrEmpty(schema))
     {
         throw new ArgumentNullException("schema");
     }
     if (stream == null)
     {
         throw new ArgumentNullException("stream");
     }
     if (codec == null)
     {
         throw new ArgumentNullException("codec");
     }
     return(new StreamWriter <object>(stream, leaveOpen, AvroSerializer.CreateGeneric(schema), codec));
 }
        public void GenericSerializer_SerializeNestedRecord()
        {
            const string StringSchema = @"{
                                        ""type"":""record"",
                                        ""name"":""Microsoft.Hadoop.Avro.Tests.NestedClass"",
                                        ""fields"":[
                                            {
                                                ""name"":""Nested"",
                                                ""type"":[
                                                    ""null"",
                                                    {
                                                        ""type"":""record"",
                                                        ""name"":""Microsoft.Hadoop.Avro.Tests.SimpleIntClass"",
                                                        ""fields"":[
                                                            {
                                                                 ""name"":""IntField"",
                                                                 ""type"":""int""
                                                            }
                                                        ]
                                                    }
                                                ]
                                            },
                                            {
                                                ""name"":""IntNestedField"",""type"":""int""
                                            }
                                        ]
                                    }";

            var serializer = AvroSerializer.CreateGeneric(StringSchema);
            var nested     = serializer.ReaderSchema as RecordSchema;

            using (var stream = new MemoryStream())
            {
                dynamic expected = new AvroRecord(nested);
                expected.IntNestedField  = 5;
                expected.Nested          = new AvroRecord((nested.GetField("Nested").TypeSchema as UnionSchema).Schemas[1]);
                expected.Nested.IntField = 3;

                serializer.Serialize(stream, expected);
                stream.Position = 0;

                var actual = serializer.Deserialize(stream) as AvroRecord;
                Assert.AreEqual(expected.GetField <AvroRecord>("Nested").GetField <int>("IntField"),
                                actual.GetField <AvroRecord>("Nested").GetField <int>("IntField"));
            }
        }
        public static TypeSchema GetItemSchema(TypeSchema schema)
        {
            var unionSchema = schema as UnionSchema;

            if (unionSchema != null)
            {
                schema = unionSchema.Schemas.OfType <ArraySchema>().SingleOrDefault();
            }

            var arraySchema = schema as ArraySchema;

            if (arraySchema != null)
            {
                return(((ArraySchema)AvroSerializer.CreateGeneric(arraySchema.ToString()).WriterSchema).ItemSchema);
            }

            throw new ApplicationException("Schema must be array schema");
        }
Beispiel #30
0
        public void GenericSerializer_UnionOfNullAndEnumWithInvalidEnum()
        {
            Assert.Throws <SerializationException>(() =>
            {
                const string Schema = @"
                        [
                            ""null"",
                            {
                                ""type"":""enum"",
                                ""name"":""Microsoft.Hadoop.Avro.Tests.TestEnum"",
                                ""symbols"":
                                [
                                    ""EnumValue3"",
                                    ""EnumValue2"",
                                    ""EnumValue1""
                                ]
                            }
                        ]";

                const string InvalidEnumSchema = @"{
                        ""type"":""enum"",
                        ""name"":""Microsoft.Hadoop.Avro.Tests.InvalidEnum"",
                        ""symbols"":
                        [
                            ""InvalidValue3"",
                            ""InvalidValue2"",
                            ""InvalidValue1""
                        ]
                    }";

                var invalidSchemaSerializer = AvroSerializer.CreateGeneric(InvalidEnumSchema);
                var invalidEnum             = new AvroEnum(invalidSchemaSerializer.WriterSchema)
                {
                    Value = "InvalidValue3"
                };
                var serializer = AvroSerializer.CreateGeneric(Schema);

                using (var memoryStream = new MemoryStream())
                {
                    serializer.Serialize(memoryStream, invalidEnum);
                }
            }
                                                   );
        }