Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8800765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:40:34+00:00 2026-06-14T00:40:34+00:00

I have a many-to-many mapping table between two collections. Each row in the mapping

  • 0

I have a many-to-many mapping table between two collections. Each row in the mapping table represents a possible mapping with a weight score.

mapping(id1, id2, weight)

Query: Generate one to one mapping between id1 and id2. Use lowest weight to remove duplicate mappings. If there is tie, output any arbitrary one.

Example input:

(1, X, 1)
(1, Y, 2)
(2, X, 3)
(2, Y, 1)
(3, Z, 2)

Output

(1, X)
(2, Y)
(3, Z)

1 and 2 are both mapped to X and Y. We pick mapping (1, X) and (2, Y) because they have the lowest weight.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T00:40:35+00:00Added an answer on June 14, 2026 at 12:40 am

    Solved it by using Java UDF. it’s not perfect in a sense that it won’t maximize the number of one-to-one mappings but it’s good enough.

    Pig:

    d = load 'test' as (fid, iid, priority:double);
    g = group d by fid;
    o = foreach g generate FLATTEN(com.propeld.pig.DEDUP(d)) as (fid, iid, priority);
    store o into 'output';
    
    g2 = group o by iid;
    o2 = foreach g2 generate FLATTEN(com.propeld.pig.DEDUP(o)) as (fid, iid, priority);
    store o2 into 'output2';
    

    Java UDF:

    package com.propeld.pig;
    
    import java.io.IOException;
    import java.util.Iterator;
    
    import org.apache.pig.Algebraic;
    import org.apache.pig.EvalFunc;
    import org.apache.pig.PigException;
    import org.apache.pig.backend.executionengine.ExecException;
    import org.apache.pig.data.DataBag;
    import org.apache.pig.data.Tuple;
    import org.apache.pig.data.TupleFactory;
    
    public class DEDUP extends EvalFunc<Tuple> implements Algebraic{
        public String getInitial() {return Initial.class.getName();}
        public String getIntermed() {return Intermed.class.getName();}
        public String getFinal() {return Final.class.getName();}
        static public class Initial extends EvalFunc<Tuple> {
            private static TupleFactory tfact = TupleFactory.getInstance();
            public Tuple exec(Tuple input) throws IOException {
                // Initial is called in the map.
                // we just send the tuple down
                try {
                    // input is a bag with one tuple containing
                    // the column we are trying to operate on
                    DataBag bg = (DataBag) input.get(0);
                    if (bg.iterator().hasNext()) {
                        Tuple dba = (Tuple) bg.iterator().next();
                        return dba;
                    } else {
                        // make sure that we call the object constructor, not the list constructor
                        return tfact.newTuple((Object) null);
                    }
                } catch (ExecException e) {
                    throw e;
                } catch (Exception e) {
                    int errCode = 2106;
                    throw new ExecException("Error executing an algebraic function", errCode, PigException.BUG, e);
                }
            }
        }
        static public class Intermed extends EvalFunc<Tuple> {
            public Tuple exec(Tuple input) throws IOException {
                return dedup(input);
            }
        }
        static public class Final extends EvalFunc<Tuple> {
            public Tuple exec(Tuple input) throws IOException {return dedup(input);}
        }
    
        static protected Tuple dedup(Tuple input) throws ExecException, NumberFormatException {
            DataBag values = (DataBag)input.get(0);
            Double min = Double.MAX_VALUE;
            Tuple result = null;
            for (Iterator<Tuple> it = values.iterator(); it.hasNext();) {
                Tuple t = (Tuple) it.next();
    
                if ((Double)t.get(2) < min){
                    min = (Double)t.get(2);
                    result = t;
                }
            }
            return result;
        }
    
        @Override
        public Tuple exec(Tuple input) throws IOException {
            return dedup(input);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem :( I have a many-many table between two tables(1&2), via
I have created a many to many mapping in spring roo between two entities
I have a Users table and a Networks table with a many-to-many relationship between
I have two domain classes with a many-2-many relationship between them, e.g. User and
I have one-to-many mapping in hibernate between a Parent and a Child . In
I am trying to generate a unidirectional one-to-many mapping between two JPA entities. In
I have a many to many relationship between two existing tables (User: UserId, Name)
I have parent child relationship between two entities(Parent and Child). My Parent mapping is
I have a simple application using ADO.NET EntityFramework using a many-to-many relationship between two
I have a many to many relationship between entities and there is a table

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.