oracles数据迁移
It was simply a no-brainer: NoSQL, JSON-storing capabilities, Oracle’s cloud-based Autonomous Database (I’m no DBA, so having an enterprise-level database at my fingertips, but without having to go through all the admin stuff all by myself, eliminated a big non-starter), plus having all this available under the free tier — I knew I had to use that in a Swift app for Apple’s iOS and MacOS devices.
这简直是不费吹灰之力: NoSQL , JSON存储功能,Oracle基于云的自治数据库 (我不是DBA,因此可以轻松触及企业级数据库,而不必经历所有管理工作我自己消除了一个很大的入门者),并在免费套餐下提供了所有这些功能-我知道我必须在Apple iOS和MacOS设备的Swift应用程序中使用它。
There are only 3 easy steps:Step 1. Create a database instanceStep 2. Add test dataStep 3. The Swift app
So let’s dive right in …
因此,让我们潜入……
步骤1.创建数据库实例 (Step 1. Create a database instance)
First things first, let’s start by creating our database on Oracle Cloud.
首先,让我们从在Oracle Cloud上创建数据库开始。
注册免费试用帐户 (Register for a free trial account)
If you don’t have one already, go to this page and register for one.
如果您还没有,请转到此页面注册一个。
Please note that even if the free trial has a (temporal and financial) limit, we will use only resources under the free tier — so they’ll stay accessible and free even after the trial is over.
请注意,即使免费试用版有(时间和财务上的)限制,我们也将仅使用免费层下的资源-这样,即使试用期结束,它们也将保持可访问和免费的状态。
创建数据库实例 (Create the database instance)
In the welcome email you received from OCI you’ll find the Cloud Console
login link. Use it and login into your main dashboard.
在您从OCI收到的欢迎电子邮件中,您会找到Cloud Console
登录链接。 使用它并登录到主仪表板。
Now let’s go through the instance creation steps:
现在让我们完成实例创建步骤:
![Image for post](https://i-blog.csdnimg.cn/blog_migrate/7f7b6ca4961b7df8293ed7cb71b0b57a.png)
![Image for post](https://i-blog.csdnimg.cn/blog_migrate/d34a0bad35cd64701d0edd8b2405369d.png)
In the database settings pane, we can safely use all the defaults.
在数据库设置窗格中,我们可以安全地使用所有默认值。
Just check that Workload Type
is set to Transaction Processing
and Access type
is Allow secure access from everywhere
.
只需检查Workload Type
设置为Transaction Processing
和 Access type
是 Allow secure access from everywhere
。
It may be useful to use your own Database name
and Display name
and of course, you must input your own password.
使用自己的Database name
和Display name
可能很有用,当然,您必须输入自己的密码 。
Click the Create Autonomous Database button and wait for the instance to be created.
![Image for post](https://miro.medium.com/max/9999/1*AarmFsj-imEvmgfnExsY7g.png)
You will get a free tier database instance, with 1 OCPU and 20TB storage. That should be more than enough for our exercise, and maybe it may carry you through all the “side-project” phase of your app :-) There are actually two of these available for free at the time of writing, please look here to see all current details.
您将获得一个带有1个OCPU和20TB存储空间的免费层数据库实例。 对于我们的练习来说,这应该绰绰有余了,也许它可以带您完成应用程序的所有“副项目”阶段:-)在撰写本文时,实际上其中有两个是免费的,请查看此处查看所有当前详细信息。
访问您的实例 (Access your instance)
Once the database is provisioned you’ll see a screen like this one:
设置数据库后,您将看到如下屏幕:
![Image for post](https://i-blog.csdnimg.cn/blog_migrate/ba528f6605520f9384c513b2e70a871c.png)
Let’s have a first glimpse at the JSON/NoSQL capabilities of the database. For a more in-depth view please take a look at this article by Todd Sharp.
让我们首先看一下数据库的JSON / NoSQL功能。 如需更深入的了解,请阅读Todd Sharp的这篇文章 。
After opening the Service Console
go to the Development
section:
打开Service Console
转到“ Development
部分:
![Image for post](https://miro.medium.com/max/9999/1*DqvfxoR7ILEc1A3ljW-yqQ.png)
First, scroll down to the section RESTful Services and SODA and make note of the endpoint URL associated with your database. We’ll use it to access our db via REST later. It’s something like https://something-specific-to-your-database.adb.eu-frankfurt-1.oraclecloudapps.com/ords/
Now, open the SQL Developer Web
tool (by clicking the appropriate button in the Service Console
).
现在,打开SQL Developer Web
工具(通过单击Service Console
的相应按钮)。
![Image for post](https://miro.medium.com/max/9999/1*_gj4rOasLVYSU8VqMPmSPw.png)
步骤2.添加测试数据 (Step 2. Add test data)
We have now a fully working relational database, but how do we store our JSON documents and query them afterward?
我们现在有一个可以正常运行的关系数据库,但是我们如何存储JSON文档并在以后查询它们呢?
With …
与...
Simple Oracle Document Access (SODA) is a set of NoSQL-style APIs that let you create and store collections of documents (in particular JSON) in Oracle Database, retrieve them, and query them, without needing to know Structured Query Language (SQL) or how the documents are stored in the database
简单Oracle文档访问(SODA)是一组NoSQL风格的API,可让您在Oracle数据库中创建和存储文档集合(特别是JSON),检索文档并进行查询,而无需了解结构化查询语言(SQL)或文档如何存储在数据库中
Data in SODA is organized in collections
and documents
, so let’s dive right in by creating a collection named fruit
.
SODA中的数据以collections
和documents
形式进行组织,因此让我们直接创建一个名为fruit
的集合。
![Image for post](https://i-blog.csdnimg.cn/blog_migrate/60ea0fad2b32c83493db9224830d23c4.png)
![Image for post](https://i-blog.csdnimg.cn/blog_migrate/dcc9f00b5ee637ad8f8c034de9f071d3.png)
And now let’s add some JSON-based documents (and yes, these are documents, so there is no fixed schema).
现在,我们添加一些基于JSON的文档(是的,这些都是文档,因此没有固定的架构)。
![Image for post](https://i-blog.csdnimg.cn/blog_migrate/b9b49cb5bd69f968df9bbdf3c4982302.png)
At this stage, you could (and really should) go ahead and add some fruits of your own.
在这个阶段,您可以(并且确实应该)继续添加自己的成果。
We will revert for a while now to SQL, just to see how one could query the SODA data (and to see how many fruits we already have in our collection).
现在,我们将恢复到SQL一段时间,以查看如何查询SODA数据(并查看我们的集合中已有多少水果)。
![Image for post](https://miro.medium.com/max/9999/1*nXMBm2SKyoOWw6Nd4nBqRg.png)
Nice. We’re done with setting up the database and test data, so let’s move now to …
真好 我们已经完成了数据库和测试数据的设置,现在让我们转到…
步骤3. Swift应用 (Step 3. The Swift app)
We will use the REST API available for our cloud autonomous database, called Oracle REST Data Services (ORDS
).
我们将使用可用于我们的云自治数据库的REST API,称为Oracle REST数据服务 ( ORDS
)。
For the purposes of this article, I’ll try to keep things as simple as possible. So, for now, our iPhone app will do one thing only: retrieve the fruit collection from our database and display it in a list.
出于本文的目的,我将尝试使事情尽可能简单。 因此,就目前而言,我们的iPhone应用程序只会做一件事: 从数据库中检索水果集合并将其显示在列表中 。
The ORDS
call for obtaining the listing of a collection is (now it’s time to remember the endpoint URL I’ve asked you to save):
用于获取集合列表的ORDS
调用是( 现在是时候记住我要求您保存的端点URL了 ):
$ curl -X GET https://YOUR-ENDPOINT-URL/admin/soda/latest/fruit
If you are inclined to test this from the command line please add the Authorization
HTTP header field for ADMIN:[YOUR-PASSWORD]
如果您倾向于从命令行进行测试,请添加ADMIN:[YOUR-PASSWORD]
的Authorization
HTTP标头字段ADMIN:[YOUR-PASSWORD]
设置应用 (Setting up the app)
Let’s keep things fun and use the latest Xcode 12 and SwiftUI 2.0 (both in beta when I write this, but we like living on the edge, right ?). It goes without saying that all our async code will use Combine.
让我们保持乐趣,并使用最新的Xcode 12和SwiftUI 2.0 (在我撰写本文时都处于beta版,但我们喜欢生活在边缘,对吗?)。 不用说,我们所有的异步代码都将使用Combine 。
So, fire up Xcode and create a new App project using Swift/SwiftUI.
因此,启动Xcode并使用Swift / SwiftUI创建一个新的App项目。
The HTTP abstraction layer is implemented using ideas and code “borrowed” from this great article by Vadim Bulavin. So if you feel you need a more thorough understanding of the concepts you’re encouraged to read it.
HTTP抽象层是使用Vadim Bulavin的这篇出色文章中 “构思”的思想和代码实现的。 因此,如果您需要对这些概念有更全面的了解,建议您阅读。
中介 (The Agent)
The actual REST request is made using Combine
’s own dataTaskPublisher
, encapsulated in Vadim’s Agent
struct.
实际的REST请求是采用由Combine
自身dataTaskPublisher
,封装在瓦迪姆的Agent
结构。
该模型 (The model)
We need now to what kind of JSON we get back from the database. if you run the curl
command listed above you’ll get back this:
现在,我们需要从数据库中获取哪种JSON。 如果运行上面列出的curl
命令,您将得到以下信息:
Pretty much all the entities are not changing based on our schema, the only variable part is under the value
key.
几乎所有实体都不会根据我们的架构进行更改,唯一的可变部分在value
键下。
So let’s model first the invariant entities. The SODA
namespace encapsulates everything related to SODA (obviously :-) and can be reused between apps, the use of generics ensures that it works with any kind of documents (the T
in there is meant to be replaced by our document’s model).
因此,让我们首先为不变实体建模。 SODA
命名空间封装了与SODA相关的所有内容(显然是:-),并且可以在应用程序之间重用。泛型的使用确保了它可以与任何类型的文档一起使用(其中的T
被我们的文档模型所取代)。
The Item
struct should be Identifiable
(and we’re lucky we have an id
key in the source JSON string) because we’ll use it in our SwiftUI views.
Item
结构应该是可Identifiable
(很幸运,我们在源JSON字符串中有一个id
键),因为我们将在SwiftUI视图中使用它。
While we’re at it, I added things like our agent and the connection information. You’ll put here you’re own endpoint URL and password.
在进行此操作时,我添加了诸如代理和连接信息之类的内容。 您将在此处输入自己的端点URL和密码。
Now we will model our fruit
document, based on the fields we used when we entered our test data.
现在,我们将根据输入测试数据时使用的字段对fruit
文档进行建模。
color
is optional because not all documents contain it.
color
是可选的,因为并非所有文档都包含该color
。
API请求 (The API request)
Let’s create now a publisher encapsulating our API request (remember, the one returning all the documents in our collection)
现在创建一个封装我们的API请求的发布者(请记住,该发布者返回了我们集合中的所有文档)
Again, it’s generic, so it’ll accept any kind of document model.
同样,它是通用的,因此它将接受任何类型的文档模型。
The Agent
publisher is sending a Response
document, containing both the actual decoded document model object and the HTTP response. On line 9 we keep only the model object and we pass it downstream.
Agent
发布者正在发送Response
文档,其中包含实际的已解码文档模型对象和HTTP响应。 在第9行,我们仅保留模型对象,然后将其传递到下游。
Our publisher sends a SODA.Collection<T>
downstream, where T
will be “replaced” by our document model (the Fruit
struct in our example).
我们的发布者向下游发送SODA.Collection<T>
,其中T
将被我们的文档模型(在本示例中为Fruit
结构)“替换”。
视图模型 (The view-model)
We need now to actually call the API endpoint, parse the results and store them for later usage in the view.
现在,我们需要实际调用API端点,解析结果并将其存储以供以后在视图中使用。
Now, that’s a pretty standard ObservableObject
.
现在,这是一个非常标准的ObservableObject
。
On line 3 we store the database items containing our Fruits
(and using the generics mechanism we ensure that the JSON decoder in the agent uses the Fruit
model).
在第3行中,我们存储了包含Fruits
的数据库项(并使用泛型机制,确保代理中的JSON解码器使用Fruit
模型)。
Line 12: We receive a SODA.Collection
value and we keep only the items
array.
第12行 :我们收到了SODA.Collection
值,我们只保留items
数组。
Line 13: Yes, I know, proper error handling should be implemented here, but remember, we want to keep things simple for now.
第13行 :是的,我知道,应该在此处实施适当的错误处理,但是请记住,我们现在想使事情保持简单。
风景 (The View)
This is really the easiest part, we iterate through our items and display them in a readable format.
这确实是最简单的部分,我们遍历项目并以可读格式显示它们。
And that’s the cheerful result, exactly what we expected:
这就是令人欣喜的结果,正是我们所期望的:
![Image for post](https://miro.medium.com/max/9999/1*gf4diWCZyxAHhJHQ16AQBQ.png)
结论和下一步 (Conclusion and next steps)
This is only minimal proof of concept, but it goes through all the basics. We’ve created a cloud-hosted Oracle autonomous database, added JSON documents to it, executed some very basic queries, both in SODA and “classic” SQL and then we connected to it in a Swift iOS app via REST/ORDS.
这只是概念的最低限度的证明,但它涵盖了所有基础知识。 我们创建了一个云托管的Oracle自治数据库,向其中添加了JSON文档,在SODA和“经典” SQL中执行了一些非常基本的查询,然后我们通过REST / ORDS在Swift iOS应用中将其连接到数据库。
IMHO, until recently, the cost of entry for anyone wanting to use an enterprise-level database as the backend for a mobile app was too high. It required very specialized skills to install, configure, and operate, plus the cost of licenses was above the budget of an independent developer, side-project, or small startup.
恕我直言,直到最近,任何想要使用企业级数据库作为移动应用程序后端的人的入门成本都太高了。 它需要非常专业的技能来进行安装,配置和操作,此外,许可证的成本要高于独立开发人员,附带项目或小型创业公司的预算。
The overall solution was too complex, even factoring in the benefits (like raw power, stability, technological maturity, and very high scalability potential). With the advent of the cloud versions of enterprise software, that’s not the case anymore.
整体解决方案过于复杂,甚至没有考虑到收益(例如原始功率,稳定性,技术成熟度和极高的可扩展性潜力)。 随着企业软件云版本的问世,情况已不再如此。
So, I plan to use this opportunity to expand our little example a little in future articles, adding all the CRUD operations and, of course, error handling.
因此,我计划利用这一机会在以后的文章中稍微扩展一下我们的小例子,添加所有CRUD操作,当然还有错误处理。
The full Swift source is accessible on Github.
完整的Swift源代码可以在Github上访问。
oracles数据迁移