日志系统搭建一(flume+hadoop+hive)

由于公司现在业务日志量逐渐增加,一台服务器io满足不了需求,打在多台服务器上面,准备搭建一个简单日志系统。

许多公司的平台每天会产生大量的日志(一般为流式数据,如,搜索引擎的pv,查询等),处理这些日志需要特定的日志系统,一般而言,这些系统需要具有以下特征:

(1) 构建应用系统和分析系统的桥梁,并将它们之间的关联解耦;

(2) 支持近实时的在线分析系统和类似于Hadoop之类的离线分析系统;

(3) 具有高可扩展性。即:当数据量增加时,可以通过增加节点进行水平扩展。

本文从设计架构,负载均衡,可扩展性和容错性等方面对比了当今开源的日志系统,包括facebook的scribe,apache的chukwa,linkedin的kafka和cloudera的flume等。

下面表格对比了这四个系统:


通过对比分析,选用了flume,主要安装简单,易配置,功能强大。

flume的安装就不说了。

主要遇到一个配置问题,日志要求按照天归类,很多网友都会有这个问题,我解决方法如下:

1.下载flume 1.3.1版本

2.配置文件如下

#Name the components on this agent
a1.sources = r1
a1.sinks = k1
a1.channels = c1
#a1.sources.testtail.interceptors = i1
#a1.sources.testtail.interceptors.i1.type = org.apache.flume.interceptor.TimestampInterceptor$Builder


# Describe/configure the source
a1.sources.r1.type = spooldir
a1.sources.r1.spoolDir = /data_disk/logs/flume/
a1.sources.r1.fileHeader = false
a1.sources.r1.interceptors = i1
a1.sources.r1.interceptors.i1.type = timestamp
#a1.sources.r1.command = tail -F  /data_disk/logs/2014-05-11.txt
#a1.sources.r1.port = 44444


#Describe the sink
#a1.sinks.k1.type = logger
a1.sinks.k1.type = hdfs
a1.sinks.k1.channel = c1
a1.sinks.k1.hdfs.path = hdfs://10.160.0.237:9000/flume/%Y-%m-%d/
a1.sinks.k1.hdfs.filePrefix = update.u.gsie.cn_
a1.sinks.k1.hdfs.fileType = DataStream
a1.sinks.k1.hdfs.rollInterval = 1
#a1.sinks.k1.hdfs.roundValue = 1
a1.sinks.k1.hdfs.writeFormat = Text
#a1.sinks.k1.hdfs.useLocalTimeStamp = true


#Use a channel which buffers events in memory
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100


#Bind the source and sink to the channel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1

3.hadoop安装以及配置

参照网上配置即可

4.hadoop程序编写

cd /data_disk/hadoop_code/

mkdir -p FirstJar/class

cd FirstJar

  vim WordCount,java

  1. package  test ;
  2.        
  3. import  java.io.IOException ;
  4. import  java.util.* ;
  5.        
  6. import  org.apache.hadoop.fs.Path ;
  7. import  org.apache.hadoop.conf.* ;
  8. import  org.apache.hadoop.io.* ;
  9. import  org.apache.hadoop.mapreduce.* ;
  10. import  org.apache.hadoop.mapreduce.lib.input.FileInputFormat ;
  11. import  org.apache.hadoop.mapreduce.lib.input.TextInputFormat ;
  12. import  org.apache.hadoop.mapreduce.lib.output.FileOutputFormat ;
  13. import  org.apache.hadoop.mapreduce.lib.output.TextOutputFormat ;
  14.        
  15. public  class WordCount {
  16.      public  static  class  Map  extends Mapper <LongWritable,  TextText, IntWritable >  {
  17.          private  final  static IntWritable one =  new IntWritable ( 1 ) ;
  18.          private  Text word =  new  Text ( ) ;
  19.        
  20.          public  void map (LongWritable key,  Text value,  Context context )  throws  IOExceptionInterruptedException  {
  21.              String line = value. toString ( ) ;
  22.              StringTokenizer tokenizer =  new  StringTokenizer (line ) ;
  23.              while  (tokenizer. hasMoreTokens ( ) )  {
  24.                 word. set (tokenizer. nextToken ( ) ) ;
  25.                 context. write (word, one ) ;
  26.              }
  27.          }
  28.      }
  29.        
  30.      public  static  class Reduce  extends Reducer < Text, IntWritable,  Text, IntWritable >  {
  31.          public  void reduce ( Text key,  Iterable <IntWritable > values,  Context context )  throws  IOExceptionInterruptedException  {
  32.              int sum =  0 ;
  33.              for  (IntWritable val : values )  {
  34.                 sum += val. get ( ) ;
  35.              }
  36.             context. write (key,  new IntWritable (sum ) ) ;
  37.          }
  38.      }
  39.        
  40.      public  static  void main ( String [ ] args )  throws  Exception  {
  41.          Configuration conf =  new  Configuration ( ) ;
  42.        
  43.         Job job =  new Job (conf,  "wordcount" ) ;
  44.    
  45.         job. setJarByClass (WordCount. class ) ;     // +++++
  46.         job. setOutputKeyClass ( Text. class ) ;
  47.         job. setOutputValueClass (IntWritable. class ) ;
  48.        
  49.         job. setMapperClass ( Map. class ) ;
  50.         job. setReducerClass (Reduce. class ) ;
  51.        
  52.         job. setInputFormatClass (TextInputFormat. class ) ;
  53.         job. setOutputFormatClass (TextOutputFormat. class ) ;
  54.        
  55.         FileInputFormat. addInputPath (job,  new Path (args [ 0 ] ) ) ;
  56.         FileOutputFormat. setOutputPath (job,  new Path (args [ 1 ] ) ) ;
  57.        
  58.         job. waitForCompletion ( true ) ;
  59.      }    
  60. }
    hadoop 输入文件:
vim file01

hello world by world

vim file02

hello hadoop by hadoop

vim input.txt

world 1 2 3 ii pp world hadoop 1 hadoop

hadoop dfs -put input.txt input

hadoop dfs -put file0* input

javac -classpath /data_disk/software/hadoop/hadoop-core-1.2.1.jar -d class WordCount.java

jar -cvf wordcount.jar -C class .

hadoop jar wordcount.jar test.WordCount input output

 hadoop dfs -ls output
Found 3 items
-rw-r--r--   3 root supergroup          0 2014-05-18 17:54 /user/root/output/_SUCCESS
drwxr-xr-x   - root supergroup          0 2014-05-18 17:53 /user/root/output/_logs
-rw-r--r--   3 root supergroup         39 2014-05-18 17:54 /user/root/output/part-r-00000

 hadoop dfs -ls output/part-r-00000

5.hive

利用hive把log数据导入表中,统计计算

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值