I'm trying to import the following json in hive
[{"time":1521115600,"latitude":44.3959,"longitude":26.1025,"altitude":53,"pm1":21.70905,"pm25":16.5,"pm10":14.60085,"gas1":0,"gas2":0.12,"gas3":0,"gas4":0,"temperature":null,"pressure":0,"humidity":0,"noise":0},{"time":1521115659,"latitude":44.3959,"longitude":26.1025,"altitude":53,"pm1":24.34045,"pm25":18.5,"pm10":16.37065,"gas1":0,"gas2":0.08,"gas3":0,"gas4":0,"temperature":null,"pressure":0,"humidity":0,"noise":0},{"time":1521115720,"latitude":44.3959,"longitude":26.1025,"altitude":53,"pm1":23.6826,"pm25":18,"pm10":15.9282,"gas1":0,"gas2":0,"gas3":0,"gas4":0,"temperature":null,"pressure":0,"humidity":0,"noise":0},{"time":1521115779,"latitude":44.3959,"longitude":26.1025,"altitude":53,"pm1":25.65615,"pm25":19.5,"pm10":17.25555,"gas1":0,"gas2":0.04,"gas3":0,"gas4":0,"temperature":null,"pressure":0,"humidity":0,"noise":0}]
CREATE TABLE json_serde (
s array<struct<time: timestamp, latitude: string, longitude: string, pm1: string>>)
ROW FORMAT SERDE 'org.openx.data.jsonserde.JsonSerDe'
WITH SERDEPROPERTIES (
'mapping.value' = 'value'
)
STORED AS TEXTFILE
location '/user/hduser';
the import works but if i try
Select * from json_serde;
it will return from every document that is on hadoop/user/hduser only the first element per file.
there is a good documentation on working with json array??
The JSON that you provided is not correct. A JSON always starts with an opening curly brace
"{"
and ends with an ending curly brace"}"
. So, the first thing to look out here is that your JSON is wrong.Your JSON should have been like the one below:
And, the second thing is you have declared the data-type of the "time" field as timestamp. But the data (1521115600) is in milliseconds. The timestamp data-type need data in the format YYYY-MM-DD HH:MM:SS[.fffffffff].
So, your data should ideally be in the below format:
Now, you can use query to select the records from the table.
If you want each value separately displayed in tabular format, you can use the below query.
The result of the above query would be like this:
Beautiful. Is n't it?
Happy Learning.
You have an array of structs. What you pasted is only one line.
If you want to see all the elements, you need to use inline
Alternatively, you need to rewrite your files such that each object within that array is a single JSON object on its own line of the file
Also, I don't see a value field in your data, so I'm not sure what you're mapping in the serde properties
If you can not use update your input file format you can directly import in spark and use it, once data is finalized write back to Hive table.
Alternatively you can directly hive table
If I may suggest you another approach to just load the whole
JSON
string into a column asString
datatype into an external table. The only restriction is to defineLINES TERMINATED BY
properly. e.g. If you may have each json in one line, then you can create table as below:e.g.
Use Hive
get_json_object
to extract individual columns. this commands support basicxPath
like query to json string E.g.If json_data column has below JSON string
The below query fetches
returns
amy
In this way you could extract each
json
element as column from the table.