1.首先定义两个实体类
(1)订单
public class BatchEntity {
@QuerySqlField
private Long id;
@QuerySqlField
private String bccode;
@QuerySqlField
private String customercode;
@QuerySqlField
private String countrycode;
//需要加上getset防范这里省略
}
(2)访销数据
public class BatchEntity {
@QuerySqlField
private Long id;
@QuerySqlField
private String bccode;
@QuerySqlField
private String customercode;
@QuerySqlField
private String countrycode;
//也需要加上getset方法
}
注意创建实体的时候需要通过注解 @QuerySqlField,对实体中的属性进行设置.这样在后面的操作中,可以直接写sql语句进行查询.
(3)定义一个入口启动类,这里我叫做AppMain,在这个类中,我们进行关联统计操作.
public static void main(String args[]) {
try {
//连接ignite
Ignite ignite = Ignition.start();
//获取订单数据实体
IgniteCache orderconf = getOrderEntries(ignite);
//获取访销数据实体
IgniteCache batchconf = getBatchEntries(ignite);
//查询sql
SqlFieldsQuery sql = new SqlFieldsQuery(
"select count(*) from BatchEntity as ba join OR.OrderEntity as or on ba.countrycode = or.countrycode"//126204
// "select count(*) from OrderEntity as or join BA.BatchEntity as ba on or.countrycode= ba.countrycode "//126204
);
FieldsQueryCursor> lists = batchconf.query(sql);
// FieldsQueryCursor> lists = orderconf.query(sql);
System.out.println("查询结果");
lists.forEach(line -> {
System.out.println(line);
});
} catch (Exception e) {
e.printStackTrace();
}
}
//获取访销数据的Igniteconf
private static IgniteCache getOrderEntries(Ignite ignite) throws IOException {
CacheConfiguration conf = new CacheConfiguration<>();
conf.setName("OR");
conf.setCacheMode(CacheMode.PARTITIONED);
conf.setIndexedTypes(Long.class, OrderEntity.class);
IgniteCache ordercof = ignite.getOrCreateCache(conf);
//封装对象
List orderlist = FileUtils.readLines(new File("./src/main/resources/order.txt"), "utf-8");
List orderEntityList = Lists.newArrayList();
AtomicLong batchAtomic = new AtomicLong(0);
for (String s : orderlist) {
String[] split = s.split("\t");
Long id = batchAtomic.getAndIncrement();
String countrycode = split[0];
String adate = split[1];
String amount = split[2];
OrderEntity orderEntity = new OrderEntity(id, countrycode, adate, Integer.parseInt(amount));
orderEntityList.add(orderEntity);
}
orderEntityList.forEach(orderEntity -> {
ordercof.put(orderEntity.getId(), orderEntity);
});
return ordercof;
}
//获取访销数据的Igniteconf
private static IgniteCache getBatchEntries(Ignite ignite) throws IOException {
CacheConfiguration conf = new CacheConfiguration<>();
conf.setName("BA");
conf.setCacheMode(CacheMode.PARTITIONED);
conf.setIndexedTypes(Long.class, BatchEntity.class);
IgniteCache batchconf = ignite.getOrCreateCache(conf);
//封装对象
List orderlist = FileUtils.readLines(new File("./src/main/resources/data.txt"), "utf-8");
List batchEntityList = Lists.newArrayList();
AtomicLong batchAtomic = new AtomicLong(0);
for (String s : orderlist) {
String[] split = s.split("\t");
Long id = batchAtomic.getAndIncrement();
String bccode = split[0];
String customercode = split[1];
String countrycode = split[2];
BatchEntity orderEntity = new BatchEntity(id, bccode, customercode, countrycode);
batchEntityList.add(orderEntity);
}
batchEntityList.forEach(batchEntity -> {
batchconf.put(batchEntity.getId(), batchEntity);
});
return batchconf;
}
在main方法中就可以通过sql语句进行查询,查询的字段就是实体中定义的字段,也可以理解为存储在Ignite表中的列名.
这里说一个注意事项:
"select count(*) from BatchEntity as ba join OR.OrderEntity as or on ba.countrycode = or.countrycode"//126204
在执行上面的sql语句的会后,总是出现找不到表的情况.但是我的实体明明是存在的.湿了很多次,发现进行关联的时候,你选择的conf要注意,我们获取的conf有两个,通过两个方法获取的
IgniteCache orderconf = getOrderEntries(ignite);
IgniteCache batchconf = getBatchEntries(ignite);
当你需要通过订单表order关联访销表batch的时候,你要使用的是订单的conf—orderconf去执行query()方法
并且, 在 join的表前面需要加上你设置的name --BA,
否则就会找不到表,出现下面的异常. 反过来也是一样,如果你用访销表batch去关联订单表order的时候,需要用batchconf去执行query()方法. 同样需要在join 的订单表前加上你设置的名字OR.
在join前是不需要加别名.但是感觉加上也没有影响.应该不会报错,你们可以自己试试.
javax.cache.CacheException: Failed to parse query. Table "ORDERENTITY" not found; SQL statement:
以上就是最近看ignite的一些心得.希望对你们有所帮助.