Passing a parameter with a white space

Viewed 57

When I run my script with the command showed below, with police_force parameter set as "Surrey Police", it gives me an error

"ERROR org.apache.pig.tools.grunt.Grunt - ERROR 1000: Error during parsing. File not found: Police"" 

If I pass in the value as "Surrey_Police" it runs fine but doesn't return anything

-- knownvalues: dataset presented
-- date1: one date of comparison
-- date2: 2nd date of comparison
-- police_force: falls within 
-- crime_type
-- Usage: exec -param knownvalues='/user/cw/input/all.txt' -param date1='2017-05' -param date2='2017-06' -param police_force="Surrey Police" /home/xiaorui/CW/compare_crime.pig


knownvalues = LOAD '$knownvalues' USING PigStorage(',') AS (crimeid:chararray,month:chararray,reportedby:chararray,fallswithin:chararray,longitude:float,latitude:float,location:chararray,lsoacode:chararray,lsoaname:chararray,crimetype:chararray,lastoutcome:chararray,context:chararray);


knownvalues = SAMPLE knownvalues 0.00001;

location = FILTER knownvalues BY (fallswithin MATCHES $police_force);

first_date = FILTER location BY (month MATCHES '$date1');

second_date = FILTER location BY (month MATCHES '$date2');

DUMP first_date;

If i use the line below, code works as intended

location = FILTER knownvalues BY (fallswithin MATCHES 'Surrey Police');
1 Answers

I got it achieved with below steps.

a.)Firstly the police_force from filter command should be enclosed in single quotes like below. location = FILTER knownvalues BY (fallswithin MATCHES '$police_force');

b.) Secondly we need to include escaping character() as well along with either single or double quotes in execution command.

pig -x local -param knownvalues='/home/ec2-user/data' -param police_force="Surrey\ Police" /home/ec2-user/test.pig
or
pig -x local -param knownvalues='/home/ec2-user/data' -param police_force='Surrey\ Police' /home/ec2-user/test.pig

Below is my testing code and commands.

Pig Input data file: cat data

mary,19
john,18
joe,18
Surrey Police,20

Pig Sample Code: cat test.pig

knownvalues = LOAD '$knownvalues' USING PigStorage(',') AS (name:chararray,age:int);
dump knownvalues;
describe knownvalues;

location = FILTER knownvalues BY (name MATCHES '$police_force');

dump location;
describe location;

Output:

After load:

(mary,19)
(john,18)
(joe,18)
(Surrey Police,20)
knownvalues: {name: chararray,age: int}

After filter:

(Surrey Police,20)
location: {name: chararray,age: int}
Related