在C#中使用嵌套对象的Cassnadra大集合插入

时间:2015-10-31 22:37:02

标签: c# cassandra bulkinsert

在与Cassandra数据库长期斗争后,我正在写这个问题。我想插入大型集合(~1000000)的Movie对象:

    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; }
    }

使用嵌套字段Director:

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

我正在使用DataStax C#Driver而且我采用了不同的方式,但仍然没有。目前我的代码如下所示:

    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();

    }

两种方法都成功运行,但我想动态创建导向。 我将不胜感激任何帮助。

btw。这是衡量cassandra批量插入性能的好方法吗?

谢谢, P

1 个答案:

答案 0 :(得分:1)

您必须将Cassandra UDT(用户定义类型)director映射到Director C#类。
有关更多信息,请阅读文档:
http://docs.datastax.com/en/developer/csharp-driver/2.7/csharp-driver/reference/21features/mappingUdts.html