【Flink系列】使用logback

官网对于Flink使用logback的规范:

1. 所有的classpath下不能有org.apache.logging.log4j:log4j-slf4j-impl ,包括flink自己的lib目录以及自定义jar包,检查flink的lib目录,如果有这个jar,把他移除,检查自己的jar,如果有直接和间接依赖,exclude掉

2. ch.qos.logback:logback-core 和 ch.qos.logback:logback-classic,需要提供这两个依赖,如果决定统一使用logback输出日志,建议就把这两个包放在flink的lib目录下


我自己在完成以上两个步骤以后发现依然使用的是log4j,如果这样,将flink的conf目录下的log4j相关配置文件全部移除,只留下logback相关的配置文件,logback配置才会生效。


Flink的conf目录有三个默认的logback配置文件:

logback-session.properties、logback-console.properties、logback.xml,在不同场景下使用,目前我使用的是HadoopYarn资源平台,任务模式为per-job,加载的是logback.xml的配置,参考配置:

<!--
  ~ Licensed to the Apache Software Foundation (ASF) under one
  ~ or more contributor license agreements.  See the NOTICE file
  ~ distributed with this work for additional information
  ~ regarding copyright ownership.  The ASF licenses this file
  ~ to you 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
  ~
  ~     http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~ 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.
  -->

<configuration>
    <appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${log.file}</file>
	<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
          <fileNamePattern>${log.file}.%i</fileNamePattern>
          <minIndex>1</minIndex>
          <maxIndex>10</maxIndex>
        </rollingPolicy>
        <append>false</append>
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
          <maxFileSize>10MB</maxFileSize>
        </triggeringPolicy>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{60} %X{sourceThread} - %msg%n</pattern>
        </encoder>
    </appender>
    <!-- This affects logging for both user code and Flink -->
    <root level="INFO">
        <appender-ref ref="file"/>
    </root>

    <!-- Uncomment this if you want to only change Flink's logging -->
    <!--<logger name="org.apache.flink" level="INFO">-->
        <!--<appender-ref ref="file"/>-->
    <!--</logger>-->

    <!-- The following lines keep the log level of common libraries/connectors on
         log level INFO. The root logger does not override this. You have to manually
         change the log levels here. -->
    <logger name="akka" level="INFO">
        <appender-ref ref="file"/>
    </logger>
    <logger name="org.apache.kafka" level="INFO">
        <appender-ref ref="file"/>
    </logger>
    <logger name="org.apache.hadoop" level="INFO">
        <appender-ref ref="file"/>
    </logger>
    <logger name="org.apache.zookeeper" level="INFO">
        <appender-ref ref="file"/>
    </logger>


    <!-- Suppress the irrelevant (wrong) warnings from the Netty channel handler -->
    <logger name="org.apache.flink.shaded.akka.org.jboss.netty.channel.DefaultChannelPipeline" level="ERROR">
        <appender-ref ref="file"/>
    </logger>
</configuration>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Flink是一个流处理和批处理框架,使用Java编写的Flink应用程序可以实现高效的大规模数据处理。下面是使用Flink Java API的基本步骤: 1. 导入依赖:在您的Java项目中,首先需要添加Flink的依赖项。您可以在Maven或Gradle中添加以下依赖项: ```xml <dependency> <groupId>org.apache.flink</groupId> <artifactId>flink-java</artifactId> <version>1.13.2</version> </dependency> ``` 2. 创建ExecutionEnvironment或StreamExecutionEnvironment:对于批处理任务,使用ExecutionEnvironment;对于流处理任务,使用StreamExecutionEnvironment。例如: ```java ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment(); StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); ``` 3. 读取输入数据:使用相应的数据源读取器从文件、Socket、Kafka等源中读取数据。例如,使用`readTextFile`方法从文本文件中读取数据: ```java DataSet<String> text = env.readTextFile("path/to/input/file.txt"); DataStream<String> text = env.readTextFile("path/to/input/file.txt"); ``` 4. 转换和操作数据:使用各种转换和操作函数对数据进行处理。例如,使用`map`函数对每个元素进行转换: ```java DataSet<Integer> lengths = text.map(line -> line.length()); DataStream<Integer> lengths = text.map(line -> line.length()); ``` 5. 定义计算逻辑:根据需求定义Flink作业的计算逻辑,例如,使用`filter`函数过滤出符合条件的数据: ```java DataSet<Integer> filteredLengths = lengths.filter(len -> len > 10); DataStream<Integer> filteredLengths = lengths.filter(len -> len > 10); ``` 6. 输出结果:使用相应的sink函数将计算结果输出到文件、数据库、Kafka等。例如,使用`writeAsText`函数将结果写入文本文件: ```java filteredLengths.writeAsText("path/to/output/file.txt"); filteredLengths.print(); // 在控制台输出结果 ``` 7. 执行作业:调用`execute`方法执行Flink作业。 ```java env.execute("My Flink Job"); ``` 这只是Flink Java API的基本用法。您可以根据具体需求,使用Flink提供的更多功能和操作函数来构建复杂的数据处理应用程序。希望可以帮助到您!如果有更多问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值