You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@zookeeper.apache.org by Yuhan Zhang <yz...@onescreen.com> on 2011/11/15 04:10:23 UTC

how to set the starting value of the created sequence?

Hi all,

I'd like to create a distributed unique int key with zookeeper sequential
by:
     String file = zooKeeper.create( folderPath + "/0", new byte[0],
Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
     String sequence = file.substring(file.lastIndexOf("/")+1);
zooKeeper.delete(file, 0);
     int id = Integer.parseInt( sequence );

It works fine, even though the sequence number may not be adjacent.
However, I'd like to have the key to start at a certain value,
say 1000, as the ids under 1000 has been in use. Currently I will have to
call this function until it generates an id greater than 1000. Is there a
way in zooKeeper to set the starting value of the created sequence?

Thank you.

Yuhan

Re: how to set the starting value of the created sequence?

Posted by Patrick Hunt <ph...@apache.org>.
How about using something like this instead?
http://svn.apache.org/viewvc/incubator/flume/trunk/flume-core/src/main/java/com/cloudera/flume/master/ZooKeeperCounter.java?view=markup

A second option would be to store the offset as the data for the znode
(or in the parent), etc...

Patrick

On Mon, Nov 14, 2011 at 7:29 PM, Camille Fournier <ca...@apache.org> wrote:
> As far as I know this is based on information stored by the server in
> the parent node, and is not settable by the user.
>
> On Mon, Nov 14, 2011 at 10:10 PM, Yuhan Zhang <yz...@onescreen.com> wrote:
>> Hi all,
>>
>> I'd like to create a distributed unique int key with zookeeper sequential
>> by:
>>     String file = zooKeeper.create( folderPath + "/0", new byte[0],
>> Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
>>     String sequence = file.substring(file.lastIndexOf("/")+1);
>> zooKeeper.delete(file, 0);
>>     int id = Integer.parseInt( sequence );
>>
>> It works fine, even though the sequence number may not be adjacent.
>> However, I'd like to have the key to start at a certain value,
>> say 1000, as the ids under 1000 has been in use. Currently I will have to
>> call this function until it generates an id greater than 1000. Is there a
>> way in zooKeeper to set the starting value of the created sequence?
>>
>> Thank you.
>>
>> Yuhan
>>
>

Re: how to set the starting value of the created sequence?

Posted by Camille Fournier <ca...@apache.org>.
As far as I know this is based on information stored by the server in
the parent node, and is not settable by the user.

On Mon, Nov 14, 2011 at 10:10 PM, Yuhan Zhang <yz...@onescreen.com> wrote:
> Hi all,
>
> I'd like to create a distributed unique int key with zookeeper sequential
> by:
>     String file = zooKeeper.create( folderPath + "/0", new byte[0],
> Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
>     String sequence = file.substring(file.lastIndexOf("/")+1);
> zooKeeper.delete(file, 0);
>     int id = Integer.parseInt( sequence );
>
> It works fine, even though the sequence number may not be adjacent.
> However, I'd like to have the key to start at a certain value,
> say 1000, as the ids under 1000 has been in use. Currently I will have to
> call this function until it generates an id greater than 1000. Is there a
> way in zooKeeper to set the starting value of the created sequence?
>
> Thank you.
>
> Yuhan
>