5 Replies Latest reply on Sep 12, 2017 8:03 AM by Anthony Chou

    Tableau WDC with an Unknown Schema

    Anthony Chou

      Hello!

       

      I've spent some time looking at the WDC tutorials and have begun building my own for work. The tutorial examples are very straight forward, with clean data and a defined schema. However, not every project is this easy to configure. I have a service that allows users to submit a database query, and then view the results in tableau after we do some magic. My issue is, all the examples I've looked at have hard coded schemas. What if you don't know the schema before hand, and need to generate it dynamically?

       

      In the Earthquake example, the schema is conveniently defined. Can we generate this instead? I have a data map, where key is the column name ,and value is the value, and I have been able to build tableau tdes with this by storing them inside tableau.connectionData. Unfortunately, I also have an external schema that I need to include. I can't even use "document.getElementById('external_schema').value;", it appears that it must use connectionData. However, that is reserved for data, and not schema. I would need to find another variable to pass in schema, like tableau.connectionSchema

       

      Does anyone have an idea for generating schema on the fly? Assume I have a JSON schema string, which I can then parse into a map that tells me column name and tableau datatype.

       

       

      myConnector.getSchema = function(schemaCallback) {

              var cols = [{

                  id: "id",

                  dataType: tableau.dataTypeEnum.string // Hard coded Schema. Can we write a function that creates var cols instead?

              }, {

                  id: "mag",

                  alias: "magnitude",

                  dataType: tableau.dataTypeEnum.float

              }, {

                  id: "title",

                  alias: "title",

                  dataType: tableau.dataTypeEnum.string

              }, {

                  id: "lat",

                  alias: "latitude",

                  columnRole: "dimension",

                  // Do not aggregate values as measures in Tableau--makes it easier to add to a map

                  dataType: tableau.dataTypeEnum.float

              }, {

                  id: "lon",

                  alias: "longitude",

                  columnRole: "dimension",

                  // Do not aggregate values as measures in Tableau--makes it easier to add to a map

                  dataType: tableau.dataTypeEnum.float

              }];

       

       

              var tableSchema = {

                  id: "earthquakeFeed",

                  alias: "Earthquakes with magnitude greater than 4.5 in the last seven days",

                  columns: cols

              };

       

       

              schemaCallback([tableSchema]);

          };

        • 1. Re: Tableau WDC with an Unknown Schema
          Brendan Lee

          Hey Anthony,

           

          You can definitely create your schema dynamically for WDCs instead of using a static schema.  The getSchema call is executed every time the data extract is generated.  So what you need to do is perform a fetch (for example an ajax call) from your external resource and then construct your schema object based on the returned data.  This is similar to how you fetch data in the getData method. 

           

          The connectionData property is used to pass configuration options between the phases of the web data connector, it is not intended to store all the data that the web data connector brings back. Please read more about the lifecycle of the WDC here: WDC Lifecycle and Phases.   There are two distinct phases of the WDC, and they do not share state.  That is perhaps why you are unable to access the value in your external_schema element. 

           

          Hope that helps,
          Brendan

          • 2. Re: Tableau WDC with an Unknown Schema
            Anthony Chou

            Hi Brendon,

             

            I ended up writing my own Javascript functions to generate them on the fly. However, I have another question, I'm hoping to run by you when you have a moment.

             

            As I use my custom built WDC from Tableau, I realized I need some additional parameters from Tableau as an input. I know Tableau has a function called "USERNAME()", which returns exactly what I need, the username of the person currently on the WDC. However, this field is not something I can pass to my WDC. I need to know who the user is, so I can then take action accordingly on my API side.

             

            I've seen options available with an authentication layer, but I'm not sure that's what we want. We just want the Tableau username to be passed, somehow, through the WDC interface so our custom WDC can take action depending on user.

             

            Thanks,

            Anthony

            • 3. Re: Tableau WDC with an Unknown Schema
              Brendan Lee

              Hey Anthony,

               

              There is nothing from the WDC API side that will allow you to automatically retrieve a username.


              Instead, I would recommend you create a small form in your web data connector UI that allows the user to enter their username (and password if needed).  You can store this attribute in the tableau.username API property to be used in the data gathering phase.

              • 4. Re: Tableau WDC with an Unknown Schema
                padma k

                Hi Anthony,

                 

                Can you help me with same question and in my case JSON data which i need to read from API is dynamic i.e if i have my JSON returning only two columns today and its going to return three columns tomorrow and it keeps on changing.

                 

                I need to create wdc in a way that its dynamic i.e whenever my JSON output from API changes my wdc should return table with respective columns.

                 

                Hope i am clear on explaining my problem.Any help would be great

                • 5. Re: Tableau WDC with an Unknown Schema
                  Anthony Chou

                  You would create a new function that takes your data, and splits it by some deliminator into an array. Then for each element in that array, create a map and put each 'id' (column name) and 'datatype' in a map.

                  Put each of those maps in a new array, so you have an array of maps, with each map being one column with its datatype. Use this last array as 'cols' from the example