DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
11 Monitoring and Observability Tools for 2023
Learn more
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. RavenDB Sharding–Map/Reduce in a cluster

RavenDB Sharding–Map/Reduce in a cluster

Oren Eini user avatar by
Oren Eini
·
Mar. 26, 12 · Interview
Like (0)
Save
Tweet
Share
3.69K Views

Join the DZone community and get the full member experience.

Join For Free

in my previous post , i introduced ravendb sharding and discussed how we can use sharding in ravendb. we discussed both blind sharding and data driven sharding. today i want to introduce another aspect of ravendb sharding. the usage of map/reduce to  gather information from multiple shards.

we start by defining a map/reduce index. in this case, we want to look at the invoice totals per date. we define the index like this:

    public class invoicesamountbydate : abstractindexcreationtask<invoice, invoicesamountbydate.reduceresult>
    {
        public class reduceresult
        {
            public decimal amount { get; set; }
            public datetime issuedat { get; set; }
        }

        public invoicesamountbydate()
        {
            map = invoices =>
                  from invoice in invoices
                  select new
                  {
                      invoice.amount,
                    invoice.issuedat
                  };

            reduce = results =>
                     from result in results
                     group result by result.issuedat
                     into g
                     select new
                     {
                         amount = g.sum(x => x.amount),
                        issuedat = g.key
                     };
        }
    }

and then we execute the following code:

using (var session = documentstore.opensession())
{
    var asian = new company { name = "company 1", region = "asia" };
    session.store(asian);
    var middleeastern = new company { name = "company 2", region = "middle-east" };
    session.store(middleeastern);
    var american = new company { name = "company 3", region = "america" };
    session.store(american);

    session.store(new invoice { companyid = american.id, amount = 3, issuedat = datetime.today.adddays(-1)});
    session.store(new invoice { companyid = asian.id, amount = 5, issuedat = datetime.today.adddays(-1) });
    session.store(new invoice { companyid = middleeastern.id, amount = 12, issuedat = datetime.today });
    session.savechanges();
}

we use a three way sharding, based on the region of the company, so we actually have the following document sin three different servers:

first server, asia:

image

second server, middle east:

image

third server, america:

image

now, let us see what happen when we use the map/reduce query:

using (var session = documentstore.opensession())
{
    var reduceresults = session.query<invoicesamountbydate.reduceresult, invoicesamountbydate>()
        .tolist();

    foreach (var reduceresult in reduceresults)
    {
        string datestr = reduceresult.issuedat.tostring("mmm dd, yyyy", cultureinfo.invariantculture);
        console.writeline("{0}: {1}", datestr, reduceresult.amount);
    }
    console.writeline();
}

as you can see, again, we make no distinction in our code about using sharding, we just query it normally. the results, however, are quite interesting:

image

as you can see, we got the correct results, cluster wide .

ravendb was able to query all the servers in the cluster for their results, reduce them again, and get us the total across all three servers.

and that, my friends, it truly awesome.

cluster

Published at DZone with permission of Oren Eini, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 5 Common Firewall Misconfigurations and How to Address Them
  • Spring Cloud
  • An End-to-End Guide to Vue.js Testing
  • DevSecOps: The Future of Secure Software Development

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: