Skip to content
Permalink
Browse files
IGNITE-16244 - Updated examples based on latest changes (#588)
  • Loading branch information
vkulichenko committed Jan 25, 2022
1 parent 7746d49 commit 8db3c700fe40a925c26030dcb5da251ea045f0f2
Show file tree
Hide file tree
Showing 9 changed files with 967 additions and 475 deletions.
@@ -19,14 +19,13 @@

import java.nio.file.Files;
import java.nio.file.Path;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.Set;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgnitionManager;
import org.apache.ignite.client.IgniteClient;
import org.apache.ignite.internal.schema.configuration.SchemaConfigurationConverter;
import org.apache.ignite.schema.SchemaBuilders;
import org.apache.ignite.schema.definition.ColumnType;
import org.apache.ignite.schema.definition.TableDefinition;
import org.apache.ignite.table.KeyValueView;
import org.apache.ignite.table.Tuple;

@@ -64,132 +63,113 @@ public class RebalanceExample {
public static void main(String[] args) throws Exception {
//--------------------------------------------------------------------------------------
//
// Starting a server node.
// Creating 'accounts' table.
//
// NOTE: An embedded server node is only needed to invoke the 'createTable' API.
// In the future releases, this API will be available on the client,
// eliminating the need to start an embedded server node in this example.
//--------------------------------------------------------------------------------------

try (
Connection conn = DriverManager.getConnection("jdbc:ignite:thin://127.0.0.1:10800/");
Statement stmt = conn.createStatement()
) {
stmt.executeUpdate(
"CREATE TABLE rebalance ("
+ "key INT PRIMARY KEY,"
+ "value VARCHAR)"
);
}

//--------------------------------------------------------------------------------------
//
// Creating a client to connect to the cluster.
//
//--------------------------------------------------------------------------------------

System.out.println("Starting a server node... Logging to file: example-node.log");
System.out.println("\nConnecting to server...");

System.setProperty("java.util.logging.config.file", "config/java.util.logging.properties");
try (IgniteClient client = IgniteClient.builder()
.addresses("127.0.0.1:10800")
.build()
) {
KeyValueView<Tuple, Tuple> kvView = client.tables().table("PUBLIC.rebalance").keyValueView();

try (Ignite server = IgnitionManager.start(
"example-node",
Files.readString(Path.of("config", "ignite-config.json")),
Path.of("work")
)) {
//--------------------------------------------------------------------------------------
//
// Creating a table. The API call below is the equivalent of the following DDL:
//
// CREATE TABLE rebalance (
// key INT PRIMARY KEY,
// value VARCHAR
// )
// Inserting several key-value pairs into the table.
//
//--------------------------------------------------------------------------------------

System.out.println("\nCreating a table...");
System.out.println("\nInserting key-value pairs...");

TableDefinition tableDef = SchemaBuilders.tableBuilder("PUBLIC", "rebalance")
.columns(
SchemaBuilders.column("key", ColumnType.INT32).build(),
SchemaBuilders.column("value", ColumnType.string()).asNullable(true).build()
)
.withPrimaryKey("key")
.build();
for (int i = 0; i < 10; i++) {
Tuple key = Tuple.create().set("key", i);
Tuple value = Tuple.create().set("value", "test_" + i);

server.tables().createTable(tableDef.canonicalName(), tableChange ->
SchemaConfigurationConverter.convert(tableDef, tableChange)
.changeReplicas(5)
.changePartitions(1)
);
kvView.put(null, key, value);
}

//--------------------------------------------------------------------------------------
//
// Creating a client to connect to the cluster.
// Retrieving the newly inserted data.
//
//--------------------------------------------------------------------------------------

System.out.println("\nConnecting to server...");
System.out.println("\nRetrieved key-value pairs:");

try (IgniteClient client = IgniteClient.builder()
.addresses("127.0.0.1:10800")
.build()
) {
KeyValueView<Tuple, Tuple> kvView = client.tables().table("PUBLIC.rebalance").keyValueView();
for (int i = 0; i < 10; i++) {
Tuple key = Tuple.create().set("key", i);
Tuple value = kvView.get(null, key);

//--------------------------------------------------------------------------------------
//
// Inserting several key-value pairs into the table.
//
//--------------------------------------------------------------------------------------

System.out.println("\nInserting key-value pairs...");

for (int i = 0; i < 10; i++) {
Tuple key = Tuple.create().set("key", i);
Tuple value = Tuple.create().set("value", "test_" + i);

kvView.put(null, key, value);
}

//--------------------------------------------------------------------------------------
//
// Retrieving the newly inserted data.
//
//--------------------------------------------------------------------------------------

System.out.println("\nRetrieved key-value pairs:");
System.out.println(" " + i + " -> " + value.stringValue("value"));
}

for (int i = 0; i < 10; i++) {
Tuple key = Tuple.create().set("key", i);
Tuple value = kvView.get(null, key);
//--------------------------------------------------------------------------------------
//
// Scaling out by adding two more nodes into the topology.
//
//--------------------------------------------------------------------------------------

System.out.println(" " + i + " -> " + value.stringValue("value"));
}
System.out.println("\n"
+ "Run the following commands using the CLI tool to start two more nodes, and then press 'Enter' to continue...\n"
+ " ignite node start --config=examples/config/ignite-config.json my-first-additional-node\n"
+ " ignite node start --config=examples/config/ignite-config.json my-second-additional-node");

//--------------------------------------------------------------------------------------
//
// Scaling out by adding two more nodes into the topology.
//
//--------------------------------------------------------------------------------------
System.in.read();

System.out.println("\n"
+ "Run the following commands using the CLI tool to start two more nodes, and then press 'Enter' to continue...\n"
+ " ignite node start --config=examples/config/ignite-config.json my-first-additional-node\n"
+ " ignite node start --config=examples/config/ignite-config.json my-second-additional-node");
//--------------------------------------------------------------------------------------
//
// Updating baseline to initiate the data rebalancing process.
//
// New topology includes the following five nodes:
// 1. 'my-first-node' -- the first node started prior to running the example
// 2. 'my-second-node' -- the second node started prior to running the example
// 3. 'additional-node-1' -- the first node added to the topology
// 4. 'additional-node-2' -- the second node added to the topology
// 5. 'example-node' -- node that is embedded into the example
//
// NOTE: An embedded server node is started here for the sole purpose of setting
// the baseline. In the future releases, this API will be provided by the
// clients as well. In addition, the process will be automated where applicable
// to eliminate the need for this manual step.
//
//--------------------------------------------------------------------------------------

System.in.read();
System.out.println("Starting a server node... Logging to file: example-node.log");

//--------------------------------------------------------------------------------------
//
// Updating baseline to initiate the data rebalancing process.
//
// New topology includes the following five nodes:
// 1. 'my-first-node' -- the first node started prior to running the example
// 2. 'my-second-node' -- the second node started prior to running the example
// 3. 'example-node' -- node that is embedded into the example
// 4. 'additional-node-1' -- the first node added to the topology
// 5. 'additional-node-2' -- the second node added to the topology
//
// NOTE: In the future releases, this API will be provided by the clients as well.
// In addition, the process will be automated where applicable to eliminate
// the need for this manual step.
//
//--------------------------------------------------------------------------------------
System.setProperty("java.util.logging.config.file", "config/java.util.logging.properties");

try (Ignite server = IgnitionManager.start(
"example-node",
Files.readString(Path.of("config", "ignite-config.json")),
Path.of("work")
)) {
System.out.println("\nUpdating the baseline and rebalancing the data...");

server.setBaseline(Set.of(
"my-first-node",
"my-second-node",
"example-node",
"my-first-additional-node",
"my-second-additional-node"
"my-second-additional-node",
"example-node"
));

//--------------------------------------------------------------------------------------
@@ -207,10 +187,15 @@ public static void main(String[] args) throws Exception {
System.out.println(" " + i + " -> " + value.stringValue("value"));
}
}
}

System.out.println("\nDropping the table and stopping the server...");
System.out.println("\nDropping the table...");

server.tables().dropTable(tableDef.canonicalName());
try (
Connection conn = DriverManager.getConnection("jdbc:ignite:thin://127.0.0.1:10800/");
Statement stmt = conn.createStatement()
) {
stmt.executeUpdate("DROP TABLE rebalance");
}
}
}

0 comments on commit 8db3c70

Please sign in to comment.