4

I'm trying to figure out how to handle my data structure within Redis. What I am trying to accomplish is how I can count events with two parameters, and then query Redis for that data by date. Here's an example: events come in with two different parameters, let's call them site and event type, and also with the time that event occurred. From there, I will need to be able to query Redis for how many events occurred over a span of dates, grouped together by site and event type.

Here's a brief example data set:

Oct 3, 2012:
   site A / event A
   site A / event A
   Site B / event A

Oct 4, 2012:
   site B / event B
   site A / event A
   Site B / event A

... and so on.

In my query I would like to know the total number of events over the date span, which will be a span of five weeks. In the example above, this would be something like:

   site A / event A ==> 3 events
   site B / event A ==> 2 events
   site B / event B ==> 1 event

I have looked at using Redis' Sorted Set feature, Hashes, and so on. It seems Sorted Set is the best way to do it, but querying the data with Redis' ZUNIONSTORE command seems like not such a great fit because these events will span five weeks. That makes for at least 35 arguments to the ZUNIONSTORE command.

Any hints, thoughts, ideas, etc?

Thanks so much for your help.

4

1 に答える 1

5

典型的な RDBMS や MongoDB とは対照的に、Redis には豊富なクエリ言語がありません。このようなストアでは、生データをストアに蓄積し、クエリを使用して統計を計算できます。Redis はこのモデルに適合していません。

Redis では、統計をオンザフライで計算し、生データの代わりに直接保存することになっています。

たとえば、ある範囲の週の統計にのみ関心があると仮定すると、次のようにデータを構造化します。

  • すべての基準が個別であるため、zsets の代わりに単純なハッシュ オブジェクトを使用できます。

  • 週に 1 つのハッシュ オブジェクト

  • 各ハッシュ オブジェクトで、カップル サイト、イベントごとに 1 つのカウンター。オプションで、サイトごとに 1 つのカウンター、および/またはイベントごとに 1 つのカウンター。

したがって、イベントが発生すると、次のコマンドを Redis にパイプラインします。

hincrby W32 site_A:event_A 1 
hincrby W32 site_A:* 1 
hincrby W32 *:event_A 1 

これらのカウンターを初期化する必要はないことに注意してください。それらが存在しない場合、HINCRBY はそれら (およびハッシュ オブジェクト) を作成します。

1 週間の統計を取得するには:

hgetall W32

統計には、サイト/イベントごと、サイトごとのみ、イベントごとのみのカウンターがあります。

数週間の統計を取得するには、次のコマンドをパイプライン処理します。

hgetall W32
hgetall W33
hgetall W34
hgetall W35
hgetall W36

クライアント側で集計を実行します(言語がマップ、辞書などの連想配列をサポートしている場合は非常に簡単です...)。

于 2012-10-13T08:51:26.393 に答える