Some applications need to expose information about schemas to data plane clients.
This document explains how to add an API for that to your application.
You need to know two things:
Your application can expose any custom API by implementing a
handler.
Information about the deployed schemas are available in the component com.yahoo.search.schema.SchemaInfo.
With this information, we can add an API exposing schemas information through the following steps.
1. Make sure your application package can contain Java components
Application packages containing Java components must follow Maven layout.
If your application package root contains a pom.xml and src/main
you're good, otherwise convert it to this layout by copying the pom.xml from
the album-recommendation.java
sample app and moving the files to follow this layout before moving on.
2. Add a handler exposing schema info
Add the following handler (to a package of your choosing):
packageai.vespa.example;importcom.yahoo.container.jdisc.HttpRequest;importcom.yahoo.container.jdisc.HttpResponse;importcom.yahoo.container.jdisc.ThreadedHttpRequestHandler;importcom.yahoo.jdisc.Metric;importcom.yahoo.search.schema.SchemaInfo;importjava.io.IOException;importjava.io.OutputStream;importjava.nio.charset.Charset;importjava.util.concurrent.Executor;publicclassSchemaInfoHandlerextendsThreadedHttpRequestHandler{privatefinalSchemaInfoschemaInfo;publicSchemaInfoHandler(Executorexecutor,Metricmetric,SchemaInfoschemaInfo){super(executor,metric);this.schemaInfo=schemaInfo;}@OverridepublicHttpResponsehandle(HttpRequesthttpRequest){// Creating JSON, handling different paths etc. left as an exercise for the readerStringBuilderresponse=newStringBuilder();for(varschema:schemaInfo.schemas().values()){response.append("schema: "+schema.name()+"\n");for(varfield:schema.fields().values())response.append(" field: "+field.name()+"\n");}returnnewResponse(200,response.toString());}privatestaticclassResponseextendsHttpResponse{privatefinalbyte[]data;Response(intcode,byte[]data){super(code);this.data=data;}Response(intcode,Stringdata){this(code,data.getBytes(Charset.forName(DEFAULT_CHARACTER_ENCODING)));}@OverridepublicStringgetContentType(){return"application/json";}@Overridepublicvoidrender(OutputStreamoutputStream)throwsIOException{outputStream.write(data);}}privatestaticclassErrorResponseextendsResponse{ErrorResponse(intcode,Stringmessage){super(code,"{\"error\":\""+message+"\"}");}}}
3. Add the new API handler to your container cluster
In your services.xml file, under <container>, add: