翔子 发表于 2014-12-17 17:53:21

Using the avro schema-repo

项目地址: https://github.com/schema-repo/schema-repo

Dependency Jars:

    avro-repo-bundle-1.7.5-SNAPSHOT-withdeps.jar
    avro-repo-client-1.7.5-SNAPSHOT.jar
    avro-repo-common-1.7.5-SNAPSHOT.jar
    avro-repo-server-1.7.5-SNAPSHOT.jar
    javax.inject-1.jar
    jersey-core-1.15.jar
    jersey-server-1.15.jar
    jersey-servlet-1.15.jar
    jetty-server-8.1.8.v20121106.jar
    jetty-util-8.1.8.v20121106.jar
    paranamer-2.3.jar
    schema-repo.jar
    servlet-api-2.5.jar

Properties file contents (if your property keys are incorrect you will see Guice exceptions):

cat test.prop

repo.class=org.apache.avro.repo.FileRepository
avro.repo.file-repo-path=target/data/

Filesystem Repo Directory:

    target/data/

Starting the repo:

    java -cp “*” org.apache.avro.repo.server.RepositoryServer test.prop

Create a subejct if it does not already exist:

curl -I -H "Content-Type: application/x-www-form-urlencoded" -XPUT "http://localhost:2876/schema-repo/tests"

Return:
HTTP/1.1 200 OK
Content-Type: text/plain
Transfer-Encoding: chunked
Server: Jetty(8.y.z-SNAPSHOT)

Get a subject:

$ curl -I "http://localhost:2876/schema-repo/tests"

Return:
HTTP/1.1 200 OK
Content-Type: text/plain
Content-Length: 0
Server: Jetty(8.y.z-SNAPSHOT)

Register a schema with a subject:

$ curl -H "Content-type: text/plain" -XPUT "http://localhost:2876/schema-repo/tests/register" -d '{"namespace":"tts.tests.avro","type":"record","name":"Test","fields":[{"name":"test_name","type":"string"},{"name":"ts","type":"int"},{"name":"value","type":["string","null"]}]}'

Return (returns the schema id):
0

Get the latest schema for a subject:

$ curl "http://localhost:2876/schema-repo/tests/latest"

Return:
0 {“namespace”:”tts.tests.avro”,”type”:”record”,”name”:”Test”,”fields”:[{“name”:”test_name”,”type”:”string”},{“name”:”ts”,”type”:”int”},{“name”:”value”,”type”:[“string”,”null”]}]}

Get the config for a schema:

$ curl "http://localhost:2876/schema-repo/tests/config"

Return:
#Wed Apr 10 11:08:30 EDT 2013

Look up an id by a subject + schema pair:
curl -H "Content-type: text/plain" -XPOST "http://localhost:2876/schema-repo/tests/schema" -d '{"namespace":"tts.tests.avro","type":"record","name":"Test","fields":[{"name":"test_name","type":"string"},{"name":"ts","type":"int"},{"name":"value","type":["string","null"]}]}'

Return:
0

Look up schema by subject + id pair:

$ curl "http://localhost:2876/schema-repo/tests/id/0"

Return:
{“namespace”:”tts.tests.avro”,”type”:”record”,”name”:”Test”,”fields”:[{“name”:”test_name”,”type”:”string”},{“name”:”ts”,”type”:”int”},{“name”:”value”,”type”:[“string”,”null”]}]}

Look up all schemas for a subject (subject=tests):

$ curl "http://localhost:2876/schema-repo/tests/all"

Return:
0 {“namespace”:”tts.tests.avro”,”type”:”record”,”name”:”Test”,”fields”:[{“name”:”test_name”,”type”:”string”},{“name”:”ts”,”type”:”int”},{“name”:”value”,”type”:[“string”,”null”]}]}

Adding another schema to the same subject (*Note: if schemas are the same it returns the existing id):

$ curl -H "Content-type: text/plain" -XPUT "http://localhost:2876/schema-repo/tests/register" -d '{"namespace":"tts.tests.avro","type":"record","name":"Test","fields":[{"name":"test_name","type":"string"},{"name":"ts","type":"int"},{"name":"value","type":["string","null"]},{"note":"test_note","type":"string"}]}'

Return:
1

Look up all schemas for a subject … again (subject=tests):

$ curl "http://localhost:2876/schema-repo/tests/all"

Return:
1 {“namespace”:”tts.tests.avro”,”type”:”record”,”name”:”Test”,”fields”:[{“name”:”test_name”,”type”:”string”},{“name”:”ts”,”type”:”int”},{“name”:”value”,”type”:[“string”,”null”]},{“note”:”test_note”,”type”:”string”}]}
0 {“namespace”:”tts.tests.avro”,”type”:”record”,”name”:”Test”,”fields”:[{“name”:”test_name”,”type”:”string”},{“name”:”ts”,”type”:”int”},{“name”:”value”,”type”:[“string”,”null”]}]}

Exploring the filesystem:

    $ ll target/data/tests/
    total 32
    -rw-r–r– 1 w3ndns staff 177 Apr 10 11:06 0.schema
    -rw-r–r– 1 w3ndns staff 214 Apr 10 11:14 1.schema
    -rw-r–r– 1 w3ndns staff 4 Apr 10 11:14 schema_ids
    -rw-r–r– 1 w3ndns staff 68 Apr 10 10:59 subject.properties
    $ cd target/data/tests/
    $ ll
    total 32
    -rw-r–r– 1 w3ndns staff 177 Apr 10 11:06 0.schema
    -rw-r–r– 1 w3ndns staff 214 Apr 10 11:14 1.schema
    -rw-r–r– 1 w3ndns staff 4 Apr 10 11:14 schema_ids
    -rw-r–r– 1 w3ndns staff 68 Apr 10 10:59 subject.properties
    $ cat 0.schema
    {“namespace”:”tts.tests.avro”,”type”:”record”,”name”:”Test”,”fields”:[{“name”:”test_name”,”type”:”string”},{“name”:”ts”,”type”:”int”},{“name”:”value”,”type”:[“string”,”null”]}]}
    $ cat 1.schema
    {“namespace”:”tts.tests.avro”,”type”:”record”,”name”:”Test”,”fields”:[{“name”:”test_name”,”type”:”string”},{“name”:”ts”,”type”:”int”},{“name”:”value”,”type”:[“string”,”null”]},{“note”:”test_note”,”type”:”string”}]}
    $ cat schema_ids
    0
    1
    $ cat subject.properties
    #Schema Repository Subject Properties
    #Wed Apr 10 10:59:43 EDT 2013

    If you delete a(n) schema file(s), the schema will remain persistent in memory until the RepositoryServer is restarted.
页: [1]
查看完整版本: Using the avro schema-repo