2015-12-18 3 views
1

Я использую версию cassandra 2.x и имею требование вставить/обновить одну строку до нескольких таблиц. все таблицы имеют другой первичный ключ. Как добавить несколько назначений для обновления и более одного имени таблицы до QueryBuilder. Использование драйвера datastax. Любой указатель был бы полезен.Обновить строку в нескольких таблицах в Cassandra

+0

Выполняет ли инструкция 'batch' вашу потребность? Если вы не знакомы с пакетными операторами, см. Этот [SO ответ] (http://stackoverflow.com/questions/28348717/how-do-atomic-batches-work-in-cassandra/28348718#28348718) об атомарности в пакетных операторах. – Nathan

+0

Я знаком с Batch, но не знаю, как я могу подготовить BatchStatement с несколькими таблицами. – NewJavaBee

+0

Глядя на это снова, требуется атомарность? В противном случае вы просто создадите несколько подготовленных операторов и выполните их после события. – Nathan

ответ

1

Кто-то еще спросил how to batch update multiple tables в группе google. Они referred to a C# driver example обновления нескольких таблиц. Этот принцип применим к Java, и я скопировал соответствующий метод BatchStatement из cassandra java driver.

Соответствующий код цитировал драйвера Java ниже:

/** 
    * Adds a new statement to this batch. 
    * <p/> 
    * Note that {@code statement} can be any {@code Statement}. It is allowed to mix 
    * {@code RegularStatement} and {@code BoundStatement} in the same 
    * {@code BatchStatement} in particular. Adding another {@code BatchStatement} 
    * is also allowed for convenience and is equivalent to adding all the {@code Statement} 
    * contained in that other {@code BatchStatement}. 
    * <p/> 
    * When adding a {@code BoundStatement}, all of its values must be set, otherwise an 
    * {@code IllegalStateException} will be thrown when submitting the batch statement. 
    * See {@link BoundStatement} for more details, in particular how to handle {@code null} 
    * values. 
    * <p/> 
    * Please note that the options of the added Statement (all those defined directly by the 
    * {@link Statement} class: consistency level, fetch size, tracing, ...) will be ignored 
    * for the purpose of the execution of the Batch. Instead, the options used are the one 
    * of this {@code BatchStatement} object. 
    * 
    * @param statement the new statement to add. 
    * @return this batch statement. 
    * @throws IllegalStateException if adding the new statement means that this 
    *        {@code BatchStatement} has more than 65536 statements (since this is the maximum number 
    *        of statements for a BatchStatement allowed by the underlying protocol). 
    */ 
    public BatchStatement add(Statement statement) { 

     // We handle BatchStatement here (rather than in getIdAndValues) as it make it slightly 
     // easier to avoid endless loop if the use mistakenly pass a batch that depends on this 
     // object (or this directly). 
     if (statement instanceof BatchStatement) { 
      for (Statement subStatements : ((BatchStatement) statement).statements) { 
       add(subStatements); 
      } 
     } else { 
      if (statements.size() >= 0xFFFF) 
       throw new IllegalStateException("Batch statement cannot contain more than " + 0xFFFF + " statements."); 
      statements.add(statement); 
     } 
     return this; 
    } 

Основная идея заключается в создании переменной для каждого обновления таблицы и добавления каждой из переменных функции BatchStatement.

+0

Этот конкретный пример для csharp, я использую java-драйвер. – NewJavaBee

Смежные вопросы