= Grok DataFormat
:doctitle: Grok
:shortname: grok
:artifactid: camel-grok
:description: Unmarshal unstructured data to objects using Logstash based Grok patterns.
:since: 3.0
:supportlevel: Stable
//Manually maintained attributes
:camel-spring-boot-name: grok

*Since Camel {since}*

This component provides dataformat for processing inputs with grok patterns.
Grok patterns are used to process unstructured data into structured objects - `List<Map<String, Object>>`.

This component is based on https://github.com/thekrakken/java-grok[Java Grok library]

Maven users will need to add the following dependency to their `pom.xml`
for this component:

[source,xml]
------------------------------------------------------------
<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-grok</artifactId>
    <version>x.x.x</version>
    <!-- use the same version as your Camel core version -->
</dependency>
------------------------------------------------------------

== Basic usage

Extract all IP adresses from input
[source,java]
--------------------------------------------------------------------------------
from("direct:in")
    .unmarshal().grok("%{IP:ip}")
    .to("log:out");
--------------------------------------------------------------------------------

Parse Apache logs and process only 4xx responses
[source,java]
--------------------------------------------------------------------------------
from("file://apacheLogs")
    .unmarshal().grok("%{COMBINEDAPACHELOG")
    .split(body()).filter(simple("${body[response]} starts with '4'"))
    .to("log:4xx")
--------------------------------------------------------------------------------

== Preregistered patterns

This component comes with preregistered patterns, which are based on Logstash patterns.
All https://github.com/thekrakken/java-grok/tree/master/src/main/resources/patterns/patterns[Java Grok Default Patterns] are preregistered and as such could be used without manual registration.

== Custom patterns

Camel Grok DataFormat supports plugable patterns, which are auto loaded from Camel Registry.
You can register patterns with Java DSL and Spring DSL

Spring DSL:
[source,xml]
--------------------------------------------------------------------------------
<beans>
    <bean id="myCustomPatternBean" class="org.apache.camel.component.grok.GrokPattern">
        <constructor-arg value="FOOBAR"/>
        <constructor-arg value="foo|bar"/>
    </bean>
<beans>
<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
    <route>
        <from uri="direct:in"/>
        <unmarshal>
            <grok pattern="%{FOOBAR:fooBar}"/>
        </unmarshal>
        <to uri="log:out"/>
    </route>
</camelContext>
--------------------------------------------------------------------------------

Java DSL:
[source,java]
--------------------------------------------------------------------------------
public class MyRouteBuilder extends RouteBuilder {

    @Override
    public void configure() throws Exception {
        bindToRegistry("myCustomPatternBean", new GrokPattern("FOOBAR", "foo|bar"));

        from("direct:in")
            .unmarshal().grok("%{FOOBAR:fooBar}")
            .to("log:out");
    }
}
--------------------------------------------------------------------------------


== Grok Dataformat Options

// dataformat options: START
include::partial$dataformat-options.adoc[]
// dataformat options: END


include::spring-boot:partial$starter.adoc[]
