Ingesting Data into CDAP using Apache Flume

Source Code Repository: Source code (and other resources) for this guide are available at the CDAP Guides GitHub repository.

Ingesting realtime log data into Hadoop for analysis is a common use case which can be solved with Apache Flume. In this guide, you will learn how to ingest data into CDAP with Apache Flume and process it in realtime.

What You Will Build

You will build a CDAP application that uses web logs aggregated by Flume to find page view counts. You will:

  • Configure Flume to ingest data into a CDAP Stream;
  • Build a realtime Flow to process the ingested web logs; and
  • Build a Service to serve the analysis results via HTTP.

Let’s Build It!

The following sections will guide you through configuring and running Flume, and implementing an application from scratch. If you want to deploy and run the application right away, you can clone the sources from this GitHub repository. In that case, feel free to skip the following two sections and jump directly to the Build and Run Application section.

Application Design

Web logs are aggregated using Flume which pushes the data to a webLogs Stream using a special StreamSink from the cdap-ingest library. Then, logs are processed in realtime with a Flow that consumes data from the webLogs Stream and persists the computation results in a pageViews Dataset. The WebLogAnalyticsService makes the computation results stored in the pageViews Dataset accessible via HTTP.


First, we will build the app, then deploy the app and start it. Once it is ready to accept and process the data, we will configure Flume to push data into the stream in realtime.

Application Implementation

The recommended way to build a CDAP application from scratch is to use a maven project. Use this directory structure:


WebLogAnalyticsApplication declares that the application has a Stream, a Flow, a Service and uses a Dataset:

public class WebLogAnalyticsApplication extends AbstractApplication {

  public void configure() {
    addStream(new Stream("webLogs"));
    createDataset("pageViewTable", KeyValueTable.class);
    addFlow(new WebLogAnalyticsFlow());
    addService("WebLogAnalyticsService", new WebLogAnalyticsHandler());

The WebLogAnalyticsFlow makes use of the PageViewCounterFlowlet:

public class WebLogAnalyticsFlow extends AbstractFlow {

  public void configure() {
    setDescription("A flow that collects and performs web log analysis");
    addFlowlet("pageViewCounter", new PageViewCounterFlowlet());
    connectStream("webLogs", "pageViewCounter");

The PageViewCounterFlowlet receives the log events from the webLogs Stream. It parses the log event and extracts the requested page URL from the log event. Then it increments respective counter in the pageViewTable Dataset:

public class PageViewCounterFlowlet extends AbstractFlowlet {
  private static final Logger LOG = LoggerFactory.getLogger(PageViewCounterFlowlet.class);
  private static final Pattern ACCESS_LOG_PATTERN = Pattern.compile(
    //   IP       id    user      date          request     code     size    referrer    user agent
    "^([\\d.]+) (\\S+) (\\S+) \\[([^\\]]+)\\] \"([^\"]+)\" (\\d{3}) (\\d+) \"([^\"]+)\" \"([^\"]+)\"");
  private static final Pattern REQUEST_PAGE_PATTERN = Pattern.compile("(\\S+)\\s(\\S+).*");

  private KeyValueTable pageViewTable;

  public void process(StreamEvent log) {
    String event = Charsets.UTF_8.decode(log.getBody()).toString();
    Matcher logMatcher = ACCESS_LOG_PATTERN.matcher(event);
    if (!logMatcher.matches() || logMatcher.groupCount() < 8) {"Invalid event received {}", log);
    String request =;
    Matcher requestMatcher = REQUEST_PAGE_PATTERN.matcher(request);
    if (!requestMatcher.matches() || requestMatcher.groupCount() < 2) {"Invalid event received {}", log);
    String uri =;
    pageViewTable.increment(Bytes.toBytes(uri), 1L);

For example, given the following event: - - [14/Jan/2014:08:40:43 -0400] "GET HTTP/1.0" 200 809 "" "example v4.10.5 ("

the extracted requested page URL is This will be used as a counter key in the pageViewTable Dataset.

WebLogAnalyticsHandler returns a map of the webpage and its page-views counts for an HTTP GET request at /views:

public class WebLogAnalyticsHandler extends AbstractHttpServiceHandler {
  private KeyValueTable pageViewTable;

  public void getViews(HttpServiceRequest request, HttpServiceResponder responder) {
    Iterator<KeyValue<byte[], byte[]>> pageViewScan = pageViewTable.scan(null, null);
    Map<String, Long> pageViews = Maps.newHashMap();
    while (pageViewScan.hasNext()) {
     KeyValue<byte[], byte[]> uri =;
     pageViews.put(new String(uri.getKey()), Bytes.toLong(uri.getValue()));
    responder.sendString(200, pageViews.toString(), Charsets.UTF_8);

Build and Run Application

The WebLogAnalyticsApp can be built and packaged using the Apache Maven command:

$ mvn clean package

Note that the remaining commands assume that the script is available on your PATH. If this is not the case, please add it:

$ export PATH=$PATH:<CDAP home>/bin

If you haven't already started a standalone CDAP installation, start it with the command:

$ start

We can then deploy the application to a standalone CDAP installation and start the flow and service:

$ -u localhost:10000/default load artifact target/cdap-flume-guide-<version>.jar
$ -u localhost:10000/default create app WebLogAnalyticsApp cdap-flume-guide <version> user
$ -u localhost:10000/default start flow WebLogAnalyticsApp.WebLogAnalyticsFlow
$ -u localhost:10000/default start service WebLogAnalyticsApp.WebLogAnalyticsService

Once the flow has started, it is ready to receive the web logs from the stream. Now, let’s configure and start Flume to push web logs into the stream.

Ingest Data with Flume

In the provided sources for this guide, you can find an Apache web server’s access.log file that we will use as a source of data. If you have access to live Apache web server’s access logs, you can use them instead.

In order to configure Apache Flume to push web logs to a CDAP Stream, you need to create a simple Flume flow which includes:

  • Flume source that tail access logs;
  • In-memory channel; and
  • Flume sink that sends log lines into the CDAP Stream.

In this example, we will configure the source to tail access.log and sink to send data to the webLogs stream.

Download Flume

  • You can download the Apache Flume distribution at the Apache Flume download.

  • Once downloaded, extract the archive into <flume-base-dir>:

    $ tar -xvf apache-flume-*-bin.tar.gz

Configure Flume Flow

Download the CDAP Flume sink jar into your Flume installation:

$ cd <flume-base-dir>/lib
$ curl --remote-name

The CDAP Flume sink requires a newer version of Guava library than that is usually shipped with Flume. You need to replace the existing Flume Guava library with guava-17.0.jar:

$ cd <flume-base-dir>/lib
$ rm guava-*.jar
$ curl --remote-name

Now, let’s configure the flow by creating the configuration file weblog-analysis.conf at <flume-base-dir>/conf with these contents:

a1.sources = r1
a1.channels = c1
a1.sources.r1.type = exec
a1.sources.r1.command = tail -F <cdap-flume-ingest-guide-basedir>/data/access.log
a1.sources.r1.channels = c1
a1.sinks = k1
a1.sinks.k1.type = co.cask.cdap.flume.StreamSink = c1  =
a1.sinks.k1.namespace = default
a1.sinks.k1.port = 10000
a1.sinks.k1.streamName = webLogs
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100

Change <cdap-flume-ingest-guide-basedir> in the configuration file to point to the <cdap-flume-ingest-guide> directory. Alternatively, you can point it to /tmp/access.log, and create /tmp/access.log with these sample contents: - - [14/Jan/2014:06:51:04 -0400] "GET HTTP/1.1" 200 392 "" "Mozilla/5.0 (compatible; YandexBot/3.0; +" - - [14/Jan/2014:08:03:05 -0400] "GET HTTP/1.1" 404 182 "" "Mozilla/5.0 (compatible; Googlebot/2.1; +" - - [14/Jan/2014:08:03:05 -0400] "GET HTTP/1.1" 200 394 "" "Mozilla/5.0 (compatible; Googlebot/2.1; +" - - [14/Jan/2014:08:40:43 -0400] "GET HTTP/1.0" 404 208 "" "example v4.10.5 (" - - [14/Jan/2014:08:40:43 -0400] "GET HTTP/1.0" 200 809 "" "example v4.10.5 (" - - [14/Jan/2014:08:40:43 -0400] "GET HTTP/1.0" 200 809 "-" "example v4.10.5 ("

Run Flume Flow with Agent

To run a Flume flow, start an agent with the flow’s configuration:

$ cd <flume-base-dir>
$ ./bin/flume-ng agent --conf conf --conf-file conf/weblog-analysis.conf --name a1 -Dflume.root.logger=INFO,console

Once the agent has started, it begins to push data to the CDAP Stream. The CDAP application, started earlier, processes the log events as soon as data is received. Then you can query the computed page views statistics.

Query Results

WebLogAnalyticsService exposes an HTTP endpoint for you to query the results of processing:

$ -u localhost:10000/default call service WebLogAnalyticsApp.WebLogAnalyticsService GET /views

Example output:


Extend This Example

To make this application more useful, you can extend it:

  • Find the top visited pages by maintaining the top pages in a Dataset and updating them from the PageViewCounterFlowlet; and
  • Calculate the bounce ratio of web pages, with batch processing.

Share and Discuss!

Have a question? Discuss at the CDAP User Mailing List.


Copyright © 2014-2015 Cask Data, Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.