Is there any particular reason you are not using PigStorage? Because it could make life so much easier for you :) .
a = load '/file1' USING PigStorage('_') AS (num:int, char:chararray);
b = load '/file2' USING PigStorage('\t') AS (num:int, char:chararray);
c = join a by num, b by num;
dump c;
Also note that, in file1 you used underscore as delimiter, but you give "-" as argument to STRSPLIT.
edit:
I have spent some more time on the scripts you provided; script 1 & 2 indeed does not work and the script 3 also works like this (without the extra foreach):
a = load 'file1' as (str:chararry);
b = load 'file2' as (num:int, ch:chararry);
a1 = foreach a generate flatten(STRSPLIT(str,'_',2));
c = join a1 by (int)($0), b by num;
dump c;
As for the source of the problem, i'll take a wild guess and say it might be related to this (as stated in Pig Documentation) combined with pig's run cycle optimizations :
If you FLATTEN a bag with empty inner schema, the schema for the resulting relation is null.
In your case, I believe schema of the STRSPLIT result is unknown until runtime.
edit2:
Ok, here is my theory explained:
This is the complete -explain- output for script 2 and this is for script 3. I'll just paste the interesting parts here.
|---a2: (Name: LOForEach Schema: num#288:int,ch#289:chararray)
| | |
| | (Name: LOGenerate[false,false] Schema: num#288:int,ch#289:chararray)ColumnPrune:InputUids=[288, 289]ColumnPrune:OutputUids=[288, 289]
| | | |
| | | (Name: Cast Type: int Uid: 288)
| | | |
| | | |---num:(Name: Project Type: int Uid: 288 Input: 0 Column: (*))
Above section is for script 2; see the last line. It assumes output of flatten(STRSPLIT)
will have a first element of type integer
(because you provided the schema that way). But in fact STRSPLIT
has a null
output schema which is treated as bytearray
fields; so output of flatten(STRSPLIT)
is actually (n:bytearray, c:bytearray)
. Because you provided a schema, pig tries to make a java cast (to the output of a1
) to num
field; which fails as num
is in fact a java String
represented as bytearray. Since this java-cast fails, pig does not even try to make the explicit cast in the line above.
Let's see the situation for script 3:
|---a2: (Name: LOForEach Schema: num#85:int,ch#87:bytearray)
| | |
| | (Name: LOGenerate[false,false] Schema: num#85:int,ch#87:bytearray)ColumnPrune:InputUids=[]ColumnPrune:OutputUids=[85, 87]
| | | |
| | | (Name: Cast Type: int Uid: 85)
| | | |
| | | |---(Name: Project Type: bytearray Uid: 85 Input: 0 Column: (*))
See the last line, here output of a1
is properly treated as bytearray
, no problems here. And now look at the second to last line; pig tries (and succeeds) to make an explicit cast operation from bytearray
to integer
.