{ "cells": [ { "cell_type": "markdown", "id": "ee3d560d-7b4b-4583-b099-a9db0849473a", "metadata": { "tags": [] }, "source": [ "# Setup\n", "The following cells need to be run in order to use the Java kernel and set dependencies" ] }, { "cell_type": "code", "execution_count": null, "id": "0794f230", "metadata": { "tags": [], "vscode": { "languageId": "java" } }, "outputs": [], "source": [ "import io.github.spencerpark.ijava.IJava;\n", "import io.github.spencerpark.jupyter.kernel.magic.common.Shell;\n", "IJava.getKernelInstance().getMagics().registerMagics(Shell.class);\n", "%sh asd" ] }, { "cell_type": "code", "execution_count": null, "id": "f1d68081", "metadata": { "vscode": { "languageId": "java" } }, "outputs": [], "source": [ "%%loadFromPOM\n", "\n", " \n", " org.slf4j\n", " slf4j-simple\n", " 1.7.25\n", " \n", " \n", " com.aerospike\n", " aerospike-client\n", " 6.1.4\n", " \n", " \n", " com.aerospike\n", " aerospike-document-api\n", " 1.2.0\n", " \n", " \n", " commons-io\n", " commons-io\n", " 2.6\n", " \n", " \n", " com.googlecode.json-simple\n", " json-simple\n", " 1.1.1\n", "\n", "" ] }, { "cell_type": "markdown", "id": "282f15c1-a0db-4040-a290-19adf0820479", "metadata": { "tags": [] }, "source": [ "## Import dependencies, Connect to Aerospike" ] }, { "cell_type": "code", "execution_count": null, "id": "c4ca2e5f", "metadata": { "vscode": { "languageId": "java" } }, "outputs": [], "source": [ "import com.aerospike.client.AerospikeClient;\n", "import com.aerospike.client.Bin;\n", "import com.aerospike.client.Host;\n", "import com.aerospike.client.Key;\n", "import com.aerospike.client.Record;\n", "import com.aerospike.client.policy.WritePolicy;\n", "import com.aerospike.documentapi.*;\n", "import com.aerospike.documentapi.JsonConverters;\n", "\n", "import com.fasterxml.jackson.databind.JsonNode;\n", "import com.jayway.jsonpath.JsonPath;\n", "import java.util.List;\n", "import java.util.Map;\n", "import java.nio.charset.StandardCharsets;\n", "import org.apache.commons.io.FileUtils;\n", "import org.json.simple.JSONArray;\n", "import org.json.simple.JSONObject;\n", "import org.json.simple.parser.JSONParser;\n", "\n", "Host[] hosts = new Host[] {\n", " new Host(\"127.0.0.1\", 3000)\n", "};\n", "\n", "WritePolicy writePolicy = new WritePolicy();\n", "writePolicy.sendKey = true;\n", "\n", "AerospikeClient client = new AerospikeClient(null, hosts);\n", "System.out.println(\"Initialized Aerospike client and connected to the cluster.\");\n", "\n", "AerospikeDocumentClient docClient = new AerospikeDocumentClient(client);\n", "System.out.println(\"Initialized document client from the Aerospike client.\");" ] }, { "cell_type": "markdown", "id": "39d81f09-6310-443d-ac00-1de353e77606", "metadata": { "tags": [] }, "source": [ "# JSON Document" ] }, { "cell_type": "markdown", "id": "320e6b15-4b94-4ba3-a32e-a700c277299b", "metadata": { "tags": [] }, "source": [ "## Document Format" ] }, { "cell_type": "markdown", "id": "bee1150f-3a4b-4d7f-a5ea-1459a1b4304a", "metadata": { "tags": [] }, "source": [ "The document is an array of objects like the example below:\n", "\n", "```json\n", "{\n", " \"_id\": \"58e3dc8ede9c102b036044e7\",\n", " \"company\": {\n", " \"_id\": \"58e3dcf2de9c102b036044e8\",\n", " \"about\": \"Solenix is a privately owned, independent and internationally active company...\",\n", " \"backgroundImage\": \"https://spaceindividuals.com/var/www/images/52251f825f0c59bf2ee429c075262fa8.jpg\",\n", " \"companyLocation\": \"Bornstrasse 3, 4616 Kappel, Switzerland\",\n", " \"companyName\": \"Solenix GmbH\",\n", " \"createdAt\": \"2018-03-03T14:00:04.000Z\",\n", " \"culture\": \"- Attractive salary, benefits and rewards. Besides the standard benefits you would expect...\",\n", " \"email\": \"career@solenix.ch\",\n", " \"galleryImages\": [\n", " \"https://spaceindividuals.com/var/www/images/be4dc86c6921c9f0729241f5f81dbaec.jpg\",\n", " \"https://spaceindividuals.com/var/www/images/3e13ef7d2b8cb7b6b059994e68398d9c.jpg\",\n", " \"https://spaceindividuals.com/var/www/images/0bf399d70514c1edf6d913e4edbabd4f.jpg\"\n", " ],\n", " \"isPublished\": true,\n", " \"kindOfEmployeeLookFor\": null,\n", " \"logoURL\": \"https://spaceindividuals.com/var/www/images/3b71131a4a213e1f17af26bf80ac0d97.png\",\n", " \"mission\": null,\n", " \"paymentType\": \"free\",\n", " \"phone\": \"+41 62 216 35 02\",\n", " \"sizeCompany\": \"25-50 employees\",\n", " \"specialities\": \"Solenix provides consultancy and software engineering services...\",\n", " \"testimonials\": [],\n", " \"weOfferChallenges\": null,\n", " \"weOfferKindOfChallenges\": \"Whether you are passionate about the space industry or you want...\",\n", " \"weOfferKindOfTrainings\": null,\n", " \"weOfferTrainings\": null,\n", " \"webSite\": \"http://www.solenix.ch\"\n", " },\n", " \"createdAt\": \"2018-03-03T14:00:04.000Z\",\n", " \"email\": \"mara.mcclain.pleskot@solenix.ch\",\n", " \"jobs\": null,\n", " \"numberOfJobs\": 0,\n", " \"updatedAt\": \"2020-11-09T14:27:50.753Z\"\n", "}\n", "```" ] }, { "cell_type": "markdown", "id": "36285bc8-e458-4a56-8ad3-614498723fd5", "metadata": { "tags": [] }, "source": [ "## Parse the JSON file" ] }, { "cell_type": "code", "execution_count": null, "id": "2c3d290d-29f7-4a7b-940a-9b3649711041", "metadata": { "vscode": { "languageId": "java" } }, "outputs": [], "source": [ "String companiesString = FileUtils.readFileToString(new File(\"./space-companies.json\"), StandardCharsets.UTF_8);\n", "\n", "System.out.println(\"Document parsed\");" ] }, { "cell_type": "markdown", "id": "570f52ee-f61a-44e7-8b9c-c9d8822eeeb4", "metadata": { "tags": [] }, "source": [ "# Write Data" ] }, { "cell_type": "markdown", "id": "04a1192b-76f5-4701-a9d5-ec061770661b", "metadata": {}, "source": [ "### Single record, single bin\n", "The following code will write the entire document to a single bin." ] }, { "cell_type": "code", "execution_count": null, "id": "ed2d7244-6fc1-4964-9f59-40339a9880a4", "metadata": { "vscode": { "languageId": "java" } }, "outputs": [], "source": [ "System.out.println(\"Writing record...\\n\");\n", "\n", "Key key = new Key(\"demo\", \"space-doc\", \"companies\");\n", "\n", "JsonNode spaceNode = JsonConverters.convertStringToJsonNode(companiesString);\n", "\n", "docClient.put(writePolicy, key, \"company_bin\", spaceNode);\n", "\n", "Record record = client.get(null, key);\n", "System.out.format(\"Companies: %2.100s\\n\\n\", record.bins.get(\"company_bin\"));\n", "\n", "System.out.println(\"Writing record complete\");" ] }, { "cell_type": "markdown", "id": "c1e791db-6d2d-4850-8814-d761b6eb3b24", "metadata": { "tags": [] }, "source": [ "# Read Data" ] }, { "cell_type": "markdown", "id": "20a6bbfd-be8f-4b17-aa84-0a4b8dfc2ef1", "metadata": {}, "source": [ "### Read entire document \n", "Return the entry for the company Pixxel." ] }, { "cell_type": "code", "execution_count": null, "id": "333437fb-8db1-4bf6-8e0d-c13f75102852", "metadata": { "vscode": { "languageId": "java" } }, "outputs": [], "source": [ "System.out.println(\"Getting company data...\\n\");\n", "Key key = new Key(\"demo\", \"space-doc\", \"companies\");\n", "\n", "Object pixxel = docClient.get(key, \"company_bin\", \"$.companies[*].company[?(@.companyName == 'Pixxel')]\");\n", "\n", "System.out.format(\"%s\\n\\n\", pixxel);\n", "System.out.println(\"Company data returned\");" ] }, { "cell_type": "markdown", "id": "402d0c88-dba6-4d8f-9f21-2d3424766ca9", "metadata": {}, "source": [ "### Read specific keys from the document\n", "Return only specific keys from the Pixxel document." ] }, { "cell_type": "code", "execution_count": null, "id": "8c30e7d9-ea0e-4c68-9c50-4cdb9601a44f", "metadata": { "vscode": { "languageId": "java" } }, "outputs": [], "source": [ "System.out.println(\"Getting company data...\\n\");\n", "Key key = new Key(\"demo\", \"space-doc\", \"companies\");\n", "\n", "Object pixxel = docClient.get(key, \"company_bin\", \"$.companies[*].company[?(@.companyName == 'Pixxel')].['companyName','about','email','webSite']\");\n", "\n", "System.out.format(\"%s\\n\\n\", pixxel);\n", "System.out.println(\"Company data returned\");" ] }, { "cell_type": "markdown", "id": "d9b1a8a8-4ec1-4b2d-a5ba-cecf52232af6", "metadata": {}, "source": [ "### Read specific keys from the document with a complex filter\n", "Return only specific information about a company with more than 2 employee testimonials and a `paymentType` of free. " ] }, { "cell_type": "code", "execution_count": null, "id": "3872bb55-6a29-4bc7-9f22-8460fed66d2e", "metadata": { "vscode": { "languageId": "java" } }, "outputs": [], "source": [ "System.out.println(\"Getting companies data...\\n\");\n", "Key key = new Key(\"demo\", \"space-doc\", \"companies\");\n", "\n", "List companyData = (List) docClient.get(key, \"company_bin\", \"$.companies[*].company[?(@.testimonials.length() > 2 && @.paymentType == 'free')].['companyName','about','email','webSite']\");\n", "\n", "for(int i = 0; i < companyData.size(); i++){\n", " System.out.format(\"%s\\n\\n\", companyData.get(i));\n", "}\n", "System.out.println(\"Company data returned\");" ] }, { "cell_type": "markdown", "id": "e2dec37b-5faf-4cde-9e6f-71b31d43fbb7", "metadata": { "tags": [] }, "source": [ "# Update Data" ] }, { "cell_type": "markdown", "id": "75afd298-f610-4a98-b989-7fac6ba67a0b", "metadata": {}, "source": [ "## Create new employee testimonial for CERN" ] }, { "cell_type": "code", "execution_count": null, "id": "b104bd47-8ccd-4b4a-86f7-902a31366c8d", "metadata": { "vscode": { "languageId": "java" } }, "outputs": [], "source": [ "// Create a new employee testimonial for CERN\n", "Map empTest = new TreeMap();\n", "\n", "empTest.put(\"content\", \"My summer internship at CERN was absolutely incredible. The amount of knowledge I have gained while working with some of the best scientists in the world is sure to propel my career forward. I hope to have the opportunity to return to CERN in the future\");\n", "empTest.put(\"person\", \"Rebecca\");\n", "empTest.put(\"position\", \"Summer intern at CERN\");\n", "\n", "System.out.println(\"Testimonial created\");" ] }, { "cell_type": "markdown", "id": "4ff4584b-8967-45c1-891e-d94ec5d21310", "metadata": { "tags": [] }, "source": [ "### Update the CERN entry with the new employee testimonial" ] }, { "cell_type": "code", "execution_count": null, "id": "3159fe56-f926-48ef-ac2f-1bce06da8b80", "metadata": { "vscode": { "languageId": "java" } }, "outputs": [], "source": [ "System.out.println(\"Adding new testimonial...\\n\");\n", "Key key = new Key(\"demo\", \"space-doc\", \"companies\");\n", "\n", "// Add the new testimonial\n", "docClient.append(key, \"company_bin\", \"$.companies[*].company[?(@.companyName=='CERN')].testimonials\", empTest);\n", "\n", "// Read back the testimonials to see the new entry\n", "List test = (List) docClient.get(key, \"company_bin\", \"$.companies[*].company[?(@.companyName=='CERN')]..testimonials\");\n", "\n", "List testimonials = (List) test.get(0);\n", "for (int i = 0; i < testimonials.size(); i++){\n", " System.out.format(\"%s\\n\\n\", testimonials.get(i));\n", "}\n", "\n", "System.out.println(\"Testimonial added\");" ] }, { "cell_type": "markdown", "id": "7ee7dffb-30d9-4f36-841c-f83bf6c45258", "metadata": { "tags": [] }, "source": [ "# Delete Data" ] }, { "cell_type": "markdown", "id": "c6f422bb-4718-49e8-8c45-389b5443a60d", "metadata": {}, "source": [ "Delete all companies with a payment type **NOT** equal to 'free'" ] }, { "cell_type": "code", "execution_count": null, "id": "c3cf9be3-4d1b-418f-88c5-6cd0880d111d", "metadata": { "vscode": { "languageId": "java" } }, "outputs": [], "source": [ "// Get number of companies in the document\n", "Object companies = docClient.get(key, \"company_bin\", \"$.companies.length()\");\n", "System.out.println(companies + \" Companies in the list\\n\");\n", "\n", "// Delete all companies with a payment type NOT equal to 'free'\n", "docClient.delete(key, \"company_bin\", \"$.companies[?(@.company.paymentType!='free')]\");\n", "System.out.println(\"Companies deleted\\n\");\n", "\n", "// Get new count of companies in the document\n", "companies = docClient.get(key, \"company_bin\", \"$.companies.length()\");\n", "System.out.println(companies + \" Companies in the list\");" ] }, { "cell_type": "markdown", "id": "49c47baa-5b38-4102-8b5d-b82c5d08bb89", "metadata": {}, "source": [ "## Cleanup" ] }, { "cell_type": "code", "execution_count": null, "id": "ab70f4c9-6b99-49df-b6dd-27d068f60d9a", "metadata": { "vscode": { "languageId": "java" } }, "outputs": [], "source": [ "// Remove the data\n", "client.truncate(null, \"demo\", \"space-doc\", null);\n", "System.out.println(\"Data removed\");\n", "\n", "client.close();" ] } ], "metadata": { "kernelspec": { "display_name": "Java", "language": "java", "name": "java" }, "language_info": { "codemirror_mode": "java", "file_extension": ".jshell", "mimetype": "text/x-java-source", "name": "Java", "pygments_lexer": "java", "version": "17.0.5+8-Ubuntu-2ubuntu120.04" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false }, "toc-autonumbering": false, "toc-showcode": false, "toc-showmarkdowntxt": false, "toc-showtags": false, "vscode": { "interpreter": { "hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49" } } }, "nbformat": 4, "nbformat_minor": 5 }