Is there a way to bulk import csv data into cosmos db gremlin API Azure?

54 Views Asked by At

I am new to Azure Cosmos db and i have to import data into my cosmos db account.

I have tried out https://learn.microsoft.com/en-us/azure/cosmos-db/gremlin/bulk-executor-dotnet#java

In this link they have imported some randomly generated values from a predefined seed values: SeedGenerationValues.java (Or I think so.)

Is there a way where I can input a few csv and the schema of the graph and get the complete graph?

I have tried to change each record into a gremlin query and tried to insert the data from local: DataWeekender-DataLoad.ipynb

But this is very inefficient and takes lot of time to import a large dataset (with 1 million records). Is there a way like the bulk import provided by Microsoft Azure which imports the data efficiently into the gremlin cosmos db?

Is there a repository which I can use where I can simply give the csv data's and the schema as input and get the complete graph, just like how we do in neo4j and import data?

1

There are 1 best solutions below

2
Balaji On

Is there a way to bulk import csv data into cosmos db gremlin API Azure

To import .csv file into Azure Cosmos DB Gremlin API you can use below code. Below is the .NET code which imports data into Cosmos DB Gremlin API container.

class Program
    {
        static async Task Main(string[] args)
        {
            string connectionString = "*****";
            CosmosClient client = new CosmosClient(connectionString);
            var database = client.GetDatabase("newdb");
            var container = database.GetContainer("Graph1");

            using (var reader = new StreamReader("C:\\Users\\****\\****\\Desktop\\MyTrails\\firstFile.csv"))
            using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
            {
                var records = csv.GetRecords<MyData>();

                foreach (var record in records)
                {
                    dynamic vertex = new
                    {
                        id = record.Id.ToString(),
                        label = "Person",
                        name = record.Name,
                        age = record.Age
                    };

                    try
                    {
                        await container.CreateItemAsync(vertex);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine($"Failed to insert record with id{record.Id}: {ex.Message}");
                    }
                }
            }
        }
    }

    public class MyData
    {
        [Index(0)]
        public int Id { get; set; }

        [Index(1)]
        public string Name { get; set; }

        [Index(2)]
        public int Age { get; set; }
    }

Output: enter image description here