Bael 1603 (#3984)
* apache ignite module added * Ignite more extended * apache ignite full code * employee json * apache ignite * reformatting the loop * move apache ignite code snippets into libraries-data
This commit is contained in:
parent
42360c980d
commit
d6d44c7cb9
@ -65,6 +65,26 @@
|
|||||||
<classifier>test</classifier>
|
<classifier>test</classifier>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.ignite</groupId>
|
||||||
|
<artifactId>ignite-core</artifactId>
|
||||||
|
<version>${ignite.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.ignite</groupId>
|
||||||
|
<artifactId>ignite-spring</artifactId>
|
||||||
|
<version>${ignite.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.ignite</groupId>
|
||||||
|
<artifactId>ignite-indexing</artifactId>
|
||||||
|
<version>${ignite.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.google.code.gson</groupId>
|
||||||
|
<artifactId>gson</artifactId>
|
||||||
|
<version>${gson.version}</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
@ -181,5 +201,7 @@
|
|||||||
<maven-compiler-plugin.version>3.7.0</maven-compiler-plugin.version>
|
<maven-compiler-plugin.version>3.7.0</maven-compiler-plugin.version>
|
||||||
<ormlite.version>5.0</ormlite.version>
|
<ormlite.version>5.0</ormlite.version>
|
||||||
<kafka.version>1.0.0</kafka.version>
|
<kafka.version>1.0.0</kafka.version>
|
||||||
|
<ignite.version>2.3.0</ignite.version>
|
||||||
|
<gson.version>2.8.2</gson.version>
|
||||||
</properties>
|
</properties>
|
||||||
</project>
|
</project>
|
14
libraries-data/src/main/java/com/baeldung/ignite/cache/CustomLifecycleBean.java
vendored
Normal file
14
libraries-data/src/main/java/com/baeldung/ignite/cache/CustomLifecycleBean.java
vendored
Normal file
@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
58
libraries-data/src/main/java/com/baeldung/ignite/cache/IgniteCacheExample.java
vendored
Normal file
58
libraries-data/src/main/java/com/baeldung/ignite/cache/IgniteCacheExample.java
vendored
Normal file
@ -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<Integer, String> cache = ignite.cache("baeldungCache");
|
||||||
|
|
||||||
|
cache.put(1, "baeldung cache value");
|
||||||
|
|
||||||
|
String message = cache.get(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void getObjectFromCache(Ignite ignite) {
|
||||||
|
|
||||||
|
IgniteCache<Integer, Employee> 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<Integer, Employee> cache = ignite.cache("baeldungCache");
|
||||||
|
|
||||||
|
SqlFieldsQuery sql = new SqlFieldsQuery(
|
||||||
|
"select name from Employee where isEmployed = 'true'");
|
||||||
|
|
||||||
|
QueryCursor<List<?>> 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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));
|
||||||
|
}
|
||||||
|
}
|
@ -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 +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
@ -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<Integer, Employee> employeeCache() {
|
||||||
|
|
||||||
|
CacheConfiguration<Integer, Employee> config = new CacheConfiguration<>("baeldungEmployees");
|
||||||
|
|
||||||
|
config.setIndexedTypes(Integer.class, Employee.class);
|
||||||
|
config.setExpiryPolicyFactory(FactoryBuilder.factoryOf(
|
||||||
|
new CreatedExpiryPolicy(new Duration(TimeUnit.SECONDS, 5))));
|
||||||
|
|
||||||
|
return config;
|
||||||
|
}
|
||||||
|
}
|
@ -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<Integer, Employee> cache = ignite.getOrCreateCache(CacheConfig.employeeCache());
|
||||||
|
IgniteDataStreamer<Integer, Employee> 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);
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
3
libraries-data/src/main/resources/employee.txt
Normal file
3
libraries-data/src/main/resources/employee.txt
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
{id:"1", name="John", isEmployed: "true"}
|
||||||
|
{id:"1", name="Anna", isEmployed: "false"}
|
||||||
|
{id:"1", name="George", isEmployed: "true"}
|
Loading…
x
Reference in New Issue
Block a user