Пример #1
0
        private static GenericRecord mkRecord(object[] kv, RecordSchema s)
        {
            GenericRecord input = new GenericRecord(s);

            for (int i = 0; i < kv.Length; i += 2)
            {
                string fieldName  = (string)kv[i];
                object fieldValue = kv[i + 1];
                Schema inner      = s[fieldName].Schema;
                if (inner is EnumSchema)
                {
                    GenericEnum ge = new GenericEnum(inner as EnumSchema, (string)fieldValue);
                    fieldValue = ge;
                }
                else if (inner is FixedSchema)
                {
                    GenericFixed gf = new GenericFixed(inner as FixedSchema);
                    gf.Value   = (byte[])fieldValue;
                    fieldValue = gf;
                }
                input.Add(fieldName, fieldValue);
            }
            return(input);
        }
Пример #2
0
        public void EnumGenericTest()
        {
            var enumSchema = AvroParser.ReadSchema <EnumSchema>(@"{""name"":""Avro.Test.Generic.TestEnum"",""type"":""enum"",""symbols"":[""A"",""B"",""C""]}");

            var writer = new DatumWriter <GenericEnum>(enumSchema);
            var reader = new DatumReader <GenericEnum>(enumSchema, enumSchema);

            var expectedValue = new GenericEnum(enumSchema, "B");

            using (var stream = new MemoryStream())
                using (var encoder = new BinaryEncoder(stream))
                    using (var decoder = new BinaryDecoder(stream))
                    {
                        writer.Write(encoder, expectedValue);
                        stream.Seek(0, SeekOrigin.Begin);
                        var actualValue = reader.Read(decoder);
                        Assert.AreEqual(expectedValue, actualValue);

                        var expectedPosition = stream.Position;
                        stream.Seek(0, SeekOrigin.Begin);
                        reader.Skip(decoder);
                        Assert.AreEqual(expectedPosition, stream.Position);
                    }
        }
 private JToken EncodeEnum(
     EnumSchema schema,
     GenericEnum value)
 {
     return(new JValue(value.Value));
 }
Пример #4
0
        public void TestSimple()
        {
            var schema = AvroParser.ReadSchema(@"{
                ""name"":""Test.Foobar.Record.Thing"",
                ""type"":""record"",
                ""fields"":[
                    {""name"":""ID"",""type"":""int""},
                    {""name"":""Name"",""type"":""string""},
                    {""name"":""Nuller"",""type"":""null""},
                    {""name"":""family"",""type"":{
                        ""type"":""map"",""values"":""string""
                    }},
                    {""name"":""stuff"",""type"":{
                        ""name"":""stuffs"",
                        ""type"":""record"",
                        ""fields"":[
                            {""name"":""key"",""type"":""bytes""},
                            {""name"":""keytype"",""type"":{
                                ""name"":""keyenum"",
                                ""type"":""enum"",
                                ""symbols"":[""ANALOG"",""DIGITAL""]
                            }}
                        ]
                    }},
                    {""name"":""NullableThing"",""type"":[""null"",""string""]},
                    {""name"":""Tags"",""type"":{
                        ""type"":""array"",""items"":""string""
                    }}
                ]
            }") as RecordSchema;

            var stuffSchema = schema.First(r => r.Name == "stuff").Type as RecordSchema;
            var enumSchema  = stuffSchema.First(r => r.Name == "keytype").Type as EnumSchema;


            var genericRecord = new GenericRecord(schema);
            var reader        = new DatumReader <GenericRecord>(schema);
            var writer        = new DatumWriter <GenericRecord>(schema);


            var delimiter     = Environment.NewLine;
            var stringBuilder = new StringBuilder();

            using (var stream = new StringWriter(stringBuilder))
                using (var encoder = new JsonEncoder(stream, schema, delimiter))
                {
                    for (int i = 0; i < 10; i++)
                    {
                        var record = new GenericRecord(genericRecord);
                        record[0] = i;
                        record[1] = $"foo{i}";
                        record[2] = null;
                        record[3] = new Dictionary <string, string>()
                        {
                            { "Brother", "John" }
                        };

                        var stuffRecord = new GenericRecord(stuffSchema);
                        var keyEnum     = new GenericEnum(enumSchema, i % enumSchema.Symbols.Count);

                        stuffRecord[0] = Guid.NewGuid().ToByteArray();
                        stuffRecord[1] = keyEnum;

                        record[4] = stuffRecord;

                        record[5] = (i % 2) == 0 ? "ToNullOrNotToNull" : null;
                        record[6] = new List <string> {
                        };
                        writer.Write(encoder, record);

                        var singleJson = stringBuilder.ToString();
                    }
                }
            var allJson = stringBuilder.ToString();


            using (var stream = new StringReader(allJson))
                using (var decoder = new JsonDecoder(stream, schema, delimiter))
                {
                    for (int i = 0; i < 10; i++)
                    {
                        var record = reader.Read(decoder);
                        Debug.WriteLine(record.ToString());
                    }
                }
        }
Пример #5
0
        static void GenerateInteropData(string schemaPath, string outputDir)
        {
            RecordSchema schema = null;

            using (var reader = new StreamReader(schemaPath))
            {
                schema = Schema.Parse(reader.ReadToEnd()) as RecordSchema;
            }

            var mapFieldSchema  = (schema.Fields.Find(x => x.Name == "mapField").Schema as MapSchema).ValueSchema as RecordSchema;
            var mapFieldRecord0 = new GenericRecord(mapFieldSchema);
            var mapFieldRecord1 = new GenericRecord(mapFieldSchema);

            mapFieldRecord0.Add("label", "a");
            mapFieldRecord1.Add("label", "cee");
            var mapFieldValue = new Dictionary <string, GenericRecord>
            {
                { "a", mapFieldRecord0 },
                { "bee", mapFieldRecord1 }
            };

            var enumFieldValue = new GenericEnum(schema.Fields.Find(x => x.Name == "enumField").Schema as EnumSchema, "C");

            var fixedFieldValue = new GenericFixed(
                schema.Fields.Find(x => x.Name == "fixedField").Schema as FixedSchema,
                Encoding.ASCII.GetBytes("1019181716151413"));

            var nodeSchema            = schema.Fields.Find(x => x.Name == "recordField").Schema as RecordSchema;
            var recordFieldValue      = new GenericRecord(nodeSchema);
            var innerRecordFieldValue = new GenericRecord(nodeSchema);

            innerRecordFieldValue.Add("label", "inner");
            innerRecordFieldValue.Add("children", new GenericRecord[] { });
            recordFieldValue.Add("label", "blah");
            recordFieldValue.Add("children", new GenericRecord[] { innerRecordFieldValue });

            GenericRecord record = new GenericRecord(schema);

            record.Add("intField", 12);
            record.Add("longField", 15234324L);
            record.Add("stringField", "hey");
            record.Add("boolField", true);
            record.Add("floatField", 1234.0f);
            record.Add("doubleField", -1234.0);
            record.Add("bytesField", Encoding.UTF8.GetBytes("12312adf"));
            record.Add("nullField", null);
            record.Add("arrayField", new double[] { 5.0, 0.0, 12.0 });
            record.Add("mapField", mapFieldValue);
            record.Add("unionField", 12.0);
            record.Add("enumField", enumFieldValue);
            record.Add("fixedField", fixedFieldValue);
            record.Add("recordField", recordFieldValue);

            var datumWriter = new GenericDatumWriter <GenericRecord>(schema);

            foreach (var codecName in InteropDataConstants.SupportedCodecNames)
            {
                var outputFile = "csharp.avro";
                if (codecName != DataFileConstants.NullCodec)
                {
                    outputFile = string.Format("csharp_{0}.avro", codecName);
                }
                var outputPath = Path.Combine(outputDir, outputFile);
                var codec      = Codec.CreateCodecFromString(codecName);
                using (var dataFileWriter = DataFileWriter <GenericRecord> .OpenWriter(datumWriter, outputPath, codec))
                {
                    dataFileWriter.Append(record);
                }
            }
        }