Skip to content
Permalink
Browse files
IGNITE-14996 - Updated examples structure
  • Loading branch information
vkulichenko committed Jun 25, 2021
1 parent adfc9d4 commit 9230f61151786011f95dc9106d8b9044c0d41af2
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 59 deletions.
@@ -64,6 +64,7 @@
<directory>examples</directory>
<outputDirectory>/examples</outputDirectory>
<includes>
<include>config/**</include>
<include>src/**</include>
</includes>
</fileSet>
@@ -0,0 +1,21 @@
# Apache Ignite 3 Examples

This project contains code examples for Apache Ignite 3.

Examples are shipped as a separate Maven project, so to start running you simply need
to import provided `pom.xml` file into your favourite IDE.

The following examples are included:
* `TableExample` - demonstrates the usage of the `org.apache.ignite.table.Table` API
* `KeyValueBinaryViewExample` - demonstrates the usage of the `org.apache.ignite.table.KeyValueBinaryView` API

To run an example, do the following:
1. Import the examples project into you IDE.
2. (optional) Run one or more standalone nodes using the CLI tool:
```
ignite node start --config=$IGNITE_HOME/examples/config/ignite-config.json node-1
ignite node start --config=$IGNITE_HOME/examples/config/ignite-config.json node-2
...
ignite node start --config=$IGNITE_HOME/examples/config/ignite-config.json node-n
```
3. Run the preferred example in the IDE.
@@ -1,7 +1,7 @@
{
"node": {
"metastorageNodes": [
"node0"
"node-0", "node-1", "node-2"
]
},
"network": {
@@ -25,11 +25,26 @@
import org.apache.ignite.table.Table;
import org.apache.ignite.table.Tuple;

/**
* This example demonstrates the usage of the {@link KeyValueBinaryView} API.
* <p>
* To run the example, do the following:
* <ol>
* <li>Import the examples project into you IDE.</li>
* <li>
* (optional) Run one or more standalone nodes using the CLI tool:<br>
* {@code ignite node start --config=$IGNITE_HOME/examples/config/ignite-config.json node-1}<br>
* {@code ignite node start --config=$IGNITE_HOME/examples/config/ignite-config.json node-2}<br>
* {@code ...}<br>
* {@code ignite node start --config=$IGNITE_HOME/examples/config/ignite-config.json node-n}<br>
* </li>
* <li>Run the example in the IDE.</li>
* </ol>
*/
public class KeyValueBinaryViewExample {
public static void main(String[] args) throws Exception {
String config = Files.readString(Path.of(ClassLoader.getSystemResource("ignite-config.json").toURI()));

Ignite ignite = IgnitionManager.start("node0", config);
Ignite ignite = IgnitionManager.start("node-0",
Files.readString(Path.of("config/ignite-config.json")));

//---------------------------------------------------------------------------------
//
@@ -24,72 +24,86 @@
import org.apache.ignite.table.Table;
import org.apache.ignite.table.Tuple;

/**
* This example demonstrates the usage of the {@link Table} API.
* <p>
* To run the example, do the following:
* <ol>
* <li>Import the examples project into you IDE.</li>
* <li>
* (optional) Run one or more standalone nodes using the CLI tool:<br>
* {@code ignite node start --config=$IGNITE_HOME/examples/config/ignite-config.json node-1}<br>
* {@code ignite node start --config=$IGNITE_HOME/examples/config/ignite-config.json node-2}<br>
* {@code ...}<br>
* {@code ignite node start --config=$IGNITE_HOME/examples/config/ignite-config.json node-n}<br>
* </li>
* <li>Run the example in the IDE.</li>
* </ol>
*/
public class TableExample {
public static void main(String[] args) throws Exception {
String config = Files.readString(Path.of(ClassLoader.getSystemResource("ignite-config.json").toURI()));

try (Ignite ignite = IgnitionManager.start("node0", config)) {
Ignite ignite = IgnitionManager.start("node-0",
Files.readString(Path.of("config/ignite-config.json")));

//---------------------------------------------------------------------------------
//
// Creating a table. The API call below is the equivalent of the following DDL:
//
// CREATE TABLE accounts (
// accountNumber INT PRIMARY KEY,
// firstName VARCHAR,
// lastName VARCHAR,
// balance DOUBLE
// )
//
//---------------------------------------------------------------------------------
//---------------------------------------------------------------------------------
//
// Creating a table. The API call below is the equivalent of the following DDL:
//
// CREATE TABLE accounts (
// accountNumber INT PRIMARY KEY,
// firstName VARCHAR,
// lastName VARCHAR,
// balance DOUBLE
// )
//
//---------------------------------------------------------------------------------

Table accounts = ignite.tables().createTable("PUBLIC.accounts", tbl -> tbl
.changeName("PUBLIC.accounts")
.changeColumns(cols -> cols
.create("0", c -> c.changeName("accountNumber").changeType(t -> t.changeType("int32")).changeNullable(false))
.create("1", c -> c.changeName("firstName").changeType(t -> t.changeType("string")).changeNullable(true))
.create("2", c -> c.changeName("lastName").changeType(t -> t.changeType("string")).changeNullable(true))
.create("3", c -> c.changeName("balance").changeType(t -> t.changeType("double")).changeNullable(true))
)
.changeIndices(idxs -> idxs
.create("PK", idx -> idx
.changeName("PK")
.changeType("PK")
.changeColumns(cols -> cols.create("0", c -> c.changeName("accountNumber").changeAsc(true)))
)
Table accounts = ignite.tables().createTable("PUBLIC.accounts", tbl -> tbl
.changeName("PUBLIC.accounts")
.changeColumns(cols -> cols
.create("0", c -> c.changeName("accountNumber").changeType(t -> t.changeType("int32")).changeNullable(false))
.create("1", c -> c.changeName("firstName").changeType(t -> t.changeType("string")).changeNullable(true))
.create("2", c -> c.changeName("lastName").changeType(t -> t.changeType("string")).changeNullable(true))
.create("3", c -> c.changeName("balance").changeType(t -> t.changeType("double")).changeNullable(true))
)
.changeIndices(idxs -> idxs
.create("PK", idx -> idx
.changeName("PK")
.changeType("PK")
.changeColumns(cols -> cols.create("0", c -> c.changeName("accountNumber").changeAsc(true)))
)
);
)
);

//---------------------------------------------------------------------------------
//
// Tuple API: insert operation.
//
//---------------------------------------------------------------------------------
//---------------------------------------------------------------------------------
//
// Tuple API: insert operation.
//
//---------------------------------------------------------------------------------

Tuple newAccountTuple = accounts.tupleBuilder()
.set("accountNumber", 123456)
.set("firstName", "Val")
.set("lastName", "Kulichenko")
.set("balance", 100.00d)
.build();
Tuple newAccountTuple = accounts.tupleBuilder()
.set("accountNumber", 123456)
.set("firstName", "Val")
.set("lastName", "Kulichenko")
.set("balance", 100.00d)
.build();

accounts.insert(newAccountTuple);
accounts.insert(newAccountTuple);

//---------------------------------------------------------------------------------
//
// Tuple API: get operation.
//
//---------------------------------------------------------------------------------
//---------------------------------------------------------------------------------
//
// Tuple API: get operation.
//
//---------------------------------------------------------------------------------

Tuple accountNumberTuple = accounts.tupleBuilder().set("accountNumber", 123456).build();
Tuple accountNumberTuple = accounts.tupleBuilder().set("accountNumber", 123456).build();

Tuple accountTuple = accounts.get(accountNumberTuple);
Tuple accountTuple = accounts.get(accountNumberTuple);

System.out.println(
"Retrieved using Tuple API\n" +
" Account Number: " + accountTuple.intValue("accountNumber") + '\n' +
" Owner: " + accountTuple.stringValue("firstName") + " " + accountTuple.stringValue("lastName") + '\n' +
" Balance: $" + accountTuple.doubleValue("balance"));
}
System.out.println(
"Retrieved using Tuple API\n" +
" Account Number: " + accountTuple.intValue("accountNumber") + '\n' +
" Owner: " + accountTuple.stringValue("firstName") + " " + accountTuple.stringValue("lastName") + '\n' +
" Balance: $" + accountTuple.doubleValue("balance"));
}
}

0 comments on commit 9230f61

Please sign in to comment.