Forum Discussion

andrew_holbrook's avatar
andrew_holbrook
New Contributor III
8 years ago

Example of defineInputSchema for multi-level inputs

Hi,

I’m trying to create an input schema for my data source. Here is an example input data format:

{ “data”: { “name”:“Andrew”, “value”:1}}

How do I represent this “layered” schema? Can you provide an example?

6 Replies

  • aleung's avatar
    aleung
    Contributor III

    here:

      {
        "DocumentRoot": {
          "Document": [
            {
              "Metadata": {
                "id": "e0408dd2-6564-11e5-a3cb-512b7ad797bd",
                "asMap": {
                  "global": {
                    "doc_id": "e0408dd2-6564-11e5-a3cb-512b7ad797bd"
                  }
                }
              },
              "Data": {
                "id": "1",
                "first_name": "John",
                "last_name": "Doe",
                "gender": "M",
                "city": "Santa Ana",
                "state": "California",
                "street_address": "9 Melby Terrace",
                "zip_code": "92725"
              }
            },
            {
              "Metadata": {
                "id": "e0408dd3-6564-11e5-a3cb-512b7ad797bd",
                "asMap": {
                  "global": {
                    "doc_id": "e0408dd3-6564-11e5-a3cb-512b7ad797bd"
                  }
                }
              },
              "Data": {
                "id": "2",
                "first_name": "Katy",
                "last_name": "Perry",
                "gender": "F",
                "city": "Flushing",
                "state": "New York",
                "street_address": "43 Golf View Street",
                "zip_code": "11355"
              }
            }
          ]
        }
      }
    
  • andrew_holbrook's avatar
    andrew_holbrook
    New Contributor III

    @aleung (and I suppose @robin), thanks but what I’m looking for more of is how to change this function (pulled from here):

    public void defineInputSchema(final SchemaProvider provider) {
            Schema colA = provider.createSchema(SnapType.STRING, COL_A);
            provider.getSchemaBuilder(INPUT_VIEW_NAME)
                    .withChildSchema(colA)
                    .build();
        }
    

    How do I do a nested schema?

    • del's avatar
      del
      Contributor III

      Hey @andrew_holbrook,

      I’ve played around with this code and I believe I found your solution. Using your data sample from your original post:

      You’ll need to import com.snaplogic.snap.schema.api.ObjectSchema into your class and then your code will look something like this:

      private static final String DATA = "data";
      private static final String NAME = "name";
      private static final String VALUE = "value";
      private static final String INPUT_VIEW_NAME = "input0";
      private static final String OUTPUT_VIEW_NAME = "output0";
      
      @Override
      public void defineInputSchema(final SchemaProvider provider) {
          ObjectSchema data = provider.createSchema(SnapType.COMPOSITE, DATA);
          Schema name = provider.createSchema(SnapType.STRING, NAME);
          Schema value = provider.createSchema(SnapType.BOOLEAN, VALUE);
          data.addChild(name);
          data.addChild(value);
          provider.getSchemaBuilder(INPUT_VIEW_NAME)
                  .withChildSchema(data)
                  .build();
      }
      

      I don’t know if SnapType.COMPOSITE is the correct type, but it seems to work for me.

    • ryagnik's avatar
      ryagnik
      New Contributor

      we had the same question but could not get any answer here, answer will be much appreciated here.

  • andrew_holbrook's avatar
    andrew_holbrook
    New Contributor III

    @del @robin that appears to be working, thank you!

    One last question is how we think doing an array?

    For now I’ve done it like this:
    ObjectSchema array_schema = provider.createSchema(SnapType.TABLE, "data");
    and then I leverage addChild to define schema elements to a potential object in an array?