-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexample3
executable file
·69 lines (57 loc) · 1.71 KB
/
example3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/awk -f
#
# simple word counting
#
# You'll provide a input file as the first argument to this program:
#
# $ ./example3 input.txt
#
# It will create/update the key "words" in the Redis server, then
# prints the top 20 words. Since it will not remove/delete the key
# "words" from the server, you may additionally aggregates word counts
# from multiple input files simulteneously.
#
@include "redis.awk"
BEGIN {
# ctx = redis_open("redis.hello.org", 6379)
ctx = redis_open() # Use env. variable redis_ENDPOINT or
# the default, "localhost:6379"
RS="\r\n"
}
{
# update the internal word count stored in the array, 'freq'.
for (i = 1; i <= NF; i++)
freq[$i]++
}
END {
nreq = 0
# update the word count in the redis in atomic ways
for (word in freq) {
nreq = redis_pipe(ctx, nreq, 4, "ZINCRBY", "words", freq[word], word)
}
for (i = 0; i < nreq; i++)
redis_resp(ctx, resp)
if (length(N) == 0)
N = 20
else {
N = strtonum(N)
if (N <= 0)
N = 20
}
# Get the list of top N words from the key, 'words'.
redis_command(ctx, resp, 4, "ZREVRANGE", "words", 1, N)
size = length(resp[0])
for (i = 0; i < size; i += 2) {
# result[i][0] will hold the name of the word
result[i / 2][0] = resp[0][i]
}
# Get the score of each word, then update result[i][1] with the count
for (i = 0; i < length(result); i++) {
redis_command(ctx, resp, 3, "ZSCORE", "words", result[i][0])
result[i][1] = resp[0]
}
for (i = 0; i < length(result); i++) {
printf "[%s]\t%d\n", result[i][0], result[i][1]
}
redis_close(ctx)
}