kettle进行正则分析

使用kettle进行日志分析
分析日志是一个大数据分析中较为常见的场景。在Unix类操作系统里,Syslog广泛被应用于系统或者应用的日志记录中。Syslog通常被记录在本地文件内,比如Ubuntu内为/var/log/syslog文件名,也可以被发送给远程Syslog服务器。Syslog日志内一般包括产生日志的时间、主机名、程序模块、进程名、进程ID、严重性和日志内容。具体的日志内容如下所示:
Jun 01 17:29:28 localhost bash[39095]: 10.212.143.73 : root : /root : ls --color=auto /var/log/messages
Jun 01 17:29:30 localhost bash[39132]: 10.212.143.73 : root : /root : vim /var/log/messages
Jun 01 17:29:45 localhost bash[39217]: 10.212.143.73 : root : /root : tail -2 /var/log/messages
Jun 01 17:29:50 localhost bash[39242]: 10.212.143.73 : root : /root : tail -5 /var/log/messages
数据最前面为时间,主机名、进程名,可选的进程ID,用户名,最后是日志内容。
将日志从文件中提取,并使用正则表达式控件获取日志的内容,分别放于时间、主机名等字段中;

遇到的现象和发生背景,请写出第一个错误信息

无法进行正则匹配

img

img

用代码块功能插入代码,请勿粘贴截图。 不用代码块回答率下降 50%

正则表达式:(\w{3} \d{2} [\d:]+) ([\w]+) [\w]+[(\d+)]: ([\d.]+) : (\w+) : /\w+ : (.+)

运行结果及详细报错内容

img

img

img

解答思路和尝试过的方法

我觉得正则表达式应该是没错,应该是文本输入出了错

正则表达式写错了
^(\w{3}\s\d{2}\s\d{2}:\d{2}:\d{2})\s(\S+)\s(\S+)[(\d+)]:\s+(\S+)\s+:\s+(.*)$

你可以拿你的正则表达式和你的数据匹配一下,你会发现你写的无法匹配,业务上你肯定要根据数据调整表达式

该回答引用ChatGPT
根据提供的信息,出现错误的原因可能是因为数据输入的格式和正则表达式不匹配导致解析失败。建议尝试以下操作:

  1. 使用kettle中的“文本文件输入”步骤读取/syslog文件,确认输入的文件格式和编码是否正确。

  2. 在“文本文件输入”步骤中设置正确的分隔符和字段位置,比如使用空格分隔时应该选择“按照正则表达式分隔”,然后设置正则表达式为“\s+”,以确保每个字段被正确划分。

  3. 在上一步中的“字段”选项卡中,设置与正则表达式一致的字段数量和位置。

  4. 在“字段”选项卡中设置字段的类型和格式,确保能够正确解析每个字段。

  5. 在“正则表达式”控件中验证正则表达式是否正确,并且检查输入的文本是否与正则表达式匹配。

  6. 最后,运行作业并查看输出结果是否与预期一致。

以下是可能的解决方法:

  1. 将正则表达式修改为“(\w{3}\s\d{2}\s[\d:]+)\s([\w]+)\s[\w]+[(\d+)]:\s([\d.]+)\s:\s(\w+)\s:\s/\w+\s:\s(.+)”,去掉“[]”中的“+”。

  2. 尝试更改分隔符为“-”或无分隔符,并设置“字段数量”的选项为正确的值。

  3. 尝试检查文本文件的编码格式是否正确,并在“文本文件输入”步骤中设置正确的编码格式。

示例代码如下:

文本文件输入:

正则表达式:(\w{3}\s\d{2}\s[\d:]+)\s([\w]+)\s[\w]+[(\d+)]:\s([\d.]+)\s:\s(\w+)\s:\s/\w+\s:\s(.+)

解析结果输出到excel文件:

参照以下代码


<job>
  <entry>
    <name>job_test</name>
    <description/>
    <type>job</type>
    <configuration>
      <entry>
        <key>job-log-table</key>
        <value/>
        <description/>
      </entry>
      <entry>
        <key>job-log-connection</key>
        <value/>
        <description/>
      </entry>
    </configuration>
    <job-entries>
      <entry>
        <name>get_log_files</name>
        <description/>
        <type>TRANS</type>
        <filename>get_log_files.ktr</filename>
        <entry>          
          <name>ExecutionLogTableStep()</name>
          <description/>
          <type>TRANS</type>
          <filename>ExecutionLogTableStep.ktr</filename>
          <arguments>
            <argument>
              <name>log_table</name>
              <value>${log_table}</value>
              <description/>
              <type>4</type>
            </argument>
            <argument>
              <name>log_connection</name>
              <value>${log_connection}</value>
              <description/>
              <type>4</type>
            </argument>
          </arguments>
          <transformation-name>ExecutionLogTableStep.ktr</transformation-name>
          <transformation-file>ExecutionLogTableStep.ktr</transformation-file>
        </entry>
        <job-entries>
          <entry>
            <name>text_file_input</name>
            <description/>
            <type>FILEINPUT</type>
            <filename>${fileDir}/${fileName}</filename>
            <file>
              <file_required>N</file_required>
              <include_subfolders>N</include_subfolders>
              <type>ALL</type>
              <name mask="${fileName}"/>
              <exclude_mask></exclude_mask>
              <file_field>FILE_NAME</file_field>
              <include>Y</include>
              <recurse>Y</recurse>
            </file>
            <exclude_filemask_regexp></exclude_filemask_regexp>
            <include_filename_regexp></include_filename_regexp>
            <exclude_filename_regexp></exclude_filename_regexp>
            <fields>
              <field>
                <column_name>log_time</column_name>
                <element_type>DATE</element_type>
                <format>yyyy/MM/dd HH:mm:ss.SSS</format>
                <currency></currency>
                <decimal></decimal>
                <group></group>
                <nullif></nullif>
                <ifnull></ifnull>
                <position>1</position>
                <length>-1</length>
                <precision>-1</precision>                
                <trim_type>NONE</trim_type>                
              </field>
              <field>
                <column_name>host_name</column_name>
                <element_type>STRING</element_type>
                <format></format>
                <currency></currency>
                <decimal></decimal>
                <group></group>
                <nullif></nullif>
                <ifnull></ifnull>
                <position>2</position>
                <length>-1</length>
                <precision>-1</precision>
                <trim_type>NONE</trim_type>
              </field>
              <field>
                <column_name>pid</column_name>
                <element_type>INTEGER</element_type>
                <format></format>
                <currency></currency>
                <decimal></decimal>
                <group></group>
                <nullif></nullif>
                <ifnull></ifnull>
                <position>3</position>
                <length>-1</length>
                <precision>-1</precision>
                <trim_type>NONE</trim_type>
              </field>
              <field>
                <column_name>ip_addr</column_name>
                <element_type>STRING</element_type>
                <format></format>
                <currency></currency>
                <decimal></decimal>
                <group></group>
                <nullif></nullif>
                <ifnull></ifnull>
                <position>4</position>
                <length>-1</length>
                <precision>-1</precision>
                <trim_type>NONE</trim_type>
              </field>
              <field>
                <column_name>username</column_name>
                <element_type>STRING</element_type>
                <format></format>
                <currency></currency>
                <decimal></decimal>
                <group></group>
                <nullif></nullif>
                <ifnull></ifnull>
                <position>5</position>
                <length>-1</length>
                <precision>-1</precision>
                <trim_type>NONE</trim_type>
              </field>
              <field>
                <column_name>log_content</column_name>
                <element_type>STRING</element_type>
                <format></format>
                <currency></currency>
                <decimal></decimal>
                <group></group>
                <nullif></nullif>
                <ifnull></ifnull>
                <position>6</position>
                <length>-1</length>
                <precision>-1</precision>
                <trim_type>NONE</trim_type>
              </field>
            </fields>
            <filter>
              <exclude_filenames>N</exclude_filenames>
              <include>N</include>
              <filter_string></filter_string>
              <filterposition>LAST_LINE</filterposition>
              <filterStringOperation>OR</filterStringOperation>
            </filter>
            <additional_output>
              <do_not_create_file>N</do_not_create_file>
            </additional_output>
          </entry>
          <entry>
            <name>update_log_table</name>
            <description/>
            <type>TRANS</type>
            <filename>update_log_table.ktr</filename>
            <arguments>
              <argument>
                <name>log_table</name>
                <value>${log_table}</value>
                <description/>
                <type>4</type>
              </argument>
              <argument>
                <name>log_connection</name>
                <value>${log_connection}</value>
                <description/>
                <type>4</type>
              </argument>
            </arguments>
            <transformation-name>update_log_table</transformation-name>
            <transformation-file>update_log_table.ktr</transformation-file>
          </entry>
        </job-entries>
        <parallel>N</parallel>
        <draw>Y</draw>
        <nr>0</nr>
        <xloc>142</xloc>
        <yloc>30</yloc>
      </entry>
      <entry>
        <name>ExecutionLogTableStep()</name>
        <description/>
        <type>TRANS</type>
        <filename>ExecutionLogTableStep.ktr</filename>
        <arguments>
          <argument>
            <name>log_table</name>
            <value>${log_table}</value>
            <description/>
            <type>4</type>
          </argument>
          <argument>
            <name>log_connection</name>
            <value>${log_connection}</value>
            <description/>
            <type>4</type>
          </argument>
        </arguments>
        <transformation-name>ExecutionLogTableStep.ktr</transformation-name>
        <transformation-file>ExecutionLogTableStep.ktr</transformation-file>
        <parallel>N</parallel>
        <draw>Y</draw>
        <nr>1</nr>
        <xloc>142</xloc>
        <yloc>215</yloc>
      </entry>
      <entry>
        <name>output_to_excel</name>
        <description/>
        <type>TRANS</type>
        <filename>output_to_excel.ktr</filename>
        <arguments>
          <argument>
            <name>output_file</name>
            <value>${output_file}</value>
            <description/>
            <type>4</type>
          </argument>
          <argument>
            <name>excel_sheet_name</name>
            <value>${excel_sheet_name}</value>
            <description/>
            <type>4</type>
          </argument>
        </arguments>
        <transformation-name>output_to_excel</transformation-name>
        <transformation-file>output_to_excel.ktr</transformation-file>
        <parallel>N</parallel>
        <draw>Y</draw>
        <nr>2</nr>
        <xloc>340</xloc>
        <yloc>30</yloc>
      </entry>
    </job-entries>
    <logging>
      <log level="Basic"/>
    </logging>
    <max-log-lines>100000</max-log-lines>
    <max-log-lines-job>10000</max-log-lines-job>
    <created_user/>
    <created_date>2021/4/13 19:12:10</created_date>
    <modified_user/>
    <modified_date>2021/4/13 19:12:10</modified_date>
    <job-version/>
  </entry>
</job>