Created
May 6, 2013 00:40
-
-
Save PureKrome/5522741 to your computer and use it in GitHub Desktop.
RavenDb bulk insert example.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using Raven.Client.Document; | |
namespace ConsoleApplication6 | |
{ | |
internal class Program | |
{ | |
private static void Main(string[] args) | |
{ | |
// ** Arrange. | |
var documentStore = new DocumentStore | |
{ | |
Url = "http://localhost:8080", | |
DefaultDatabase = "BulkInsertTest" | |
}; | |
documentStore.Initialize(); | |
// Grab the data from wherever. This can be a file, a server, a space station .. anywhere. | |
var someStuff = ReadInStuffFromWherever(); | |
// ** Act. | |
// Now insert this quickly into the db. | |
using (var bulkInsert = documentStore.BulkInsert()) | |
{ | |
foreach (var stuff in someStuff) | |
{ | |
//stuff.Id = "stuffs/" + stuff.Id; | |
bulkInsert.Store(stuff); | |
} | |
} | |
// ** Assert. | |
// Now lets check if this worked. | |
IList<Stuff> existingStuff; | |
using (var documentSession = documentStore.OpenSession()) | |
{ | |
existingStuff = documentSession.Query<Stuff>().ToList(); | |
} | |
foreach (var stuff in existingStuff) | |
{ | |
Console.WriteLine("{0} : {1}", stuff.Id, stuff.SomeNumber); | |
} | |
documentStore.Dispose(); | |
} | |
private static IEnumerable<Stuff> ReadInStuffFromWherever() | |
{ | |
var results = new List<Stuff>(); | |
for (int i = 1; i <= 10; i++) | |
{ | |
results.Add(new Stuff {SomeNumber = i}); | |
} | |
return results; | |
} | |
private class Stuff | |
{ | |
public string Id { get; set; } | |
public int SomeNumber { get; set; } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment