List of usage examples for com.mongodb MongoClient getDatabase
public MongoDatabase getDatabase(final String databaseName)
From source file:org.ang.streaming.SpeedStreaming.SpeedStreamingPerrosParques.java
private static void saveToMongo(String jsonData) throws Exception { try {/*from www.ja v a 2s . c om*/ //toFile("Method: saveToMongo: Trace: " + "** BEGIN EXEC ** ", LOG_PATH); //MongoClient mongoClient = new MongoClient(new MongoClientURI("mongodb://10.1.1.110:27017")); MongoClient mongoClient = new MongoClient(new MongoClientURI(MONGODB)); MongoDatabase db = mongoClient.getDatabase("kml_db"); //toFile("Method: saveToMongo: Trace: " + "db: " + db.getName(), LOG_PATH); //toFile("Method: saveToMongo: Trace: " + "db: " + db.listCollections(), LOG_PATH); JSONArray array = JsonDecode(jsonData); //toFile("Method: saveToMongo: Trace: " + "JSONArray: " + array.toString(), LOG_PATH); //toFile("Method: saveToMongo: Trace: " + "JSONObject: " + array.get(0).toString(), LOG_PATH); JSONObject data = (JSONObject) array.get(0); //toFile("Method: saveToMongo: Trace: " + "Data: " + data.toJSONString(), LOG_PATH); Document key = new Document("id_collar", data.get("id")); Document doc = new Document(); doc.putAll(data); doc.append("id_collar", data.get("id")).append("createdAt", System.currentTimeMillis()); //.append("createdAt", System.currentTimeMillis()).remove("id"); //toFile("Method: saveToMongo: Trace: " + "key: " + key.toJson(), LOG_PATH); //toFile("Method: saveToMongo: Trace: " + "Data Exists: " + db.getCollection("perros_loc").find(key).first(), SpeedStreamingPerrosParques.LOG_PATH); if (db.getCollection("perros_loc").find(key).first() == null) { db.getCollection("perros_loc").insertOne(doc); } else { db.getCollection("perros_loc").updateOne(key, new Document("$set", doc)); } //toFile("Method: saveToMongo: Trace: " + "** END EXEC ** ", LOG_PATH); } catch (Exception e) { toFile("Method: saveToMongo, Exception: " + e.getMessage(), LOG_PATH); } }
From source file:org.apache.airavata.db.DBWrapper.java
License:Apache License
public DBWrapper() { MongoClient mongoClient = new MongoClient(); db = mongoClient.getDatabase("resource_allocation"); modelConversionHelper = new ModelConversionHelper(); }
From source file:org.apache.calcite.adapter.mongodb.MongoDatabasePolicy.java
License:Apache License
private MongoDatabasePolicy(MongoClient client) { this.client = Objects.requireNonNull(client, "client"); this.database = client.getDatabase(DB_NAME); }
From source file:org.apache.drill.exec.store.mongo.MongoGroupScan.java
License:Apache License
private boolean isShardedCluster(MongoClient client) { MongoDatabase db = client.getDatabase(scanSpec.getDbName()); String msg = db.runCommand(new Document("isMaster", 1)).getString("msg"); return msg == null ? false : msg.equals("isdbgrid"); }
From source file:org.apache.drill.exec.store.mongo.MongoGroupScan.java
License:Apache License
@SuppressWarnings({ "rawtypes" }) private void init() throws IOException { List<String> h = storagePluginConfig.getHosts(); List<ServerAddress> addresses = Lists.newArrayList(); for (String host : h) { addresses.add(new ServerAddress(host)); }// ww w .j a va 2s . co m MongoClient client = storagePlugin.getClient(); chunksMapping = Maps.newHashMap(); chunksInverseMapping = Maps.newLinkedHashMap(); if (isShardedCluster(client)) { MongoDatabase db = client.getDatabase(CONFIG); MongoCollection<Document> chunksCollection = db.getCollection(CHUNKS); Document filter = new Document(); filter.put(NS, this.scanSpec.getDbName() + "." + this.scanSpec.getCollectionName()); Document projection = new Document(); projection.put(SHARD, select); projection.put(MIN, select); projection.put(MAX, select); FindIterable<Document> chunkCursor = chunksCollection.find(filter).projection(projection); MongoCursor<Document> iterator = chunkCursor.iterator(); MongoCollection<Document> shardsCollection = db.getCollection(SHARDS); projection = new Document(); projection.put(HOST, select); boolean hasChunks = false; while (iterator.hasNext()) { Document chunkObj = iterator.next(); String shardName = (String) chunkObj.get(SHARD); String chunkId = (String) chunkObj.get(ID); filter = new Document(ID, shardName); FindIterable<Document> hostCursor = shardsCollection.find(filter).projection(projection); MongoCursor<Document> hostIterator = hostCursor.iterator(); while (hostIterator.hasNext()) { Document hostObj = hostIterator.next(); String hostEntry = (String) hostObj.get(HOST); String[] tagAndHost = StringUtils.split(hostEntry, '/'); String[] hosts = tagAndHost.length > 1 ? StringUtils.split(tagAndHost[1], ',') : StringUtils.split(tagAndHost[0], ','); List<String> chunkHosts = Arrays.asList(hosts); Set<ServerAddress> addressList = getPreferredHosts(storagePlugin.getClient(addresses), chunkHosts); if (addressList == null) { addressList = Sets.newHashSet(); for (String host : chunkHosts) { addressList.add(new ServerAddress(host)); } } chunksMapping.put(chunkId, addressList); ServerAddress address = addressList.iterator().next(); List<ChunkInfo> chunkList = chunksInverseMapping.get(address.getHost()); if (chunkList == null) { chunkList = Lists.newArrayList(); chunksInverseMapping.put(address.getHost(), chunkList); } List<String> chunkHostsList = new ArrayList<String>(); for (ServerAddress serverAddr : addressList) { chunkHostsList.add(serverAddr.toString()); } ChunkInfo chunkInfo = new ChunkInfo(chunkHostsList, chunkId); Document minMap = (Document) chunkObj.get(MIN); Map<String, Object> minFilters = Maps.newHashMap(); Set keySet = minMap.keySet(); for (Object keyObj : keySet) { Object object = minMap.get(keyObj); if (!(object instanceof MinKey)) { minFilters.put(keyObj.toString(), object); } } chunkInfo.setMinFilters(minFilters); Map<String, Object> maxFilters = Maps.newHashMap(); Map maxMap = (Document) chunkObj.get(MAX); keySet = maxMap.keySet(); for (Object keyObj : keySet) { Object object = maxMap.get(keyObj); if (!(object instanceof MaxKey)) { maxFilters.put(keyObj.toString(), object); } } chunkInfo.setMaxFilters(maxFilters); chunkList.add(chunkInfo); } hasChunks = true; } // In a sharded environment, if a collection doesn't have any chunks, it is considered as an // unsharded collection and it will be stored in the primary shard of that database. if (!hasChunks) { handleUnshardedCollection(getPrimaryShardInfo(client)); } } else { handleUnshardedCollection(storagePluginConfig.getHosts()); } }
From source file:org.apache.drill.exec.store.mongo.MongoGroupScan.java
License:Apache License
@SuppressWarnings("unchecked") private Set<ServerAddress> getPreferredHosts(MongoClient client, List<String> hosts) { Set<ServerAddress> addressList = Sets.newHashSet(); MongoDatabase db = client.getDatabase(scanSpec.getDbName()); ReadPreference readPreference = client.getReadPreference(); Document command = db.runCommand(new Document("isMaster", 1)); final String primaryHost = command.getString("primary"); final List<String> hostsList = (List<String>) command.get("hosts"); switch (readPreference.getName().toUpperCase()) { case "PRIMARY": case "PRIMARYPREFERRED": if (primaryHost == null) { return null; }// w w w. j a va 2s . co m addressList.add(new ServerAddress(primaryHost)); return addressList; case "SECONDARY": case "SECONDARYPREFERRED": if (primaryHost == null || hostsList == null) { return null; } hostsList.remove(primaryHost); for (String host : hostsList) { addressList.add(new ServerAddress(host)); } return addressList; case "NEAREST": if (hostsList == null) { return null; } for (String host : hostsList) { addressList.add(new ServerAddress(host)); } return addressList; default: return null; } }
From source file:org.apache.drill.exec.store.mongo.MongoGroupScan.java
License:Apache License
@Override public ScanStats getScanStats() { try {/* w w w. j a va 2 s . c o m*/ MongoClient client = storagePlugin.getClient(); MongoDatabase db = client.getDatabase(scanSpec.getDbName()); MongoCollection<Document> collection = db.getCollection(scanSpec.getCollectionName()); long numDocs = collection.count(); float approxDiskCost = 0; if (numDocs != 0) { String json = collection.find().first().toJson(); approxDiskCost = json.getBytes().length * numDocs; } return new ScanStats(GroupScanProperty.EXACT_ROW_COUNT, numDocs, 1, approxDiskCost); } catch (Exception e) { throw new DrillRuntimeException(e.getMessage(), e); } }
From source file:org.apache.drill.exec.store.mongo.MongoRecordReader.java
License:Apache License
private void init(MongoSubScan.MongoSubScanSpec subScanSpec) { List<String> hosts = subScanSpec.getHosts(); List<ServerAddress> addresses = Lists.newArrayList(); for (String host : hosts) { addresses.add(new ServerAddress(host)); }//from www. j av a2 s . c o m MongoClient client = plugin.getClient(addresses); MongoDatabase db = client.getDatabase(subScanSpec.getDbName()); this.unionEnabled = fragmentContext.getOptions().getOption(ExecConstants.ENABLE_UNION_TYPE); collection = db.getCollection(subScanSpec.getCollectionName(), BsonDocument.class); }
From source file:org.apache.metamodel.DataContextFactory.java
License:Apache License
/** * Creates a new MongoDB datacontext./*from w ww. j a v a2s.c o m*/ * * @param hostname * The hostname of the MongoDB instance * @param port * the port of the MongoDB instance, or null if the default port * should be used. * @param databaseName * the name of the database * @param username * the username, or null if unauthenticated access should be used * @param password * the password, or null if unathenticated access should be used * @param tableDefs * an array of table definitions, or null if table definitions * should be autodetected. * @return a DataContext object that matches the request */ public static UpdateableDataContext createMongoDbDataContext(String hostname, Integer port, String databaseName, String username, char[] password, SimpleTableDef[] tableDefs) { try { final ServerAddress serverAddress; if (port == null) { serverAddress = new ServerAddress(hostname); } else { serverAddress = new ServerAddress(hostname, port); } final MongoClient mongoClient; final MongoDatabase mongoDb; if (Strings.isNullOrEmpty(username)) { mongoClient = new MongoClient(serverAddress); } else { final MongoCredential credential = MongoCredential.createCredential(username, databaseName, password); mongoClient = new MongoClient(serverAddress, Arrays.asList(credential)); } mongoDb = mongoClient.getDatabase(databaseName); if (tableDefs == null || tableDefs.length == 0) { return new MongoDbDataContext(mongoDb); } return new MongoDbDataContext(mongoDb, tableDefs); } catch (Exception e) { if (e instanceof RuntimeException) { throw (RuntimeException) e; } throw new IllegalStateException(e); } }
From source file:org.apache.metamodel.mongodb.mongo3.MongoDbDataCopyer.java
License:Apache License
public static void main(String[] args) throws Exception { System.setProperty("derby.storage.tempDirector", FileHelper.getTempDir().getAbsolutePath()); System.setProperty("derby.stream.error.file", File.createTempFile("metamodel-derby", ".log").getAbsolutePath()); File dbFile = new File("../jdbc/src/test/resources/derby_testdb.jar"); dbFile = dbFile.getCanonicalFile();/*from www . j a v a 2 s .c om*/ if (!dbFile.exists()) { throw new IllegalStateException("File does not exist: " + dbFile); } Class.forName("org.apache.derby.jdbc.EmbeddedDriver"); Connection connection = DriverManager .getConnection("jdbc:derby:jar:(" + dbFile.getAbsolutePath() + ")derby_testdb;territory=en"); connection.setReadOnly(true); MongoClient client = new MongoClient(); MongoDatabase mongoDb = client.getDatabase("orderdb_copy"); DataContext sourceDataContext = new JdbcDataContext(connection); new MongoDbDataCopyer(mongoDb, "orders", sourceDataContext, "APP", "orders").copy(); new MongoDbDataCopyer(mongoDb, "offices", sourceDataContext, "APP", "offices").copy(); new MongoDbDataCopyer(mongoDb, "payments", sourceDataContext, "APP", "payments").copy(); new MongoDbDataCopyer(mongoDb, "orderfact", sourceDataContext, "APP", "orderfact").copy(); new MongoDbDataCopyer(mongoDb, "products", sourceDataContext, "APP", "products").copy(); connection.close(); client.close(); }