Java 将ApacheIgnite二进制对象与SQL表混合使用
我正在评估ApacheIgnite,并试图了解各种模型如何相互映射。据我所知,无论您使用何种引擎/API来访问数据,底层存储都是KV对(对吗?) 现在我想到了几个问题:Java 将ApacheIgnite二进制对象与SQL表混合使用,java,ignite,Java,Ignite,我正在评估ApacheIgnite,并试图了解各种模型如何相互映射。据我所知,无论您使用何种引擎/API来访问数据,底层存储都是KV对(对吗?) 现在我想到了几个问题: 每个SQL表是否映射到不同的缓存,即创建表a和创建表b创建两个缓存,a和b 我之所以这样问是因为API,当前的API允许您创建单个缓存实例并对其运行多个create table查询。起初,我认为这意味着缓存类似于RDBMS中的DB构造,但示例中来自ignite master branch(如下所示)的注释表明情况并非如此 您可以
创建表a
和创建表b
创建两个缓存,a
和b
BinaryObject
如何与SQL表相关。。。?在我在树中查看的示例中,只要使用QueryEntity
API提供映射,就可以创建一个二进制对象,然后通过SQL进行访问BinaryObject
s是否有好处?在我看来,到目前为止,create表应该只映射到底层impl中的二进制对象。索引、类型和QueryEntity
映射等功能将自动为您完成a.b
的名称作为字段名,但我不清楚如何通过SQL访问它,因为我认为这样的名称会与现有语义相冲突import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.Ignition;
import org.apache.ignite.cache.CacheMode;
import org.apache.ignite.cache.QueryEntity;
import org.apache.ignite.cache.QueryIndex;
import org.apache.ignite.cache.QueryIndexType;
import org.apache.ignite.cache.query.SqlFieldsQuery;
import org.apache.ignite.configuration.CacheConfiguration;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
public class TableAndBinaryObjectCacheExperiment {
private static final String cacheName = "some-cache-name";
@SuppressWarnings({"unused", "ThrowFromFinallyBlock"})
public static void main(String[] args) throws Exception {
try (Ignite ignite = Ignition.start("ignite/ignite.xml")) {
if (!ignite.cluster().active()) ignite.cluster().active(true);
// Create dummy cache to act as an entry point for SQL queries (new SQL API which do not require this
// will appear in future versions, JDBC and ODBC drivers do not require it already).
CacheConfiguration<?, ?> cacheCfg = new CacheConfiguration<>(cacheName).setSqlSchema("PUBLIC");
//
LinkedHashMap<String, String> fields = new LinkedHashMap<>();
fields.put("person_id", Long.class.getName());
fields.put("name", String.class.getName());
fields.put("address.postcode", String.class.getName());
fields.put("age", Integer.class.getName());
fields.put("about", String.class.getName());
fields.put("misc", String.class.getName());
QueryEntity testBinType = new QueryEntity();
testBinType.setKeyType(String.class.getName());
testBinType.setValueType("TestType");
//primary key
testBinType.setKeyType(Long.class.getName());
testBinType.setKeyFieldName("test_id");
testBinType.setFields(fields);
testBinType.setTableName("test_type");
testBinType.setIndexes(Arrays.asList(
new QueryIndex("name"),
new QueryIndex("address.postcode"),
new QueryIndex("age"),
new QueryIndex("about", QueryIndexType.FULLTEXT),
new QueryIndex("person_id")
));
CacheConfiguration<?, ?> binaryConf1 = new CacheConfiguration<>(cacheName);
binaryConf1.setCacheMode(CacheMode.PARTITIONED);
binaryConf1.setQueryEntities(Collections.singletonList(testBinType));
//
try (
IgniteCache<?, ?> cache = ignite.getOrCreateCache(cacheCfg);
IgniteCache<?, ?> binCacheX = ignite.getOrCreateCache(binaryConf1)
) {
IgniteCache<?, ?> binCache = cache.withKeepBinary();
// Create reference City table based on REPLICATED template.
cache.query(new SqlFieldsQuery("CREATE TABLE IF NOT EXISTS city (id LONG PRIMARY KEY, name VARCHAR) WITH \"template=replicated\"")).getAll();
// Create table based on PARTITIONED template with one backup.
cache.query(new SqlFieldsQuery("CREATE TABLE IF NOT EXISTS person (id LONG, name VARCHAR, city_id LONG, PRIMARY KEY (id, city_id)) WITH \"backups=1, affinity_key=city_id\"")).getAll();
// Create an index.
cache.query(new SqlFieldsQuery("CREATE INDEX IF NOT EXISTS on Person (city_id)")).getAll();
print("Created database objects.");
SqlFieldsQuery qry = new SqlFieldsQuery("INSERT INTO city (id, name) VALUES (?, ?)");
cache.query(qry.setArgs(1L, "Forest Hill")).getAll();
cache.query(qry.setArgs(2L, "Denver")).getAll();
cache.query(qry.setArgs(3L, "St. Petersburg")).getAll();
qry = new SqlFieldsQuery("INSERT INTO person (id, name, city_id) values (?, ?, ?)");
cache.query(qry.setArgs(1L, "John Doe", 3L)).getAll();
cache.query(qry.setArgs(2L, "Jane Roe", 2L)).getAll();
cache.query(qry.setArgs(3L, "Mary Major", 1L)).getAll();
cache.query(qry.setArgs(4L, "Richard Miles", 2L)).getAll();
qry = new SqlFieldsQuery("INSERT INTO test_type (test_id, name, age, about, \"address.postcode\") values (? ?, ?, ?, ?)");
cache.query(qry.setArgs(1L, "Courtney", 12, "this is about me", "AB12CD", 3L));
SqlFieldsQuery joinQuery = new SqlFieldsQuery(
"SELECT p.name, c.name, t.about, \"t.address.postcode\" " +
"FROM Person p " +
"INNER JOIN City c on c.id = p.city_id " +
"INNER JOIN test_type t on p.id = t.person_id " +
"LIMIT 50");
List<List<?>> res = cache.query(joinQuery).getAll();
for (Object next : res)
System.out.println(">>> " + next);
} finally {
// Distributed cache can be removed from cluster only by #destroyCache() call.
ignite.destroyCache(cacheName);
}
print("Cache query DDL example finished.");
}
}
/**
* Prints message.
*
* @param msg Message to print before all objects are printed.
*/
private static void print(String msg) {
System.out.println();
System.out.println(">>> " + msg);
}
}
import org.apache.ignite.ignite;
导入org.apache.ignite.IgniteCache;
导入org.apache.ignite.Ignition;
导入org.apache.ignite.cache.CacheMode;
导入org.apache.ignite.cache.QueryEntity;
导入org.apache.ignite.cache.QueryIndex;
导入org.apache.ignite.cache.QueryIndexType;
导入org.apache.ignite.cache.query.SqlFieldsQuery;
导入org.apache.ignite.configuration.CacheConfiguration;
导入java.util.array;
导入java.util.Collections;
导入java.util.LinkedHashMap;
导入java.util.List;
公共类TableandBinaryObjectCacheExperience{
私有静态最终字符串cacheName=“某些缓存名称”;
@SuppressWarnings({“未使用”、“ThrowFromFinallyBlock”})
公共静态void main(字符串[]args)引发异常{
try(Ignite=Ignition.start(“Ignite/Ignite.xml”)){
如果(!ignite.cluster().active())ignite.cluster().active(true);
//创建虚拟缓存作为SQL查询的入口点(不需要此功能的新SQL API
//将出现在将来的版本中,JDBC和ODBC驱动程序不再需要它)。
CacheConfiguration cacheCfg=新的CacheConfiguration(cacheName).setSqlSchema(“PUBLIC”);
//
LinkedHashMap字段=新建LinkedHashMap();
fields.put(“person_id”,Long.class.getName());
fields.put(“name”,String.class.getName());
fields.put(“address.postcode”,String.class.getName());
fields.put(“age”,Integer.class.getName());
fields.put(“about”,String.class.getName());
fields.put(“misc”,String.class.getName());
QueryEntity testBinType=新的QueryEntity();
testBinType.setKeyType(String.class.getName());
setValueType(“TestType”);
//主键
setKeyType(Long.class.getName());
testBinType.setKeyFieldName(“测试id”);
testBinType.setFields(字段);
setTableName(“测试类型”);
setIndexes(Arrays.asList(
新查询索引(“名称”),
新查询索引(“地址.邮政编码”),
新查询索引(“年龄”),
新的查询索引(“about”,QueryIndexType.FULLTEXT),
新查询索引(“人员id”)
));
CacheConfiguration binaryConf1=新的CacheConfiguration(cacheName);
binaryConf1.setCacheMode(CacheMode.PARTITIONED);
binaryConf1.setQueryEntities(Collections.singletonList(testBinType));
//
试一试(
IgniteCache cache=ignite.getOrCreateCache(cacheCfg);
IgniteCache binCacheX=ignite.getOrCreateCache(binaryConf1)
) {
IgniteCache binCache=cache.withKeepBinary();
//基于复制的模板创建参考城市表。
query(新的SqlFieldsQuery(“创建表,如果不存在city(id-LONG主键,名称VARCHAR),使用\“template=replicated\”)).getAll();
//基于带一个备份的分区模板创建表。
cache.query(新的SqlFieldsQuery(“如果不存在创建表person(id LONG,name VARCHAR,city_id LONG,PRIMARY KEY(id,city_id)),带有\“backups=1,affinity_KEY=city_id\”).getAll();
//创建一个索引。
query(新的SqlFieldsQuery(“如果Person(city_id)上不存在创建索引)”).getAll();
打印(“创建的数据库对象”);
SqlFieldsQuery qry=新的SqlFieldsQuery(“插入城市(id,名称)值(?,)”;
query(qry.setArgs(1L,“森林山”)).getAll();
query(qry.setArgs(2L,“Denver”).getAll();
cache.query
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.Ignition;
import org.apache.ignite.cache.CacheMode;
import org.apache.ignite.cache.QueryEntity;
import org.apache.ignite.cache.QueryIndex;
import org.apache.ignite.cache.QueryIndexType;
import org.apache.ignite.cache.query.SqlFieldsQuery;
import org.apache.ignite.configuration.CacheConfiguration;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
public class TableAndBinaryObjectCacheExperiment {
private static final String cacheName = "some-cache-name";
@SuppressWarnings({"unused", "ThrowFromFinallyBlock"})
public static void main(String[] args) throws Exception {
try (Ignite ignite = Ignition.start("ignite/ignite.xml")) {
if (!ignite.cluster().active()) ignite.cluster().active(true);
// Create dummy cache to act as an entry point for SQL queries (new SQL API which do not require this
// will appear in future versions, JDBC and ODBC drivers do not require it already).
CacheConfiguration<?, ?> cacheCfg = new CacheConfiguration<>("default").setSqlSchema("PUBLIC");
//
LinkedHashMap<String, String> fields = new LinkedHashMap<>();
fields.put("person_id", Long.class.getName());
fields.put("name", String.class.getName());
fields.put("address_postcode", String.class.getName());
fields.put("age", Integer.class.getName());
fields.put("about", String.class.getName());
fields.put("misc", String.class.getName());
QueryEntity testBinType = new QueryEntity();
testBinType.setValueType("TestType");
//primary key
testBinType.setKeyType(Long.class.getName());
testBinType.setKeyFieldName("person_id");
testBinType.setFields(fields);
testBinType.setTableName("test_type");
testBinType.setIndexes(Arrays.asList(
new QueryIndex("name"),
new QueryIndex("address_postcode"),
new QueryIndex("age"),
new QueryIndex("about", QueryIndexType.FULLTEXT)
));
CacheConfiguration<?, ?> binaryConf1 = new CacheConfiguration<>(cacheName);
binaryConf1.setCacheMode(CacheMode.PARTITIONED);
binaryConf1.setQueryEntities(Collections.singletonList(testBinType));
//
try (
IgniteCache<?, ?> cache = ignite.getOrCreateCache(cacheCfg);
IgniteCache<?, ?> binCacheX = ignite.getOrCreateCache(binaryConf1)
) {
IgniteCache<?, ?> binCache = cache.withKeepBinary();
// Create reference City table based on REPLICATED template.
cache.query(new SqlFieldsQuery("CREATE TABLE IF NOT EXISTS city (id LONG PRIMARY KEY, name VARCHAR) WITH \"template=replicated\"")).getAll();
// Create table based on PARTITIONED template with one backup.
cache.query(new SqlFieldsQuery("CREATE TABLE IF NOT EXISTS person (id LONG, name VARCHAR, city_id LONG, PRIMARY KEY (id, city_id)) WITH \"backups=1, affinity_key=city_id\"")).getAll();
// Create an index.
cache.query(new SqlFieldsQuery("CREATE INDEX IF NOT EXISTS on Person (city_id)")).getAll();
print("Created database objects.");
SqlFieldsQuery qry = new SqlFieldsQuery("INSERT INTO city (id, name) VALUES (?, ?)");
cache.query(qry.setArgs(1L, "Forest Hill")).getAll();
cache.query(qry.setArgs(2L, "Denver")).getAll();
cache.query(qry.setArgs(3L, "St. Petersburg")).getAll();
qry = new SqlFieldsQuery("INSERT INTO person (id, name, city_id) values (?, ?, ?)");
cache.query(qry.setArgs(1L, "John Doe", 3L)).getAll();
cache.query(qry.setArgs(2L, "Jane Roe", 2L)).getAll();
cache.query(qry.setArgs(3L, "Mary Major", 1L)).getAll();
cache.query(qry.setArgs(4L, "Richard Miles", 2L)).getAll();
qry = new SqlFieldsQuery("INSERT INTO \"some-cache-name\".test_type (person_id, name, age, about, address_postcode) values (?, ?, ?, ?, ?)");
cache.query(qry.setArgs(1L, "Courtney", 12, "this is about me", "AB12CD", 3L));
SqlFieldsQuery joinQuery = new SqlFieldsQuery(
"SELECT p.name, c.name, t.about, t.address_postcode " +
"FROM Person p " +
"INNER JOIN City c on c.id = p.city_id " +
"INNER JOIN \"some-cache-name\".test_type t on p.id = t.person_id " +
"LIMIT 50");
List<List<?>> res = cache.query(joinQuery).getAll();
for (Object next : res)
System.out.println(">>> " + next);
} finally {
// Distributed cache can be removed from cluster only by #destroyCache() call.
ignite.destroyCache(cacheName);
}
print("Cache query DDL example finished.");
}
}
/**
* Prints message.
*
* @param msg Message to print before all objects are printed.
*/
private static void print(String msg) {
System.out.println();
System.out.println(">>> " + msg);
}
}
>>> Created database objects.
>>> [John Doe, St. Petersburg, this is about me, AB12CD]
>>> Cache query DDL example finished.