Skip to content
Permalink
Browse files
IGNITE-14996 - Added examples for Table and KeyValueBinaryView
  • Loading branch information
vkulichenko committed Jun 25, 2021
1 parent 5b81332 commit adfc9d4706f46c7a31ce244ab80dbf4a275bb203
Show file tree
Hide file tree
Showing 8 changed files with 319 additions and 3 deletions.
@@ -25,26 +25,47 @@
<formats>
<format>zip</format>
</formats>

<files>
<file>
<source>LICENSE</source>
<outputDirectory/>
</file>

<file>
<source>NOTICE</source>
<outputDirectory/>
</file>

<file>
<source>assembly/README</source>
<outputDirectory/>
</file>

<file>
<source>modules/cli/target/ignite</source>
<outputDirectory/>
</file>

<file>
<source>modules/cli/target/ignite.exe</source>
<outputDirectory/>
</file>

<file>
<source>examples/pom-standalone.xml</source>
<outputDirectory>/examples</outputDirectory>
<destName>pom.xml</destName>
</file>
</files>

<fileSets>
<fileSet>
<directory>examples</directory>
<outputDirectory>/examples</outputDirectory>
<includes>
<include>src/**</include>
</includes>
</fileSet>
</fileSets>
</assembly>
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--
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.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.apache.ignite</groupId>
<artifactId>ignite-examples</artifactId>
<version>3.0.0-alpha2</version>

<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.ignite</groupId>
<artifactId>ignite-runner</artifactId>
<version>3.0.0-alpha2</version>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--
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.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.apache.ignite</groupId>
<artifactId>ignite-parent</artifactId>
<version>1</version>
<relativePath>../parent/pom.xml</relativePath>
</parent>

<artifactId>ignite-examples</artifactId>
<version>3.0.0-alpha2</version>

<dependencies>
<dependency>
<groupId>org.apache.ignite</groupId>
<artifactId>ignite-runner</artifactId>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,98 @@
/*
* 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.table;

import java.nio.file.Files;
import java.nio.file.Path;
import org.apache.ignite.app.Ignite;
import org.apache.ignite.app.IgnitionManager;
import org.apache.ignite.table.KeyValueBinaryView;
import org.apache.ignite.table.Table;
import org.apache.ignite.table.Tuple;

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);

//---------------------------------------------------------------------------------
//
// 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)))
)
)
);

KeyValueBinaryView kvView = accounts.kvView();

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

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

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

kvView.put(key, value);

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

value = accounts.get(key);

System.out.println(
"Retrieved using Key-Value API\n" +
" Account Number: " + key.intValue("accountNumber") + '\n' +
" Owner: " + value.stringValue("firstName") + " " + value.stringValue("lastName") + '\n' +
" Balance: $" + value.doubleValue("balance"));
}
}
@@ -0,0 +1,95 @@
/*
* 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.table;

import java.nio.file.Files;
import java.nio.file.Path;
import org.apache.ignite.app.Ignite;
import org.apache.ignite.app.IgnitionManager;
import org.apache.ignite.table.Table;
import org.apache.ignite.table.Tuple;

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)) {

//---------------------------------------------------------------------------------
//
// 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)))
)
)
);

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

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

accounts.insert(newAccountTuple);

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

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

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"));
}
}
}
@@ -0,0 +1,13 @@
{
"node": {
"metastorageNodes": [
"node0"
]
},
"network": {
"port": 3344,
"netClusterNodes": [
"localhost:3344"
]
}
}
@@ -169,7 +169,7 @@
<artifactId>ignite-configuration-api</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.apache.ignite</groupId>
<artifactId>ignite-core</artifactId>
@@ -187,7 +187,7 @@
<artifactId>ignite-metastorage-client</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.apache.ignite</groupId>
<artifactId>ignite-metastorage-common</artifactId>
@@ -254,6 +254,12 @@
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.apache.ignite</groupId>
<artifactId>ignite-runner</artifactId>
<version>${project.version}</version>
</dependency>

<!-- 3rd party dependencies -->
<dependency>
<groupId>org.jetbrains</groupId>
@@ -680,7 +686,7 @@
</executions>
</plugin>
</plugins>
</build>
</build>
</profile>
</profiles>

@@ -59,6 +59,7 @@
<module>modules/schema</module>
<module>modules/table</module>
<module>modules/vault</module>
<module>examples</module>
</modules>

<build>

0 comments on commit adfc9d4

Please sign in to comment.