13. Spring Data for Apache Geodeとは
• Spring DataのUmbrellaプロジェクト
13
Springによる統一的な
プログラミングモデルを提供
(APIを抽象化・共通化)
発生する例外を共通例外に変換
Repositoryインターフェースで
簡単にアクセス
バックエンドが変わっても
簡単に変更できる
Release Train KayよりSpring Data for Apache Geodeも追加
14. Spring Dataが分かれば簡単に使える
• Apache Geodeだからといって難しい要素はなし
14
@Service
public class UserService {
@Autowired
private UserRepository repository;
public Customer createCustomer(Customer customer) {
Customer newCustomer = repository.save(customer);
return newCustomer;
}
public List<Customer> findAllCustomers() {
return repository.findAll();
}
}
@Region(“User")
public class User {
@Id
private Long id;
private String username;
private String email;
private boolean isActive;
…
}
Service実装(Repository利用クラス)
Repository実装
@Repository
public interface UserRepository
extends CrudRepository<User, Long> {
}
Entity実装
15. Spring Data for Apache Geodeを使うメリット
15
リージョンを都度作らなくて良い
(Entityから自動生成してくれる)
Apache Geode特有の多くの面倒な設定をやってくれる
バックエンドのデータストアを意識する必要がない
使い方がSpringユーザーに馴染む
16. Spring Data for Apache Geodeアーキテクチャー
• Apache GeodeはAPPと同じプロセスのメモリーを使用してデータを管理する
• Spring Data for Apache GeodeはGeodeへのアクセス方法を抽象化する
• 他の構成も取れるけど、ほとんどはこの構成で良い
16
プロセス
APP
Spring Data for Apache Geode
プロセス
APP
RDB
Spring Data JPA
RDBの場合 Spring Data for Apache Geodeの場合
17. 参考:Spring Data for Apache Geode アプリケーションタイプ
• アノテーションを指定してApache Geodeのトポロジーを決定する
17
プロセス
APP
Spring Data for Apache Geode
@PeerCacheApplication
プロセス
APP
Spring Data for Apache Geode
@CacheServerApplication
プロセス
APP
Spring Data for Apache Geode
@ClientCacheApplication
外部からのアクセス不可 外部からのアクセス可能
@SpringBootApplication
@PeerCacheApplication
public class GeodeApplication {
…
}
@SpringBootApplication
@CacheServerApplication
public class GeodeApplication {
…
}
@SpringBootApplication
@ClientCacheApplication
public class GeodeApplication {
…
}
外部からのアクセス可能
24. データの取得・登録:Repositoryの作成(3)
• 自動実装されるメソッドの命名ルールについてもSpring Dataと同じ
24
Keyword Sample Logical result
GreaterThan findByAgeGreaterThan(int age) x.age > $1
GreaterThanEqual findByAgeGreaterThanEqual(int age) x.age >= $1
LessThan findByAgeLessThan(int age) x.age < $1
LessThanEqual findByAgeLessThanEqual(int age) x.age ⇐ $1
IsNotNull, NotNull findByFirstnameNotNull() x.firstname =! NULL
IsNull, Null findByFirstnameNull() x.firstname = NULL
In findByFirstnameIn(Collection<String> x) x.firstname IN SET $1
NotIn findByFirstnameNotIn(Collection<String> x) x.firstname NOT IN SET $1
IgnoreCase findByFirstnameIgnoreCase(String firstName) x.firstname.equalsIgnoreCase($1)
(No keyword) findByFirstname(String name) x.firstname = $1
Like findByFirstnameLike(String name) x.firstname LIKE $1
Not findByFirstnameNot(String name) x.firstname != $1
IsTrue, True findByActiveIsTrue() x.active = true
IsFalse, False findByActiveIsFalse() x.active = false
25. データの取得・登録:キー以外で検索したい場合は?(1)
• Apache Geodeはjava.uitl.Mapベースのため、キー以外での検索が通常は出来ない
• キー以外で検索する場合はSQLライクなOQL(Object Query Language)を使用する
• @QueryへOQLを設定すると、そのOQLが実行される
25
@Repository
public interface UserRepository
extends CrudRepository<User, Long> {
@Query("SELECT * FROM /User u WHERE u.username IN SET $1")
Collection<User> findByUsernamesAnnotated(Collection<String> usernames);
@Query("SELECT * FROM /User u WHERE u.email like $1")
Collection<User> findByEmailAnnotated(String email);
}
26. データの取得・登録:キー以外で検索したい場合は?(2)
• OQLでの特徴的な箇所は以下の通り
26
種別 例 特徴
リージョン名 SELECT * FROM /exampleRegion 先頭に”/”が必要
IN SELECT * FROM /exampleRegion portfolio WHERE portfolio.ID IN SET(1, 2) in set
Order by制約 SELECT DISTINCT * FROM /exampleRegion WHERE ID < 101 ORDER BY ID distinct必須
Mapへのアクセス
SELECT p.get('account') FROM /exampleRegion p
SELECT p['account‘] FROM /exampleRegion p
get使用
Arrayへのアクセス SELECT * FROM /exampleRegion p WHERE p.names[0] = 'aaa' 添え字使用
ネストしたCollectionへの
アクセス
SELECT p, pos FROM /exampleRegion p, p.positions.values pos WHERE pos.secId = 'VMW' FROMに複数指定
リージョンKeyへのアクセス SELECT * FROM /exampleRegion.keys k WHERE k.ID = 1 FROMにkeys指定
リージョンValueへのアクセス SELECT DISTINCT entry.value FROM /exampleRegion.entries entry WHERE entry.key = '1'
データ集合の比較 SELECT * FROM /exampleRegion WHERE sp = set('20', '21', '22')
Javaメソッドの使用
SELECT * FROM /exampleRegion p WHERE p.length > 1
SELECT DISTINCT * FROM /exampleRegion p WHERE p.positions.size >= 2
SELECT DISTINCT * FROM /exampleRegion p WHERE p.positions.isEmpty
SELECT DISTINCT * FROM /exampleRegion p WHERE p.name.startsWith('Bo')