You can download a template project directly from your workspace. You'll also need to generate an API token in the API integrations page. This token should be kept secret, as it has full access to your data.
The following code snippet shows how to create a graph object that can then be used to perform data operations. You can check the available methods in the API overview section.
usingCuriosity.Library;usingMicrosoft.Extensions.Logging;var token =Environment.GetEnvironmentVariable("CURIOSITY_API_TOKEN");var url ="http://your-workspace-address";if (string.IsNullOrEmpty(token)){PrintHelp();return-1;}var loggerFactory =LoggerFactory.Create(log =>log.AddConsole());var logger =loggerFactory.CreateLogger("My Connector");using (var graph =Graph.Connect(url, token,"My Connector") .WithLoggingFactory(loggerFactory)){try {awaitgraph.LogAsync("Starting connector");awaitCreateSchemaAsync(graph);awaitIngestDataAsync(graph);awaitgraph.CommitPendingAsync();awaitgraph.LogAsync("Finished connector"); }catch(Exception E) {awaitgraph.LogErrorAsync(E.ToString());throw; }}asyncTaskCreateSchemaAsync(Graph graph){ // Create your schemas in the graph here}asyncTaskIngestDataAsync(Graph graph){ // Your data connector logic goes here}voidPrintHelp() =>Console.WriteLine("Missing API token, you can set it using the CURIOSITY_API_TOKEN environment variable.");
from curiosity import Graphimport osdefmain(): url ="http://your-workspace-address" token = os.environ.get('CURIOSITY_API_TOKEN') connector_name ="My Connector"with Graph.connect(url, token, connector_name)as graph:create_schema(graph)ingest_data(graph)defcreate_schema(graph)# Create your schemas in the graph heredefingest_data(graph)# Your data connector logic goes hereif__name__=='__main__':main()
Creating your schema using the connector
You can define the schema of your graph directly in the data connector. Alternatively you can do the same from the user interface if you prefer, and omit this step in your data connector.
When you create a schema from the connector, it will never delete your existing data in case of conflicts due to non-compatible changes. You'll need to first manually delete the previous schema and then run the connector again. As long as the changes are compatible, you can add new fields to the schema, and they'll automatically be created next time you run, with default values for any existing data.
When working with edges, it is useful to structure your edge types with the help of a static class. This helps you avoid having strings for edge types everywhere, and helps with refactoring, so if you want to rename an edge type, all places in your connector will be automatically renamed.
If you create your edges directly in the Schema interface, you can also download such a helper class inside the API Integration template.
Ingesting data using the connector
There are two main steps when ingesting data into your workspace, creating the nodes and adding the relationships in the graph. The following code shows how you can add two distinct node types and link them together in the graph:
asyncTaskIngestDataAsync(Graph graph){var booksPerAuthor =awaitFetchBooksAsync();foreach(var (author, books) in booksPerAuthor) {var authorNode =graph.AddOrUpdate(author);foreach(var book in books) {var bookNode =graph.AddOrUpdate(book);graph.Link(authorNode, bookNode,Edges.AuthorOf,Edges.HasAuthor); } }}asyncTask<Dictionary<Author,Book[]>> FetchBooksAsync(){ // ... fetch books and authors from your data source ...}
For more information on available methods, please check the API overview section.