|
| 1 | +package com.oracle; |
| 2 | + |
| 3 | +import oracle.soda.*; |
| 4 | +import oracle.soda.rdbms.OracleRDBMSClient; |
| 5 | +import oracle.jdbc.datasource.impl.OracleDataSource; |
| 6 | +import java.util.List; |
| 7 | + |
| 8 | +public class Main { |
| 9 | + private static OracleDatabase sodaDb; |
| 10 | + private static OracleDatabaseAdmin sodaDbAdmin; |
| 11 | + private static OracleCollection sodaSampleCollection; |
| 12 | + |
| 13 | + public static void prepareSODAConnection() throws Exception { |
| 14 | + OracleDataSource ods = new OracleDataSource(); |
| 15 | + ods.setURL(System.getenv("DB_URL")); |
| 16 | + ods.setUser(System.getenv("DB_USERNAME")); |
| 17 | + ods.setPassword(System.getenv("DB_PASSWORD")); |
| 18 | + OracleRDBMSClient client = new OracleRDBMSClient(); |
| 19 | + sodaDb = client.getDatabase(ods.getConnection()); |
| 20 | + sodaDbAdmin = sodaDb.admin(); |
| 21 | + } |
| 22 | + |
| 23 | + public static void listExistingCollections() throws Exception { |
| 24 | + List<String> collections = sodaDbAdmin.getCollectionNames(); |
| 25 | + System.out.println("Listing existing collections : "); |
| 26 | + |
| 27 | + for (String name: collections) { |
| 28 | + System.out.println("Collection : "+name); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + public static void resetSampleCollection() { |
| 33 | + try { |
| 34 | + sodaSampleCollection = sodaDb.openCollection("sodaSampleCollection"); |
| 35 | + sodaSampleCollection.admin().drop(); |
| 36 | + System.out.println("sodaSampleCollection dropped succesfully"); |
| 37 | + } |
| 38 | + catch (Exception e) { |
| 39 | + System.out.println("sodaSampleCollection has not been created yet. Dropping was unneeded."); |
| 40 | + } |
| 41 | + |
| 42 | + try { |
| 43 | + sodaSampleCollection = sodaDbAdmin.createCollection("sodaSampleCollection"); |
| 44 | + System.out.println("sodaSampleCollection created succesfully"); |
| 45 | + } |
| 46 | + catch (Exception e) { |
| 47 | + e.printStackTrace(); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + public static void insertSampleData() throws Exception { |
| 52 | + String docs; |
| 53 | + OracleDocument doc; |
| 54 | + for ( int i = 0; i < 100; i++ ) { |
| 55 | + docs = "{\"name\": \"SampleDoc"+i+"\",\"value\":"+i+"}"; |
| 56 | + System.out.println("Inserting document# : "+i+" : "+docs); |
| 57 | + doc = sodaDb.createDocumentFromString(docs); |
| 58 | + sodaSampleCollection.insert(doc); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + public static void findSampleData() throws Exception { |
| 63 | + System.out.println("Looking for document 12"); |
| 64 | + OracleCursor cursor = sodaSampleCollection.find().filter("{\"value\":12}").getCursor(); |
| 65 | + System.out.println("Results : "); |
| 66 | + while (cursor.hasNext()) { |
| 67 | + OracleDocument doc = cursor.next(); |
| 68 | + System.out.println("Key : "+doc.getKey()); |
| 69 | + System.out.println("Media type : "+doc.getMediaType()); |
| 70 | + System.out.println("Doc : "+doc.getContentAsString()); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + public static void closeSODAConnection() throws Exception { |
| 75 | + sodaDbAdmin.getConnection().close(); |
| 76 | + System.out.println("Database connection closed succesfully."); |
| 77 | + } |
| 78 | + |
| 79 | + public static void main(String[] args) { |
| 80 | + try { |
| 81 | + prepareSODAConnection(); |
| 82 | + listExistingCollections(); |
| 83 | + resetSampleCollection(); |
| 84 | + insertSampleData(); |
| 85 | + findSampleData(); |
| 86 | + closeSODAConnection(); |
| 87 | + } |
| 88 | + catch (Exception e) {e.printStackTrace();} |
| 89 | + } |
| 90 | +} |
0 commit comments