You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by vk...@apache.org on 2021/06/25 18:37:58 UTC

[ignite-3] 04/06: IGNITE-14996 - Added examples for Table and KeyValueBinaryView

This is an automated email from the ASF dual-hosted git repository.

vkulichenko pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/ignite-3.git

commit adfc9d4706f46c7a31ce244ab80dbf4a275bb203
Author: Valentin Kulichenko <va...@gmail.com>
AuthorDate: Fri Jun 25 11:32:02 2021 -0700

    IGNITE-14996 - Added examples for Table and KeyValueBinaryView
---
 assembly/assembly.xml                              | 21 +++++
 examples/pom-standalone.xml                        | 41 +++++++++
 examples/pom.xml                                   | 41 +++++++++
 .../example/table/KeyValueBinaryViewExample.java   | 98 ++++++++++++++++++++++
 .../apache/ignite/example/table/TableExample.java  | 95 +++++++++++++++++++++
 examples/src/main/resources/ignite-config.json     | 13 +++
 parent/pom.xml                                     | 12 ++-
 pom.xml                                            |  1 +
 8 files changed, 319 insertions(+), 3 deletions(-)

diff --git a/assembly/assembly.xml b/assembly/assembly.xml
index 7ecb499..a517b0f 100644
--- a/assembly/assembly.xml
+++ b/assembly/assembly.xml
@@ -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>
diff --git a/examples/pom-standalone.xml b/examples/pom-standalone.xml
new file mode 100644
index 0000000..23da513
--- /dev/null
+++ b/examples/pom-standalone.xml
@@ -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>
diff --git a/examples/pom.xml b/examples/pom.xml
new file mode 100644
index 0000000..fd5fee8
--- /dev/null
+++ b/examples/pom.xml
@@ -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>
diff --git a/examples/src/main/java/org/apache/ignite/example/table/KeyValueBinaryViewExample.java b/examples/src/main/java/org/apache/ignite/example/table/KeyValueBinaryViewExample.java
new file mode 100644
index 0000000..b8e7610
--- /dev/null
+++ b/examples/src/main/java/org/apache/ignite/example/table/KeyValueBinaryViewExample.java
@@ -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"));
+    }
+}
diff --git a/examples/src/main/java/org/apache/ignite/example/table/TableExample.java b/examples/src/main/java/org/apache/ignite/example/table/TableExample.java
new file mode 100644
index 0000000..222d772
--- /dev/null
+++ b/examples/src/main/java/org/apache/ignite/example/table/TableExample.java
@@ -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"));
+        }
+    }
+}
diff --git a/examples/src/main/resources/ignite-config.json b/examples/src/main/resources/ignite-config.json
new file mode 100644
index 0000000..785d27c
--- /dev/null
+++ b/examples/src/main/resources/ignite-config.json
@@ -0,0 +1,13 @@
+{
+    "node": {
+        "metastorageNodes": [
+            "node0"
+        ]
+    },
+    "network": {
+        "port": 3344,
+        "netClusterNodes": [
+            "localhost:3344"
+        ]
+    }
+}
diff --git a/parent/pom.xml b/parent/pom.xml
index ea4c7ec..8de7554 100644
--- a/parent/pom.xml
+++ b/parent/pom.xml
@@ -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>
 
diff --git a/pom.xml b/pom.xml
index 2d139e9..f09d6cf 100644
--- a/pom.xml
+++ b/pom.xml
@@ -59,6 +59,7 @@
         <module>modules/schema</module>
         <module>modules/table</module>
         <module>modules/vault</module>
+        <module>examples</module>
     </modules>
 
     <build>