Monday, 5 May 2014

Fluming the User activities in to HBASE/HDFS using AVRO Client.




          Fluming the User activities in to HBASE/HDFS using AVRO Client.



This article is writing with the assumption that you already installed Flume, base and Log4J in your system.

We have a project which will recommend the user based on his activities on the Hosted page in the server.
This project will be written in 4 articles.

In first article we will see how we track the user activities and how we dump in to HDFS or HBASE.
After dumping the data in to hadoop cluster we can do some kind of basket analysis and will project the same to the transaction database for recommendations.

1) Fluming in to HBASE.

Step-1) Log in to Hbase shell -> bin/hbase shell
        Create a table User with activities as column family.

Step-2) Then we need to prepare a flume config (flume-hbaseagent.conf) with Avro as Source and Hbase as sink as below. 

# We have to tell to the hbaseagent what are its sources and sinks.
# hbaseagent which ones we want to activate.
hbaseagent.channels = ch1
hbaseagent.sources = avro-source1
hbaseagent.sinks = sink1

# Define an Avro source called avro-source1 on hbaseagent and tell it
# to bind to 0.0.0.0:41415. Connect it to channel ch1.
hbaseagent.sources.avro-source1.type = avro
hbaseagent.sources.avro-source1.bind = 0.0.0.0
hbaseagent.sources.avro-source1.port = 41415

# Define a memory channel called ch1 on hbaseagent
hbaseagent.channels.ch1.type = memory

#Define a Hbase sink called sink1 on hbaseagent and tell its hbase table and columnFamily

hbaseagent.sinks.sink1.type = org.apache.flume.sink.hbase.HBaseSink
hbaseagent.sinks.sink1.table = user
hbaseagent.sinks.sink1.columnFamily = activites
hbaseagent.sinks.sink1.serializer = org.apache.flume.sink.hbase.SimpleHbaseEventSerializer
hbaseagent.sinks.sink1.serializer.payloadColumn = col1
hbaseagent.sinks.sink1.serializer.keyType = timestamp
hbaseagent.sinks.sink1.serializer.rowPrefix = 1
hbaseagent.sinks.sink1.serializer.suffix = timestamp 

#communicate source and sink with the channel ch1
hbaseagent.sources.avro-source1.channels = ch1

hbaseagent.sinks.sink1.channel = ch1



Step-3) Start Flume with the above config file as:

bin/flume-ng agent --conf ./conf/ -f conf/flume-hbaseagent.conf -Dflume.root.logger=DEBUG,console -n agent1

Above will start the flume and your avro source will start listening at 41415.
If you want to test whether your avro source was listening at 41415.Run the below command then you will see the records of /etc/hosts into your Hbase table.

bin/flume-ng avro-client --conf conf -H localhost -p 41414 -F /etc/hosts  -flume.root.logger=DEBUG,console

Now you can see some records in the user table of Hbase.
Now we will flume the data in to Hbase from a webpage. For this write a java program in eclipse which will log the data of the user activities using LOG4J.

Step-4) Create a project in Eclipse and include all files in the flume lib folder as dependencies.
Create a log4j.propeties file and include this in the SRC folder of your project.

# Define the root logger with appender file
log = /home/vm4learning/WorkSpace/BigData/Log4J-Example/log
log4j.rootLogger = INFO, FILE, flume

# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=${log}/log.out
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n

# Define the flume appender
log4j.appender.flume = org.apache.flume.clients.log4jappender.Log4jAppender
log4j.appender.flume.Hostname = localhost
log4j.appender.flume.Port = 41415
log4j.appender.flume.UnsafeMode = false
log4j.appender.flume.layout=org.apache.log4j.PatternLayout


Here in the config file I am specifying that my java program will write the data in to log.out which was located at  /home/vm4learning/WorkSpace/BigData/Log4J-Example/log.Then this log.out will be taken from AVRO and connect to the flume-hbaseagent.conf which we ran in step-3.

Below is the Java Program.

import java.io.IOException;
import java.sql.SQLException;
import org.apache.log4j.Logger;

public class log4jExample {

    static Logger log = Logger.getRootLogger();
             
    public static void main(String[] args) throws IOException, SQLException {
         for (int i=1; i <=10; i++)
             log.info("Apple is:" + i*2);
     log.info("Samsung is:" +i);
    }
}


This will write the information of user activities like the phones he searched for the 10 times in to the log.out which will be flumed in to the HBASE table User for the activities column family.

Step-5) Scan the table user in Hbase and you will find the java program output as activities.
You even do the same above conf of flume with the source as Tail. Specify the log.out in the tail command in flume, this also automatically flumes the data in to HBASE.
Java Program -> Log.out ->specify in the Flume conf as Tail –F ${PATH}/Log.out in the source command.

hbase-agent.sources = tail
hbase-agent.sinks = sink1
hbase-agent.channels = ch1
hbase-agent.sources.tail.type = exec
hbase-agent.sources.tail.command = tail -F /home/vm4learning/tail.log
hbase-agent.sources.tail.channels = ch1
hbase-agent.sinks.sink1.type = org.apache.flume.sink.hbase.HBaseSink
hbase-agent.sinks.sink1.channel = ch1
hbase-agent.sinks.sink1.table = users
hbase-agent.sinks.sink1.columnFamily = activites
hbase-agent.sinks.sink1.serializer = org.apache.flume.sink.hbase.SimpleHbaseEventSerializer
hbase-agent.sinks.sink1.serializer.payloadColumn = col1
hbase-agent.sinks.sink1.serializer.keyType = timestamp
hbase-agent.sinks.sink1.serializer.rowPrefix = 1
hbase-agent.sinks.sink1.serializer.suffix = timestamp

hbase-agent.channels.ch1.type=memory




In the next article we will see how to sqoop the RDBMS table ITEMS in to HBASE table called ITEMS.And will perform some joins on the USER table with the ITEMS table of HBASE in HIVE with the help of HBASE storage Handlers or joins using Phoenix.

And we will prepare incremental load in sqoop to get the latest changed /inserted in the ITEMS table of RDBMS which was tied to the real time application(Web Page).This Incremental load script will be scheduled to run daily in the OOZIE Job.

Please add some ideas to make this more complex.




No comments:

Post a Comment