Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ The following examples are included:
* `SqlJdbcExample` - demonstrates the usage of the Apache Ignite JDBC driver.
* `RebalanceExample` - demonstrates the data rebalancing process.
* `VolatilePageMemoryStorageExample` - demonstrates the usage of the PageMemory storage engine configured with an in-memory data region.
* `PersistentPageMemoryStorageExample` - demonstrates the usage of the PageMemory storage engine configured with a persistent data region.

Before running the examples, read about [cli](https://ignite.apache.org/docs/3.0.0-alpha/ignite-cli-tool).

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,30 @@

import java.util.concurrent.TimeUnit;
import org.apache.ignite.example.AbstractExamplesTest;
import org.apache.ignite.internal.storage.pagememory.PageMemoryStorageEngine;
import org.apache.ignite.internal.storage.pagememory.configuration.schema.PageMemoryStorageEngineConfiguration;
import org.junit.jupiter.api.Test;

/**
* For {@link VolatilePageMemoryStorageExample} testing.
* For testing examples demonstrating work with {@link PageMemoryStorageEngine}.
*/
public class ItVolatilePageMemoryStorageExampleTest extends AbstractExamplesTest {
public class ItPageMemoryStorageExampleTest extends AbstractExamplesTest {
@Test
public void testExample() throws Exception {
ignite
.clusterConfiguration()
.getConfiguration(PageMemoryStorageEngineConfiguration.KEY)
.regions()
.change(regionsChange -> regionsChange.create("in-memory", regionChange -> regionChange.changePersistent(false)))
.get(1, TimeUnit.SECONDS);
public void testPersistentExample() throws Exception {
addDataRegionConfig("persistent", true);

assertConsoleOutputContains(PersistentPageMemoryStorageExample::main, EMPTY_ARGS,
"\nAll accounts:\n"
+ " 1, John, Doe, 1000.0\n"
+ " 2, Jane, Roe, 2000.0\n"
+ " 3, Mary, Major, 1500.0\n"
+ " 4, Richard, Miles, 1450.0\n"
);
}

@Test
public void testInMemoryExample() throws Exception {
addDataRegionConfig("in-memory", false);

assertConsoleOutputContains(VolatilePageMemoryStorageExample::main, EMPTY_ARGS,
"\nAll accounts:\n"
Expand All @@ -45,4 +54,11 @@ public void testExample() throws Exception {
+ " 4, Richard, Miles, 1450.0\n"
);
}

private void addDataRegionConfig(String name, boolean persistent) throws Exception {
ignite.clusterConfiguration().getConfiguration(PageMemoryStorageEngineConfiguration.KEY)
.regions()
.change(regionsChange -> regionsChange.create(name, regionChange -> regionChange.changePersistent(persistent)))
.get(1, TimeUnit.SECONDS);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
* {@code ignite init}
* </li>
* <li>
* Start a server node using the CLI tool:<br>
* Start an Ignite node using the CLI tool:<br>
* {@code ignite node start --config=$IGNITE_HOME/examples/config/ignite-config.json my-first-node}
* </li>
* <li>
Expand All @@ -43,7 +43,7 @@
* </li>
* <li>Run the example in the IDE.</li>
* <li>
* Stop a server node using the CLI tool:<br>
* Stop the Ignite node using the CLI tool:<br>
* {@code ignite node stop my-first-node}
* </li>
* </ol>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.ignite.example.storage;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

/**
* This example demonstrates a usage of the PageMemory storage engine configured with a persistent data region.
*
* <p>To run the example, do the following:
* <ol>
* <li>Import the examples project into you IDE.</li>
* <li>
* Download and prepare artifacts for running an Ignite node using the CLI tool (if not done yet):<br>
* {@code ignite init}
* </li>
* <li>
* Start an Ignite node using the CLI tool:<br>
* {@code ignite node start --config=$IGNITE_HOME/examples/config/ignite-config.json my-first-node}
* </li>
* <li>
* Cluster initialization using the CLI tool (if not done yet):<br>
* {@code ignite cluster init --cluster-name=ignite-cluster --node-endpoint=localhost:10300 --meta-storage-node=my-first-node}
* </li>
* <li>
* Add configuration for persistent data region of of the PageMemory storage engine using the CLI tool (if not done yet):<br>
* {@code ignite config set --type=cluster "pageMemory.regions.persistent:{persistent=true}"}
* </li>
* <li>Run the example in the IDE.</li>
* <li>
* Stop the Ignite node using the CLI tool:<br>
* {@code ignite node stop my-first-node}
* </li>
* </ol>
*/
public class PersistentPageMemoryStorageExample {
/**
* Main method of the example.
*
* @param args The command line arguments.
* @throws Exception If failed.
*/
public static void main(String[] args) throws Exception {
//--------------------------------------------------------------------------------------
//
// Creating a JDBC connection to connect to the cluster.
//
//--------------------------------------------------------------------------------------

System.out.println("\nConnecting to server...");

try (Connection conn = DriverManager.getConnection("jdbc:ignite:thin://127.0.0.1:10800/")) {
//--------------------------------------------------------------------------------------
//
// Creating table.
//
//--------------------------------------------------------------------------------------

try (Statement stmt = conn.createStatement()) {
stmt.executeUpdate(
"CREATE TABLE ACCOUNTS ( "
+ "ACCOUNT_ID INT PRIMARY KEY,"
+ "FIRST_NAME VARCHAR, "
+ "LAST_NAME VARCHAR, "
+ "BALANCE DOUBLE) "
+ "ENGINE pagememory "
+ "WITH dataRegion='persistent'"
);
}

//--------------------------------------------------------------------------------------
//
// Populating 'ACCOUNTS' table.
//
//--------------------------------------------------------------------------------------

System.out.println("\nPopulating 'ACCOUNTS' table...");

try (PreparedStatement stmt = conn.prepareStatement(
"INSERT INTO ACCOUNTS (ACCOUNT_ID, FIRST_NAME, LAST_NAME, BALANCE) values (?, ?, ?, ?)"
)) {
stmt.setInt(1, 1);
stmt.setString(2, "John");
stmt.setString(3, "Doe");
stmt.setDouble(4, 1000.0d);
stmt.executeUpdate();

stmt.setInt(1, 2);
stmt.setString(2, "Jane");
stmt.setString(3, "Roe");
stmt.setDouble(4, 2000.0d);
stmt.executeUpdate();

stmt.setInt(1, 3);
stmt.setString(2, "Mary");
stmt.setString(3, "Major");
stmt.setDouble(4, 1500.0d);
stmt.executeUpdate();

stmt.setInt(1, 4);
stmt.setString(2, "Richard");
stmt.setString(3, "Miles");
stmt.setDouble(4, 1450.0d);
stmt.executeUpdate();
}

//--------------------------------------------------------------------------------------
//
// Requesting information about all account owners.
//
//--------------------------------------------------------------------------------------

System.out.println("\nAll accounts:");

try (Statement stmt = conn.createStatement()) {
try (ResultSet rs = stmt.executeQuery(
"SELECT ACCOUNT_ID, FIRST_NAME, LAST_NAME, BALANCE FROM ACCOUNTS ORDER BY ACCOUNT_ID"
)) {
while (rs.next()) {
System.out.println(" "
+ rs.getString(1) + ", "
+ rs.getString(2) + ", "
+ rs.getString(3) + ", "
+ rs.getString(4));
}
}
}
} finally {
System.out.println("\nDropping the table...");

try (
Connection conn = DriverManager.getConnection("jdbc:ignite:thin://127.0.0.1:10800/");
Statement stmt = conn.createStatement()
) {
stmt.executeUpdate("DROP TABLE ACCOUNTS");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
* {@code ignite init}
* </li>
* <li>
* Start a server node using the CLI tool:<br>
* Start an Ignite node using the CLI tool:<br>
* {@code ignite node start --config=$IGNITE_HOME/examples/config/ignite-config.json my-first-node}
* </li>
* <li>
Expand All @@ -47,7 +47,7 @@
* </li>
* <li>Run the example in the IDE.</li>
* <li>
* Stop a server node using the CLI tool:<br>
* Stop the Ignite node using the CLI tool:<br>
* {@code ignite node stop my-first-node}
* </li>
* </ol>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
* {@code ignite init}
* </li>
* <li>
* Start a server node using the CLI tool:<br>
* Start an Ignite node using the CLI tool:<br>
* {@code ignite node start --config=$IGNITE_HOME/examples/config/ignite-config.json my-first-node}
* </li>
* <li>
Expand All @@ -44,7 +44,7 @@
* </li>
* <li>Run the example in the IDE.</li>
* <li>
* Stop a server node using the CLI tool:<br>
* Stop the Ignite node using the CLI tool:<br>
* {@code ignite node stop my-first-node}
* </li>
* </ol>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
* {@code ignite init}
* </li>
* <li>
* Start a server node using the CLI tool:<br>
* Start an Ignite node using the CLI tool:<br>
* {@code ignite node start --config=$IGNITE_HOME/examples/config/ignite-config.json my-first-node}
* </li>
* <li>
Expand All @@ -43,7 +43,7 @@
* </li>
* <li>Run the example in the IDE.</li>
* <li>
* Stop a server node using the CLI tool:<br>
* Stop the Ignite node using the CLI tool:<br>
* {@code ignite node stop my-first-node}
* </li>
* </ol>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
* {@code ignite init}
* </li>
* <li>
* Start a server node using the CLI tool:<br>
* Start an Ignite node using the CLI tool:<br>
* {@code ignite node start --config=$IGNITE_HOME/examples/config/ignite-config.json my-first-node}
* </li>
* <li>
Expand All @@ -44,7 +44,7 @@
* </li>
* <li>Run the example in the IDE.</li>
* <li>
* Stop a server node using the CLI tool:<br>
* Stop the Ignite node using the CLI tool:<br>
* {@code ignite node stop my-first-node}
* </li>
* </ol>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
* {@code ignite init}
* </li>
* <li>
* Start a server node using the CLI tool:<br>
* Start an Ignite node using the CLI tool:<br>
* {@code ignite node start --config=$IGNITE_HOME/examples/config/ignite-config.json my-first-node}
* </li>
* <li>
Expand All @@ -43,7 +43,7 @@
* </li>
* <li>Run the example in the IDE.</li>
* <li>
* Stop a server node using the CLI tool:<br>
* Stop the Ignite node using the CLI tool:<br>
* {@code ignite node stop my-first-node}
* </li>
* </ol>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
* {@code ignite init}
* </li>
* <li>
* Start a server node using the CLI tool:<br>
* Start an Ignite node using the CLI tool:<br>
* {@code ignite node start --config=$IGNITE_HOME/examples/config/ignite-config.json my-first-node}
* </li>
* <li>
Expand All @@ -45,7 +45,7 @@
* </li>
* <li>Run the example in the IDE.</li>
* <li>
* Stop a server node using the CLI tool:<br>
* Stop the Ignite node using the CLI tool:<br>
* {@code ignite node stop my-first-node}
* </li>
* </ol>
Expand Down