最近数据异构的项目时需要将MongoDB的数据导入Hive数据仓库中,总结了下,得出一下四种导入方案 1. mongoexport json文件导入 2. mongoexport csv文件导入 3. hive映射mongo库 4. mongodump bson 导入
缺点:mongo导出的json文件中,存在“$”符号,这在hive中无法识别
准备 准备这个3个jar包,版本根据自己的hive版本选定。 json-hive-schema-1.0-jar-with-dependencies.jar json-serde-1.3.8-jar-with-dependencies.jar json-udf-1.3.8-jar-with-dependencies.jar
配置
将json-serde-1.3.8-jar-with-dependencies.jar和json-udf-1.3.8-jar-with-dependencies.jar放到/data/cloudera/var/lib/hive下
在Hive的 hive-site.xml 的 Hive 服务高级配置代码段(安全阀) 中添加以下参数,以便在beeline中可执行admin的操作
<property> <name>hive.server2.authorization.external.exec</name> <value>true</value> </property> <property> <name>hive.security.authorization.enabled</name> <value>false</value> </property> <property> <name>hive.aux.jars.path</name> <value>/data/cloudera/var/lib/hive</value> </property>步骤
生成创建表的语句:java -jar json-hive-schema-1.0-jar-with-dependencies.jar students.dat(文件名) students_text1(表名)在hive命令行中:LOAD DATA LOCAL INPATH ‘/data/cloudera/students.dat’(数据文件) OVERWRITE INTO TABLE students_text1(导入的表)备注
如导入数据时出现json格式不兼容,在创建表时可添加属性:WITH SERDEPROPERTIES (“ignore.malformed.json” = “true”);hive中无法支持$符号。参考 https://github.com/rcongiu/Hive-JSON-Serde
缺点:mongo命令导出时无法指定分隔符,默认的分隔符“,”在数组列中无法分辨,导入时数据会出现错乱
步骤
使用mongo的工具导出csv文件,csv文件的分隔符不能使用“,”,因为使用“,”时,导入数组列会识别错;
将csv文件写入到HDFS中;
创建Hive表:
CREATE EXTERNAL TABLE table ( `_id` string, ...... text string) ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.OpenCSVSerde' WITH SERDEPROPERTIES ( "separatorChar" = "\;", "quoteChar" = "'", "escapeChar" = "\\" ) STORED AS TEXTFILE LOCATION '/user/hive/';,location不能与hdfs中的csv文件路径一样; - 导入数据:LOAD DATA INPATH ‘/user/seewo/mongo.csv’ OVERWRITE INTO TABLE table; - 使用select语句查看数据。Done!
备注 如使用mongoexport命令,无法指定csv文件的分隔符,需要写js才能完成。缺点: hive中无实体数据,如mongo数据丢失或无法访问,hive就无法使用
准备 准备这个3个jar包,版本根据自己的hive版本选定。 mongo-hadoop-core-2.0.0.jar mongo-hadoop-hive-2.0.0.jar mongo-java-driver-3.4.2.jar
步骤
将mongo-hadoop-core-2.0.0.jar、mongo-hadoop-hive-2.0.0.jar和mongo-java-driver-3.4.2.jar放到/data/cloudera/var/lib/hive下,/data/cloudera/var/lib/hive是参数hive.aux.jars.path的值;
在Hive中创建外部表,添加以下参数:
STORED BY 'com.mongodb.hadoop.hive.MongoStorageHandler' WITH SERDEPROPERTIES('mongo.columns.mapping'='{"id":"_id"}') TBLPROPERTIES('mongo.uri'='mongodb://user:password@ip:port/db.collection');例子:
CREATE EXTERNAL TABLE ep_class_student_performance_detail ( `_id` string, app_key string, ...... ) STORED BY 'com.mongodb.hadoop.hive.MongoStorageHandler' WITH SERDEPROPERTIES('mongo.columns.mapping'='{"id":"_id"}') TBLPROPERTIES('mongo.uri'='mongodb://${ip}:${port}/${mongodb}.${mongo_collection}');使用select语句查看数据。Done!
备注 此方案基于开源项目mongo-hadoop:https://github.com/mongodb/mongo-hadoop
缺点:目前非没发现缺点
准备 准备一下3个jar包,版本根据hive和MongoDB版本选择。 mongo-hadoop-core-2.0.0.jar mongo-hadoop-hive-2.0.0.jar mongo-java-driver-3.4.2.jar 将mongo-hadoop-core-2.0.0.jar、mongo-hadoop-hive-2.0.0.jar和mongo-java-driver-3.4.2.jar放到/data/cloudera/var/lib/hive下,/data/cloudera/var/lib/hive是参数hive.aux.jars.path的值
步骤
生成bson文件:mongodump -h host -d db -c collectio -o output_file,例子: mongodump -h ${ip}:${port} -d ${db} -c ${collection} -o /data/cloudera/mongodump; 将bson文件放入HDFS中:hdfs dfs -put output_file hdfs_file_path,例子: hdfs dfs -put /data/cloudera/mongodump/${db}/${collection}.bson /user/hive/; 创建Hive表: CREATE TABLE ${table_name} ( `_id` string, ...... value int) row format serde 'com.mongodb.hadoop.hive.BSONSerDe' with serdeproperties('mongo.columns.mapping'='{"id":"_id"}') stored as inputformat 'com.mongodb.hadoop.mapred.BSONFileInputFormat' outputformat 'com.mongodb.hadoop.hive.output.HiveBSONFileOutputFormat' location '/user/hive/';, 使用select语句查看数据。Done!