kundera icon indicating copy to clipboard operation
kundera copied to clipboard

Issue while using Kundera with counters

Open Gargee opened this issue 13 years ago • 7 comments

Hi, I am trying to increment a counter using kundera. My code looks like this:- public String getCounterValue(String id) { EntityManager em = cKDao.getEntityManager(); int i=0; Counters counter = new Counters(); counter = em.find(Counters.class, id); i=counter.getCounter(); counter.setCounter(i+1); counter.setId(id); em.persist(counter);
counter = em.find(Counters.class, id); return counter.getCounter()+id;

}

I even Tried update statements then i get a different set of errors. My Counter class:- @Entity @Table(name = "Counters",schema = "EPCContent@cassandra_pu") public class Counters { @Id private String id;

@Column
private int counter;



/**
 * @return the id
 */
public String getId()
{
    return id;
}

/**
 * @param id
 *            the id to set
 */
public void setId(String id)
{
    this.id = id;
}

/**
 * @return the counter
 */
public int getCounter()
{
    return counter;
}

/**
 * @param counter
 *            the counter to set
 */
public void setCounter(int counter)
{
    this.counter = counter;
}
/**

} I am getting this error:- 013-10-07 13:30:59,285 [ERROR] [main] com.impetus.client.cassandra.pelops.PelopsClient - Error during persist while executing query INSERT INTO "Counters"("id","counter") VALUES('ch',1) , Caused by: . InvalidRequestException(why:INSERT statement are not allowed on counter tables, use UPDATE instead) at org.apache.cassandra.thrift.Cassandra$execute_cql3_query_result.read(Cassandra.java:37849) at org.apache.thrift.TServiceClient.receiveBase(TServiceClient.java:78) at org.apache.cassandra.thrift.Cassandra$Client.recv_execute_cql3_query(Cassandra.java:1562) at org.apache.cassandra.thrift.Cassandra$Client.execute_cql3_query(Cassandra.java:1547) at com.impetus.client.cassandra.CassandraClientBase.executeCQLQuery(CassandraClientBase.java:1734) at com.impetus.client.cassandra.pelops.PelopsClient.onPersist(PelopsClient.java:461) at com.impetus.kundera.client.ClientBase.persist(ClientBase.java:88) at com.impetus.kundera.lifecycle.states.ManagedState.handleFlush(ManagedState.java:183) at com.impetus.kundera.graph.Node.flush(Node.java:533) at com.impetus.kundera.persistence.PersistenceDelegator.flush(PersistenceDelegator.java:413) at com.impetus.kundera.persistence.PersistenceDelegator.persist(PersistenceDelegator.java:151) at com.impetus.kundera.persistence.EntityManagerImpl.persist(EntityManagerImpl.java:168) at com.expedia.lux.content.dal.PhotoDal.getCounterValue(PhotoDal.java:51) at com.expedia.lux.content.mainClass.KunderaExample.main(KunderaExample.java:17) Exception in thread "main" com.impetus.kundera.KunderaException: com.impetus.kundera.KunderaException: InvalidRequestException(why:INSERT statement are not allowed on counter tables, use UPDATE instead) at com.impetus.kundera.persistence.EntityManagerImpl.persist(EntityManagerImpl.java:174) at com.expedia.lux.content.dal.PhotoDal.getCounterValue(PhotoDal.java:51) at com.expedia.lux.content.mainClass.KunderaExample.main(KunderaExample.java:17) Caused by: com.impetus.kundera.KunderaException: InvalidRequestException(why:INSERT statement are not allowed on counter tables, use UPDATE instead) at com.impetus.client.cassandra.pelops.PelopsClient.onPersist(PelopsClient.java:466) at com.impetus.kundera.client.ClientBase.persist(ClientBase.java:88) at com.impetus.kundera.lifecycle.states.ManagedState.handleFlush(ManagedState.java:183) at com.impetus.kundera.graph.Node.flush(Node.java:533) at com.impetus.kundera.persistence.PersistenceDelegator.flush(PersistenceDelegator.java:413) at com.impetus.kundera.persistence.PersistenceDelegator.persist(PersistenceDelegator.java:151) at com.impetus.kundera.persistence.EntityManagerImpl.persist(EntityManagerImpl.java:168) ... 2 more Caused by: InvalidRequestException(why:INSERT statement are not allowed on counter tables, use UPDATE instead) at org.apache.cassandra.thrift.Cassandra$execute_cql3_query_result.read(Cassandra.java:37849) at org.apache.thrift.TServiceClient.receiveBase(TServiceClient.java:78) at org.apache.cassandra.thrift.Cassandra$Client.recv_execute_cql3_query(Cassandra.java:1562) at org.apache.cassandra.thrift.Cassandra$Client.execute_cql3_query(Cassandra.java:1547) at com.impetus.client.cassandra.CassandraClientBase.executeCQLQuery(CassandraClientBase.java:1734) at com.impetus.client.cassandra.pelops.PelopsClient.onPersist(PelopsClient.java:461) ... 8 more

Gargee avatar Oct 07 '13 20:10 Gargee

Hi,

the exception is ok, you try to persist an already persisted Counters - use merge instead.

Something like this (not tested of course):

public String getCounterValue(String id) { EntityManager em = cKDao.getEntityManager();

Counters counter = em.find(Counters.class, id); if(counter != null) { counter.setCounter(counter.getCounter()); //better introduce a method incrementCounter() em.merge(counter); } else { counter = new Counters(); counter.setCounter(1); //better introduce a constrcutor to set the value em.persist(counter); }

return counter.getCounter(); }

Thomas-S-B avatar Oct 08 '13 04:10 Thomas-S-B

Hi, Cassandra doesn't allow insert INSERT over counter columns. I will try but it looks quite similar to

https://github.com/impetus-opensource/Kundera/blob/trunk/src/kundera-cassandra/src/test/java/com/impetus/client/crud/countercolumns/CountersTest.java

-Vivek

mevivs avatar Oct 08 '13 07:10 mevivs

Hi I tried,Merge,Persist and even Native update nothing is working :( @mevivs i tried to use the code given in the example you have pointed to and couldnot get this working.I am using 2.8 -SNAPSHOT JARS. So my basic question is how do i increment/decrement counters using kundera in cassandra.

Gargee avatar Oct 08 '13 16:10 Gargee

Hi @Gargee, I recommend to use ThriftClientFactory as client.lookup.class for better performance and latest Kundera development. I have fixed this for PelopsClientFactory as well

Thanks Kuldeep

kkmishra avatar Oct 09 '13 09:10 kkmishra

Verified, added a junit for the same.

mevivs avatar Oct 16 '13 12:10 mevivs

I'm trying to use this within a batch but it's not working.

public UsersFollowing create(String truid, String followingTruid) {
    EntityManager em = getEntityManagerFactory(persistenceUnit)
            .createEntityManager();
    em.setFlushMode(FlushModeType.COMMIT);
    em.getTransaction().begin();

    UsersFollowingKey usersFollowingKey = new UsersFollowingKey(truid,
            followingTruid);
    UsersFollowersKey usersFollowersKey = new UsersFollowersKey(
            followingTruid, truid);

    try {
        if (truid != null && followingTruid != null
                && em.find(UsersFollowing.class, usersFollowingKey) == null
                && em.find(UsersFollowers.class, usersFollowersKey) == null) {

            UsersFollowing usersFollowing = new UsersFollowing(
                    usersFollowingKey);
            em.persist(usersFollowing);

            UsersFollowers usersFollowers = new UsersFollowers(
                    usersFollowersKey);
            em.persist(usersFollowers);

            UsersFollowCounts truidUsersFollowCounts = em.find(
                    UsersFollowCounts.class, truid);
            if (truidUsersFollowCounts == null) {
                truidUsersFollowCounts = new UsersFollowCounts(truid, 0, 0);
                em.persist(truidUsersFollowCounts);
                em.clear();
                UsersFollowCounts truidUsersFollowCountsUpdated = em.find(
                        UsersFollowCounts.class, truid);
                truidUsersFollowCountsUpdated.setFollowingCount(truidUsersFollowCountsUpdated.getFollowingCount() + 1);
                em.merge(truidUsersFollowCountsUpdated);
                em.clear();
            } else {
                truidUsersFollowCounts
                        .setFollowingCount(truidUsersFollowCounts
                                .getFollowingCount() + 1);
                em.merge(truidUsersFollowCounts);
            }

            UsersFollowCounts followingTruidUsersFollowCounts = em.find(
                    UsersFollowCounts.class, followingTruid);
            if (followingTruidUsersFollowCounts == null) {
                followingTruidUsersFollowCounts = new UsersFollowCounts(
                        followingTruid, 0, 0);
                em.persist(followingTruidUsersFollowCounts);
                em.clear();
                UsersFollowCounts followingTruidUsersFollowCountsUpdated = em.find(UsersFollowCounts.class, followingTruid);
                followingTruidUsersFollowCountsUpdated.setFollowerCount(followingTruidUsersFollowCountsUpdated.getFollowerCount() + 1);
                em.merge(followingTruidUsersFollowCountsUpdated);                   
                em.clear();
            } else {
                followingTruidUsersFollowCounts
                        .setFollowerCount(followingTruidUsersFollowCounts
                                .getFollowerCount() + 1);
                em.merge(followingTruidUsersFollowCounts);
            }

            em.getTransaction().commit();

            return em.find(UsersFollowing.class, usersFollowingKey);
        }
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        em.getTransaction().rollback();
    } finally {
        em.close();
    }
    return null;
}

15:27:38.707 [RxCachedThreadScheduler-1] INFO n.d.cassandra.pool.ConnectionPool - ConnectionPool initialized. 15:27:38.709 [RxCachedThreadScheduler-1] INFO c.i.k.p.EntityManagerFactoryImpl - EntityManagerFactory created for persistence unit : followers_pu 15:27:38.711 [RxCachedThreadScheduler-1] DEBUG c.i.k.persistence.EntityManagerImpl - Creating EntityManager for persistence unit : followers_pu 15:27:38.716 [RxCachedThreadScheduler-1] INFO c.i.k.persistence.EntityManagerImpl - Returning client instance for persistence unit followers_pu. 15:27:38.716 [RxCachedThreadScheduler-1] DEBUG c.i.k.loader.GenericClientFactory - Returning fresh client instance for persistence unit : followers_pu 15:27:38.728 [RxCachedThreadScheduler-1] DEBUG c.i.k.persistence.EntityManagerImpl - Created EntityManager for persistence unit : followers_pu 15:27:38.733 [RxCachedThreadScheduler-1] INFO c.i.c.cassandra.CassandraClientBase - Executing cql query SELECT * FROM "users_following" WHERE "truid" = '123' AND "following_truid" = '456'. 15:27:38.733 [RxCachedThreadScheduler-1] INFO c.i.c.cassandra.CassandraClientBase - Executing query SELECT * FROM "users_following" WHERE "truid" = '123' AND "following_truid" = '456'. 15:27:38.733 [RxCachedThreadScheduler-1] DEBUG c.i.c.c.thrift.ThriftClientFactory - Returning connection of localhost :9160 . 15:27:38.738 [RxCachedThreadScheduler-1] INFO c.i.c.cassandra.CassandraClientBase - Executing cql query SELECT * FROM "users_followers" WHERE "truid" = '456' AND "followers_truid" = '123'. 15:27:38.738 [RxCachedThreadScheduler-1] INFO c.i.c.cassandra.CassandraClientBase - Executing query SELECT * FROM "users_followers" WHERE "truid" = '456' AND "followers_truid" = '123'. 15:27:38.738 [RxCachedThreadScheduler-1] DEBUG c.i.c.c.thrift.ThriftClientFactory - Returning connection of localhost :9160 . 15:27:38.747 [RxCachedThreadScheduler-1] DEBUG c.i.k.p.PersistenceDelegator - Flushing following flush stack to database(s) (showing stack objects from top to bottom): [[com.thomsonreuters.follow.entity.UsersFollowing$com.thomsonreuters.follow.entity.UsersFollowingKey@5a06825f]com.thomsonreuters.follow.entity.UsersFollowing$com.thomsonreuters.follow.entity.UsersFollowingKey@5a06825f] 15:27:38.747 [RxCachedThreadScheduler-1] DEBUG c.i.k.p.PersistenceDelegator - Data persisted successfully for entity class com.thomsonreuters.follow.entity.UsersFollowing. 15:27:38.747 [RxCachedThreadScheduler-1] DEBUG c.i.k.p.PersistenceDelegator - Flushing following flush stack to database(s) (showing stack objects from top to bottom): [[com.thomsonreuters.follow.entity.UsersFollowers$com.thomsonreuters.follow.entity.UsersFollowersKey@7ca35b39]com.thomsonreuters.follow.entity.UsersFollowers$com.thomsonreuters.follow.entity.UsersFollowersKey@7ca35b39] 15:27:38.747 [RxCachedThreadScheduler-1] DEBUG c.i.k.p.PersistenceDelegator - Data persisted successfully for entity class com.thomsonreuters.follow.entity.UsersFollowers. 15:27:38.747 [RxCachedThreadScheduler-1] INFO c.i.c.cassandra.CassandraClientBase - Executing cql query SELECT * FROM "users_follow_counts" WHERE "truid" = '123'. 15:27:38.747 [RxCachedThreadScheduler-1] INFO c.i.c.cassandra.CassandraClientBase - Executing query SELECT * FROM "users_follow_counts" WHERE "truid" = '123'. 15:27:38.747 [RxCachedThreadScheduler-1] DEBUG c.i.c.c.thrift.ThriftClientFactory - Returning connection of localhost :9160 . 15:27:38.761 [RxCachedThreadScheduler-1] DEBUG c.i.k.persistence.context.CacheBase - Node: com.thomsonreuters.follow.entity.UsersFollowCounts$123:: ADDED TO Persistence Context 15:27:38.761 [RxCachedThreadScheduler-1] DEBUG c.i.k.p.PersistenceDelegator - Flushing following flush stack to database(s) (showing stack objects from top to bottom): [[com.thomsonreuters.follow.entity.UsersFollowCounts$123]com.thomsonreuters.follow.entity.UsersFollowCounts$123] 15:27:38.761 [RxCachedThreadScheduler-1] DEBUG c.i.k.p.PersistenceDelegator - Data persisted successfully for entity class com.thomsonreuters.follow.entity.UsersFollowCounts. 15:27:38.761 [RxCachedThreadScheduler-1] INFO c.i.c.cassandra.CassandraClientBase - Executing cql query SELECT * FROM "users_follow_counts" WHERE "truid" = '123'. 15:27:38.761 [RxCachedThreadScheduler-1] INFO c.i.c.cassandra.CassandraClientBase - Executing query SELECT * FROM "users_follow_counts" WHERE "truid" = '123'. 15:27:38.761 [RxCachedThreadScheduler-1] DEBUG c.i.c.c.thrift.ThriftClientFactory - Returning connection of localhost :9160 . 15:27:38.765 [RxCachedThreadScheduler-1] ERROR c.t.f.dao.impl.UsersFollowingDaoImpl - null java.lang.NullPointerException: null at com.thomsonreuters.follow.dao.impl.UsersFollowingDaoImpl.create(UsersFollowingDaoImpl.java:55) ~[bin/:na]

ivyblossom avatar Aug 04 '15 19:08 ivyblossom

at com.thomsonreuters.follow.dao.impl.UsersFollowingDaoImpl.create(UsersFollowingDaoImpl.java:55)

is

                truidUsersFollowCountsUpdated.setFollowingCount(truidUsersFollowCountsUpdated.getFollowingCount() + 1);

ivyblossom avatar Aug 04 '15 19:08 ivyblossom