1"use strict";(self.webpackChunk=self.webpackChunk||[]).push([[7950],{3905:(e,t,n)=>{n.d(t,{Zo:()=>d,kt:()=>h});var a=n(7294);function o(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function i(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var a=Object.getOwnPropertySymbols(e);t&&(a=a.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,a)}return n}function l(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?i(Object(n),!0).forEach((function(t){o(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):i(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}function s(e,t){if(null==e)return{};var n,a,o=function(e,t){if(null==e)return{};var n,a,o={},i=Object.keys(e);for(a=0;a<i.length;a++)n=i[a],t.indexOf(n)>=0||(o[n]=e[n]);return o}(e,t);if(Object.getOwnPropertySymbols){var i=Object.getOwnPropertySymbols(e);for(a=0;a<i.length;a++)n=i[a],t.indexOf(n)>=0||Object.prototype.propertyIsEnumerable.call(e,n)&&(o[n]=e[n])}return o}var r=a.createContext({}),c=function(e){var t=a.useContext(r),n=t;return e&&(n="function"==typeof e?e(t):l(l({},t),e)),n},d=function(e){var t=c(e.components);return a.createElement(r.Provider,{value:t},e.children)},u="mdxType",p={inlineCode:"code",wrapper:function(e){var t=e.children;return a.createElement(a.Fragment,{},t)}},m=a.forwardRef((function(e,t){var n=e.components,o=e.mdxType,i=e.originalType,r=e.parentName,d=s(e,["components","mdxType","originalType","parentName"]),u=c(n),m=o,h=u["".concat(r,".").concat(m)]||u[m]||p[m]||i;return n?a.createElement(h,l(l({ref:t},d),{},{components:n})):a.createElement(h,l({ref:t},d))}));function h(e,t){var n=arguments,o=t&&t.mdxType;if("string"==typeof e||o){var i=n.length,l=new Array(i);l[0]=m;var s={};for(var r in t)hasOwnProperty.call(t,r)&&(s[r]=t[r]);s.originalType=e,s[u]="string"==typeof e?e:o,l[1]=s;for(var c=2;c<i;c++)l[c]=n[c];return a.createElement.apply(null,l)}return a.createElement.apply(null,n)}m.displayName="MDXCreateElement"},426:(e,t,n)=>{n.r(t),n.d(t,{assets:()=>d,contentTitle:()=>r,default:()=>h,frontMatter:()=>s,metadata:()=>c,toc:()=>u});var a=n(7462),o=n(3366),i=(n(7294),n(3905)),l=["components"],s={title:"Database Service"},r=void 0,c={unversionedId:"sdk/hive/java/guides/database-service",id:"sdk/hive/java/guides/database-service",title:"Database Service",description:"Hive SDK uploads unstructured data to the corresponding Vault through DatabaseService class. Unstructured database type data is one of the data types supported by Hive SDK. DatabaseService class is one of the derived sub-services in Vault Service, which is used to support operations on unstructured data types, such as insert/update/query of the CRUD functions. Once the unstructured data is saved in Hive Node, it will be persisted in the corresponding MongoDB service.",source:"@site/../docs/7.sdk/hive/java/guides/database-service.md",sourceDirName:"7.sdk/hive/java/guides",slug:"/sdk/hive/java/guides/database-service",permalink:"/sdk/hive/java/guides/database-service",draft:!1,editUrl:"https://github.com/elastos/Elastos.Wiki/edit/master/website/../docs/7.sdk/hive/java/guides/database-service.md",tags:[],version:"current",lastUpdatedBy:"racollette",lastUpdatedAt:1678815391,formattedLastUpdatedAt:"Mar 14, 2023",frontMatter:{title:"Database Service"},sidebar:"sdkSidebar",previous:{title:"Files Service",permalink:"/sdk/hive/java/guides/files-service"},next:{title:"Scripting Service",permalink:"/sdk/hive/java/guides/scripting-service"}},d={},u=[{value:"Insert a Document",id:"insert-a-document",level:2},{value:"Create Collection",id:"create-collection",level:2},{value:"Delete Collection",id:"delete-collection",level:2},{value:"Insert",id:"insert",level:2},{value:"Update",id:"update",level:2},{value:"Example of updating multiple pieces of data:",id:"example-of-updating-multiple-pieces-of-data",level:4},{value:"Delete",id:"delete",level:2},{value:"Example of deleting multiple pieces of data that meet the conditions are as follows:",id:"example-of-deleting-multiple-pieces-of-data-that-meet-the-conditions-are-as-follows",level:4},{value:"Count",id:"count",level:2},{value:"Find and Query",id:"find-and-query",level:2},{value:"An example of multiple search results is as follows:",id:"an-example-of-multiple-search-results-is-as-follows",level:4}],p={toc:u},m="wrapper";function h(e){var t=e.components,n=(0,o.Z)(e,l);return(0,i.kt)(m,(0,a.Z)({},p,n,{components:t,mdxType:"MDXLayout"}),(0,i.kt)("p",null,"Hive SDK uploads unstructured data to the corresponding Vault through DatabaseService class. Unstructured database type data is one of the data types supported by Hive SDK. DatabaseService class is one of the derived sub-services in Vault Service, which is used to support operations on unstructured data types, such as insert/update/query of the CRUD functions. Once the unstructured data is saved in Hive Node, it will be persisted in the corresponding MongoDB service."),(0,i.kt)("h2",{id:"insert-a-document"},"Insert a Document"),(0,i.kt)("p",null,"An example of inserted data is as follows:"),(0,i.kt)("pre",null,(0,i.kt)("code",{parentName:"pre",className:"language-java"},'Vault vault = new Vault(context, getVaultProviderAddress());\nDatabaseServic dbService = vault.getDatabaseServic();\n\nObjectNode doc = JsonNodeFactory.instance.objectNode();\ndoc.put("author", "john doe1");\ndoc.put("title", "Eve for Dummies1");\ndbService.insertOne(YOUR_COLLECT_NAME, doc,\nnew InsertOptions().bypassDocumentValidation(false))\n.thenAcceptAsync(insertResult -> {\nSystem.out.println("Inserted a document to database in success.");\n}).exceptionally(ex ->
1 {\nSystem.out.println("Failed to insert a document.");\nex.printStackTrace();\nreturn null;\n});\n')),(0,i.kt)("p",null,"To insert data, you need to construct Json data first and then insert it into the specified data table (COLLECTION_NAME). At the same time, you can specify the InsertOptions, which are derived from the corresponding functions of MongoDB database."),(0,i.kt)("h2",{id:"create-collection"},"Create Collection"),(0,i.kt)("p",null,"In MongoDB, collection represents a data table where one with a specified name is created. After creation, user data can be inserted."),(0,i.kt)("pre",null,(0,i.kt)("code",{parentName:"pre",className:"language-java"},'databaseService.createCollection("your_collection")\n.thenAcceptAsync(result -> {\nSystem.out.println("Created a collection successfully");\n}).exceptionally(ex -> {\nSystem.out.println("Failed to create the collection");\nex.printStackTrace();\nreturn null;\n});\n')),(0,i.kt)("h2",{id:"delete-collection"},"Delete Collection"),(0,i.kt)("p",null,"Delete the MongoDB data table and all the data within will be deleted."),(0,i.kt)("pre",null,(0,i.kt)("code",{parentName:"pre",className:"language-java"},'\ndatabaseService.deleteCollection("your_collection")\n.thenAcceptAsync(result -> {\nSystem.out.println("Deleted the collection successfully");\n}).exceptionally(ex -> {\nSystem.out.println("Failed to delete the collection");\nex.printStackTrace();\nreturn null;\n});\n')),(0,i.kt)("h2",{id:"insert"},"Insert"),(0,i.kt)("p",null,"Insert data into the data table, which can be a single or multiple pieces of data. As shown in the example above, the insert option InsertOptions originates from MongoDB\u2019s option to insert document. Please refer to the definition of InsertOptions for specific options. Here is an example of inserting multiple pieces of data:"),(0,i.kt)("pre",null,(0,i.kt)("code",{parentName:"pre",className:"language-java"},'List<JsonNode> nodes = new ArrayList<>();\n\nObjectNode doc = JsonNodeFactory.instance.objectNode();\ndoc.put("author", "john doe2");\ndoc.put("title", "Eve for Dummies2");\nnodes.add(doc);\n\ndoc.removeAll();\n\ndoc = JsonNodeFactory.instance.objectNode();\ndoc.put("author", "john doe3");\ndoc.put("title", "Eve for Dummies3");\nnodes.add(doc);\ndbService.insertMany("your_collection", nodes,\nnew InsertOptions().bypassDocumentValidation(false).ordered(true))\n.thenAcceptAsync(insertResult -> {\nSystem.out.println("Inserted two documents successfully.");\n}).exceptionally(ex -> {\nSystem.out.println("Failed to insert documents.");\nex.printStackTrace();\nreturn null;\n});\n')),(0,i.kt)("h2",{id:"update"},"Update"),(0,i.kt)("p",null,"Just like inserting data, updating data also provides two versions of interfaces: updating the first or multiple pieces of data that meet the conditions, and updating data also supports providing update options. Update a single piece of data as follows:"),(0,i.kt)("pre",null,(0,i.kt)("code",{parentName:"pre",className:"language-java"},'ObjectNode filter = JsonNodeFactory.instance.objectNode();\nfilter.put("author", "john doe1");\nObjectNode doc = JsonNodeFactory.instance.objectNode();\ndoc.put("author", "john doe1");\ndoc.put("title", "Eve for Dummies1_1");\nObjectNode update = JsonNodeFactory.instance.objectNode();\nupdate.put("$set", doc);\ndbService.updateOne("your_collection", filter, update,\n new UpdateOptions().setBypassDocumentValidation(false).setUpsert(true))\n.thenAcceptAsync(updateResult -> {\n System.out.println("updated the document successfully.");\n}).exceptionally(ex -> {\n System.out.println("Failed to update the document.");\n ex.printStackTrace();\n return null;\n});\n')),(0,i.kt)("h4",{id:"example-of-updating-multiple-pieces-of-data"},"Example of updating multiple pieces of data:"),(0,i.kt)("pre",null,(0,i.kt)("code",{parentName:"pre",className:"language-java"},'ObjectNode filter = JsonNodeFactory.instance.objectNode();\nfilter.put("author", "john doe1");\nObjectNode doc = JsonNodeFactory.instance.objectNode();\ndoc.put("author", "john doe1");\ndoc.put("title", "Eve for Dummies1_1");\nObjectNode update = JsonNodeFactory.instance.objectNode();\nupdate.put("$set", doc);\ndatabaseService.updateMany("your_collection", filter, update,\nnew UpdateOptions().setBypassDocumentValidation(false).setUpsert(true))\n.thenAcceptAsync(updateResult -> {\nSystem.out.println("Updated the documents successfully.");\n}).exceptionally(ex ->
1 {\nSystem.out.println("Failed to update the documents.");\nex.printStackTrace();\nreturn null;\n});\n')),(0,i.kt)("h2",{id:"delete"},"Delete"),(0,i.kt)("p",null,"The method of deleting data is the same - two versions are provided. Only by providing indication and deletion conditions, the data that meets the conditions will be deleted. The following is an example of deleting the first piece of data that meets the conditions:"),(0,i.kt)("pre",null,(0,i.kt)("code",{parentName:"pre",className:"language-java"},'ObjectNode filter = JsonNodeFactory.instance.objectNode();\nfilter.put("author", "john doe2");\ndatabaseService.deleteOne("your_collection", filter)\n.thenAcceptAsync(deleteResult -> {\nSystem.out.println("delete the document successfully.");\n}).exceptionally(ex -> {\nSystem.out.println("failed to delete the document.");\nex.printStackTrace();\nreturn null;\n});\n')),(0,i.kt)("h4",{id:"example-of-deleting-multiple-pieces-of-data-that-meet-the-conditions-are-as-follows"},"Example of deleting multiple pieces of data that meet the conditions are as follows:"),(0,i.kt)("pre",null,(0,i.kt)("code",{parentName:"pre",className:"language-java"},'ObjectNode filter = JsonNodeFactory.instance.objectNode();\nfilter.put("author", "john doe2");\ndatabaseService.deleteMany("your_collection", filter)\n.thenAcceptAsync(deleteResult -> {\nSystem.out.println("delete the documents successfully.");\n}).exceptionally(ex -> {\nSystem.out.println("failed to delete the documents.");\nex.printStackTrace();\nreturn null;\n});\n')),(0,i.kt)("h2",{id:"count"},"Count"),(0,i.kt)("p",null,"The following methods can be used to calculate the amount of data in the table. You need to specify the table name, query conditions, and parameters."),(0,i.kt)("pre",null,(0,i.kt)("code",{parentName:"pre",className:"language-java"},'ObjectNode filter = JsonNodeFactory.instance.objectNode();\nfilter.put("author", "john doe1");\ndatabaseService.countDocuments("your_collection", filter,\nnew CountOptions().setLimit(1L).setSkip(0L).setMaxTimeMS(1000000000L))\n.thenAcceptAsync(count -> {\nSystem.out.println("Count documents successfully.");\nSystem.out.println("count => " + count);\n}).exceptionally(ex -> {\nSystem.out.println("Failed to count the documents.");\nex.printStackTrace();\nreturn null;\n});\n')),(0,i.kt)("h2",{id:"find-and-query"},"Find and Query"),(0,i.kt)("p",null,"There are three ways to search data: search the first piece of data that meets the conditions, search multiple pieces of data that meet the conditions, and return version of multiple pieces of data with multiple query conditions. All three methods are required: table name, query conditions and query options."),(0,i.kt)("pre",null,(0,i.kt)("code",{parentName:"pre",className:"language-java"},'ObjectNode query = JsonNodeFactory.instance.objectNode();\nquery.put("author", "john doe1");\ndatabaseService.findOne("test_collection", query, new FindOptions().setSkip(0).setLimit(0))\n.thenAcceptAsync(document -> {\nSystem.out.println("find the document successfully.");\nSystem.out.println("author => " + document.get("author").textValue());\nSystem.out.println("title => " + document.get("title").textValue());\n}).exceptionally(ex -> {\nSystem.out.println("failed to find the document.");\nex.printStackTrace();\nreturn null;\n});\n')),(0,i.kt)("h4",{id:"an-example-of-multiple-search-results-is-as-follows"},"An example of multiple search results is as follows:"),(0,i.kt)("pre",null,(0,i.kt)("code",{parentName:"pre",className:"language-java"},'ObjectNode query = JsonNodeFactory.instance.objectNode();\nquery.put("author", "john doe1");\ndatabaseService.findMany("test_collection", query, new FindOptions().setSkip(0).setLimit(0))\n.thenAcceptAsync(list -> {\nSystem.out.println("find the documents successfully.");\nSystem.out.println("author => " + document.get("author").textValue());\nSystem.out.println("title => " + document.get("title").textValue());\n}).exceptionally(ex -> {\nSystem.out.println("failed to find the documents.");\nex.printStackTrace();\nreturn null;\n});\n')),(0,i.kt)("p",null,"Below is an example of more query options that can be used when finding multiple results is as follows:"),(0,i.kt)("pre",null,(0,i.kt)("code",{parentName:"pre",className:"language-java"},'ObjectNode query = JsonNodeFactory.instance.objectNode();\nquery.put("author", "john doe1");\nAssertions.assertNotNull(databaseService.query(COLLECTION_NAME, query, null)\n.thenAcceptAsync(list -> {\nSystem.out.println("find the documents successfully.");\nSystem.out.println("author => " + document.get("author").textValue());\nSystem.out.println("title => " + document.get("title").textValue());\n}).exceptionally(ex ->
1 {\nSystem.out.println("failed to find the documents.");\nex.printStackTrace();\nreturn null;\n});\n')))}h.isMDXComponent=!0}}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.