KMR
wc.reducer.c
Go to the documentation of this file.
1 /** \file wc.reducer.c
2  \brief Example for KMR shell command pipeline. It is a reducer
3  for word count. */
4 
5 #include <stdio.h>
6 #include <string.h>
7 #include <stdlib.h>
8 
9 /** Maximum line length. */
10 #define LINELEN 32767
11 
12 /** Maximum key-value string length. */
13 #define KEYLEN 32767
14 
15 /** Reducer main routine.
16  Read stdin and reduce. Write result to stdout. */
17 
18 int
19 main(int argc, char *argv[])
20 {
21  char line[LINELEN], prevkey[KEYLEN];
22  char *key = NULL;
23  long sum = 0;
24 
25  memset(prevkey, 0, sizeof(prevkey));
26  while (fgets(line, sizeof(line), stdin) != NULL) {
27  char *cp = line;
28  int len = (int)strlen(line);
29  long count;
30 
31  // chomp
32  if (cp[len-1] == '\n') {
33  cp[len-1] = '\0';
34  }
35  key = line;
36  cp = strchr(line, ' ');
37  if (cp == NULL) {
38  // No value field.
39  //printf("skip line\n");
40  continue;
41  }
42  *cp++ = '\0';
43 
44  count = 0;
45  while (cp - line < len) {
46  long val;
47  char *endptr = NULL;
48  val = strtol(cp, &endptr, 10);
49  if (val == 0 && cp == endptr) {
50  // no value field.
51  break;
52  }
53  count += val;
54  cp = endptr;
55  }
56 
57  if (strlen(prevkey) == 0) {
58  // No saved key.
59  memset(prevkey, sizeof(prevkey), 0);
60  // save key.
61  strncpy(prevkey, key, sizeof(prevkey)-1);
62  sum = count;
63  continue;
64  }
65  if (strcmp(prevkey, key) != 0) {
66  // key changed.
67  printf("%s %ld\n", prevkey, sum);
68  memset(prevkey, sizeof(prevkey), 0);
69  // save 'current' key.
70  strncpy(prevkey, key, sizeof(prevkey)-1);
71  sum = count;
72  } else {
73  // continue with same key. Add count.
74  sum += count;
75  }
76  }
77  if (strlen(prevkey) > 0) {
78  printf("%s %ld\n", prevkey, sum);
79  }
80  fflush(stdout);
81  return 0;
82 }
#define KEYLEN
Maximum key-value string length.
Definition: wc.reducer.c:13
int main(int argc, char *argv[])
Reducer main routine.
Definition: wc.reducer.c:19
#define LINELEN
Maximum line length.
Definition: wc.reducer.c:10