2011-01-13 8 views
3

http://www.riptano.com/blog/whats-new-cassandra-07-secondary-indexes에서 데모를 실행하려고했지만 결과가 CLI에서 실행하는 것과 다릅니다. 카산드라는 색인이 추가 된 후에 만 ​​색인을 생성 할 수 있습니다. 이전의 모든 데이터는 색인화되지 않은 상태로 남습니다.프로그래밍 방식으로 카산드라 0.7에 색인을 추가하는 방법

전체 소스 코드는 다음과 같습니다 : -

public static void main(String[] args) { 
    try { 
     try { 
      transport.open(); 
     } catch (TTransportException ex) { 
      Logger.getLogger(IndexLaterTest.class.getName()).log(Level.SEVERE, null, ex); 
      System.exit(1); 
     } 
     KsDef ksDef = new KsDef(); 
     ksDef.name = KEYSPACE_NAME; 
     ksDef.replication_factor = 1; 
     ksDef.strategy_class = "org.apache.cassandra.locator.SimpleStrategy"; 
     CfDef cfDef = new CfDef(KEYSPACE_NAME, COLUMN_FAMILY_NAME); 
     cfDef.comparator_type = "UTF8Type"; 
     ColumnDef columnDef = new ColumnDef(ByteBuffer.wrap(FULL_NAME.getBytes()), "UTF8Type"); 

     cfDef.addToColumn_metadata(columnDef); 
     ColumnDef columnDef1 = new ColumnDef(ByteBuffer.wrap(BIRTH_DATE.getBytes()), "LongType"); 
     columnDef1.index_type = IndexType.KEYS; 
     cfDef.addToColumn_metadata(columnDef1); 
     ksDef.cf_defs = Arrays.asList(cfDef); 
     try { 
      client.system_add_keyspace(ksDef); 

      client.set_keyspace(KEYSPACE_NAME); 

      ColumnParent columnParent = new ColumnParent(); 
      columnParent.column_family = COLUMN_FAMILY_NAME; 
      Column column = new Column(ByteBuffer.wrap(FULL_NAME.getBytes()), ByteBuffer.wrap("Brandon Sanderson".getBytes()), System.currentTimeMillis()); 
      client.insert(ByteBuffer.wrap("bsanderson".getBytes()), columnParent, column, ConsistencyLevel.ONE); 
      column.name = ByteBuffer.wrap(BIRTH_DATE.getBytes()); 
      column.value = ByteBuffer.allocate(8).putLong(1975); 
      client.insert(ByteBuffer.wrap("bsanderson".getBytes()), columnParent, column, ConsistencyLevel.ONE); 

      column.name = ByteBuffer.wrap(FULL_NAME.getBytes()); 
      column.value = ByteBuffer.wrap("Patrick Rothfuss".getBytes()); 
      client.insert(ByteBuffer.wrap("prothfuss".getBytes()), columnParent, column, ConsistencyLevel.ONE); 
      column.name = ByteBuffer.wrap(BIRTH_DATE.getBytes()); 
      column.value = ByteBuffer.allocate(8).putLong(1973); 
      client.insert(ByteBuffer.wrap("prothfuss".getBytes()), columnParent, column, ConsistencyLevel.ONE); 

      column.name = ByteBuffer.wrap(FULL_NAME.getBytes()); 
      column.value = ByteBuffer.wrap("Howard Tayler".getBytes()); 
      client.insert(ByteBuffer.wrap("htayler".getBytes()), columnParent, column, ConsistencyLevel.ONE); 
      column.name = ByteBuffer.wrap(BIRTH_DATE.getBytes()); 
      column.value = ByteBuffer.allocate(8).putLong(1968); 
      client.insert(ByteBuffer.wrap("htayler".getBytes()), columnParent, column, ConsistencyLevel.ONE); 

      column.name = ByteBuffer.wrap(STATE.getBytes()); 
      column.value = ByteBuffer.wrap("WI".getBytes()); 
      client.insert(ByteBuffer.wrap("prothfuss".getBytes()), columnParent, column, ConsistencyLevel.ONE); 
      column.value = ByteBuffer.wrap("UT".getBytes()); 
      client.insert(ByteBuffer.wrap("htayler".getBytes()), columnParent, column, ConsistencyLevel.ONE); 

      KsDef ks = client.describe_keyspace(KEYSPACE_NAME); 
      cfDef = new CfDef(ks.cf_defs.get(0)); 
      ColumnDef columnDef2 = new ColumnDef(ByteBuffer.wrap(STATE.getBytes()), "UTF8Type"); 
      columnDef2.index_type = IndexType.KEYS; 
      cfDef.setColumn_metadata(Arrays.asList(columnDef, columnDef1, columnDef2)); 

      client.system_update_column_family(cfDef); 
      Thread.sleep(120000);//give cassandra enough time to build the index. 
      client.insert(ByteBuffer.wrap("bsanderson".getBytes()), columnParent, column, ConsistencyLevel.ONE); 

      IndexClause indexClause = new IndexClause(); 
      indexClause.start_key = ByteBuffer.allocate(0); 
      IndexExpression indexExpression = new IndexExpression(); 
      indexExpression.column_name = ByteBuffer.wrap(STATE.getBytes()); 
      indexExpression.value = ByteBuffer.wrap("UT".getBytes()); 
      indexExpression.op = IndexOperator.EQ; 
      indexClause.addToExpressions(indexExpression); 
      SliceRange sliceRange = new SliceRange(); 
      sliceRange.count = 10; 
      sliceRange.start = ByteBuffer.allocate(0); 
      sliceRange.finish = ByteBuffer.allocate(0); 
      sliceRange.reversed = false; 
      SlicePredicate slicePredicate = new SlicePredicate(); 
      slicePredicate.slice_range = sliceRange; 
      List<KeySlice> keys = client.get_indexed_slices(columnParent, indexClause, slicePredicate, ConsistencyLevel.ONE); 
      if (!keys.isEmpty()) { 
       System.out.println("expecting: bsanderson htayler"); 
       System.out.print("actual: "); 
       for (KeySlice key : keys) { 
        System.out.print(new String(key.getKey()) + " "); 
       } 
      } else { 
       System.out.println("failed to find indexed item"); 
      } 
     } catch (Exception ex) { 
      Logger.getLogger(IndexLaterTest.class.getName()).log(Level.SEVERE, null, ex); 
     } finally { 
      try { 
       client.system_drop_keyspace(KEYSPACE_NAME); 
      } catch (Exception ex) { 
       Logger.getLogger(IndexLaterTest.class.getName()).log(Level.SEVERE, null, ex); 
      } 
     } 
    } finally { 
     transport.close(); 
    } 
} 

결과는 다음과 같습니다 : - 인덱스가 구축 될 수 있도록

expecting: bsanderson htayler 
actual: bsanderson 

답변

1

기록 목적으로. 문제는 다음 코드에 있습니다.

ColumnDef columnDef1 = new ColumnDef(ByteBuffer.wrap(BIRTH_DATE.getBytes()), "LongType"); 
columnDef1.index_type = IndexType.KEYS; 

index_type만으로는 충분하지 않습니다. index_name을 설정해야 작동 할 수 있습니다.

0

나는 약간의 시간이 걸릴 않습니다. 이 경우에는 1 초 미만이 소요되지만 쿼리를 작성할 때 완료되지 않았을 수 있습니다.

인덱스를 만든 후 1 ~ 2 초 동안 수면을 시도하고 결과가 변경되는지 확인하십시오.

+0

2 분 동안 시도했습니다. 여전히 같은 결과입니다. 감사. – tanyehzheng

+0

여기 조나단 엘리스 (Jonathan Ellis)의 제안이 귀하의 의견에 반영 되었습니까? http://www.riptano.com/blog/whats-new-cassandra-07-secondary-indexes –

+0

아니요. 모든 getBytes를 getBytes ("UTF-8")로 변경했지만 여전히 작동하지 않습니다. 카산드라 사용자 메일 링리스트에도 요청했지만 한 사람 만 3 일 후에 응답했습니다. – tanyehzheng

관련 문제