如何将json数据导入到Hive中

大神有没有人讲详细点的,我想讲解下,如何将json数据导入到Hive中
最新回答
夜见树

2025-03-01 05:03:36

Solution 1 : 将json格式数据导入到MongoDB中,然后MongoDB可以将数据转换为CSV格式数据,然后导入到mysql中;

CSSer.com采用的是wordpress程序,数据库为mysql,要想移植到MongoDB数据库,则需要进行数据转换。

数据转移有多种方案,本质上需要将mysql数据转换为一种MongoDB可以直接导入的格式即可。MongoDB提供了mongoimport工具,可以支持导入json,csv的格式。

先来看一下mongoimport支持的参数:

$ mongoimport --help
options:
--help produce help message
-v [ --verbose ] be more verbose (include multiple times for more
verbosity e.g. -vvvvv)
-h [ --host ] arg mongo host to connect to ( <set name>/s1,s2 for sets)
--port arg server port. Can also use --host hostname:port
--ipv6 enable IPv6 support (disabled by default)
-u [ --username ] arg username
-p [ --password ] arg password
--dbpath arg directly access mongod database files in the given
path, instead of connecting to a mongod server -
needs to lock the data directory, so cannot be used
if a mongod is currently accessing the same path
--directoryperdb if dbpath specified, each db is in a separate
directory
-d [ --db ] arg database to use
-c [ --collection ] arg collection to use (some commands)
-f [ --fields ] arg comma separated list of field names e.g. -f name,age
--fieldFile arg file with fields names - 1 per line
--ignoreBlanks if given, empty fields in csv and tsv will be ignored
--type arg type of file to import. default: json (json,csv,tsv)
--file arg file to import from; if not specified stdin is used
--drop drop collection first
--headerline CSV,TSV only - use first line as headers
--upsert insert or update objects that already exist
--upsertFields arg comma-separated fields for the query part of the
upsert. You should make sure this is indexed
--stopOnError stop importing at first error rather than continuing
--jsonArray load a json array, not one item per line. Currently
limited to 4MB.

由上面的帮助文档可以看出,采用csv作为中间数据格式,无论对于mysql的导出,还是mongodb的导入,都算得上是成本最低了,于是一回就尝试了一把:

首先,将mysql数据库中的wp-posts表导出,一回偷懒了,直接用phpmyadmin的导出功能,选择csv格式导出,并选中了“删除字段中的换行符”以及“将字段名放在第一行”,保存文件名为csser.csv。

接着,到mongodb服务器,shell下连接MongoDB数据库,并进行数据导入:

$ mongoimport -d csser -c posts -type csv -file csser.csv --headerline
connected to: 127.0.0.1
imported 548 objects

$ mongo
MongoDB shell version: 1.8.1
connecting to: test
> use csser
switched to db csser
> db.posts.count()
547

> db.posts.find({}, {"post_title":1}).sort({"ID":-1}).limit(1)
{ "_id" : ObjectId("4df4641d31b0642fe609426d"), "post_title" : "CSS Sprites在线应用推荐-CSS-sprit" }

Solution2 : 通过Hive中SerDe将JSON数据转换为hive理解的数据格式,原因有:

1、创建Hive表使用序列化时,需要自写一个实现Deserializer的类,并且选用create命令的row format参数;

2、在处理海量数据的时候,如果数据的格式与表结构吻合,可以用到Hive的反序列化而不需要对数据进行转换,可以 节省大量的时间。