Skip to content

Commit d6d44c7

Browse files
MherBaghinyanpivovarit
authored andcommitted
Bael 1603 (eugenp#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
1 parent 42360c9 commit d6d44c7

8 files changed

Lines changed: 271 additions & 0 deletions

File tree

‎libraries-data/pom.xml‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,26 @@
6565
<classifier>test</classifier>
6666
<scope>test</scope>
6767
</dependency>
68+
<dependency>
69+
<groupId>org.apache.ignite</groupId>
70+
<artifactId>ignite-core</artifactId>
71+
<version>${ignite.version}</version>
72+
</dependency>
73+
<dependency>
74+
<groupId>org.apache.ignite</groupId>
75+
<artifactId>ignite-spring</artifactId>
76+
<version>${ignite.version}</version>
77+
</dependency>
78+
<dependency>
79+
<groupId>org.apache.ignite</groupId>
80+
<artifactId>ignite-indexing</artifactId>
81+
<version>${ignite.version}</version>
82+
</dependency>
83+
<dependency>
84+
<groupId>com.google.code.gson</groupId>
85+
<artifactId>gson</artifactId>
86+
<version>${gson.version}</version>
87+
</dependency>
6888
</dependencies>
6989

7090
<build>
@@ -181,5 +201,7 @@
181201
<maven-compiler-plugin.version>3.7.0</maven-compiler-plugin.version>
182202
<ormlite.version>5.0</ormlite.version>
183203
<kafka.version>1.0.0</kafka.version>
204+
<ignite.version>2.3.0</ignite.version>
205+
<gson.version>2.8.2</gson.version>
184206
</properties>
185207
</project>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.ignite.cache;
2+
3+
import org.apache.ignite.IgniteException;
4+
import org.apache.ignite.lifecycle.LifecycleBean;
5+
import org.apache.ignite.lifecycle.LifecycleEventType;
6+
7+
public class CustomLifecycleBean implements LifecycleBean {
8+
@Override
9+
public void onLifecycleEvent(LifecycleEventType lifecycleEventType) throws IgniteException {
10+
if (lifecycleEventType == LifecycleEventType.AFTER_NODE_START) {
11+
//do something right after the Ignite node starts
12+
}
13+
}
14+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.baeldung.ignite.cache;
2+
3+
import com.baeldung.ignite.model.Employee;
4+
import org.apache.ignite.Ignite;
5+
import org.apache.ignite.IgniteCache;
6+
import org.apache.ignite.Ignition;
7+
import org.apache.ignite.cache.query.QueryCursor;
8+
import org.apache.ignite.cache.query.SqlFieldsQuery;
9+
import org.apache.ignite.configuration.IgniteConfiguration;
10+
11+
import java.util.List;
12+
13+
public class IgniteCacheExample {
14+
15+
public static void main(String[] args) {
16+
17+
Ignite ignite = Ignition.ignite();
18+
19+
IgniteCache<Integer, String> cache = ignite.cache("baeldungCache");
20+
21+
cache.put(1, "baeldung cache value");
22+
23+
String message = cache.get(1);
24+
}
25+
26+
private static void getObjectFromCache(Ignite ignite) {
27+
28+
IgniteCache<Integer, Employee> cache = ignite.getOrCreateCache("baeldungCache");
29+
30+
cache.put(1, new Employee(1, "John", true));
31+
cache.put(2, new Employee(2, "Anna", false));
32+
cache.put(3, new Employee(3, "George", true));
33+
34+
Employee employee = cache.get(1);
35+
}
36+
37+
private static void getFromCacheWithSQl(Ignite ignite) {
38+
39+
IgniteCache<Integer, Employee> cache = ignite.cache("baeldungCache");
40+
41+
SqlFieldsQuery sql = new SqlFieldsQuery(
42+
"select name from Employee where isEmployed = 'true'");
43+
44+
QueryCursor<List<?>> cursor = cache.query(sql);
45+
46+
for (List<?> row : cursor) {
47+
System.out.println(row.get(0));
48+
}
49+
}
50+
51+
private static void customInitialization() {
52+
53+
IgniteConfiguration configuration = new IgniteConfiguration();
54+
configuration.setLifecycleBeans(new CustomLifecycleBean());
55+
Ignite ignite = Ignition.start(configuration);
56+
}
57+
58+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.baeldung.ignite.jdbc;
2+
3+
import java.sql.*;
4+
5+
/**
6+
* Created by Gebruiker on 3/14/2018.
7+
*/
8+
public class IgniteJDBC {
9+
10+
public static void main(String[] args) throws ClassNotFoundException, SQLException {
11+
12+
Class.forName("org.apache.ignite.IgniteJdbcThinDriver");
13+
14+
Connection conn = DriverManager.getConnection("jdbc:ignite:thin://127.0.0.1/");
15+
16+
createDatabaseTables(conn);
17+
18+
insertData(conn);
19+
20+
getData(conn);
21+
}
22+
23+
private static void createDatabaseTables(Connection conn) throws SQLException {
24+
25+
Statement sql = conn.createStatement();
26+
sql.executeUpdate("CREATE TABLE Employee (" +
27+
" id INTEGER PRIMARY KEY, name VARCHAR, isEmployed timyint(1)) " +
28+
" WITH \"template=replicated\"");
29+
30+
sql.executeUpdate("CREATE INDEX idx_employee_name ON Employee (name)");
31+
}
32+
33+
private static void insertData(Connection conn) throws SQLException {
34+
35+
PreparedStatement sql =
36+
conn.prepareStatement("INSERT INTO Employee (id, name, isEmployed) VALUES (?, ?, ?)");
37+
sql.setLong(1, 1);
38+
sql.setString(2, "James");
39+
sql.setBoolean(3, true);
40+
sql.executeUpdate();
41+
42+
sql.setLong(1, 2);
43+
sql.setString(2, "Monica");
44+
sql.setBoolean(3, false);
45+
sql.executeUpdate();
46+
}
47+
48+
private static void getData(Connection conn) throws SQLException {
49+
50+
Statement sql = conn.createStatement();
51+
ResultSet rs = sql.executeQuery("SELECT e.name, e.isEmployed " +
52+
" FROM Employee e " +
53+
" WHERE e.isEmployed = TRUE ");
54+
55+
while (rs.next())
56+
System.out.println(rs.getString(1) + ", " + rs.getString(2));
57+
}
58+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.baeldung.ignite.model;
2+
3+
4+
public class Employee {
5+
6+
private Integer id;
7+
private String name;
8+
private boolean isEmployed;
9+
10+
public Employee(Integer id, String name, boolean isEmployed) {
11+
this.id = id;
12+
this.name = name;
13+
this.isEmployed = isEmployed;
14+
}
15+
16+
public Integer getId() {
17+
return id;
18+
}
19+
20+
public void setId(Integer id) {
21+
this.id = id;
22+
}
23+
24+
public String getName() {
25+
return name;
26+
}
27+
28+
public void setName(String name) {
29+
this.name = name;
30+
}
31+
32+
public boolean isEmployed() {
33+
return isEmployed;
34+
}
35+
36+
public void setEmployed(boolean employed) {
37+
isEmployed = employed;
38+
}
39+
40+
@Override
41+
public String toString() {
42+
return "Employee{" +
43+
"id=" + id +
44+
", name='" + name + '\'' +
45+
", isEmployed=" + isEmployed +
46+
'}';
47+
}
48+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.ignite.stream;
2+
3+
import com.baeldung.ignite.model.Employee;
4+
import org.apache.ignite.configuration.CacheConfiguration;
5+
6+
import javax.cache.configuration.FactoryBuilder;
7+
import javax.cache.expiry.CreatedExpiryPolicy;
8+
import javax.cache.expiry.Duration;
9+
import java.util.concurrent.TimeUnit;
10+
11+
12+
public class CacheConfig {
13+
14+
public static CacheConfiguration<Integer, Employee> employeeCache() {
15+
16+
CacheConfiguration<Integer, Employee> config = new CacheConfiguration<>("baeldungEmployees");
17+
18+
config.setIndexedTypes(Integer.class, Employee.class);
19+
config.setExpiryPolicyFactory(FactoryBuilder.factoryOf(
20+
new CreatedExpiryPolicy(new Duration(TimeUnit.SECONDS, 5))));
21+
22+
return config;
23+
}
24+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.baeldung.ignite.stream;
2+
3+
import com.baeldung.ignite.model.Employee;
4+
import com.google.gson.Gson;
5+
import org.apache.ignite.Ignite;
6+
import org.apache.ignite.IgniteCache;
7+
import org.apache.ignite.IgniteDataStreamer;
8+
import org.apache.ignite.Ignition;
9+
import org.apache.ignite.stream.StreamTransformer;
10+
11+
import java.nio.file.Files;
12+
import java.nio.file.Path;
13+
import java.nio.file.Paths;
14+
15+
public class IgniteStream {
16+
17+
public static void main(String[] args) throws Exception {
18+
19+
Ignition.setClientMode(true);
20+
Ignite ignite = Ignition.start();
21+
22+
IgniteCache<Integer, Employee> cache = ignite.getOrCreateCache(CacheConfig.employeeCache());
23+
IgniteDataStreamer<Integer, Employee> streamer = ignite.dataStreamer(cache.getName());
24+
streamer.allowOverwrite(true);
25+
26+
streamer.receiver(StreamTransformer.from((e, arg) -> {
27+
28+
Employee employee = e.getValue();
29+
employee.setEmployed(true);
30+
e.setValue(employee);
31+
32+
return null;
33+
}));
34+
35+
Path path = Paths.get(IgniteStream.class.getResource("employees.txt").toURI());
36+
37+
Files.lines(path)
38+
.forEach(line -> {
39+
Employee employee = new Gson().fromJson(line, Employee.class);
40+
streamer.addData(employee.getId(), employee);
41+
});
42+
43+
}
44+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{id:"1", name="John", isEmployed: "true"}
2+
{id:"1", name="Anna", isEmployed: "false"}
3+
{id:"1", name="George", isEmployed: "true"}

0 commit comments

Comments
 (0)