CDH5.15.1 hive 連接mongodb配置及增刪改查
阿新 • • 發佈:2018-09-19
uri str prope upd info with oot ODB ble
wget http://repo1.maven.org/maven2/org/mongodb/mongo-hadoop/mongo-hadoop-core/2.0.2/mongo-hadoop-core-2.0.2.jar wget http://repo1.maven.org/maven2/org/mongodb/mongo-java-driver/3.6.0/mongo-java-driver-3.6.0.jar 2. 放入hive的lib目錄 因為本人搭建的是CDH版本的hadoop集群,所以hive的lib目錄在: /opt/cloudera/parcels/CDH/lib/hive/lib 把上面三個jar包分別放入集群每個節點上的目錄中。然後做成軟鏈接,如下: ln -s mongo-hadoop-hive-2.0.2.jar mongo-hadoop-hive.jar ln -s mongo-hadoop-core-2.0.2.jar mongo-hadoop-core.jar ln -s mongo-java-driver-3.6.0.jar mongo-java-driver.jar 如圖:
3. mongdb中插入一些測試數據:
db.student.insert({"name":"張三","age":"22","sex":"男","class":"計算機2班"});//如果數據庫中不存在集合,就創建
db.student.insert({"name":"李四","age":"23","sex":"女","class":"計算機3班"});
db.student.insert({"name":"王五","age":"24","sex":"男","class":"計算機2班"});
db.student.insert({"name":"劉六","age":"25","sex":"男","class":"計算機3班"});
db.student.insert({"name":"趙七","age":"26","sex":"女","class":"計算機3班"});
db.student.insert({"name":"吳八","age":"28","sex":"女","class":"計算機2班"});
4. hive 中創建表
create external table student
( id string,
name string,
age string,
sex string,
class string
)
stored by ‘com.mongodb.hadoop.hive.MongoStorageHandler‘
with serdeproperties(‘mongo.columns.mapping‘=‘{"id":"_id","name":"name","age":"age","sex":"sex","class":"class"}‘)
tblproperties(‘mongo.uri‘=‘mongodb://root:[email protected]:40000/test_v3.student‘);
查詢數據如下:
select * from student;
1. 下載
wget http://repo1.maven.org/maven2/org/mongodb/mongo-hadoop/mongo-hadoop-hive/2.0.2/mongo-hadoop-hive-2.0.2.jarwget http://repo1.maven.org/maven2/org/mongodb/mongo-hadoop/mongo-hadoop-core/2.0.2/mongo-hadoop-core-2.0.2.jar wget http://repo1.maven.org/maven2/org/mongodb/mongo-java-driver/3.6.0/mongo-java-driver-3.6.0.jar 2. 放入hive的lib目錄 因為本人搭建的是CDH版本的hadoop集群,所以hive的lib目錄在: /opt/cloudera/parcels/CDH/lib/hive/lib 把上面三個jar包分別放入集群每個節點上的目錄中。然後做成軟鏈接,如下: ln -s mongo-hadoop-hive-2.0.2.jar mongo-hadoop-hive.jar ln -s mongo-hadoop-core-2.0.2.jar mongo-hadoop-core.jar ln -s mongo-java-driver-3.6.0.jar mongo-java-driver.jar 如圖:
數據是實時同步的:
mongodb中插入一條數據如下:
db.student.insert({"name":"楊十","age":"28","sex":"男","class":"計算機3班"});
在做查詢:
mongodb中修改一條數據如下:
db.student.update({"name":"張三"},{$set:{"name":"張無忌"}});#只想改某個key的value使用set
mongodb中刪除一條數據如下:
db.student.remove({"name":"張無忌"});#刪除
看張無忌大哥已經不在了,緬懷一下下。
CDH5.15.1 hive 連接mongodb配置及增刪改查