离线数仓构建案例一

本文围绕大数据数据采集及离线数仓环境准备展开。介绍了日志数据从文件到Kafka、再同步到Hadoop的hdfs的采集过程,包括flume配置、拦截器使用等;还阐述了业务数据从MySQL到HDFS的同步方案,如全量和增量同步策略及工具;最后提及了hive安装以准备离线数仓环境。

数据采集

日志数据(文件)到Kafka

自己写个程序模拟一些用户的行为数据,这些数据存在一个文件夹中。

接着使用flume监控采集这些文件,然后发送给kafka中待消费。

1、flume采集配置文件

监控文件将数据发给kafka的flume配置文件:


#定义组件
a1.sources = r1
a1.channels = c1

#配置source
a1.sources.r1.type = TAILDIR
a1.sources.r1.filegroups = f1
a1.sources.r1.filegroups.f1 = /opt/module/applog/log/app.*
a1.sources.r1.positionFile = /opt/module/flume/taildir_position.json
a1.sources.r1.interceptors =  i1
a1.sources.r1.interceptors.i1.type = com.atguigu.gmall.flume.interceptor.ETLInterceptor$Builder

#配置channel
a1.channels.c1.type = org.apache.flume.channel.kafka.KafkaChannel
a1.channels.c1.kafka.bootstrap.servers = 192.168.10.100:9092
a1.channels.c1.kafka.topic = topic_log
a1.channels.c1.parseAsFlumeEvent = false

#组装 
a1.sources.r1.channels = c1

a1.sources.r1.channels = c1

这边设置parseAsFlumeEvent = false后,数据就不会以flume的事件event的形式传递,就没有head了,只有body数据,head虽然对这个离线案例有用,但是如果要弄实时数仓,flink也会到kafka中取数据,这时head对于实时的就没用了。所以这边设置成false,也能减少数据传输的大小。

2、拦截器过滤数据

在source和channel之间设置拦截器,做一个轻度的清洗。

编写Flume拦截器

(1)创建Maven工程flume-interceptor

(2)创建包:com.atguigu.gmall.flume.interceptor

(3)在pom.xml文件中添加如下配置

<dependencies>

    <dependency>

        <groupId>org.apache.flume</groupId>

        <artifactId>flume-ng-core</artifactId>

        <version>1.9.0</version>

        <scope>provided</scope>

    </dependency>



    <dependency>

        <groupId>com.alibaba</groupId>

        <artifactId>fastjson</artifactId>

        <version>1.2.62</version>

    </dependency>

</dependencies>



<build>

    <plugins>

        <plugin>

            <artifactId>maven-compiler-plugin</artifactId>

            <version>2.3.2</version>

            <configuration>

                <source>1.8</source>

                <target>1.8</target>

            </configuration>

        </plugin>

        <plugin>

            <artifactId>maven-assembly-plugin</artifactId>

            <configuration>

                <descriptorRefs>

                    <descriptorRef>jar-with-dependencies</descriptorRef>

                </descriptorRefs>

            </configuration>

            <executions>

                <execution>

                    <id>make-assembly</id>

                    <phase>package</phase>

                    <goals>

                        <goal>single</goal>

                    </goals>

                </execution>

            </executions>

        </plugin>

    </plugins>

</build>

(4)在com.atguigu.gmall.flume.utils包下创建JSONUtil类

package com.atguigu.gmall.flume.utils;



import com.alibaba.fastjson.JSONObject;

import com.alibaba.fastjson.JSONException;



public class JSONUtil {

/*

* 通过异常判断是否是json字符串

* 是:返回true  不是:返回false

* */

    public static boolean isJSONValidate(String log){

        try {

            JSONObject.parseObject(log);

            return true;

        }catch (JSONException e){

            return false;

        }

    }

}

(5)在com.atguigu.gmall.flume.interceptor包下创建ETLInterceptor类

package com.atguigu.gmall.flume.interceptor;



import com.atguigu.gmall.flume.utils.JSONUtil;

import org.apache.flume.Context;

import org.apache.flume.Event;

import org.apache.flume.interceptor.Interceptor;





import java.nio.charset.StandardCharsets;

import java.util.Iterator;

import java.util.List;



public class ETLInterceptor implements Interceptor {



    @Override

    public void initialize() {



    }



    @Override

    public Event intercept(Event event) {



//1、获取body当中的数据并转成字符串

        byte[] body = event.getBody();

        String log = new String(body, StandardCharsets.UTF_8);

//2、判断字符串是否是一个合法的json,是:返回当前event;不是:返回null

        if (JSONUtil.isJSONValidate(log)) {

            return event;

        } else {

            return null;

        }

    }



&nbs
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

躺着听Jay

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值