Cassnadra大型集合插入了C#中的嵌套对象
本文关键字:嵌套 对象 大型 集合 插入 Cassnadra | 更新日期: 2023-09-27 18:19:59
在与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; }
}
带嵌套字段控制器:
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();
}
这两种方法都运行成功,但我希望动态创建控制器。如果有任何帮助,我将不胜感激
顺便说一句 这是衡量卡桑德拉批量插入性能的好方法吗?
谢谢,P
您必须将Cassandra UDT(用户定义类型)director
映射到Director
C#类
有关详细信息,您应该阅读文档:
http://docs.datastax.com/en/developer/csharp-driver/2.7/csharp-driver/reference/21features/mappingUdts.html