|
| 1 | + |
| 2 | +package com.oracle; |
| 3 | + |
| 4 | +import com.mongodb.ConnectionString; |
| 5 | +import com.mongodb.MongoClientSettings; |
| 6 | +import com.mongodb.ServerApi; |
| 7 | +import com.mongodb.ServerApiVersion; |
| 8 | +import com.mongodb.client.result.InsertManyResult; |
| 9 | +import com.mongodb.reactivestreams.client.MongoCollection; |
| 10 | +import org.bson.Document; |
| 11 | +import com.mongodb.reactivestreams.client.MongoClient; |
| 12 | +import com.mongodb.reactivestreams.client.MongoClients; |
| 13 | +import com.mongodb.reactivestreams.client.MongoDatabase; |
| 14 | +import org.reactivestreams.Publisher; |
| 15 | +import reactor.core.publisher.Mono; |
| 16 | +import java.util.Arrays; |
| 17 | +import java.util.List; |
| 18 | +import static com.mongodb.client.model.Filters.eq; |
| 19 | +//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or |
| 20 | +// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter. |
| 21 | +public class Main { |
| 22 | + |
| 23 | + private static MongoDatabase database; |
| 24 | + private static boolean latch = true; |
| 25 | + |
| 26 | + |
| 27 | + public static void readSampleData() { |
| 28 | + // due to asynchronous nature of the code there is need to use |
| 29 | + // try-with-resource blocks, they guarantee that all the resources are freed |
| 30 | + // after the execution |
| 31 | + //using reactor package to perform operations in the reactive mode |
| 32 | + String uri = System.getenv("DB_URI"); |
| 33 | + ServerApi serverApi = ServerApi.builder() |
| 34 | + .version(ServerApiVersion.V1) |
| 35 | + .build(); |
| 36 | + MongoClientSettings settings = MongoClientSettings.builder() |
| 37 | + .applyConnectionString(new ConnectionString(uri)) |
| 38 | + .serverApi(serverApi) |
| 39 | + .build(); |
| 40 | + try (MongoClient mongoClient = MongoClients.create(settings)) { |
| 41 | + MongoDatabase database = mongoClient.getDatabase("oradev"); |
| 42 | + MongoCollection<Document> employees = database.getCollection("EMP_JSON_VIEW"); |
| 43 | + Mono.from(employees.find(eq("LAST_NAME", "King"))) |
| 44 | + .doOnSuccess(i -> System.out.println(i)) |
| 45 | + .doOnError(err -> System.out.println("Error: " + err.getMessage())) |
| 46 | + .block(); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + public static void insertData() { |
| 51 | + Document doc1 = new Document("name", "Pink"), |
| 52 | + doc2 = new Document("name", "Grey"), |
| 53 | + doc3 = new Document("name","Dark-Green"), |
| 54 | + doc4 = new Document("name","Magenta"), |
| 55 | + doc5 = new Document("name","Black"), |
| 56 | + doc6 = new Document("name","White"), |
| 57 | + doc7 = new Document("name","Red"), |
| 58 | + doc8 = new Document("name","Green"), |
| 59 | + doc9 = new Document("name","Blue"), |
| 60 | + doc10 = new Document("name","Yellow"); |
| 61 | + |
| 62 | + List<Document> docs = Arrays.asList(doc1,doc2,doc3,doc4,doc5,doc6,doc7,doc8,doc9,doc10); |
| 63 | + |
| 64 | + String uri = System.getenv("DB_URI"); |
| 65 | + ServerApi serverApi = ServerApi.builder() |
| 66 | + .version(ServerApiVersion.V1) |
| 67 | + .build(); |
| 68 | + MongoClientSettings settings = MongoClientSettings.builder() |
| 69 | + .applyConnectionString(new ConnectionString(uri)) |
| 70 | + .serverApi(serverApi) |
| 71 | + .build(); |
| 72 | + try (MongoClient mongoClient = MongoClients.create(settings)) { |
| 73 | + MongoDatabase database = mongoClient.getDatabase("oradev"); |
| 74 | + MongoCollection<Document> colors = database.getCollection("colors"); |
| 75 | + |
| 76 | + Publisher<InsertManyResult> iPublisher = colors.insertMany(docs); |
| 77 | + Mono.from(iPublisher).block(); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + public static void main(String[] args) { |
| 82 | + readSampleData(); |
| 83 | + System.out.println("Data read"); |
| 84 | + insertData(); |
| 85 | + System.out.println("Data inserted"); |
| 86 | + } |
| 87 | +} |
0 commit comments