diff --git a/libraries-data/pom.xml b/libraries-data/pom.xml index 4727ca2222..55bf02ae16 100644 --- a/libraries-data/pom.xml +++ b/libraries-data/pom.xml @@ -65,6 +65,26 @@ test test + + org.apache.ignite + ignite-core + ${ignite.version} + + + org.apache.ignite + ignite-spring + ${ignite.version} + + + org.apache.ignite + ignite-indexing + ${ignite.version} + + + com.google.code.gson + gson + ${gson.version} + @@ -181,5 +201,7 @@ 3.7.0 5.0 1.0.0 + 2.3.0 + 2.8.2 \ No newline at end of file diff --git a/libraries-data/src/main/java/com/baeldung/ignite/cache/CustomLifecycleBean.java b/libraries-data/src/main/java/com/baeldung/ignite/cache/CustomLifecycleBean.java new file mode 100644 index 0000000000..0b603dcee0 --- /dev/null +++ b/libraries-data/src/main/java/com/baeldung/ignite/cache/CustomLifecycleBean.java @@ -0,0 +1,14 @@ +package com.baeldung.ignite.cache; + +import org.apache.ignite.IgniteException; +import org.apache.ignite.lifecycle.LifecycleBean; +import org.apache.ignite.lifecycle.LifecycleEventType; + +public class CustomLifecycleBean implements LifecycleBean { + @Override + public void onLifecycleEvent(LifecycleEventType lifecycleEventType) throws IgniteException { + if (lifecycleEventType == LifecycleEventType.AFTER_NODE_START) { + //do something right after the Ignite node starts + } + } +} diff --git a/libraries-data/src/main/java/com/baeldung/ignite/cache/IgniteCacheExample.java b/libraries-data/src/main/java/com/baeldung/ignite/cache/IgniteCacheExample.java new file mode 100644 index 0000000000..8c40b8f312 --- /dev/null +++ b/libraries-data/src/main/java/com/baeldung/ignite/cache/IgniteCacheExample.java @@ -0,0 +1,58 @@ +package com.baeldung.ignite.cache; + +import com.baeldung.ignite.model.Employee; +import org.apache.ignite.Ignite; +import org.apache.ignite.IgniteCache; +import org.apache.ignite.Ignition; +import org.apache.ignite.cache.query.QueryCursor; +import org.apache.ignite.cache.query.SqlFieldsQuery; +import org.apache.ignite.configuration.IgniteConfiguration; + +import java.util.List; + +public class IgniteCacheExample { + + public static void main(String[] args) { + + Ignite ignite = Ignition.ignite(); + + IgniteCache cache = ignite.cache("baeldungCache"); + + cache.put(1, "baeldung cache value"); + + String message = cache.get(1); + } + + private static void getObjectFromCache(Ignite ignite) { + + IgniteCache cache = ignite.getOrCreateCache("baeldungCache"); + + cache.put(1, new Employee(1, "John", true)); + cache.put(2, new Employee(2, "Anna", false)); + cache.put(3, new Employee(3, "George", true)); + + Employee employee = cache.get(1); + } + + private static void getFromCacheWithSQl(Ignite ignite) { + + IgniteCache cache = ignite.cache("baeldungCache"); + + SqlFieldsQuery sql = new SqlFieldsQuery( + "select name from Employee where isEmployed = 'true'"); + + QueryCursor> cursor = cache.query(sql); + + for (List row : cursor) { + System.out.println(row.get(0)); + } + } + + private static void customInitialization() { + + IgniteConfiguration configuration = new IgniteConfiguration(); + configuration.setLifecycleBeans(new CustomLifecycleBean()); + Ignite ignite = Ignition.start(configuration); + } + +} diff --git a/libraries-data/src/main/java/com/baeldung/ignite/jdbc/IgniteJDBC.java b/libraries-data/src/main/java/com/baeldung/ignite/jdbc/IgniteJDBC.java new file mode 100644 index 0000000000..de144711b3 --- /dev/null +++ b/libraries-data/src/main/java/com/baeldung/ignite/jdbc/IgniteJDBC.java @@ -0,0 +1,58 @@ +package com.baeldung.ignite.jdbc; + +import java.sql.*; + +/** + * Created by Gebruiker on 3/14/2018. + */ +public class IgniteJDBC { + + public static void main(String[] args) throws ClassNotFoundException, SQLException { + + Class.forName("org.apache.ignite.IgniteJdbcThinDriver"); + + Connection conn = DriverManager.getConnection("jdbc:ignite:thin://127.0.0.1/"); + + createDatabaseTables(conn); + + insertData(conn); + + getData(conn); + } + + private static void createDatabaseTables(Connection conn) throws SQLException { + + Statement sql = conn.createStatement(); + sql.executeUpdate("CREATE TABLE Employee (" + + " id INTEGER PRIMARY KEY, name VARCHAR, isEmployed timyint(1)) " + + " WITH \"template=replicated\""); + + sql.executeUpdate("CREATE INDEX idx_employee_name ON Employee (name)"); + } + + private static void insertData(Connection conn) throws SQLException { + + PreparedStatement sql = + conn.prepareStatement("INSERT INTO Employee (id, name, isEmployed) VALUES (?, ?, ?)"); + sql.setLong(1, 1); + sql.setString(2, "James"); + sql.setBoolean(3, true); + sql.executeUpdate(); + + sql.setLong(1, 2); + sql.setString(2, "Monica"); + sql.setBoolean(3, false); + sql.executeUpdate(); + } + + private static void getData(Connection conn) throws SQLException { + + Statement sql = conn.createStatement(); + ResultSet rs = sql.executeQuery("SELECT e.name, e.isEmployed " + + " FROM Employee e " + + " WHERE e.isEmployed = TRUE "); + + while (rs.next()) + System.out.println(rs.getString(1) + ", " + rs.getString(2)); + } +} diff --git a/libraries-data/src/main/java/com/baeldung/ignite/model/Employee.java b/libraries-data/src/main/java/com/baeldung/ignite/model/Employee.java new file mode 100644 index 0000000000..21f5ef89d0 --- /dev/null +++ b/libraries-data/src/main/java/com/baeldung/ignite/model/Employee.java @@ -0,0 +1,48 @@ +package com.baeldung.ignite.model; + + +public class Employee { + + private Integer id; + private String name; + private boolean isEmployed; + + public Employee(Integer id, String name, boolean isEmployed) { + this.id = id; + this.name = name; + this.isEmployed = isEmployed; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public boolean isEmployed() { + return isEmployed; + } + + public void setEmployed(boolean employed) { + isEmployed = employed; + } + + @Override + public String toString() { + return "Employee{" + + "id=" + id + + ", name='" + name + '\'' + + ", isEmployed=" + isEmployed + + '}'; + } +} diff --git a/libraries-data/src/main/java/com/baeldung/ignite/stream/CacheConfig.java b/libraries-data/src/main/java/com/baeldung/ignite/stream/CacheConfig.java new file mode 100644 index 0000000000..2b0f71e0b9 --- /dev/null +++ b/libraries-data/src/main/java/com/baeldung/ignite/stream/CacheConfig.java @@ -0,0 +1,24 @@ +package com.baeldung.ignite.stream; + +import com.baeldung.ignite.model.Employee; +import org.apache.ignite.configuration.CacheConfiguration; + +import javax.cache.configuration.FactoryBuilder; +import javax.cache.expiry.CreatedExpiryPolicy; +import javax.cache.expiry.Duration; +import java.util.concurrent.TimeUnit; + + +public class CacheConfig { + + public static CacheConfiguration employeeCache() { + + CacheConfiguration config = new CacheConfiguration<>("baeldungEmployees"); + + config.setIndexedTypes(Integer.class, Employee.class); + config.setExpiryPolicyFactory(FactoryBuilder.factoryOf( + new CreatedExpiryPolicy(new Duration(TimeUnit.SECONDS, 5)))); + + return config; + } +} \ No newline at end of file diff --git a/libraries-data/src/main/java/com/baeldung/ignite/stream/IgniteStream.java b/libraries-data/src/main/java/com/baeldung/ignite/stream/IgniteStream.java new file mode 100644 index 0000000000..839da36c22 --- /dev/null +++ b/libraries-data/src/main/java/com/baeldung/ignite/stream/IgniteStream.java @@ -0,0 +1,44 @@ +package com.baeldung.ignite.stream; + +import com.baeldung.ignite.model.Employee; +import com.google.gson.Gson; +import org.apache.ignite.Ignite; +import org.apache.ignite.IgniteCache; +import org.apache.ignite.IgniteDataStreamer; +import org.apache.ignite.Ignition; +import org.apache.ignite.stream.StreamTransformer; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +public class IgniteStream { + + public static void main(String[] args) throws Exception { + + Ignition.setClientMode(true); + Ignite ignite = Ignition.start(); + + IgniteCache cache = ignite.getOrCreateCache(CacheConfig.employeeCache()); + IgniteDataStreamer streamer = ignite.dataStreamer(cache.getName()); + streamer.allowOverwrite(true); + + streamer.receiver(StreamTransformer.from((e, arg) -> { + + Employee employee = e.getValue(); + employee.setEmployed(true); + e.setValue(employee); + + return null; + })); + + Path path = Paths.get(IgniteStream.class.getResource("employees.txt").toURI()); + + Files.lines(path) + .forEach(line -> { + Employee employee = new Gson().fromJson(line, Employee.class); + streamer.addData(employee.getId(), employee); + }); + + } +} diff --git a/libraries-data/src/main/resources/employee.txt b/libraries-data/src/main/resources/employee.txt new file mode 100644 index 0000000000..6c476f39fa --- /dev/null +++ b/libraries-data/src/main/resources/employee.txt @@ -0,0 +1,3 @@ +{id:"1", name="John", isEmployed: "true"} +{id:"1", name="Anna", isEmployed: "false"} +{id:"1", name="George", isEmployed: "true"} \ No newline at end of file