2016-02-24 41 views
0

你好,我試圖從卡桑德拉表中取出10行數據。但是每個請求中同樣有10行正在返回。請在這裏看看我的邏輯。並告訴我哪裏做錯了這裏 -從春季卡桑德拉表中取出行

public class CustomerRequestDaoImpl implements CustomerRequestDao 
{ 
    private static Cluster cluster; 

@Resource 
    private CassandraSessionFactory cassandraSessionFactory; 

    /** The ProductsByTagDaoImpl session. */ 
    private Session session; 

    @Override 
    public List<CustomerRequest> getCustomerRequestData(final String productId, final String receiptPeriod) 
    { 

    final int RESULTS_PER_PAGE = 10; 

    session = cassandraSessionFactory.getSession(); 
    final List<CustomerRequest> customerRequestdata = new ArrayList<CustomerRequest>(); 
    try 
    { 
    final PreparedStatement statement = 
     session.prepare("select * from customer_request where product_id = :id and receipt_period = :receiptPeriod"); 

    final BoundStatement boundStatement = new BoundStatement(statement); 
    boundStatement.setFetchSize(RESULTS_PER_PAGE); 
    boundStatement.setString("id", productId); 
    boundStatement.setString("receiptPeriod", receiptPeriod); 
    final ResultSet resultSet = session.execute(boundStatement); 
    final Iterator<Row> iter = resultSet.iterator(); 
    final PagingState nextPage = resultSet.getExecutionInfo().getPagingState(); 

    int remaining = resultSet.getAvailableWithoutFetching(); 
    for (final Row rowdt : resultSet) 
    { 
     customerRequestdata.add(constructCustomerReq(rowdt)); 
     if (--remaining == 0) 
     { 
     break; 
     } 
     } 

    } 
    catch (final Exception e) 
    { 
     e.printStackTrace(); 
    } 
    return customerRequestdata; 
    } 

@PostConstruct 
    public void init() 
    { 
    session = cassandraSessionFactory.getSession(); 
    cluster = session.getCluster(); 

    } 

}

我的表-1 我的表結構: -

CREATE TABLE customer_request (
    product_id varchar PRIMARY KEY, 
    id varchar, 
    receipt_period varchar, 
    delivery_method_status varchar, 
    first_name varchar 
); 

返回應答爲

<e> 
    <deliveryMethodStatus null="true"/> 
    <firstName null="true"/> 
    <id>0b0352f6b3904</id> 
    <lastName Adkin="true"/> 
    <orderId>FORMS8a04e</orderId> 
    <orderItemId>FORMS8a04e-1</orderItemId> 
    <productId>PI_NAME_CHANGE</productId> 
    <receiptPeriod>2016-02-06</receiptPeriod> 
    <receivedDate null="true"/> 
    <requestData null="true"/> 

+0

您是否擁有二級列的receipt_period上的索引?因爲如果沒有,Cassandra不允許你查詢「where product_id =:id and receipt_period =:receiptPeriod」 – doanduyhai

+0

o是的..我們在收貨期間有二級索引 – JOGO

+0

@ doanduyhai- Sir有沒有更新? – JOGO

回答

0

的幾點意見:

  • 不應該重新準備每一次查詢,這是一個 反模式。準備語句一次,並重新使用它爲每個方法調用

  • 告訴你,因爲你的循環爆發的一次remaining
    變量計數將總是返回的
    數據
    第一頁的源代碼降至0. PagingState對象未使用
    任何地方...

  • 您的問題還不清楚Hello am trying to fetch 10 rows of data from Cassandra table. But on each request same 10 row is returning。你想要哪10行? 第一行 10行? 10行後的閾值?

    注:尋呼

示例代碼編寫下面的查詢只一次select * from customer_request where product_id = :id and receipt_period = :receiptPeriod LIMIT :lim並用方法把它傳遞

@Override 
    public List<Tuple2<String,CustomerRequest>> getCustomerRequestData(final String productId, PreparedStatement ps, final String receiptPeriod, String pagingState) 
    { 

    final int PAGE_SIZE = 10; 

    session = cassandraSessionFactory.getSession(); 
    final List<CustomerRequest> customerRequestdata = new ArrayList<CustomerRequest>(); 
    try 
    { 

    final BoundStatement boundStatement = ps.bind(productId, receiptPeriod, PAGE_SIZE); 
    boundStatement.setPagingState(PagingState.fromString(pagingState)); 
    final ResultSet resultSet = session.execute(boundStatement); 
    final Iterator<Row> iter = resultSet.iterator(); 
    final PagingState nextPage = resultSet.getExecutionInfo().getPagingState(); 

    int remaining = resultSet.getAvailableWithoutFetching(); 
    for (final Row rowdt : resultSet) 
    { 
     customerRequestdata.add(constructCustomerReq(rowdt)); 
     if (--remaining == 0) 
     { 
     break; 
     } 
     } 

    } 
    catch (final Exception e) 
    { 
     e.printStackTrace(); 
    } 
    return new Tuple2<>(nextPage.toString(), customerRe); 

請注意使用Tuple2類返回結果列表以及分頁狀態,序列化爲一個字符串,可輕鬆傳遞到前端

+0

嗨,我同意..我試圖獲取每個請求上的行子集。對於第一次請求,希望在第二次11-20行上檢索1-10行數,第三次請求21-30行。你能否在這個場景中解釋一下我需要做些什麼?我對datastax非常陌生。如果您編輯帖子並提出建議,這將非常有幫助。謝謝你答覆先生。 – JOGO

+0

我是否需要傳遞開始行和結束行作爲參數? – JOGO

+0

爲了幫助你,我需要表格結構。你可以發佈這張表的CREATE TABLE腳本嗎? – doanduyhai