如何將Mongodb資料匯入Hive中。
原理:
By default, any table created in Hive is HDFS-based; that is, the metadata and underlying rows of data associated with the table is stored in HDFS. Mongo-Hadoop now supports the creation of MongoDB-based Hive tables and BSON-based Hive tables. Both MongoDB-based Hive tables and BSON-based Hive tables can be:
Queried just like HDFS-based Hive tables. Combined with HDFS-based Hive tables in joins and sub-queries
這裡解釋就是先在hive中建立一個hive-base的表,只有schema資訊,實際資料存在於mongodb。
前期準備: 1.下載或者build source code 產生jar包。mongodb-hadoop-core, mongodb-hadoop-hive jar包。 2.擷取mongdb-java-driver。 3.添加到hive中。 ADD JAR /path-to/mongo-hadoop-hive-<version>.jar
開始,建表:
CREATE TABLE individuals ( id INT, name STRING, age INT, work STRUCT<title:STRING, hours:INT> ) STORED BY 'com.mongodb.hadoop.hive.MongoStorageHandler' WITH SERDEPROPERTIES('mongo.columns.mapping'='{"id":"_id","work.title":"job.position"}') TBLPROPERTIES('mongo.uri'='mongodb://localhost:27017/test.persons’);
OK。外表已經建立好。接下來可以在hive中查詢到mongodb的資料了。 開始查詢:
SELECT name, age FROM individuals WHERE id > 100;
=============我是分割線==============================
然後可以利用create table select * ...來建表。 或者先create table。 然後在insert into ..select *…
上述兩種即可將mongodb資料移轉到hive中。