新增批量写入方法
This commit is contained in:
+60
-46
@@ -13,7 +13,13 @@ import com.fangjia.script.wenyan.auto.KeywordSearchUtils
|
||||
import com.mongodb.BasicDBObject
|
||||
import com.mongodb.DBCollection
|
||||
import com.mongodb.DBObject
|
||||
import com.mongodb.client.model.Filters
|
||||
import com.mongodb.client.model.UpdateOneModel
|
||||
import com.mongodb.client.model.UpdateOptions
|
||||
import com.mongodb.client.model.WriteModel
|
||||
import org.apache.commons.collections.CollectionUtils
|
||||
import org.bson.Document
|
||||
import org.bson.conversions.Bson
|
||||
import org.bson.types.ObjectId
|
||||
|
||||
import java.util.concurrent.ExecutorService
|
||||
@@ -27,74 +33,82 @@ import java.util.concurrent.Executors
|
||||
*/
|
||||
class SeaweedAroundPoiSingal {
|
||||
|
||||
static DBCollection conn136 = MongoUtils.getCollectionByName("fangjia", "seaweed_poi_sync", "192.168.0.136", 27017)
|
||||
static def conn136 = MongoUtils.getCollectionByName3("fangjia", "seaweed_poi_sync", "mongodb://developer:G00IDCDEV12385@192.168.0.136:28017")
|
||||
static final def URL = "http://192.168.0.100:38080/FangJiaDataService/saveAmapPoi"
|
||||
|
||||
static void main(String[] args) {
|
||||
def types = listAllType()
|
||||
//def types = "0601"
|
||||
DBObject query = new BasicDBObject()
|
||||
query.put("status", 1)
|
||||
conn136.find(query).batchSize(30).each {objTemp ->
|
||||
final def obj = objTemp
|
||||
def city = obj.city
|
||||
def lng = obj.lng
|
||||
def lat = obj.lat
|
||||
List<AmapPoi> pois = GaoDeUtils.searchNearDtoByPoi(city, types, lng, lat, 1000, "")
|
||||
if (CollectionUtils.isNotEmpty(pois)) {
|
||||
String jsonStr = JSONObject.toJSONString(pois)
|
||||
HttpUtils.postForJson(URL, jsonStr)
|
||||
conn136.update(new BasicDBObject("_id", obj._id), new BasicDBObject('$set', new BasicDBObject("status", 0).append("flag", "已获取周边POI")))
|
||||
} else {
|
||||
conn136.update(new BasicDBObject("_id", obj._id), new BasicDBObject('$set', new BasicDBObject("status", 2).append("flag", "周边POI为空")))
|
||||
}
|
||||
println("已处理小区:" + obj)
|
||||
}
|
||||
getNewSeaweed()
|
||||
// def types = listAllType()
|
||||
// //def types = "0601"
|
||||
// DBObject query = new BasicDBObject()
|
||||
// query.put("status", 1)
|
||||
// conn136.find(query).batchSize(30).each {objTemp ->
|
||||
// final def obj = objTemp
|
||||
// def city = obj.city
|
||||
// def lng = obj.lng
|
||||
// def lat = obj.lat
|
||||
// List<AmapPoi> pois = GaoDeUtils.searchNearDtoByPoi(city, types, lng, lat, 1000, "")
|
||||
// if (CollectionUtils.isNotEmpty(pois)) {
|
||||
// String jsonStr = JSONObject.toJSONString(pois)
|
||||
// HttpUtils.postForJson(URL, jsonStr)
|
||||
// conn136.update(new BasicDBObject("_id", obj._id), new BasicDBObject('$set', new BasicDBObject("status", 0).append("flag", "已获取周边POI")))
|
||||
// } else {
|
||||
// conn136.update(new BasicDBObject("_id", obj._id), new BasicDBObject('$set', new BasicDBObject("status", 2).append("flag", "周边POI为空")))
|
||||
// }
|
||||
// println("已处理小区:" + obj)
|
||||
// }
|
||||
}
|
||||
|
||||
static getNewSeaweed() {
|
||||
DBObject query = new BasicDBObject()
|
||||
query.put("cat", "district")
|
||||
query.put("visible", 0)
|
||||
//query.put("_id", new ObjectId("50c96f82e4b0e15299cb018a"))
|
||||
|
||||
DBObject filter = new BasicDBObject()
|
||||
filter.put("_id", 1)
|
||||
filter.put("city", 1)
|
||||
filter.put("region", 1)
|
||||
filter.put("name", 1)
|
||||
filter.put("alias", 1)
|
||||
filter.put("address", 1)
|
||||
filter.put("addr_alias", 1)
|
||||
filter.put("lng", 1)
|
||||
filter.put("lat", 1)
|
||||
|
||||
List list = []
|
||||
List<WriteModel<Document>> writeModelList = new ArrayList<>()
|
||||
DbUtils.seaweed.find(query, filter).batchSize(10000).each {obj ->
|
||||
if (conn136.count(new BasicDBObject("_id", obj._id)) > 0) {
|
||||
// 已存在小区,无需同步
|
||||
} else {
|
||||
DBObject newObj = new BasicDBObject()
|
||||
newObj.put("_id", obj._id)
|
||||
newObj.put("city", obj.city)
|
||||
newObj.put("region", obj.region)
|
||||
newObj.put("name", obj.name)
|
||||
try {
|
||||
newObj.put("lng", SeaweedUtils.formatLatLng(obj.lng))
|
||||
newObj.put("lat", SeaweedUtils.formatLatLng(obj.lat))
|
||||
} catch (Exception ex) {
|
||||
print("经纬度为空:" + obj._id + " " + obj)
|
||||
ex.printStackTrace()
|
||||
}
|
||||
newObj.put("status", 1)
|
||||
newObj.put("flag", "未获取周边POI")
|
||||
Document updateBson = new Document()
|
||||
Document update = new Document()
|
||||
update.put("city", obj.city)
|
||||
update.put("region", obj.region)
|
||||
update.put("name", obj.name)
|
||||
update.put("alias", obj.alias)
|
||||
update.put("address", obj.address)
|
||||
update.put("addr_alias", obj.addr_alias)
|
||||
try {
|
||||
update.put("lng", SeaweedUtils.formatLatLng(obj.lng))
|
||||
update.put("lat", SeaweedUtils.formatLatLng(obj.lat))
|
||||
} catch (Exception ex) {
|
||||
print("经纬度为空:" + obj._id + " " + obj)
|
||||
}
|
||||
update.put("status", 1)
|
||||
update.put("flag", "未精确获取高德信息")
|
||||
updateBson.append('$set', update)
|
||||
|
||||
list << newObj
|
||||
if (list.size() == 5000) {
|
||||
conn136.insert(list)
|
||||
list.clear()
|
||||
}
|
||||
Bson filterBson = Filters.eq("_id", obj._id)
|
||||
UpdateOptions updateOptions = new UpdateOptions();
|
||||
updateOptions.upsert(true)
|
||||
UpdateOneModel updateOneModel = new UpdateOneModel(filterBson, updateBson, updateOptions);
|
||||
writeModelList.add(updateOneModel)
|
||||
if (writeModelList.size() == 5000) {
|
||||
conn136.bulkWrite(writeModelList)
|
||||
writeModelList.clear()
|
||||
}
|
||||
}
|
||||
conn136.insert(list)
|
||||
list.clear()
|
||||
if (writeModelList.size() > 0) {
|
||||
conn136.bulkWrite(writeModelList)
|
||||
writeModelList.clear()
|
||||
}
|
||||
}
|
||||
|
||||
static def getByIdAndInsert(String id) {
|
||||
|
||||
Reference in New Issue
Block a user