简体   繁体   中英

Cassnadra large collection insert with nested object in C#

I'm writing this question after long fight with Cassandra database. I would like to insert large collection (~1000000) of Movie objects:

    public class Movie
    {
        public Guid Id { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public int Year { get; set; }
        public string Genres { get; set; }
        public int Rating { get; set; }
        public string OriginalLanguage { get; set; }
        public string ProductionCountry { get; set; }
        public int VotingsNumber { get; set; }
        public Director Director { get; set; }
    }

with nested field Director:

    public class Director
    {
        public Guid Id { get; set; }
        public string Firstname { get; set; }
        public string Lastname { get; set; }
        public int Age { get; set; }
    }

I'm using DataStax C# Driver and I tied different ways, but still nothing. Currently my code looks like this:

    private void CreateSchema()
    {
        Session.Execute("CREATE KEYSPACE IF NOT EXISTS test WITH replication " +
                        "= {'class':'SimpleStrategy', 'replication_factor':3};");
        Session.Execute("CREATE TYPE IF NOT EXISTS test.director (" +
                        "firstname text," +
                        "lastname text," +
                        "age int," +
                        ");");
        Session.Execute("CREATE TABLE IF NOT EXISTS test.Movies (" +
                        "id uuid," +
                        "title text," +
                        "description text," +
                        "year int," +
                        "genres text," +
                        "rating int," +
                        "originallanguage text," +
                        "productioncountry text," +
                        "votingsnumber int," +
                        "director frozen<director>," +
                        "PRIMARY KEY (id)" +
                        ");");
    }

    public string TimeOfCollectionInsert(int collectionEntriesNumber)
    {
        var watch = new Stopwatch();

        try
        {
            IList<Movie> moviesList = Movie.CreateMoviesCollectionForCassandra(collectionEntriesNumber);
            var preparedStatements = new List<PreparedStatement>();
            var statementBinding = new List<BoundStatement>();

            for (int i = 0; i < collectionEntriesNumber; i++)
            {
                preparedStatements.Add(Session.Prepare("INSERT INTO test.Movies (id, title, description, year, genres, rating, originallanguage, productioncountry, votingsnumber, actors) VALUES (?,?,?,?,?,?,?,?,?,{ 'director': { firstname: 'DirectorName', lastname: 'DirectorLastname', age: 50 }});"));
            }
            for (int i = 0; i < collectionEntriesNumber; i++)
            {
                statementBinding.Add(preparedStatements[i].Bind(moviesList[i].Id, moviesList[i].Title, moviesList[i].Description, moviesList[i].Year, moviesList[i].Genres, moviesList[i].Rating, moviesList[i].OriginalLanguage, moviesList[i].ProductionCountry, moviesList[i].VotingsNumber));
            }

            watch.Start();
            for (int i = 0; i < collectionEntriesNumber; i++)
            {
                Session.Execute(statementBinding[i]);
            }
            watch.Stop();

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        return watch.ElapsedMilliseconds.ToString();

    }

Both methods runs successfuly, but I would like to create directors dynamically. I will be grateful for any help.

Is this good way for measure cassandra bulk insert performace?

Thanks, P

You have to map your Cassandra UDT (user defined type) director to your Director C# class.
For more information, you should read the documentation :
http://docs.datastax.com/en/developer/csharp-driver/2.7/csharp-driver/reference/21features/mappingUdts.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM