Skip to content

Instantly share code, notes, and snippets.

@pavlukhin
Last active October 24, 2019 08:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pavlukhin/3ac27c2def1731a5158222a0d542dfa6 to your computer and use it in GitHub Desktop.
Save pavlukhin/3ac27c2def1731a5158222a0d542dfa6 to your computer and use it in GitHub Desktop.
import java.util.Collections;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.Ignition;
import org.apache.ignite.cache.query.SqlFieldsQuery;
import org.apache.ignite.configuration.CacheConfiguration;
import org.apache.ignite.configuration.IgniteConfiguration;
import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi;
import org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder;
public class NestedFieldTest {
public static void main(String[] args) {
TcpDiscoverySpi discoverySpi = new TcpDiscoverySpi()
.setIpFinder(new TcpDiscoveryVmIpFinder().setAddresses(Collections.singleton("127.0.0.1:47500..47509")));
IgniteConfiguration cfg = new IgniteConfiguration().setDiscoverySpi(discoverySpi);
try (Ignite ignite = Ignition.start(cfg)) {
IgniteCache<Object, Object> cache = ignite.createCache(new CacheConfiguration<>("cache")
.setIndexedTypes(Integer.class, Person.class));
cache.put(1, new Person("john", new Address("baker", 221)));
System.err.println(cache.query(new SqlFieldsQuery("select * from Person")).getAll());
cache.query(new SqlFieldsQuery("alter table Person add column name varchar"));
cache.query(new SqlFieldsQuery("alter table Person add column address other"));
System.err.println(cache.query(new SqlFieldsQuery("select * from Person")).getAll());
cache.query(new SqlFieldsQuery("alter table Person add column \"address.street\" varchar"));
// cache.query(new SqlFieldsQuery("alter table Person add column \"Address.street\" varchar"));
System.err.println(cache.query(new SqlFieldsQuery("select * from Person")).getAll());
cache.put(2, new Person("bill", new Address("green", 1)));
System.err.println(cache.query(new SqlFieldsQuery("select * from Person")).getAll());
}
}
}
class Person {
String name;
Address address;
Person(String name, Address address) {
this.name = name;
this.address = address;
}
}
class Address {
String street;
int number;
Address(String street, int number) {
this.street = street;
this.number = number;
}
}
import java.util.Collections;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.Ignition;
import org.apache.ignite.cache.query.SqlFieldsQuery;
import org.apache.ignite.configuration.CacheConfiguration;
import org.apache.ignite.configuration.IgniteConfiguration;
import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi;
import org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder;
import org.junit.Test;
import static org.junit.Assert.*;
public class NestedFieldUnitTest {
@Test
public void test1() throws Exception {
doTest("\"Address.street\"");
}
@Test
public void test2() throws Exception {
doTest("\"address.street\"");
}
private void doTest(String streetColumnName) {
TcpDiscoverySpi discoverySpi = new TcpDiscoverySpi()
.setIpFinder(new TcpDiscoveryVmIpFinder().setAddresses(Collections.singleton("127.0.0.1:47500..47509")));
IgniteConfiguration cfg = new IgniteConfiguration().setDiscoverySpi(discoverySpi);
try (Ignite ignite = Ignition.start(cfg)) {
IgniteCache<Object, Object> cache = ignite.createCache(new CacheConfiguration<>("cache")
.setIndexedTypes(Integer.class, Person.class));
cache.put(1, new Person("john", new Address("baker", 221)));
cache.query(new SqlFieldsQuery("alter table Person add column name varchar"));
assertEquals("john", cache.query(new SqlFieldsQuery("select name from Person")).getAll().get(0).get(0));
// cache.query(new SqlFieldsQuery("alter table Person add column address other"));
//
// assertNotNull(cache.query(new SqlFieldsQuery("select address from Person")).getAll().get(0).get(0));
cache.query(new SqlFieldsQuery("alter table Person add column " + streetColumnName + " varchar"));
assertEquals("baker", cache.query(new SqlFieldsQuery("select " + streetColumnName + " from Person")).getAll().get(0).get(0));
}
}
static class Person {
String name;
Address address;
Person(String name, Address address) {
this.name = name;
this.address = address;
}
}
static class Address {
String street;
int number;
Address(String street, int number) {
this.street = street;
this.number = number;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment