How to import data?

Weaviate on Stackoverflow badge Weaviate issues on Github badge Weaviate total Docker pulls badge

đź’ˇ You are looking at older or release candidate documentation. The current Weaviate version is v1.15.2

How to import data in Weaviate?


Introduction

Data is added through the RESTful API. Python and JavaScript clients are available. The syntax of a data object is as follows:

{
  "class": "<class name>",  // as defined during schema creation
  "id": "<UUID>",     // optional, should be in UUID format.
  "properties": {
    "<property name>": "<property value>", // specified in dataType defined during schema creation
  }
}

Prerequisites

  1. Connect to a Weaviate instance.
    If you haven’t set up a Weaviate instance yet, check the Getting started guide. In this guide we assume your instance is running at http://localhost:8080.
  2. Upload a schema.
    Learn how to create and upload a schema here. In this guide we assume to have a similar schema uploaded with the classes Publication, Article and Author.

Add a data object

Let’s add a Publication with the name New York Times to your Weaviate instance. Not all properties have to be filled when adding a data object, so we will skip the hasArticles property for now, since we don’t have any Article objects yet. Note that the UUID is given in the id parameter now, this is optional.

  import weaviate

client = weaviate.Client("http://localhost:8080")

example_data = {
    "name": "New York Times"
}

client.data_object.create(
  example_data,
  "Publication",
  "f81bfe5e-16ba-4615-a516-46c2ae2e5a80", # optional, if not provided ons is going to be generated
)
  const weaviate = require("weaviate-client");

const client = weaviate.client({
  scheme: 'http',
  host: 'localhost:8080',
});

client.data
      .creator()
      .withClassName('Publication')
      .withId("f81bfe5e-16ba-4615-a516-46c2ae2e5a80")
      .withProperties({
        "name": "New York Times"
      })
      .do()
      .then(res => {
          console.log(res)
      })
      .catch(err => {
          console.error(err)
      });
  package main

import (
  "context"
  "fmt"

  "github.com/semi-technologies/weaviate-go-client/v4/weaviate"
)

func main() {
  cfg := weaviate.Config{
    Host:   "localhost:8080",
    Scheme: "http",
  }
  client := weaviate.New(cfg)

  dataSchema := map[string]string{
    "name": "New York Times",
  }

  created, err := client.Data().Creator().
    WithClassName("Publication").
    WithID("f81bfe5e-16ba-4615-a516-46c2ae2e5a80").
    WithProperties(dataSchema).
    Do(context.Background())

  if err != nil {
    panic(err)
  }
  fmt.Printf("%v", created)
}
  package technology.semi.weaviate;

import java.util.HashMap;
import java.util.Map;
import technology.semi.weaviate.client.Config;
import technology.semi.weaviate.client.WeaviateClient;
import technology.semi.weaviate.client.base.Result;
import technology.semi.weaviate.client.v1.data.model.WeaviateObject;

public class App {
  public static void main(String[] args) {
    Config config = new Config("http", "localhost:8080");
    WeaviateClient client = new WeaviateClient(config);

    Map<String, Object> dataSchema = new HashMap<>();
    dataSchema.put("name", "New York Times");

    Result<WeaviateObject> result = client.data().creator()
      .withClassName("Publication")
      .withID("f81bfe5e-16ba-4615-a516-46c2ae2e5a80")
      .withProperties(dataSchema)
      .run();

    if (result.hasErrors()) {
      System.out.println(result.getError());
      return;
    }
    System.out.println(result.getResult());
  }
}
  $ curl \
    -X POST \
    -H "Content-Type: application/json" \
    -d '{
        "class": "Publication",
        "id": "f81bfe5e-16ba-4615-a516-46c2ae2e5a80",
        "properties": {
          "name": "New York Times"
        }
      }' \
    http://localhost:8080/v1/objects

Add a data object with reference

If you want to add data object with a reference in a property, you need to use the UUID of the reference data object. Let’s add the Author named Jodi Kantor, who writes for the New York Times:

  import weaviate

client = weaviate.Client("http://localhost:8080")

example_data = {
    "name": "Jodi Kantor",
    "writesFor": [{
      "beacon": "weaviate://localhost/f81bfe5e-16ba-4615-a516-46c2ae2e5a80"
    }]
}

data_uuid = (
  client.data_object
  .create(
    example_data,
    "Author",
    uuid="36ddd591-2dee-4e7e-a3cc-eb86d30a4303" # optional, if not provided ons is going to be generated
  )
)
  const weaviate = require("weaviate-client");

const client = weaviate.client({
  scheme: 'http',
  host: 'localhost:8080',
});

client.data
      .creator()
      .withClassName('Author')
      .withId("36ddd591-2dee-4e7e-a3cc-eb86d30a4303")
      .withProperties({
        "name": "Jodi Kantor",
        "writesFor": [{
          "beacon": "weaviate://localhost/f81bfe5e-16ba-4615-a516-46c2ae2e5a80"
        }]
      })
      .do()
      .then(res => {
          console.log(res)
      })
      .catch(err => {
          console.error(err)
      });
  package main

import (
  "context"
  "fmt"

  "github.com/semi-technologies/weaviate-go-client/v4/weaviate"
)

func main() {
  cfg := weaviate.Config{
    Host:   "localhost:8080",
    Scheme: "http",
  }
  client := weaviate.New(cfg)

  dataSchema := map[string]interface{}{
    "name": "Jodi Kantor",
    "writesFor": map[string]string{
      "beacon": "weaviate://localhost/f81bfe5e-16ba-4615-a516-46c2ae2e5a80",
    },
  }

  created, err := client.Data().Creator().
    WithClassName("Author").
    WithID("36ddd591-2dee-4e7e-a3cc-eb86d30a4303").
    WithProperties(dataSchema).
    Do(context.Background())

  if err != nil {
    panic(err)
  }
  fmt.Printf("%v", created)
}
  package technology.semi.weaviate;

import java.util.HashMap;
import java.util.Map;
import technology.semi.weaviate.client.Config;
import technology.semi.weaviate.client.WeaviateClient;
import technology.semi.weaviate.client.base.Result;
import technology.semi.weaviate.client.v1.data.model.WeaviateObject;

public class App {
  public static void main(String[] args) {
    Config config = new Config("http", "localhost:8080");
    WeaviateClient client = new WeaviateClient(config);

    Map<String, Object> dataSchema = new HashMap<>();
    dataSchema.put("name", "Jodi Kantor");
    dataSchema.put("writesFor", new HashMap() {
      {
        put("beacon", "weaviate://localhost/f81bfe5e-16ba-4615-a516-46c2ae2e5a80");
      }
    });

    Result<WeaviateObject> result = client.data().creator()
      .withClassName("Author")
      .withID("36ddd591-2dee-4e7e-a3cc-eb86d30a4303")
      .withProperties(dataSchema)
      .run();

    if (result.hasErrors()) {
      System.out.println(result.getError());
      return;
    }
    System.out.println(result.getResult());
  }
}
  $ curl \
    -X POST \
    -H "Content-Type: application/json" \
    -d '{
      "class": "Author",
      "id": "36ddd591-2dee-4e7e-a3cc-eb86d30a4303",
      "properties": {
          "name": "Jodi Kantor",
          "writesFor": [{
            "beacon": "weaviate://localhost/f81bfe5e-16ba-4615-a516-46c2ae2e5a80"
          }]
      }
  }' \
    http://localhost:8080/v1/objects

You can also add references later, when the data object is already created. The following example first creates the Author with name, and later adds the reference to a Publication. This comes in handy when you need to create data objects first before you can add references.

  import weaviate
import time

client = weaviate.Client("http://localhost:8080")

example_data = {
    "name": "Jodi Kantor"
}

client.data_object.create(
  example_data,
  "Author",
  uuid="36ddd591-2dee-4e7e-a3cc-eb86d30a4303", # optional, if not provided ons is going to be generated
)

client.data_object.reference.add(
  from_uuid="36ddd591-2dee-4e7e-a3cc-eb86d30a4303",
  from_property_name="writesFor",
  to_uuid="f81bfe5e-16ba-4615-a516-46c2ae2e5a80",
  from_class_name="Author",
  to_class_name='Publication',
)
  const weaviate = require("weaviate-client");

const client = weaviate.client({
  scheme: 'http',
  host: 'localhost:8080',
});

client.data
      .creator()
      .withClassName('Author')
      .withId("36ddd591-2dee-4e7e-a3cc-eb86d30a4303")
      .withProperties({
        "name": "Jodi Kantor"
      })
      .do()
      .then(res => {
          console.log(res)
      })
      .catch(err => {
          console.error(err)
      });

client.data
      .referenceCreator()
      .withId("36ddd591-2dee-4e7e-a3cc-eb86d30a4303")
      .withReferenceProperty('writesFor')
      .withReference(
        client.data
          .referencePayloadBuilder()
          .withId("f81bfe5e-16ba-4615-a516-46c2ae2e5a80")
          .payload(),
      )
      .do()
      .then(res => {
          console.log(res)
      })
      .catch(err => {
          console.error(err)
      });
  package main

import (
  "context"

  "github.com/semi-technologies/weaviate-go-client/v4/weaviate"
)

func main() {
  cfg := weaviate.Config{
    Host:   "localhost:8080",
    Scheme: "http",
  }
  client := weaviate.New(cfg)

  dataSchema := map[string]interface{}{
    "name": "Jodi Kantor",
  }

  _, err := client.Data().Creator().WithClassName("Author").WithID("36ddd591-2dee-4e7e-a3cc-eb86d30a4303").WithProperties(dataSchema).Do(context.Background())
  if err != nil {
    panic(err)
  }

  reference := client.Data().ReferencePayloadBuilder().WithID("f81bfe5e-16ba-4615-a516-46c2ae2e5a80").Payload()

  err = client.Data().ReferenceCreator().
    WithID("36ddd591-2dee-4e7e-a3cc-eb86d30a4303").
    WithReferenceProperty("writesFor").
    WithReference(reference).
    Do(context.Background())

  if err != nil {
    panic(err)
  }
}
  package technology.semi.weaviate;

import java.util.HashMap;
import java.util.Map;
import technology.semi.weaviate.client.Config;
import technology.semi.weaviate.client.WeaviateClient;
import technology.semi.weaviate.client.base.Result;
import technology.semi.weaviate.client.v1.data.model.SingleRef;
import technology.semi.weaviate.client.v1.data.model.WeaviateObject;

public class App {
  public static void main(String[] args) {
    Config config = new Config("http", "localhost:8080");
    WeaviateClient client = new WeaviateClient(config);

    Map<String, Object> dataSchema = new HashMap<>();
    dataSchema.put("name", "Jodi Kantor");

    Result<WeaviateObject> result = client.data().creator()
      .withClassName("Author")
      .withID("36ddd591-2dee-4e7e-a3cc-eb86d30a4303")
      .withProperties(dataSchema)
      .run();

    if (result.hasErrors()) {
      System.out.println(result.getError());
      return;
    }
    System.out.println(result.getResult());

    SingleRef reference = client.data().referencePayloadBuilder().withID("f81bfe5e-16ba-4615-a516-46c2ae2e5a80").payload();

    Result<Boolean> result_ref = client.data().referenceCreator()
      .withID("36ddd591-2dee-4e7e-a3cc-eb86d30a4303")
      .withReferenceProperty("writesFor")
      .withReference(reference)
      .run();

    if (result_ref.hasErrors()) {
      System.out.println(result_ref.getError());
      return;
    }
    System.out.println(result_ref.getResult());
  }
}
  $ curl \
    -X POST \
    -H "Content-Type: application/json" \
    -d '{
      "class": "Author",
      "id": "36ddd591-2dee-4e7e-a3cc-eb86d30a4303",
      "properties": {
          "name": "Jodi Kantor"
      }
  }' \
    http://localhost:8080/v1/objects
$ curl \
    -X POST \
    -H "Content-Type: application/json" \
    -d '{
      "beacon": "weaviate://localhost/f81bfe5e-16ba-4615-a516-46c2ae2e5a80"
  }' \
    http://localhost:8080/v1/objects/36ddd591-2dee-4e7e-a3cc-eb86d30a4303/references/writesFor

Next steps

More Resources

If you can’t find the answer to your question here, please look at the:

  1. Frequently Asked Questions. Or,
  2. Knowledge base of old issues. Or,
  3. For questions: Stackoverflow. Or,
  4. For issues: Github. Or,
  5. Ask your question in the Slack channel: Slack.