Creating a Schema

If you are working with a Couchbase database that does not already have the schema needed for JDBC applications, use the connection properties provided by the connector to create a schema and import it into the database.

Important:

Before generating a schema definition, make sure that primary indexes have been created for all of the buckets in your Couchbase instance.

You provide the configuration information to the connector in the connection URL. For more information about the syntax of the connection URL, see Building the Connection URL.

To create a schema:

  1. To create the initial schema and save it in a local JSON file, set the connection properties as follows and then connect to the database:
    • Set the SchemaMapOperation property to 1.
    • Set the LocalSchemaFile property to the full path of a JSON file that you want to use to locally store the schema.
    • Set the SampleSize property to the number of documents that you want the connector to sample to detect the structure of the data.
    • Set the TypeNameList property to a comma-separated list of the attributes that the buckets use to specify document types. Each list item must be a bucket name surrounded by back quotes (`), a colon (:), and an attribute name surrounded by back quotes (`).
    • For example, the following TypeNameList setting indicates that the product, store, and sales buckets use the type attribute to specify the type of each document, while the customer bucket uses the jsonType attribute to do the same:

      TypeNameList=`product`:`type`,`store`:`type`,

      `customer`:`jsonType`,`sales`:`type`

      When generating a schema, the connector creates a base table for each different document type specified by the attributes in the list. For example, if the product bucket contains documents that have the type values retail and internal, then the connector creates two tables named retail and internal in the schema.

      Important:

      • It is highly recommended that you set this property when generating a schema. If you do not set this property, then the connector uses the bucket name as the type for all the documents in the bucket, and the resulting schema is likely to contain errors.
      • Make sure that you list the type attribute for every data bucket in the Couchbase Server instance or cluster to which you are connecting. Otherwise, you might encounter issues when working with document types that are not part of the schema.

    When you connect to the database, the connector samples the data, generates a schema, and stores the schema in the specified JSON file.

    Important:

    The schema replaces all existing content in the JSON file.

  2. Open the JSON file in the Schema Editor, modify the schema as needed, and then save your changes. For more information about using the Schema Editor, see the Schema Editor User Guide.
  3. To import the modified schema into the database, set the connection properties as follows and then reconnect to the database:
    • Set the SchemaMapOperation property to 2.
    • Set the LocalSchemaFile property to the full path of the JSON file.

    When you connect to the database, the connector imports your schema into the database and copies it to every bucket.

    Important:

    If the database already contains a schema, the schema that you import overwrites it.