下面列出了com.mongodb.DBObject#removeField ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void insert(List<CanalEntry.Column> data, String schemaName, String tableName) {
DBObject obj = DBConvertUtil.columnToJson(data);
logger.debug("insert :{}", obj.toString());
//订单库单独处理
if (schemaName.equals("order")) {
//保存原始数据
if (tableName.startsWith("order_base_info")) {
tableName = "order_base_info";
} else if (tableName.startsWith("order_detail_info")) {
tableName = "order_detail_info";
} else {
logger.info("unknown data :{}.{}:{}", schemaName, tableName, obj);
return;
}
insertData(schemaName, tableName, obj, obj);
} else {
DBObject newObj = (DBObject) ObjectUtils.clone(obj);
if (newObj.containsField("id")) {
newObj.put("_id", newObj.get("id"));
newObj.removeField("id");
}
insertData(schemaName, tableName, newObj, obj);
}
}
public void updateData(String schemaName, String tableName, DBObject query, DBObject obj) {
String path = "/" + schemaName + "/" + tableName + "/" + CanalEntry.EventType.UPDATE.getNumber();
int i = 0;
DBObject newObj = (DBObject) ObjectUtils.clone(obj);
DBObject logObj = (DBObject) ObjectUtils.clone(obj);
//保存原始数据
try {
obj.removeField("id");
i++;
naiveMongoTemplate.getCollection(tableName).update(query, obj);
i++;
SpringUtil.doEvent(path, newObj);
i++;
} catch (MongoClientException | MongoSocketException clientException) {
//客户端连接异常抛出,阻塞同步,防止mongodb宕机
throw clientException;
} catch (Exception e) {
logError(schemaName, tableName, 2, i, logObj, e);
}
}
@Test
public void shouldSaveTheIncomingEvent() throws Exception {
application = Application.builder().name("fake").build();
eventRepository.saveIncoming(tenant, application, incomingEvent);
DBObject saved = mongoTemplate.findOne(
Query.query(Criteria.where("incoming.deviceGuid").is(deviceGuid)
.andOperator(Criteria.where("ts").is(firstEventTimestamp.toEpochMilli()))),
DBObject.class,
EventRepositoryMongoImpl.EVENTS_INCOMING_COLLECTION_NAME
);
saved.removeField("_id");
saved.removeField("_class");
assertThat(saved, equalTo(persisted));
}
private boolean isContained(List<DBObject> item, BasicDBObject doc) {
boolean res = false;
for (int i = 0; i < item.size() && !res; i++) {
DBObject aux = item.get(i);
aux.removeField("_id");
aux.removeField("timestamp");
if (aux.keySet().equals(doc.keySet())) {
res = true;
}
// Obtenemos los columnNames
List<String> cols = new ArrayList<String>(doc.keySet());
for (int x = 0; x < cols.size() && res; x++) {
if (!aux.get(cols.get(x)).equals(doc.get(cols.get(x)))) {
res = false;
} else {
res = true;
}
}
}
return res;
}
@SuppressWarnings("unchecked")
private DBObject toSubDocQuery(DBObject originalQueryDBObject, boolean isParentQuery) {
DBObject queryDBObject = appendSubField(originalQueryDBObject, isParentQuery);
for (String key : originalQueryDBObject.keySet()) {
if (key.equals("$or") || key.equals("$and")) {
List<DBObject> originalOrQueryDBObjects = (List<DBObject>) originalQueryDBObject.get(key);
List<DBObject> orQueryDBObjects = new ArrayList<DBObject>();
for (DBObject originalOrQueryDBObject : originalOrQueryDBObjects) {
DBObject orQueryDBObject = appendSubField(originalOrQueryDBObject, isParentQuery);
if (orQueryDBObject.get("_id") != null) {
addId(queryDBObject, orQueryDBObject.get("_id"));
orQueryDBObject.removeField("_id");
}
orQueryDBObjects.add(orQueryDBObject);
}
queryDBObject.put(key, orQueryDBObjects);
}
}
return queryDBObject;
}
@DELETE
@Path("/delete/{product}")
@Produces(MediaType.APPLICATION_JSON)
public Response softDeleteEntireProduct(@PathParam("product") final String product) {
final DBCollection collection = this.mongoLegacyDb.getCollection("summary");
final DBCollection targetCollection = this.mongoLegacyDb.getCollection("deletedSummary");
final BasicDBObject query = new BasicDBObject("coordinates.product", product);
final DBCursor cursor = collection.find(query);
DBObject doc;
while (cursor.hasNext()) {
doc = cursor.next();
// kill the old id
doc.removeField("_id");
try {
targetCollection.insert(doc);
} catch (final Throwable e) {
return Response.status(500).build();
}
}
collection.remove(query);
return Response.ok().build();
}
@DELETE
@Path("/delete/{product}/{version}")
@Produces(MediaType.APPLICATION_JSON)
public Response softDeleteSingleVersion(@PathParam("product") final String product,
@PathParam("version") final String version) {
final DBCollection collection = this.mongoLegacyDb.getCollection("summary");
final DBCollection targetCollection = this.mongoLegacyDb.getCollection("deletedSummary");
final Pattern productReg = java.util.regex.Pattern.compile("^" + product + "/" + version + "$");
final BasicDBObject query = new BasicDBObject("_id", productReg);
final DBCursor cursor = collection.find(query);
DBObject doc;
while (cursor.hasNext()) {
doc = cursor.next();
// kill the old id
doc.removeField("_id");
try {
targetCollection.insert(doc);
} catch (final Throwable e) {
return Response.status(500).build();
}
}
collection.remove(query);
return Response.ok().build();
}
@RequestMapping(value = "/api/{projectId}/entities/{name}/{id}", method = RequestMethod.GET, headers = "Accept=application/json")
public @ResponseBody
String getEntityDataById(@PathVariable("projectId") String projectId, @PathVariable("name") String entityName,
@PathVariable("id") String entityDataId,
@RequestHeader(value = "authToken", required = false) String authToken) {
JSONObject authRes = authService.authorize(projectId,authToken,"USER");
if(!authRes.getBoolean(SUCCESS)){
return authRes.toString(4);
}
DBCollection dbCollection = mongoTemplate.getCollection(projectId+"_"+entityName);
BasicDBObject queryObject = new BasicDBObject();
queryObject.append("_id", new ObjectId(entityDataId));
DBObject resultObject = dbCollection.findOne(queryObject);
if(resultObject == null){
return "Not Found";
}
if(entityName.equals("User")){
resultObject.removeField(PASSWORD);
}
dbRefToRelation(resultObject);
String json = resultObject.toString();
// Indentation
JSONObject jsonObject = new JSONObject(json);
return jsonObject.toString(4);
}
@Override
public DBObject convert(final User user) {
final DBObject dbObject = new BasicDBObject();
dbObject.put("name", user.getName());
dbObject.put("age", user.getAge());
if (user.getEmailAddress() != null) {
final DBObject emailDbObject = new BasicDBObject();
emailDbObject.put("value", user.getEmailAddress().getValue());
dbObject.put("email", emailDbObject);
}
dbObject.removeField("_class");
return dbObject;
}
public Representation doRepresent() {
// Ready query params
String _page = getQueryValue("page");
String _size = getQueryValue("size");
ProjectRepository projectRepo = platform.getProjectRepositoryManager().getProjectRepository();
DBCursor cursor;
if(_page != null && !"".equals(_page) && isInteger(_page) && _size != null && !"".equals(_size) && isInteger(_size)) {
int page = Integer.valueOf(_page);
int pageSize = Integer.valueOf(_size);
cursor = projectRepo.getProjects().getDbCollection().find().skip(page*pageSize).limit(pageSize);
} else {
cursor = projectRepo.getProjects().getDbCollection().find();
}
ArrayNode projects = mapper.createArrayNode();
while (cursor.hasNext()) {
try {
DBObject p = cursor.next();
p.removeField("storage");
p.removeField("metricProviderData");
p.removeField("_superTypes");
p.removeField("_id");
// FIXME: Temporary solution
p.removeField("licenses");
p.removeField("persons");
projects.add(mapper.readTree(p.toString()));
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
ObjectNode m = mapper.createObjectNode();
m.put("apicall", "list-all-projects");
return Util.generateErrorMessageRepresentation(m, e.getMessage());
}
}
cursor.close();
StringRepresentation resp = new StringRepresentation(projects.toString());
resp.setMediaType(MediaType.APPLICATION_JSON);
return resp;
}
private void process(XMLStreamReader xml) throws XMLStreamException {
String tag;
String root = null;
Stack<DBObject> json = new Stack<DBObject>();
DBObject js;
while (xml.hasNext()) {
int eventType = xml.next();
while (xml.isWhiteSpace() || eventType == XMLEvent.SPACE)
eventType = xml.next();
switch (eventType) {
case XMLEvent.START_ELEMENT:
tag = xml.getLocalName();
if (root == null) {
root = tag;
} else {
json.push(new BasicDBObject());
}
break;
case XMLEvent.END_ELEMENT:
tag = xml.getLocalName();
if (tag.equals(root)) {
// will terminate in next iteration
} else {
js = json.pop();
if (json.size() == 0) {
if (tag.equals(rowTag))
printEntry(js);
else
printUnwantedEntry(js);
} else {
putListStrOrJSON(json.peek(), tag, js);
}
}
break;
case XMLEvent.CHARACTERS:
String txt = xml.getText();
js = json.peek();
if (js.containsField(strTag)) {
txt = js.get(strTag) + txt;
js.removeField(strTag);
}
js.put(strTag, txt);
break;
case XMLEvent.START_DOCUMENT:
break;
case XMLEvent.END_DOCUMENT:
break;
case XMLEvent.COMMENT:
case XMLEvent.ENTITY_REFERENCE:
case XMLEvent.ATTRIBUTE:
case XMLEvent.PROCESSING_INSTRUCTION:
case XMLEvent.DTD:
case XMLEvent.CDATA:
case XMLEvent.SPACE:
System.out.format("%s --\n", eventType);
break;
}
}
}
public void insert(String domainName, long timestampMillis, String level, String message) {
String collectionName = domainName;
checkCollection(collectionName);
DBObject object = new BasicDBObject();
object.put("time", timestampMillis);
object.put("level", level);
object.put("message", message);
object.removeField("_class");
DBCollection collection = mongoAuditTemplate.getCollection(collectionName);
collection.insert(object);
}
@Override
protected Event doSave(Tenant tenant, Application application, Event event, Type type) throws BusinessException {
Optional.ofNullable(tenantRepository.findByDomainName(tenant.getDomainName()))
.orElseThrow(() -> new BusinessException(CommonValidations.TENANT_DOES_NOT_EXIST.getCode()));
Tenant existingTenant = tenantRepository.findByDomainName(tenant.getDomainName());
if (!application.getName().equals(event.getIncoming().getDeviceGuid())) {
Optional.ofNullable(deviceRepository.findByTenantAndGuid(existingTenant.getId(), event.getIncoming().getDeviceGuid()))
.orElseThrow(() -> new BusinessException(Validations.INCOMING_DEVICE_ID_DOES_NOT_EXIST.getCode()));
}
Optional.ofNullable(event.getCreationTimestamp())
.orElseThrow(() -> new BusinessException(Validations.EVENT_TIMESTAMP_NULL.getCode()));
if (type.equals(Type.OUTGOING)) {
Optional.ofNullable(event.getOutgoing())
.orElseThrow(() -> new BusinessException(Validations.EVENT_OUTGOING_NULL.getCode()));
Optional.ofNullable(event.getOutgoing().getDeviceGuid()).filter(s -> !s.isEmpty())
.orElseThrow(() -> new BusinessException(Validations.OUTGOING_DEVICE_GUID_NULL.getCode()));
Optional.ofNullable(event.getOutgoing().getChannel()).filter(s -> !s.isEmpty())
.orElseThrow(() -> new BusinessException(Validations.EVENT_OUTGOING_CHANNEL_NULL.getCode()));
Optional.ofNullable(
deviceRepository.findByTenantAndGuid(existingTenant.getId(),event.getOutgoing().getDeviceGuid())
).orElseThrow(() -> new BusinessException(Validations.OUTGOING_DEVICE_ID_DOES_NOT_EXIST.getCode()));
}
event.getIncoming().setTenantDomain(tenant.getDomainName());
DBObject incoming = new BasicDBObject();
incoming.put("deviceGuid",event.getIncoming().getDeviceGuid());
incoming.put("tenantDomain",event.getIncoming().getTenantDomain());
incoming.put("applicationName",event.getIncoming().getApplicationName());
incoming.put("channel",event.getIncoming().getChannel());
incoming.put("deviceId", event.getIncoming().getDeviceId());
incoming.put("locationGuid", event.getIncoming().getLocationGuid());
DBObject toSave = new BasicDBObject();
toSave.removeField("ts");
toSave.put("ts", event.getCreationTimestamp().toEpochMilli());
toSave.put("ingestedTimestamp", event.getIngestedTimestamp());
toSave.put(Type.INCOMING.getActorFieldName(), incoming);
toSave.put("payload", event.getPayload());
if (Optional.ofNullable(event.getGeolocation()).isPresent()) {
DBObject geolocation = new BasicDBObject();
geolocation.put("lat", event.getGeolocation().getLat());
geolocation.put("lon", event.getGeolocation().getLon());
geolocation.put("hdop", event.getGeolocation().getHdop());
geolocation.put("elev", event.getGeolocation().getElev());
toSave.put("geolocation", geolocation);
}
if (type.equals(Type.OUTGOING)) {
DBObject outgoing = new BasicDBObject();
outgoing.put("deviceGuid",event.getOutgoing().getDeviceGuid());
outgoing.put("tenantDomain",event.getOutgoing().getTenantDomain());
outgoing.put("applicationName",event.getOutgoing().getApplicationName());
outgoing.put("channel",event.getOutgoing().getChannel());
outgoing.put("deviceId", event.getOutgoing().getDeviceId());
outgoing.put("locationGuid", event.getOutgoing().getLocationGuid());
toSave.put(Type.OUTGOING.getActorFieldName(), outgoing);
}
mongoTemplate.save(toSave, type.getCollectionName());
return event;
}
@SuppressWarnings("unchecked")
private DBObject appendSubField(DBObject originalDBObject, boolean isParentQuery) {
DBObject newDBObject = new Query().getQueryObject();
for (String key : originalDBObject.keySet()) {
if (!key.startsWith("$")) {
String newKey = key;
Object newValue = originalDBObject.get(key);
String updatedKey = key.replace("body.", "");
if (key.equals("_id") && getIds(newValue).size() != 0) {
// use parent id for id query
try {
Set<String> parentIds = getParentIds(getIds(newValue));
if (parentIds != null && parentIds.size() > 1) {
newDBObject.put(newKey, new BasicDBObject("$in", parentIds));
} else if (parentIds != null && parentIds.size() == 1) {
newDBObject.put(newKey, parentIds.iterator().next());
}
} catch (InvalidIdException e) {
LOG.info("There was an invalid Id exception. Ignoring.");
// child id does not have parent id, qppend the subfield to original
// query, this may trigger table scan if subFiled._id is not
// indexed
// newKey = subField + "." + key;
// newDBObject.put(newKey, newValue);
LOG.error(
"Embedded entity's ID does not contain parentId. Cannot determine parent superdoc. ID: {}",
newValue);
}
}
if (lookup.containsKey(updatedKey)) {
if (newDBObject.get(updatedKey) != null) {
Object idList = newDBObject.get(updatedKey);
Set<String> combined = new HashSet<String>();
combined.addAll(extractIdSet(idList));
combined.addAll(extractIdSet(newValue));
newDBObject.put(lookup.get(updatedKey), new BasicDBObject("$in", combined));
} else {
newDBObject.put(lookup.get(key.replace("body.", "")), newValue);
}
} else {
// for other query, append the subfield to original key
newKey = subField + "." + key;
newDBObject.put(newKey, newValue);
}
} else if (key.equals("$or") || key.equals("$and")) {
List<DBObject> dbObjects = (List<DBObject>) originalDBObject.get(key);
List<DBObject> orQueryDBObjects = new ArrayList<DBObject>();
for (DBObject dbObject : dbObjects) {
DBObject subQuery = toSubDocQuery(dbObject, isParentQuery);
if (subQuery.get("_id") != null) {
addId(newDBObject, subQuery.get("_id"));
subQuery.removeField("_id");
}
orQueryDBObjects.add(subQuery);
}
newDBObject.put(key, orQueryDBObjects);
}
}
return newDBObject;
}
@SuppressWarnings("unchecked")
private void simplifyParentQuery(final DBObject query) {
final Set<String> parentSet = new HashSet<String>();
if (isSubDoc(this.subField) && subDoc(this.subField).collection.equals(this.collection)) {
final String childLoc = this.subField.concat("._id");
final String parentLoc = "_id";
final Object dbOrObj = query.get("$or");
if (dbOrObj != null && dbOrObj instanceof List) {
final List<DBObject> dbOrList = (List<DBObject>) dbOrObj;
for (DBObject childQuery : dbOrList) {
Object childInQuery = childQuery.get(childLoc);
if (childInQuery instanceof DBObject && ((DBObject) childInQuery).containsField("$in")) {
Object inList = ((DBObject) childInQuery).get("$in");
try {
Object id = query.get("_id");
if (id != null && id instanceof String) {
String singleId = (String) id;
if (getParentIds(inList).contains(singleId)) {
parentSet.add(singleId);
} else {
// No union of constraining criteria --> return
return;
}
} else {
parentSet.addAll(getParentIds(inList));
}
} catch (InvalidIdException e) {
// IDs aren't valid, we can't simplify the query
return;
}
if (parentSet.size() > 0) {
if (dbOrList.size() == 1) {
query.removeField("$or");
} else {
dbOrList.remove(childQuery);
}
}
}
}
}
if (parentSet.size() == 1) {
LOG.info("Putting parent id {} in {}", parentSet.iterator().next(), parentLoc);
query.put(parentLoc, parentSet.iterator().next());
} else if (parentSet.size() > 1) {
LOG.info("Putting parent ids $in[{}] in {}", parentSet, parentLoc);
query.put(parentLoc, new BasicDBObject("$in", parentSet));
}
}
}