0

I have a pig script.

Script.pig:

register /home/cloudera/Desktop/Pow.jar # registering the jar file
A = LOAD '/input.txt' using PigStorage(',') as (a1:int,a2:int,name:chararray); # loading the relation
B = foreach A generate A.a1,A.a2,Pow(A.a1,A.a2); # just generating field1,field2
dump B;# dumping the result

A java UDF to calculate power function.

import java.io.IOException;

import org.apache.pig.EvalFunc;
import org.apache.pig.PigWarning;
import org.apache.pig.data.Tuple;

// Pow function to calculate the power of two numbers    
public class Pow extends EvalFunc<Long> {

public Long exec(Tuple input) throws IOException {
        try {

            int base = (Integer)input.get(0);# Getting the base value from tuple.
            int exponent = (Integer)input.get(1);# Getting the second value from tuple.
            long result = 1;

            /* Probably not the most efficient method...*/
            for (int i = 0; i < exponent; i++) {
                long preresult = result;
                result *= base;
                if (preresult > result) {
                    // We overflowed.  Give a warning, but do not throw an
                    // exception.
                    warn("Overflow!", PigWarning.TOO_LARGE_FOR_INT);
                    // Returning null will indicate to Pig that we failed but
                    // we want to continue execution.
                    return null;
                }
            }
            return result;
        } catch (Exception e) {
            // Throwing an exception will cause the task to fail.
            throw new IOException("Something bad happened!", e);
        }
    }


}

Input file

Input.txt
1,2,Vijay
3,4,Ram

I am getting following error when I run the script

 ERROR 1066: Unable to open iterator for alias 

    B. Backend error : Scalar has more than one row in the output. 

    1st : (1,2,Vijay), 2nd :(3,4,Ram)# Error at this point

Please help me to solve this problem and also am new to Apache pig.

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
user846445
  • 221
  • 1
  • 3
  • 14
  • For people who found this post when looking for [ERROR 1066: Unable to open iterator for alias](http://stackoverflow.com/questions/34495085/error-1066-unable-to-open-iterator-for-alias-in-pig-generic-solution) here is a [generic solution](http://stackoverflow.com/a/34495086/983722). – Dennis Jaheruddin Dec 28 '15 at 14:34

1 Answers1

1
B = foreach A generate A.a1,A.a2,Pow(A.a1,A.a2);

should be

B = foreach A generate a1,a2,Pow(a1,a2);
LiMuBei
  • 2,868
  • 22
  • 27
  • Can you explain how your answer works? What is the difference? – rfornal Feb 14 '15 at 00:57
  • Using the dot notation in this case is not correct. The generate statement refers to the fields of the relation which are referred to by simply using the name. The dot notation is used to refer to fields of a tuple inside a relation. See http://pig.apache.org/docs/r0.12.1/basic.html#relations – LiMuBei Feb 14 '15 at 16:28
  • 1
    @user846445 If this helped, please show that your question is answered by accepting (ticking the green V in front of the answer). – Dennis Jaheruddin Dec 28 '15 at 14:34