首页 > 编程语言 >动手实践 MicroStream:Java 持久性的新方法

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

时间:2023-06-03 12:06:07浏览次数:38  
标签:args Java microstream MicroStream list 持久性 清单 import


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


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











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


2024 年 IT 最佳工作场所提名开放


清单 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正在进行的复兴的一个关键特征。

标签:args,Java,microstream,MicroStream,list,持久性,清单,import
From: https://blog.51cto.com/10zhancom/6407666

相关文章

  • Java开发 - 让你少走弯路的Redis主从实现单节点哨兵模式
    前言前一篇中,我们讲解了Redis主从的搭建方式,其实很简单呐有木有,都是配置,连句代码都没有,是不是感觉高估了Redis主从的搭建方式?哈哈,没关系,跟着博主,包你全会。今天我们的主题是哨兵,没错,就是哨兵!有了Redis,要是没有哨兵,那真是太可惜了,哨兵是很神圣的一种重要的监测工具,有了哨兵,在Redis主......
  • javaweb课程设计——商城项目
    目录本项目前端成员csdn地址:一、项目截图二、前端项目介绍最后源码地址本项目前端成员csdn地址:【后端】【前端】一、项目截图二、前端项目介绍ChangeAtWill:前台项目ChangeAtWill-admin:后台项目前端需要环境nodejsv16版本下载链接https://nodejs.org/download/release/v16......
  • Java内存模型
    一、Java内存模型简介1.Java内存模型的“底层原理”从Java代码到CPU指令的变化过程是怎样的?最开始,我们编写的Java代码,即*.Java文件在执行编译Javac命令后,从刚才的*.Java文件会变出一个新的Java字节码文件,即*.class文件JVM会执行刚才生成的*.class字节码文件,并把字节码文......
  • 多线程-线程池与java内存模型
    多线程-线程池与java内存模型线程池的使用(思路:什么是线程池->他的基本构造以及参数含义->如何使用,使用过程中需要注意什么->有哪些好用的工具类)线程池的基笨概念:首先看一下的继承关系,其次看他的状态,它是利用int的高三位表示状态,比如111表示能接受任务,具体看下面第二章图接下来看......
  • Java中的爬虫
    爬虫pattern:表示正则表达式Matcher:文本匹配器,作用按照正则表达式的规则去读取字符串,从头开始读取步骤:获取正则表达式的对象Patternp=Pattern.compile("正则表达式");获取文本匹配器的对象Stringstr="文本";Matcherm=p.matcher(str);p:规则,str:大串,m;文本匹......
  • Linux安装java(jdk8)
    1.查看Linux系统是否有自带的jdk:1、输入:java-version如果有输出版本信息,则需要卸载原本的jdk,如果没有说明linux系统没有安装jdk可以跳过下面步骤直接进行下一步的安装。2、发现有版本信息输入:rpm-qa|grepjava检测jdk的安装包,(注意:rpm符没有时记得下载一个输入:apt-getins......
  • 如何使用JavaScript格式化日期 – JS中的日期格式化
    日期是许多JavaScript应用程序的基本组成部分,无论是在网页上显示当前日期还是处理用户输入以安排事件。但以清晰一致的格式显示日期对于积极的用户体验至关重要。在本文中,我们将探讨在JavaScript中格式化日期的各种技术,使您能够以您的应用程序所需的格式显示日期。(前端教程......
  • Java官方笔记6继承
    继承Java只有单继承,最顶级的父类是Object。子类会继承父类的fields和methods,而不会继承constructors,因为constructors不属于methods,但是子类可以通过super调用父类的constructor。子类继承父类的范围是:public、protected、package-private隐式转换,子类转父类(只有1个爸爸):Objec......
  • 基于JAVA的springboot篮球论坛系统,附源码+数据库+论文+PPT
    1、项目介绍考虑到实际生活中在篮球论坛方面的需要以及对该系统认真的分析,将系统权限按管理员和用户这两类涉及用户划分。(a)管理员;管理员使用本系统涉到的功能主要有:首页、个人中心、用户管理、篮球论坛、系统管理等功能。管理员用例图如图3-1所示。(b)用户;用户使用本系统......
  • Java Native Interface Programming
    http://journals.ecs.soton.ac.uk/java/tutorial/native1.1/implementing/index.html JavaNativeInterfaceProgramming TheJDK1.1supportstheJavaNativeInterface(JNI).Ononehand,theJNIdefinesastandardnamingandcallingconventionsothattheJa......