How to escape a special character in Hive shell/JDBC statement?

Liam picture Liam · May 1, 2013 · Viewed 8.8k times · Source

I'm trying to create an external table from a JSON file, which looks like this:

{"id": "0010001", "my$entities": [{"a": "foo", "b": "bar"}]}

this is a file on HDFS and I don't have control on it.

I was using rcongiu's JSON serde and wrote my statement like this:

create external table json3(  id STRING,
my$entities STRING ) 
ROW FORMAT SERDE 'org.openx.data.jsonserde.JsonSerDe'
LOCATION '/my/path/to/dir/';

But then the hive shell kept complaining:

cannot recognize the input near 'ities' 'STRING' ')' in column type

I assume that it's the dollar sign "$" causing the problem. For right now I've tried using a backtick(`) and backward, forward slash to escape it but with no luck. I would like to know if there's a way to get around this without changing the original data?

Answer

Lorand Bendig picture Lorand Bendig · May 2, 2013

You can't use a $ sign in a Hive column name, but you can map my$entities to any other valid name. E.g:

create external table json3(  id STRING,
myentities STRING ) 
ROW FORMAT SERDE 'org.openx.data.jsonserde.JsonSerDe'
WITH SERDEPROPERTIES ( "mapping.myentities" = "my$entities" )  
LOCATION '/my/path/to/dir/';

Here the Hive column name myentity is mapped to the JSON attribute my$entity.
Reference: Hive-JSON-Serde readme