简体   繁体   English

使用Java-Hadoop连接器更新MongoDB中的现有集合

[英]Update an existing collection in MongoDB using Java-Hadoop connector

Is it possible to update existing MongoDB collection with new data. 是否可以使用新数据更新现有的MongoDB集合。 I am using hadoop job to read write data to Mongo. 我正在使用hadoop作业将写入数据读取到Mongo。 Required scenario is :- Say first collection in Mongo is 所需方案为:-说Mongo的第一个收藏是

{ 
  "_id" : 1,
  "value" : "aaa"
  "value2" : null
}

after reading data from Mongo and processing data, MongoDB should contain 从Mongo读取数据并处理数据后,MongoDB应该包含

{ 
  "_id" : 1,
  "value" : "aaa"
  "value2" : "bbb"
}

If possible, please provide some dummy code. 如果可能,请提供一些伪代码。

BasicBSONObject query=new BasicBSONObject();
query.append("fieldname", value);
BasicBSONObject update=new BasicBSONObject();

update.append("$set", new BasicBSONObject().append("newfield",value));
MongoUpdateWritable muw=new MongoUpdateWritable(query,update,false,true);
contex.write(key, muw);

query : is used for providing condition(matching condition). query :用于提供条件(匹配条件)。

update : is used for adding new field and value in existing collection. update :用于在现有集合中添加新字段和值。

MongoUpdateWritable : 3rd parameter is upsert value(same as mongodb) MongoUpdateWritable :第三个参数是upsert值(与mongodb相同)

4th parameter is multiple update in many documents in a collection. 第四个参数是集合中许多文档中的多次更新。

Set in Driver class job.setOutputValueClass(MongoUpdateWritable.class); 在Driver类中设置job.setOutputValueClass(MongoUpdateWritable.class);

我通过扩展org.apache.hadoop.mapreduce.RecordWriter并覆盖此类的write方法来完成此操作。

The Mongo-Hadoop Connector doesn't currently suppor this feature. Mongo-Hadoop Connector当前不支持此功能。 You can open a feature request in the MongoDB Jira if you like. 您可以根据需要在MongoDB Jira中打开功能请求。

我已经完成了分层工作 ,如果您正在使用spark,则可以检查一下!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM