DZone
Database Zone
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
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Database Zone > RavenDB Sharding–Map/Reduce in a cluster

RavenDB Sharding–Map/Reduce in a cluster

Oren Eini user avatar by
Oren Eini
·
Mar. 26, 12 · Database Zone · Interview
Like (0)
Save
Tweet
3.56K 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

  • Java Microservices: Code Examples, Tutorials, and More
  • SQL GROUP BY and Functional Dependencies: a Very Useful Feature
  • Caching Across Layers in Software Architecture
  • Are All Kubernetes Ingresses the Same?

Comments

Database Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • 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:

DZone.com is powered by 

AnswerHub logo