メインコンテンツまでスキップ

3. Quickstart 따라하기

키스페이스 생성

test라는 이름의 키스페이스를 만든다.

CREATE KEYSPACE IF NOT EXISTS test WITH replication = {'class': 'NetworkTopologyStrategy', 'DC1': 1};

기본 테이블과 UDT생성 및 데이터 삽입

타입 생성 (User Define Type : 일명 UDT를 생성한다. 이것은 테이블에 종속된 별도의 오브젝트이다.)

create type user_type (
first text,
last text
);

테이블 생성
위에서 생성한 user_type이라는 UDT를 useranme이라는 필드명으로서 포함 시켰다.

UDT타입은 frozen<UDT> 식으로 작성한다.  
CREATE TABLE IF NOT EXISTS test.docs
(
uid int,
username frozen<user_type>,
login text,
PRIMARY KEY (uid)
);

이렇게 2개의 데이터를 를 넣어준다.


INSERT INTO test.docs (uid, username, login)
VALUES (1, {first:'vince',last:'royer'}, 'vroyer');

INSERT INTO test.docs (uid, username, login)
VALUES (2, {first:'barthelemy',last:'delemotte'}, 'barth');

Elasticsearch를 통한 인덱싱

put메서드로 http://localhost:9200/test 이 뜻은 test라는 키 스페이스의 docs라는 테이블의 모든것을 다 찾아서 인덱싱 하라 라는 뜻인듯

{
"mappings":{
"docs":{
"discover":".*"
}
}
}

결과로 이렇게 와야한다.

{
"acknowledged": true,
"shards_acknowledged": true,
"index": "test"
}

CQL내부에서 Elasticsearch 검색을 하기위한 밑작업(매우중요)

CQL 드라이버를 통해 문서를 검색하기 위해서는 테이블 스키마에 다음 두 가짜(dummy) 열을 추가해야 합니다. 그런 다음 Elasticsearch 중첩 쿼리를 실행하면 됩니다. 이 가짜 열들은 인덱스 이름이 키스페이스 이름과 일치하지 않을 때 타겟 인덱스를 지정할 수 있도록 해줍니다.

ALTER TABLE test.docs ADD es_query text;
ALTER TABLE test.docs ADD es_options text;

위의 두 열을 추가하는것은 아주아주 중요하다.
그래야만 Elassandra를 사용할 수 있게 된다.


CQL로 Elasticsearch검색을 !!

그다음 이렇게

SELECT uid, login, username
FROM test.docs
WHERE es_query = '{ "query":{"nested":{"path":"username","query":{"term":{"username.first":"barthelemy"}}}}}'
AND es_options = 'indices=test' ALLOW FILTERING;
  • where 절의 es_query가 핵심 포인트다.
  • es_query의 내용은 json이며, json으로 펼쳐보면 알아보기 쉽다
  • elasticsearch에서 검색하는 쿼리문과 같다.
  • 예제는 UDT쪽의 완전일치 검색을 했지만, keyword 식으로 like검색도 가능하다

Restapi로 키스페이스 확인

test 키스페이스 에 대해서 확인해보자

Get메서드로
http://localhost:9200/test/_search?pretty
(결과 생략)

cql로도 확인하자

cqlsh> desc KEYSPACE test;
CREATE TABLE test.docs (
uid int PRIMARY KEY,
es_options text,
es_query text,
login text,
username frozen<user_type>
) WITH bloom_filter_fp_chance = 0.01
AND caching = {'keys': 'ALL', 'rows_per_partition': 'NONE'}
AND comment = ''
AND compaction = {'class': 'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy', 'max_threshold': '32', 'min_threshold': '4'}
AND compression = {'chunk_length_in_kb': '64', 'class': 'org.apache.cassandra.io.compress.LZ4Compressor'}
AND crc_check_chance = 1.0
AND dclocal_read_repair_chance = 0.1
AND default_time_to_live = 0
AND gc_grace_seconds = 864000
AND max_index_interval = 2048
AND memtable_flush_period_in_ms = 0
AND min_index_interval = 128
AND read_repair_chance = 0.0
AND speculative_retry = '99PERCENTILE';
CREATE CUSTOM INDEX elastic_docs_idx ON test.docs () USING 'org.elassandra.index.ExtendedElasticSecondaryIndex';

엘라스틱서치로 키스페이스와 테이블 생성

엘라스틱 서치로 키스페이스와 테이블을 동시에 생성하는것도 가능하다.
이부분은 사용할 일이 없을것 같으므로 넘어가자. 그냥 된다 정도로만 알자.

put 메서드로 http://localhost:9200/test2

{
"mappings":{
"docs":{
"properties": {
"first": {
"type":"text"
},
"last": {
"type":"text",
"cql_collection":"singleton"
}
}
}
}
}

이것도 마찬가지로 이렇게 확인 가능하다 Get 메서드로 http://localhost:9200/test2/_search?pretty 인덱싱은 아직 안했으니 인덱싱 정보는 안나오는것 같다.

이렇게 자동으로 생성된 놈을 확인하자

CREATE TABLE test2.docs (
"_id" text PRIMARY KEY,
first list<text>,
last text
) WITH bloom_filter_fp_chance = 0.01
AND caching = {'keys': 'ALL', 'rows_per_partition': 'NONE'}
AND comment = ''
AND compaction = {'class': 'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy', 'max_threshold': '32', 'min_threshold': '4'}
AND compression = {'chunk_length_in_kb': '64', 'class': 'org.apache.cassandra.io.compress.LZ4Compressor'}
AND crc_check_chance = 1.0
AND dclocal_read_repair_chance = 0.1
AND default_time_to_live = 0
AND gc_grace_seconds = 864000
AND max_index_interval = 2048
AND memtable_flush_period_in_ms = 0
AND min_index_interval = 128
AND read_repair_chance = 0.0
AND speculative_retry = '99PERCENTILE';
CREATE CUSTOM INDEX elastic_docs_idx ON test2.docs () USING 'org.elassandra.index.ExtendedElasticSecondaryIndex';