func (this *KafkaAvroDecoder) Decode(bytes []byte) (interface{}, error) {
	if bytes == nil {
		return nil, nil
	} else {
		if bytes[0] != 0 {
			return nil, errors.New("Unknown magic byte!")
		}
		id := int32(binary.BigEndian.Uint32(bytes[1:]))
		schema, err := this.schemaRegistry.GetByID(id)
		if err != nil {
			return nil, err
		}

		if schema.Type() == avro.Bytes {
			return bytes[5:], nil
		} else {
			reader := avro.NewGenericDatumReader()
			reader.SetSchema(schema)
			value := avro.NewGenericRecord(schema)
			err := reader.Read(value, avro.NewBinaryDecoder(bytes[5:]))

			return value, err
		}
	}
}
Example #2
0
func (this *MirrorMaker) AddTiming(record *avro.GenericRecord, tag string, now int64) *avro.GenericRecord {
	if !this.evolutioned.exists(record.Schema().String()) {
		currentSchema := record.Schema().(*avro.RecordSchema)
		newSchema := *record.Schema().(*avro.RecordSchema)
		for _, newField := range this.logLineSchema.Fields {
			var exists bool
			for _, currentField := range currentSchema.Fields {
				if currentField.Name == newField.Name {
					if reflect.DeepEqual(currentField, newField) {
						exists = true
						break
					}
					panic(fmt.Sprintf("Incompatible field %s in schema %s", currentField.Name, currentSchema.String()))
				}
			}
			if !exists {
				newSchema.Fields = append(newSchema.Fields, newField)
			}
		}
		newRecord := avro.NewGenericRecord(&newSchema)
		for _, field := range currentSchema.Fields {
			newRecord.Set(field.Name, record.Get(field.Name))
		}
		record = newRecord
		this.evolutioned.add(record.Schema().String())
	}

	var timings map[string]interface{}
	if record.Get("timings") == nil {
		timings = make(map[string]interface{})
	} else {
		timings = record.Get("timings").(map[string]interface{})
	}

	timings[tag] = now
	record.Set("timings", timings)

	return record
}