[Apache Pig] Dealing with Unexpected character ‘$’ error

Recently while working on an ETL script, encountered following exception when using REPLACE() API to remove $ from column.

2014-04-08 21:24:41,454 [main] ERROR org.apache.pig.PigServer - exception during parsing: Error during parsing. <file test.pig, line 16, column 51>  Unexpected character '$'
Failed to parse: <file test.pig, line 16, column 51>  Unexpected character '$'
	at org.apache.pig.parser.QueryParserDriver.parse(QueryParserDriver.java:243)
	at org.apache.pig.parser.QueryParserDriver.parse(QueryParserDriver.java:179)
	at org.apache.pig.PigServer$Graph.parseQuery(PigServer.java:1676)
	at org.apache.pig.PigServer$Graph.access$000(PigServer.java:1409)
	at org.apache.pig.PigServer.parseAndBuild(PigServer.java:342)
	at org.apache.pig.PigServer.executeBatch(PigServer.java:367)
	at org.apache.pig.PigServer.executeBatch(PigServer.java:353)
	at org.apache.pig.tools.grunt.GruntParser.executeBatch(GruntParser.java:140)
	at org.apache.pig.tools.grunt.GruntParser.processDump(GruntParser.java:769)
	at org.apache.pig.tools.pigscript.parser.PigScriptParser.parse(PigScriptParser.java:372)
	at org.apache.pig.tools.grunt.GruntParser.parseStopOnError(GruntParser.java:198)
	at org.apache.pig.tools.grunt.GruntParser.parseStopOnError(GruntParser.java:173)
	at org.apache.pig.tools.grunt.Grunt.exec(Grunt.java:84)
	at org.apache.pig.Main.run(Main.java:607)
	at org.apache.pig.Main.main(Main.java:156)

The cause was incorrect escaping in the REPLACE API.

Following is the simple fix

REPLACE($0, '\$', '')

Here, wanted to remove the $ character from the columns.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.