Пример #1
0
func (this *CachedSchemaRegistryClient) GetByID(id int32) (avro.Schema, error) {
	var schema avro.Schema
	var exists bool
	if schema, exists = this.idCache[id]; exists {
		return schema, nil
	}

	request, err := this.newDefaultRequest("GET", fmt.Sprintf(GET_SCHEMA_BY_ID, id), nil)
	if err != nil {
		return nil, err
	}
	response, err := http.DefaultClient.Do(request)
	if err != nil {
		return nil, err
	}

	if this.isOK(response) {
		decodedResponse := &GetSchemaResponse{}
		if this.handleSuccess(response, decodedResponse) != nil {
			return nil, err
		}
		schema, err := avro.ParseSchema(decodedResponse.Schema)
		this.idCache[id] = schema

		return schema, err
	} else {
		return nil, this.handleError(response)
	}
}
func createPrimitiveSchema(schemaType string) avro.Schema {
	schema, err := avro.ParseSchema(fmt.Sprintf(`{"type" : "%s" }`, schemaType))
	if err != nil {
		panic(err)
	}

	return schema
}
func TestSchemaRegistry(t *testing.T) {
	client := NewCachedSchemaRegistryClient("http://localhost:8081")
	rawSchema := "{\"namespace\": \"ly.stealth.kafka.metrics\",\"type\": \"record\",\"name\": \"Timings\",\"fields\": [{\"name\": \"id\", \"type\": \"long\"},{\"name\": \"timings\",  \"type\": {\"type\":\"array\", \"items\": \"long\"} }]}"
	schema, err := avro.ParseSchema(rawSchema)
	assert(t, err, nil)
	id, err := client.Register("test1", schema)
	assert(t, err, nil)
	assertNot(t, id, 0)

	schema, err = client.GetByID(id)
	assert(t, err, nil)
	assertNot(t, schema, nil)

	metadata, err := client.GetLatestSchemaMetadata("test1")
	assert(t, err, nil)
	assertNot(t, metadata, nil)

	version, err := client.GetVersion("test1", schema)
	assert(t, err, nil)
	assertNot(t, version, 0)
}