【问题标题】:Cassnadra large collection insert with nested object in C#Cassandra大集合插入与C#中的嵌套对象
【发布时间】:2015-10-31 22:37:02
【问题描述】:

在与 Cassandra 数据库长期斗争后,我正在写这个问题。我想插入大量电影对象(~1000000):

    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# 驱动程序,并以不同的方式绑定,但仍然没有。目前我的代码如下所示:

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

    }

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

顺便说一句。这是衡量 cassandra 批量插入性能的好方法吗?

谢谢, P

【问题讨论】:

    标签: c# cassandra bulkinsert


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多