Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@lanceharper
Created November 12, 2012 07:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lanceharper/4057901 to your computer and use it in GitHub Desktop.
Save lanceharper/4057901 to your computer and use it in GitHub Desktop.
MapReduce
public class BlogPostCountByAuthorTask : MapReduceTask<BlogPost, BlogPostCountByAuthorTask.PostCountResult>
{
public class PostCountResult
{
public string AuthorName { get; set; }
public int PostCount { get; set; }
}
public BlogPostCountByAuthorTask()
{
Map = posts => from post in posts
select new
{
AuthorName = post.AuthorName,
PostCount = 1
};
Reduce = results => from agg in results
group agg by agg.AuthorName into authors
select new PostCountResult
{
AuthorName = authors.Key,
PostCount = authors.Sum(x => x.PostCount)
};
}
}
namespace MapReducing.Tests
{
public class MapReducingTests
{
private BlogPost[] blogPosts = new[]
{
new BlogPost { AuthorName = "jnelson" },
new BlogPost { AuthorName = "jnelson" },
new BlogPost { AuthorName = "jnelson" },
new BlogPost { AuthorName = "jnelson" },
new BlogPost { AuthorName = "chicks" },
new BlogPost { AuthorName = "chicks" },
new BlogPost { AuthorName = "chicks" },
new BlogPost { AuthorName = "cgossler" },
new BlogPost { AuthorName = "lharper" },
new BlogPost { AuthorName = "lharper" }
};
[Fact]
public void BlogPostCountByAuthorTask_Returns_Correct_Count()
{
var mapReduceTask = new BlogPostCountByAuthorTask();
var results = mapReduceTask.Execute(blogPosts);
Console.WriteLine(results.Count());
Assert.Equal(4, results.Single(result => result.AuthorName == "jnelson").PostCount);
Assert.Equal(3, results.Single(result => result.AuthorName == "chicks").PostCount);
Assert.Equal(1, results.Single(result => result.AuthorName == "cgossler").PostCount);
Assert.Equal(2, results.Single(result => result.AuthorName == "lharper").PostCount);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment