use of com.mongodb.client.MongoCollection in project nifi by apache.
the class RunMongoAggregation method onTrigger.
@Override
public void onTrigger(ProcessContext context, ProcessSession session) throws ProcessException {
FlowFile flowFile = null;
if (context.hasIncomingConnection()) {
flowFile = session.get();
if (flowFile == null && context.hasNonLoopConnection()) {
return;
}
}
String query = context.getProperty(QUERY).evaluateAttributeExpressions(flowFile).getValue();
String queryAttr = context.getProperty(QUERY_ATTRIBUTE).evaluateAttributeExpressions(flowFile).getValue();
Integer batchSize = context.getProperty(BATCH_SIZE).asInteger();
Integer resultsPerFlowfile = context.getProperty(RESULTS_PER_FLOWFILE).asInteger();
Map attrs = new HashMap();
if (queryAttr != null && queryAttr.trim().length() > 0) {
attrs.put(queryAttr, query);
}
MongoCollection collection = getCollection(context);
MongoCursor iter = null;
try {
List<Bson> aggQuery = buildAggregationQuery(query);
AggregateIterable it = collection.aggregate(aggQuery);
it.batchSize(batchSize != null ? batchSize : 1);
iter = it.iterator();
List batch = new ArrayList();
while (iter.hasNext()) {
batch.add(iter.next());
if (batch.size() == resultsPerFlowfile) {
writeBatch(buildBatch(batch), flowFile, context, session, attrs, REL_RESULTS);
batch = new ArrayList();
}
}
if (batch.size() > 0) {
writeBatch(buildBatch(batch), flowFile, context, session, attrs, REL_RESULTS);
}
if (flowFile != null) {
session.transfer(flowFile, REL_ORIGINAL);
}
} catch (Exception e) {
getLogger().error("Error running MongoDB aggregation query.", e);
if (flowFile != null) {
session.transfer(flowFile, REL_FAILURE);
}
} finally {
if (iter != null) {
iter.close();
}
}
}
use of com.mongodb.client.MongoCollection in project LuckPerms by lucko.
the class MongoDao method savePlayerData.
@Override
public PlayerSaveResult savePlayerData(UUID uuid, String username) {
username = username.toLowerCase();
MongoCollection<Document> c = this.database.getCollection(this.prefix + "uuid");
// find any existing mapping
String oldUsername = getPlayerName(uuid);
// do the insert
if (!username.equalsIgnoreCase(oldUsername)) {
c.replaceOne(new Document("_id", uuid), new Document("_id", uuid).append("name", username), new UpdateOptions().upsert(true));
}
PlayerSaveResult result = PlayerSaveResult.determineBaseResult(username, oldUsername);
Set<UUID> conflicting = new HashSet<>();
try (MongoCursor<Document> cursor = c.find(new Document("name", username)).iterator()) {
if (cursor.hasNext()) {
conflicting.add(cursor.next().get("_id", UUID.class));
}
}
conflicting.remove(uuid);
if (!conflicting.isEmpty()) {
// remove the mappings for conflicting uuids
c.deleteMany(Filters.and(conflicting.stream().map(u -> Filters.eq("_id", u)).collect(Collectors.toList())));
result = result.withOtherUuidsPresent(conflicting);
}
return result;
}
use of com.mongodb.client.MongoCollection in project repairnator by Spirals-Team.
the class CleanProjectList method main.
public static void main(String[] args) throws IOException {
String projectPath = args[0];
String dbUrl = args[1];
String dbName = args[2];
String collectionName = args[3];
String destList = args[4];
List<String> allProjects = Files.readAllLines(new File(projectPath).toPath());
MongoConnection mongoConnection = new MongoConnection(dbUrl, dbName);
MongoDatabase database = mongoConnection.getMongoDatabase();
MongoCollection collection = database.getCollection(collectionName);
List<String> selectedProjects = new ArrayList<>();
for (String project : allProjects) {
Repository repo = RepositoryHelper.getRepositoryFromSlug(project);
if (repo != null) {
Build b = repo.getLastBuild(false);
if (b != null) {
if (b.getBuildTool() == BuildTool.MAVEN) {
long results = collection.count(and(eq("repositoryName", project), ne("typeOfFailures", null)));
if (results > 0) {
selectedProjects.add(project);
}
}
}
}
}
File outputFile = new File(destList);
BufferedWriter buffer = new BufferedWriter(new FileWriter(outputFile));
buffer.write(StringUtils.join(selectedProjects, "\n"));
buffer.close();
System.out.println("Read projects: " + allProjects.size() + " | Selected projects : " + selectedProjects.size());
System.out.println(StringUtils.join(selectedProjects, "\n"));
}
use of com.mongodb.client.MongoCollection in project jackrabbit-oak by apache.
the class MongoUtilsTest method createIndex.
@Test
public void createIndex() {
MongoConnection c = connectionFactory.getConnection();
c.getDB().dropDatabase();
MongoCollection collection = c.getDatabase().getCollection("test");
MongoUtils.createIndex(collection, "foo", true, false, true);
MongoUtils.createIndex(collection, "bar", false, true, false);
MongoUtils.createIndex(collection, new String[] { "baz", "qux" }, new boolean[] { true, false }, false, false);
assertTrue(MongoUtils.hasIndex(collection, "_id"));
assertTrue(MongoUtils.hasIndex(collection, "foo"));
assertFalse(MongoUtils.hasIndex(collection, "foo", "bar"));
assertTrue(MongoUtils.hasIndex(collection, "baz", "qux"));
List<Document> indexes = new ArrayList<>();
collection.listIndexes().into(indexes);
assertEquals(4, indexes.size());
for (Document info : indexes) {
Document key = (Document) info.get("key");
if (key.keySet().contains("foo")) {
assertEquals(1, key.keySet().size());
assertEquals(1, key.get("foo"));
assertEquals(Boolean.TRUE, info.get("sparse"));
} else if (key.keySet().contains("bar")) {
assertEquals(1, key.keySet().size());
assertEquals(-1, key.get("bar"));
assertEquals(Boolean.TRUE, info.get("unique"));
} else if (key.keySet().contains("baz")) {
assertEquals(2, key.keySet().size());
assertEquals(1, key.get("baz"));
assertEquals(-1, key.get("qux"));
}
}
c.getDB().dropDatabase();
}
use of com.mongodb.client.MongoCollection in project jackrabbit-oak by apache.
the class MongoUtilsTest method checkArguments.
@Test(expected = IllegalArgumentException.class)
public void checkArguments() {
MongoConnection c = connectionFactory.getConnection();
MongoCollection collection = c.getDatabase().getCollection("test");
MongoUtils.createIndex(collection, new String[] { "foo", "bar" }, new boolean[] { true }, false, true);
}
Aggregations