2015-10-31 89 views
-2

我在與Cassandra數據庫長時間戰鬥後寫這個問題。我想插入Movie對象的大集合(〜1000000):Cassnadra大集合嵌入C中的嵌套對象#

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

嵌套場導演:

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

    } 

這兩種方法都能成功運行,但我想動態創建導向器。 我將不勝感激任何幫助。

btw。這是測量卡桑德拉大容量插入性能的好方法嗎?

感謝, P

回答