动手实践 MicroStream:Java 持久性的新方法

MicroStream是目前Java领域最有趣的项目之一。它采用一种新的方法来实现 Java 持久性,充当一种无缝的对象持久性数据层。MicroStream 消除了弄清楚如何持久化应用程序的运行时图的摩擦。所以这次我们将直接进入演示。您将首先学习如何将MicroStream与默认文件系统一起使用,然后我们将更新我们的演示程序以使用MariaDB RDBMS。

设置 Java 项目

让我们从创建一个新的 Java 项目开始。我们将使用一个简单的 Maven 项目,但任何方法都有效。打开命令行并输入清单 1 中的代码,创建一个具有 Maven 原型的新项目。

清单 1.创建新项目

<span style="background-color:#e5e5e5"><span style="color:#4e4242"><code><span style="color:#000000">
$ mvn archetype</span><span style="color:#666600">:</span><span style="color:#000000">generate </span><span style="color:#666600">-</span><span style="color:#660066"><strong>DgroupId</strong></span><span style="color:#666600">=</span><span style="color:#000000">com</span><span style="color:#666600">.</span><span style="color:#000000">infoworld </span><span style="color:#666600">-</span><span style="color:#660066"><strong>DartifactId</strong></span><span style="color:#666600">=</span><span style="color:#000000">microstream </span><span style="color:#666600">-</span><span style="color:#660066"><strong>DarchetypeArtifactId</strong></span><span style="color:#666600">=</span><span style="color:#000000">maven</span><span style="color:#666600">-</span><span style="color:#000000">archetype</span><span style="color:#666600">-</span><span style="color:#000000">quickstart </span><span style="color:#666600">-</span><span style="color:#660066"><strong>DarchetypeVersion</strong></span><span style="color:#666600">=</span><span style="color:#006666">1.4</span> <span style="color:#666600">-</span><span style="color:#660066"><strong>Dpackage</strong></span><span style="color:#666600">=</span><span style="color:#000000">com</span><span style="color:#666600">.</span><span style="color:#000000">infoworld </span><span style="color:#666600">-</span><span style="color:#660066"><strong>DinteractiveMode</strong></span><span style="color:#666600">=</span><span style="color:#000088"><strong>false</strong></span></code></span></span>

请注意,我已将组 ID 设置为 ,项目 ID 设置为 ,包设置为 。com.infoworldmicrostreamcom.infoworld

 

接下来,打开文件并添加清单 2 中所示的依赖项。我们将使用MicroStream的嵌入式存储库,这是最简单的方法。此外,请确保 Maven 编译器版本设置为 1.8。/myproject/pom.xml

0 秒 0 秒的 30 秒卷 0%
 

清单 2.绒球.xml设置

<span style="background-color:#e5e5e5"><span style="color:#4e4242"><code>
<span style="color:#000088"><strong><properties></strong></span>
    <span style="color:#000088"><strong><project.build.sourceEncoding></strong></span><span style="color:#000000">UTF-8</span><span style="color:#000088"><strong></project.build.sourceEncoding></strong></span>
    <span style="color:#000088"><strong><maven.compiler.source></strong></span><span style="color:#000000">1.8</span><span style="color:#000088"><strong></maven.compiler.source></strong></span>
    <span style="color:#000088"><strong><maven.compiler.target></strong></span><span style="color:#000000">1.8</span><span style="color:#000088"><strong></maven.compiler.target></strong></span>
  <span style="color:#000088"><strong></properties></strong></span>
<span style="color:#880000"><em><!-- ... --></em></span>
<span style="color:#000088"><strong><dependency></strong></span>
    <span style="color:#000088"><strong><groupId></strong></span><span style="color:#000000">one.microstream</span><span style="color:#000088"><strong></groupId></strong></span>
    <span style="color:#000088"><strong><artifactId></strong></span><span style="color:#000000">microstream-storage-embedded</span><span style="color:#000088"><strong></artifactId></strong></span>
    <span style="color:#000088"><strong><version></strong></span><span style="color:#000000">07.01.00-MS-GA</span><span style="color:#000088"><strong></version></strong></span>
<span style="color:#000088"><strong></dependency></strong></span></code></span></span>

现在,我们可以运行 MicroStream 初学者项目并从命令行获取响应,如清单 3 所示。

清单 3.运行微流启动器

<span style="background-color:#e5e5e5"><span style="color:#4e4242"><code><span style="color:#000000">
$ mvn clean </span><span style="color:#000088"><strong>package</strong></span><span style="color:#000000">
$ java </span><span style="color:#666600">-</span><span style="color:#000000">cp target</span><span style="color:#666600">/</span><span style="color:#000000">microstream</span><span style="color:#666600">-</span><span style="color:#006666">1.0</span><span style="color:#666600">-</span><span style="color:#000000">SNAPSHOT</span><span style="color:#666600">.</span><span style="color:#000000">jar com</span><span style="color:#666600">.</span><span style="color:#000000">infoworld</span><span style="color:#666600">.</span><span style="color:#660066"><strong>App</strong></span>
<span style="color:#660066"><strong>Hello</strong></span> <span style="color:#660066"><strong>World</strong></span><span style="color:#666600">!</span></code></span></span>

添加要持久化的模型

接下来,让我们合并一个可以坚持的简单模型。我们将更改初学者项目的主类,使其采用三个命令行参数并将其保存在 .StringArrayList

清单 4.应用.java与阵列列表模型

<span style="background-color:#e5e5e5"><span style="color:#4e4242"><code>
<span style="color:#000088"><strong>package</strong></span><span style="color:#000000"> com</span><span style="color:#666600">.</span><span style="color:#000000">infoworld</span><span style="color:#666600">;</span>

<span style="color:#000088"><strong>import</strong></span><span style="color:#000000"> java</span><span style="color:#666600">.</span><span style="color:#000000">util</span><span style="color:#666600">.</span><span style="color:#660066"><strong>ArrayList</strong></span><span style="color:#666600">;</span>

<span style="color:#000088"><strong>public</strong></span> <span style="color:#000088"><strong>class</strong></span> <span style="color:#660066"><strong>App</strong></span> <span style="color:#666600">{</span>
  <span style="color:#000088"><strong>public</strong></span> <span style="color:#000088"><strong>static</strong></span> <span style="color:#000088"><strong>void</strong></span><span style="color:#000000"> main</span><span style="color:#666600">(</span><span style="color:#660066"><strong>String</strong></span><span style="color:#666600">[]</span><span style="color:#000000"> args</span><span style="color:#666600">)</span> <span style="color:#666600">{</span>
    <span style="color:#660066"><strong>ArrayList</strong></span><span style="color:#666600"><</span><span style="color:#660066"><strong>String</strong></span><span style="color:#666600">></span><span style="color:#000000"> list </span><span style="color:#666600">=</span> <span style="color:#000088"><strong>new</strong></span> <span style="color:#660066"><strong>ArrayList</strong></span><span style="color:#666600"><</span><span style="color:#660066"><strong>String</strong></span><span style="color:#666600">>();</span>

    <span style="color:#000088"><strong>for</strong></span> <span style="color:#666600">(</span><span style="color:#000088"><strong>int</strong></span><span style="color:#000000"> i </span><span style="color:#666600">=</span> <span style="color:#006666">0</span><span style="color:#666600">;</span><span style="color:#000000"> i </span><span style="color:#666600"><</span><span style="color:#000000"> args</span><span style="color:#666600">.</span><span style="color:#000000">length</span><span style="color:#666600">;</span><span style="color:#000000"> i</span><span style="color:#666600">++)</span> <span style="color:#666600">{</span><span style="color:#000000">
        list</span><span style="color:#666600">.</span><span style="color:#000000">add</span><span style="color:#666600">(</span><span style="color:#000000">args</span><span style="color:#666600">[</span><span style="color:#000000">i</span><span style="color:#666600">]);</span>
    <span style="color:#666600">}</span>

    <span style="color:#660066"><strong>System</strong></span><span style="color:#666600">.</span><span style="color:#000088"><strong>out</strong></span><span style="color:#666600">.</span><span style="color:#000000">println</span><span style="color:#666600">(</span><span style="color:#000000">list</span><span style="color:#666600">);</span>
  <span style="color:#666600">}</span>
<span style="color:#666600">}</span></code></span></span>

我们可以测试数组列表版本,如清单 5 所示。它接受传入的参数并将它们放在 中,然后输出它。List

清单 5.使用 ArrayList 模型运行应用.java

<span style="background-color:#e5e5e5"><span style="color:#4e4242"><code><span style="color:#000000">
$ mvn clean </span><span style="color:#000088"><strong>package</strong></span><span style="color:#000000">
$ java </span><span style="color:#666600">-</span><span style="color:#000000">cp target</span><span style="color:#666600">/</span><span style="color:#000000">microstream</span><span style="color:#666600">-</span><span style="color:#006666">1.0</span><span style="color:#666600">-</span><span style="color:#000000">SNAPSHOT</span><span style="color:#666600">.</span><span style="color:#000000">jar com</span><span style="color:#666600">.</span><span style="color:#000000">infoworld</span><span style="color:#666600">.</span><span style="color:#660066"><strong>App</strong></span><span style="color:#000000"> test test2 test3
</span><span style="color:#666600">[</span><span style="color:#000000">test</span><span style="color:#666600">,</span><span style="color:#000000"> test2</span><span style="color:#666600">,</span><span style="color:#000000"> test3</span><span style="color:#666600">]</span></code></span></span>

使用微流存储列表

现在,修改此代码以使用MicroStream存储列表。清单 6 显示了它是如何完成的。

清单 6.保存列表

<span style="background-color:#e5e5e5"><span style="color:#4e4242"><code>
<span style="color:#000088"><strong>package</strong></span><span style="color:#000000"> com</span><span style="color:#666600">.</span><span style="color:#000000">infoworld</span><span style="color:#666600">;</span>

<span style="color:#000088"><strong>import</strong></span><span style="color:#000000"> java</span><span style="color:#666600">.</span><span style="color:#000000">util</span><span style="color:#666600">.</span><span style="color:#660066"><strong>ArrayList</strong></span><span style="color:#666600">;</span>

<span style="color:#000088"><strong>public</strong></span> <span style="color:#000088"><strong>class</strong></span> <span style="color:#660066"><strong>App</strong></span> <span style="color:#666600">{</span>
  <span style="color:#000088"><strong>public</strong></span> <span style="color:#000088"><strong>static</strong></span> <span style="color:#000088"><strong>void</strong></span><span style="color:#000000"> main</span><span style="color:#666600">(</span><span style="color:#660066"><strong>String</strong></span><span style="color:#666600">[]</span><span style="color:#000000"> args</span><span style="color:#666600">)</span> <span style="color:#666600">{</span>
    <span style="color:#660066"><strong>ArrayList</strong></span><span style="color:#666600"><</span><span style="color:#660066"><strong>String</strong></span><span style="color:#666600">></span><span style="color:#000000"> list</span><span style="color:#666600">;</span>
      <span style="color:#000088"><strong>if</strong></span> <span style="color:#666600">(</span><span style="color:#000000">args</span><span style="color:#666600">.</span><span style="color:#000000">length </span><span style="color:#666600">></span> <span style="color:#006666">0</span><span style="color:#666600">)</span> <span style="color:#666600">{</span><span style="color:#000000">
        list </span><span style="color:#666600">=</span> <span style="color:#000088"><strong>new</strong></span> <span style="color:#660066"><strong>ArrayList</strong></span><span style="color:#666600"><</span><span style="color:#660066"><strong>String</strong></span><span style="color:#666600">>();</span>
        <span style="color:#000088"><strong>for</strong></span> <span style="color:#666600">(</span><span style="color:#000088"><strong>int</strong></span><span style="color:#000000"> i </span><span style="color:#666600">=</span> <span style="color:#006666">0</span><span style="color:#666600">;</span><span style="color:#000000"> i </span><span style="color:#666600"><</span><span style="color:#000000"> args</span><span style="color:#666600">.</span><span style="color:#000000">length</span><span style="color:#666600">;</span><span style="color:#000000"> i</span><span style="color:#666600">++)</span> <span style="color:#666600">{</span><span style="color:#000000">
          list</span><span style="color:#666600">.</span><span style="color:#000000">add</span><span style="color:#666600">(</span><span style="color:#000000">args</span><span style="color:#666600">[</span><span style="color:#000000">i</span><span style="color:#666600">]);</span>
        <span style="color:#666600">}</span>
      <span style="color:#660066"><strong>EmbeddedStorageManager</strong></span><span style="color:#000000"> manager </span><span style="color:#666600">=</span> <span style="color:#660066"><strong>EmbeddedStorage</strong></span><span style="color:#666600">.</span><span style="color:#000000">start</span><span style="color:#666600">();</span><span style="color:#000000">
      manager</span><span style="color:#666600">.</span><span style="color:#000000">setRoot</span><span style="color:#666600">(</span><span style="color:#000000">list</span><span style="color:#666600">);</span><span style="color:#000000">
      manager</span><span style="color:#666600">.</span><span style="color:#000000">storeRoot</span><span style="color:#666600">();</span>
      <span style="color:#660066"><strong>System</strong></span><span style="color:#666600">.</span><span style="color:#000088"><strong>out</strong></span><span style="color:#666600">.</span><span style="color:#000000">println</span><span style="color:#666600">(</span><span style="color:#008800">"Saved: "</span> <span style="color:#666600">+</span><span style="color:#000000"> list</span><span style="color:#666600">);</span>
  <span style="color:#666600">}</span>
<span style="color:#666600">}</span></code></span></span>

请注意,使用默认值保存状态是多么容易。当我们在没有参数的情况下调用时,它会创建一个具有默认值的新 MicroStream 引擎,包括默认文件存储位置和持久性策略。EmbeddedStorage.start()

然后,我们将根对象设置为我们的列表。每个 MicroStream 应用程序都有一个根对象,它是要持久保存的整个对象图的根。manager.setRoot()

告诉引擎保持状态。manager.storeRoot()

清单 7 显示了该应用程序的完整版本。请注意,它在传入时会保留列表;如果没有,它将检索并显示它。

清单 7.使用默认值保存和恢复对象

<span style="background-color:#e5e5e5"><span style="color:#4e4242"><code>
<span style="color:#000088"><strong>import</strong></span><span style="color:#000000"> one</span><span style="color:#666600">.</span><span style="color:#000000">microstream</span><span style="color:#666600">.</span><span style="color:#000000">storage</span><span style="color:#666600">.</span><span style="color:#000000">embedded</span><span style="color:#666600">.</span><span style="color:#000000">types</span><span style="color:#666600">.</span><span style="color:#660066"><strong>EmbeddedStorage</strong></span><span style="color:#666600">;</span>
<span style="color:#000088"><strong>import</strong></span><span style="color:#000000"> one</span><span style="color:#666600">.</span><span style="color:#000000">microstream</span><span style="color:#666600">.</span><span style="color:#000000">storage</span><span style="color:#666600">.</span><span style="color:#000000">embedded</span><span style="color:#666600">.</span><span style="color:#000000">types</span><span style="color:#666600">.</span><span style="color:#660066"><strong>EmbeddedStorageManager</strong></span><span style="color:#666600">;</span>

<span style="color:#000088"><strong>import</strong></span><span style="color:#000000"> java</span><span style="color:#666600">.</span><span style="color:#000000">nio</span><span style="color:#666600">.</span><span style="color:#000000">file</span><span style="color:#666600">.</span><span style="color:#660066"><strong>Paths</strong></span><span style="color:#666600">;</span>
<span style="color:#000088"><strong>import</strong></span><span style="color:#000000"> java</span><span style="color:#666600">.</span><span style="color:#000000">util</span><span style="color:#666600">.</span><span style="color:#660066"><strong>ArrayList</strong></span><span style="color:#666600">;</span>

<span style="color:#000088"><strong>public</strong></span> <span style="color:#000088"><strong>class</strong></span> <span style="color:#660066"><strong>App</strong></span> <span style="color:#666600">{</span>
  <span style="color:#000088"><strong>public</strong></span> <span style="color:#000088"><strong>static</strong></span> <span style="color:#000088"><strong>void</strong></span><span style="color:#000000"> main</span><span style="color:#666600">(</span><span style="color:#660066"><strong>String</strong></span><span style="color:#666600">[]</span><span style="color:#000000"> args</span><span style="color:#666600">)</span> <span style="color:#666600">{</span>
    <span style="color:#660066"><strong>ArrayList</strong></span><span style="color:#666600"><</span><span style="color:#660066"><strong>String</strong></span><span style="color:#666600">></span><span style="color:#000000"> list</span><span style="color:#666600">;</span>
    <span style="color:#000088"><strong>if</strong></span> <span style="color:#666600">(</span><span style="color:#000000">args</span><span style="color:#666600">.</span><span style="color:#000000">length </span><span style="color:#666600">></span> <span style="color:#006666">0</span><span style="color:#666600">)</span> <span style="color:#666600">{</span><span style="color:#000000">
      list </span><span style="color:#666600">=</span> <span style="color:#000088"><strong>new</strong></span> <span style="color:#660066"><strong>ArrayList</strong></span><span style="color:#666600"><</span><span style="color:#660066"><strong>String</strong></span><span style="color:#666600">>();</span>
      <span style="color:#000088"><strong>for</strong></span> <span style="color:#666600">(</span><span style="color:#000088"><strong>int</strong></span><span style="color:#000000"> i </span><span style="color:#666600">=</span> <span style="color:#006666">0</span><span style="color:#666600">;</span><span style="color:#000000"> i </span><span style="color:#666600"><</span><span style="color:#000000"> args</span><span style="color:#666600">.</span><span style="color:#000000">length</span><span style="color:#666600">;</span><span style="color:#000000"> i</span><span style="color:#666600">++)</span> <span style="color:#666600">{</span><span style="color:#000000">
        list</span><span style="color:#666600">.</span><span style="color:#000000">add</span><span style="color:#666600">(</span><span style="color:#000000">args</span><span style="color:#666600">[</span><span style="color:#000000">i</span><span style="color:#666600">]);</span>
      <span style="color:#666600">}</span>
      <span style="color:#660066"><strong>EmbeddedStorageManager</strong></span><span style="color:#000000"> manager </span><span style="color:#666600">=</span> <span style="color:#660066"><strong>EmbeddedStorage</strong></span><span style="color:#666600">.</span><span style="color:#000000">start</span><span style="color:#666600">();</span><span style="color:#000000">
      manager</span><span style="color:#666600">.</span><span style="color:#000000">setRoot</span><span style="color:#666600">(</span><span style="color:#000000">list</span><span style="color:#666600">);</span><span style="color:#000000">
      manager</span><span style="color:#666600">.</span><span style="color:#000000">storeRoot</span><span style="color:#666600">();</span>
      <span style="color:#660066"><strong>System</strong></span><span style="color:#666600">.</span><span style="color:#000088"><strong>out</strong></span><span style="color:#666600">.</span><span style="color:#000000">println</span><span style="color:#666600">(</span><span style="color:#008800">"Saved: "</span> <span style="color:#666600">+</span><span style="color:#000000"> list</span><span style="color:#666600">);</span>
    <span style="color:#666600">}</span> <span style="color:#000088"><strong>else</strong></span> <span style="color:#666600">{</span>
      <span style="color:#660066"><strong>EmbeddedStorageManager</strong></span><span style="color:#000000"> storageEngine </span><span style="color:#666600">=</span> <span style="color:#660066"><strong>EmbeddedStorage</strong></span><span style="color:#666600">.</span><span style="color:#000000">start</span><span style="color:#666600">();</span><span style="color:#000000">
      list </span><span style="color:#666600">=</span> <span style="color:#666600">(</span><span style="color:#660066"><strong>ArrayList</strong></span><span style="color:#666600"><</span><span style="color:#660066"><strong>String</strong></span><span style="color:#666600">>)</span><span style="color:#000000"> storageEngine</span><span style="color:#666600">.</span><span style="color:#000000">root</span><span style="color:#666600">();</span>
      <span style="color:#660066"><strong>System</strong></span><span style="color:#666600">.</span><span style="color:#000088"><strong>out</strong></span><span style="color:#666600">.</span><span style="color:#000000">println</span><span style="color:#666600">(</span><span style="color:#008800">"Loaded: "</span> <span style="color:#666600">+</span><span style="color:#000000"> list</span><span style="color:#666600">);</span>
    <span style="color:#666600">}</span>
    <span style="color:#660066"><strong>System</strong></span><span style="color:#666600">.</span><span style="color:#000088"><strong>exit</strong></span><span style="color:#666600">(</span><span style="color:#006666">0</span><span style="color:#666600">);</span>
  <span style="color:#666600">}</span>
<span style="color:#666600">}</span></code></span></span>

你会注意到,如果你使用参数运行程序,它会保存字符串列表,如果你在没有参数的情况下运行它,它会从存储中提取它们并显示它们。清单 8 具有这两个选项。

清单 8.保存并加载程序

<span style="background-color:#e5e5e5"><span style="color:#4e4242"><code><span style="color:#000000">
$ mvn clean </span><span style="color:#000088"><strong>package</strong></span> <span style="color:#000088"><strong>exec</strong></span><span style="color:#666600">:</span><span style="color:#000000">java </span><span style="color:#666600">-</span><span style="color:#660066"><strong>Dexec</strong></span><span style="color:#666600">.</span><span style="color:#000000">mainClass</span><span style="color:#666600">=</span><span style="color:#008800">"com.infoworld.App"</span> <span style="color:#666600">-</span><span style="color:#660066"><strong>Dexec</strong></span><span style="color:#666600">.</span><span style="color:#000000">args</span><span style="color:#666600">=</span><span style="color:#008800">"test test2 test3
Saved: [test, test2, test3]
$ mvn clean package exec:java -Dexec.mainClass="</span><span style="color:#000000">com</span><span style="color:#666600">.</span><span style="color:#000000">infoworld</span><span style="color:#666600">.</span><span style="color:#660066"><strong>App</strong></span><span style="color:#008800">"
Loaded: [test, test2, test3]</span></code></span></span>

您可能想知道MicroStream将存储文件放在哪里。缺省情况下,它使用 Java 项目的工作目录。在我们的演示中,它把它放在项目的根目录。如果你看那里,你会看到一个./microstream/microstream/my-storage-folder

如果需要更改存储文件的位置,可以使用参数对其进行配置,如清单 9 所示。在这里,我们在两个启动调用上设置了一个文件位置。请注意,我们导入了标准的 Path Java 库来处理 [what?]。start()

清单 9.设置备用目录

<span style="background-color:#e5e5e5"><span style="color:#4e4242"><code>
<span style="color:#000088"><strong>import</strong></span><span style="color:#000000"> java</span><span style="color:#666600">.</span><span style="color:#000000">nio</span><span style="color:#666600">.</span><span style="color:#000000">file</span><span style="color:#666600">.</span><span style="color:#660066"><strong>Paths</strong></span><span style="color:#666600">;</span>
<span style="color:#880000"><em>//...</em></span>
<span style="color:#660066"><strong>EmbeddedStorageManager</strong></span><span style="color:#000000"> manager </span><span style="color:#666600">=</span> <span style="color:#660066"><strong>EmbeddedStorage</strong></span><span style="color:#666600">.</span><span style="color:#000000">start</span><span style="color:#666600">(</span><span style="color:#660066"><strong>Paths</strong></span><span style="color:#666600">.</span><span style="color:#000088"><strong>get</strong></span><span style="color:#666600">(</span><span style="color:#660066"><strong>System</strong></span><span style="color:#666600">.</span><span style="color:#000000">getProperty</span><span style="color:#666600">(</span><span style="color:#008800">"user.home"</span><span style="color:#666600">),</span> <span style="color:#008800">"data"</span><span style="color:#666600">));</span></code></span></span>

MicroStream with MariaDB

现在,让我们看看如何使用备用存储系统。我们将研究如何使用本地 MariaDB 社区实例。就我而言(在 Ubuntu 上),我请参阅说明)。运行 MariaDB 后,我们可以修改应用程序以使用数据库来保存状态,如清单 10 所示。

清单 10.该应用程序的MariaDB版本

<span style="background-color:#e5e5e5"><span style="color:#4e4242"><code>
<span style="color:#000088"><strong>package</strong></span><span style="color:#000000"> com</span><span style="color:#666600">.</span><span style="color:#000000">infoworld</span><span style="color:#666600">;</span>

<span style="color:#000088"><strong>import</strong></span><span style="color:#000000"> org</span><span style="color:#666600">.</span><span style="color:#000000">mariadb</span><span style="color:#666600">.</span><span style="color:#000000">jdbc</span><span style="color:#666600">.</span><span style="color:#660066"><strong>MariaDbDataSource</strong></span><span style="color:#666600">;</span>
<span style="color:#000088"><strong>import</strong></span><span style="color:#000000"> one</span><span style="color:#666600">.</span><span style="color:#000000">microstream</span><span style="color:#666600">.</span><span style="color:#000000">afs</span><span style="color:#666600">.</span><span style="color:#000000">sql</span><span style="color:#666600">.</span><span style="color:#000000">types</span><span style="color:#666600">.</span><span style="color:#660066"><strong>SqlFileSystem</strong></span><span style="color:#666600">;</span>
<span style="color:#000088"><strong>import</strong></span><span style="color:#000000"> one</span><span style="color:#666600">.</span><span style="color:#000000">microstream</span><span style="color:#666600">.</span><span style="color:#000000">afs</span><span style="color:#666600">.</span><span style="color:#000000">sql</span><span style="color:#666600">.</span><span style="color:#000000">types</span><span style="color:#666600">.</span><span style="color:#660066"><strong>SqlConnector</strong></span><span style="color:#666600">;</span>
<span style="color:#000088"><strong>import</strong></span><span style="color:#000000"> one</span><span style="color:#666600">.</span><span style="color:#000000">microstream</span><span style="color:#666600">.</span><span style="color:#000000">afs</span><span style="color:#666600">.</span><span style="color:#000000">sql</span><span style="color:#666600">.</span><span style="color:#000000">types</span><span style="color:#666600">.</span><span style="color:#660066"><strong>SqlProviderMariaDb</strong></span><span style="color:#666600">;</span>
<span style="color:#000088"><strong>import</strong></span><span style="color:#000000"> one</span><span style="color:#666600">.</span><span style="color:#000000">microstream</span><span style="color:#666600">.</span><span style="color:#000000">storage</span><span style="color:#666600">.</span><span style="color:#000000">embedded</span><span style="color:#666600">.</span><span style="color:#000000">types</span><span style="color:#666600">.</span><span style="color:#660066"><strong>EmbeddedStorage</strong></span><span style="color:#666600">;</span>
<span style="color:#000088"><strong>import</strong></span><span style="color:#000000"> one</span><span style="color:#666600">.</span><span style="color:#000000">microstream</span><span style="color:#666600">.</span><span style="color:#000000">storage</span><span style="color:#666600">.</span><span style="color:#000000">embedded</span><span style="color:#666600">.</span><span style="color:#000000">types</span><span style="color:#666600">.</span><span style="color:#660066"><strong>EmbeddedStorageManager</strong></span><span style="color:#666600">;</span>
<span style="color:#000088"><strong>import</strong></span><span style="color:#000000"> java</span><span style="color:#666600">.</span><span style="color:#000000">util</span><span style="color:#666600">.</span><span style="color:#660066"><strong>ArrayList</strong></span><span style="color:#666600">;</span>

<span style="color:#000088"><strong>public</strong></span> <span style="color:#000088"><strong>class</strong></span> <span style="color:#660066"><strong>App</strong></span> <span style="color:#666600">{</span>
  <span style="color:#000088"><strong>public</strong></span> <span style="color:#000088"><strong>static</strong></span> <span style="color:#000088"><strong>void</strong></span><span style="color:#000000"> main</span><span style="color:#666600">(</span><span style="color:#660066"><strong>String</strong></span><span style="color:#666600">[]</span><span style="color:#000000"> args</span><span style="color:#666600">)</span> <span style="color:#666600">{</span>
    <span style="color:#000088"><strong>try</strong></span> <span style="color:#666600">{</span>
        <span style="color:#660066"><strong>MariaDbDataSource</strong></span><span style="color:#000000"> dataSource </span><span style="color:#666600">=</span> <span style="color:#000088"><strong>new</strong></span> <span style="color:#660066"><strong>MariaDbDataSource</strong></span><span style="color:#666600">();</span><span style="color:#000000">
        dataSource</span><span style="color:#666600">.</span><span style="color:#000000">setUrl</span><span style="color:#666600">(</span><span style="color:#008800">"jdbc:mysql://localhost:3306/mydb"</span><span style="color:#666600">);</span><span style="color:#000000">
        dataSource</span><span style="color:#666600">.</span><span style="color:#000000">setUser</span><span style="color:#666600">(</span><span style="color:#008800">"admin"</span><span style="color:#666600">);</span><span style="color:#000000">
        dataSource</span><span style="color:#666600">.</span><span style="color:#000000">setPassword</span><span style="color:#666600">(</span><span style="color:#008800">"password"</span><span style="color:#666600">);</span>
        <span style="color:#660066"><strong>SqlFileSystem</strong></span><span style="color:#000000"> fileSystem </span><span style="color:#666600">=</span> <span style="color:#660066"><strong>SqlFileSystem</strong></span><span style="color:#666600">.</span><span style="color:#660066"><strong>New</strong></span><span style="color:#666600">(</span><span style="color:#660066"><strong>SqlConnector</strong></span><span style="color:#666600">.</span><span style="color:#660066"><strong>Caching</strong></span><span style="color:#666600">(</span><span style="color:#660066"><strong>SqlProviderMariaDb</strong></span><span style="color:#666600">.</span><span style="color:#660066"><strong>New</strong></span><span style="color:#666600">(</span><span style="color:#000000">dataSource</span><span style="color:#666600">)));</span>

        <span style="color:#660066"><strong>EmbeddedStorageManager</strong></span><span style="color:#000000"> manager </span><span style="color:#666600">=</span> <span style="color:#660066"><strong>EmbeddedStorage</strong></span><span style="color:#666600">.</span><span style="color:#000000">start</span><span style="color:#666600">(</span><span style="color:#000000">fileSystem</span><span style="color:#666600">.</span><span style="color:#000000">ensureDirectoryPath</span><span style="color:#666600">(</span><span style="color:#008800">"microstream_storage"</span><span style="color:#666600">));</span>

        <span style="color:#660066"><strong>ArrayList</strong></span><span style="color:#666600"><</span><span style="color:#660066"><strong>String</strong></span><span style="color:#666600">></span><span style="color:#000000"> list</span><span style="color:#666600">;</span>
        <span style="color:#000088"><strong>if</strong></span> <span style="color:#666600">(</span><span style="color:#000000">args</span><span style="color:#666600">.</span><span style="color:#000000">length </span><span style="color:#666600">></span> <span style="color:#006666">0</span><span style="color:#666600">)</span> <span style="color:#666600">{</span><span style="color:#000000">
          list </span><span style="color:#666600">=</span> <span style="color:#000088"><strong>new</strong></span> <span style="color:#660066"><strong>ArrayList</strong></span><span style="color:#666600"><</span><span style="color:#660066"><strong>String</strong></span><span style="color:#666600">>();</span>
          <span style="color:#000088"><strong>for</strong></span> <span style="color:#666600">(</span><span style="color:#000088"><strong>int</strong></span><span style="color:#000000"> i </span><span style="color:#666600">=</span> <span style="color:#006666">0</span><span style="color:#666600">;</span><span style="color:#000000"> i </span><span style="color:#666600"><</span><span style="color:#000000"> args</span><span style="color:#666600">.</span><span style="color:#000000">length</span><span style="color:#666600">;</span><span style="color:#000000"> i</span><span style="color:#666600">++)</span> <span style="color:#666600">{</span><span style="color:#000000">
            list</span><span style="color:#666600">.</span><span style="color:#000000">add</span><span style="color:#666600">(</span><span style="color:#000000">args</span><span style="color:#666600">[</span><span style="color:#000000">i</span><span style="color:#666600">]);</span>
          <span style="color:#666600">}</span><span style="color:#000000">
          manager</span><span style="color:#666600">.</span><span style="color:#000000">setRoot</span><span style="color:#666600">(</span><span style="color:#000000">list</span><span style="color:#666600">);</span><span style="color:#000000">
          manager</span><span style="color:#666600">.</span><span style="color:#000000">storeRoot</span><span style="color:#666600">();</span>
          <span style="color:#660066"><strong>System</strong></span><span style="color:#666600">.</span><span style="color:#000088"><strong>out</strong></span><span style="color:#666600">.</span><span style="color:#000000">println</span><span style="color:#666600">(</span><span style="color:#008800">"Saved: "</span> <span style="color:#666600">+</span><span style="color:#000000"> list</span><span style="color:#666600">);</span>
        <span style="color:#666600">}</span> <span style="color:#000088"><strong>else</strong></span> <span style="color:#666600">{</span><span style="color:#000000">
          list </span><span style="color:#666600">=</span> <span style="color:#666600">(</span><span style="color:#660066"><strong>ArrayList</strong></span><span style="color:#666600"><</span><span style="color:#660066"><strong>String</strong></span><span style="color:#666600">>)</span><span style="color:#000000"> manager</span><span style="color:#666600">.</span><span style="color:#000000">root</span><span style="color:#666600">();</span>
          <span style="color:#660066"><strong>System</strong></span><span style="color:#666600">.</span><span style="color:#000088"><strong>out</strong></span><span style="color:#666600">.</span><span style="color:#000000">println</span><span style="color:#666600">(</span><span style="color:#008800">"Loaded: "</span> <span style="color:#666600">+</span><span style="color:#000000"> list</span><span style="color:#666600">);</span>
        <span style="color:#666600">}</span>
    <span style="color:#666600">}</span> <span style="color:#000088"><strong>catch</strong></span> <span style="color:#666600">(</span><span style="color:#660066"><strong>Exception</strong></span><span style="color:#000000"> e</span><span style="color:#666600">){</span>
      <span style="color:#660066"><strong>System</strong></span><span style="color:#666600">.</span><span style="color:#000000">err</span><span style="color:#666600">.</span><span style="color:#000000">println</span><span style="color:#666600">(</span><span style="color:#008800">"error: "</span><span style="color:#666600">+</span><span style="color:#000000"> e</span><span style="color:#666600">);</span>
      <span style="color:#000088"><strong>throw</strong></span> <span style="color:#000088"><strong>new</strong></span> <span style="color:#660066"><strong>RuntimeException</strong></span><span style="color:#666600">(</span><span style="color:#000000">e</span><span style="color:#666600">);</span>
    <span style="color:#666600">}</span>
  <span style="color:#666600">}</span>
<span style="color:#666600">}</span></code></span></span>

清单 10 提供了使用 MariaDB 本地实例而不是默认文件系统的应用程序的完整版本。此版本假定 MariaDB 正在侦听本地主机端口 3306。管理员用户密码为“密码”。

请注意调用的行;它传递了参数.其效果是将microstream_storage表添加到 mydb 数据库中(如果它还没有)。EmbeddedStorage.start()fileSystem.ensureDirectoryPath("microstream_storage")

顺便说一下,这里的错误处理只是一个令牌。在实际应用中,我们需要更加认真地处理错误。

软件包中的“afs”代表抽象文件系统。目的是让应用程序使用 MicroStream 来保存对象图,而不必过多担心它的实现方式。因此,像 AWS S3 这样的数据库、文件系统或云对象存储都可以像使用 MariaDB 一样使用,除了存储的配置方式。NoSQL和MongoDB一样,也受支持,尽管它是一个企业功能。one.microstream.afs.sql

要运行此示例,我们需要将 MariaDB 驱动程序和 JAR 文件添加到 l 依赖项中:one.microstream.afspom.xm

清单 11.添加 MariaDB 依赖项

<span style="background-color:#e5e5e5"><span style="color:#4e4242"><code>
<span style="color:#000088"><strong><dependency></strong></span>
      <span style="color:#000088"><strong><groupId></strong></span><span style="color:#000000">org.mariadb.jdbc</span><span style="color:#000088"><strong></groupId></strong></span>
      <span style="color:#000088"><strong><artifactId></strong></span><span style="color:#000000">mariadb-java-client</span><span style="color:#000088"><strong></artifactId></strong></span>
      <span style="color:#000088"><strong><version></strong></span><span style="color:#000000">2.7.1</span><span style="color:#000088"><strong></version></strong></span>
    <span style="color:#000088"><strong></dependency></strong></span>
 <span style="color:#000088"><strong><dependency></strong></span>
        <span style="color:#000088"><strong><groupId></strong></span><span style="color:#000000">one.microstream</span><span style="color:#000088"><strong></groupId></strong></span>
        <span style="color:#000088"><strong><artifactId></strong></span><span style="color:#000000">microstream-afs-sql</span><span style="color:#000088"><strong></artifactId></strong></span>
        <span style="color:#000088"><strong><version></strong></span><span style="color:#000000">07.01.00-MS-GA</span><span style="color:#000088"><strong></version></strong></span>
<span style="color:#000088"><strong></dependency></strong></span></code></span></span>

现在,可以使用清单 8 中所示的相同命令运行应用程序,并将状态持久化到 MariaDB 的本地实例中。

结论

MicroStream以不同于大多数Java开发人员习惯的架构方式接近Java持久性。它消除了安装和维护单独的独立组件来处理数据存储的要求。但是,您已经看到,如果您愿意,您仍然可以使用 RDBMS。

MicroStream类似于ORM(如Hibernate),但更加无缝。它开辟了在对象的心理领域内思考数据持久性的可能性。您仍然需要考虑保存或加载的内容和时间,但您不必搁置业务模型并进入数据层作为不同的阶段。

MicroStream对Java持久性采取了一种新的方法,这可能是Java正在进行的复兴的一个关键特征。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值