How to share global sequential number generator in Hadoop?

[亡魂溺海] 提交于 2020-01-05 09:49:06

问题


Now I am using Hadoop to process the data that will finally be loaded into the same table. I need to a shared sequential number generator to generate id for each row. Now I am using the following approach to generate the unique number:

1) Create a text file, e.g., test.seq, in HDFS for saving the current sequential number.

2) I use a lock file ".lock" to control concurrency. Suppose we have two tasks to processing the data in parallel. If task1 wants to get the number, it will check if the lock file exists. If yes, it means that task2 is accessing the number from the test.seq, then task1 has to wait. When task2 has acquired the number, it overwrites the old number by increasing 1 when it returns, and deletes the lock file ".lock". When task1 sees the .lock disappear, task1 will firstly create a ".lock" file, then does the same way to get the sequential number.

However, I am not sure if this approach is practical. Because I keep the .lock and test.seq files in the HDFS, even the content of test.seq was changed by task 1, it might not immediately be aware by the task2. As the other tasks get the information about the data in the HDFS is through by namenode. So, the datanode will first notify the change to the namenode, then the other tasks are notified the changes. Is it correct?

Another idea is to create torjan program running on the Master. so, the task get the sequential number is through RPC the Torjan program. But how to run the Torjan program on the master program?

Could anybody give me some advice? thanks!


回答1:


The main problem is that you choose hadoop because of the horizontal scalability properties.
All forms of horizontal scalability suffer greatly when you include something that needs to be coordinated from a central point.

So you have two options:

  1. You accept the scaling limitations and go for the solutions proposed by others. (like the zookeeper option)
  2. You choose a solution that does not require a form of central coordination. At the expense of some properties of the key.

I would try to see if the latter would be enough for your purposes. One such solution could be that you take the id of the current tracker instance and append a local counter value. This way the value is unique and sequentially per tracker and over multiple runs of the same job, but not within the job.




回答2:


You're correct that HDFS wouldn't give you a consistent view of quickly changing data. This approach would also burden your name node with a lot of traffic.

I strongly recommend you put the effort into deploying ZooKeeper. It's built as an independent service but was designed for global state tracking with Hadoop. Great stuff.

To solve your problem, you would create nodes in a directory that would be assigned by ZooKeeper the ascending value. It scales, it's fault tolerant, and all that good stuff.




回答3:


If you only need to have the entries in chronological order, store a timestamp instead of an id.



来源:https://stackoverflow.com/questions/7929485/how-to-share-global-sequential-number-generator-in-hadoop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!