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
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

Want to Get Rid of Documents with Duplicate Content?

Baxter Denney user avatar by
Baxter Denney
·
Oct. 31, 12 · Interview
Like (0)
Save
Tweet
Share
6.58K Views

Join the DZone community and get the full member experience.

Join For Free

Whether you’re combining data from two different data sources, have multiple purchases from the same customer or just entered the same data in a web form twice, it seems like everyone faces the problem of duplicate data at one point or the other.

In this blog post, we'll look at using views in Couchbase Server 2.0 to find matching fields among documents and retain the non duplicate documents. For the sake of this example, assume each document has three common user specified fields - first_name, last_name, postal_code. Using the ruby client for Couchbase Server and the faker ruby gem, you can build a simple data generator to load some sample duplicate data into Couchbase. To use ruby as a programming language with Couchbase, you should download the Ruby SDK here.

Here is an execution sample:

$ ruby ./generate.rb --help
Usage: generate.rb [options]
   -h, --hostname HOSTNAME           Hostname to connect to (default: 127.0.0.1:8091)
   -u, --user USERNAME               Username to log with (default: none)
   -p, --passwd PASSWORD            Password to log with (default: none)
   -b, --bucket NAME                 Name of the bucket to connect to (default: default)
   -t, --total-records NUM           The total number of the records to generate (default: 10000)
   -d, --duplicate-rate NUM          Each NUM-th record will be duplicate (default: 30)
   -?, --help                        Show this message

$ ruby ./generate.rb -t 1000 -d 5
     1000 / 1000
Each document in Couchbase has an user specified key which is accessible as meta.id in the map function of the view. In Figure 1 below, there are multiple documents loaded into Couchbase Server using the data generator client above.
 
 

 

Step 1

Write a custom map function that emits the document ID (meta.id) of all the documents if the a particular duplicate pattern matches (first_name, last_name, postal_code in this case).
function (doc, meta) {

      emit([doc.first_name + '-' + doc.last_name + '-' +  doc.postal_code], meta.id);
}
The map function defines when two documents are duplicates.  According to the map function defined above, two documents are duplicate when the first name, last name and postal code match. We use ‘-’ so that we prevent aliasing of the data when we concatenate the first name, last name and the postal code.
 
 

 

Step 2

The reduce function looks like -

function (keys, values, rereduce) {
  if (rereduce) {
    var res = [];
    for (var i = 0; i < values.length; i++){
      res = res.concat(values[i])
    }
    return res;
  } else {
    return values;
  }
}

After grouping, if there are more than one meta.id values, we concatenate them to get a list of meta.id's refering to a duplicate document.

Step 3

The core part of the data cleaner is written in Ruby.
require 'couchbase'
 
connection = Couchbase.connect(options)
ddoc = connection.design_docs[options[:design_document]]
view = ddoc.send(options[:view])
connection.run do
 view.each(:group => true) do |doc|
   dup_num = doc.value.size
   if dup_num > 1
      puts "left doc #{doc.value[0]}, "
      # delete documents from second to last
       connection.delete(doc.value[1..-1])
      puts "removed #{dup_num} duplicate(s)"
   end
 end
end
Connect to Couchbase Server and query the view. The value field is an array of meta.id’s that correspond to duplicate documents (matching first name, last name and postal code). If the array size is greater than 1, we delete all the documents except the one corresponding to the last meta.id.

If the number of meta.id’s in the value array is greater than 2, there are duplicate documents corresponding to that key. As shown in the figure above id19 and id20 are duplicate documents.

The output of the data cleaner script looks like -
 As shown in the figure below, duplicate documents are now eliminated.
Enjoy!
 
 
Document

Published at DZone with permission of Baxter Denney. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How Do the Docker Client and Docker Servers Work?
  • Why You Should Automate Code Reviews
  • Load Balancing Pattern
  • Beginners’ Guide to Run a Linux Server Securely

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: