# Not add field if jdbc\_static plugin lookup fail

**URL:** <https://discuss.elastic.co/t/not-add-field-if-jdbc-static-plugin-lookup-fail/184081>\
**Category:** Logstash\
**Created:** [June 4, 2019, 3:15am UTC](https://discuss.elastic.co/t/not-add-field-if-jdbc-static-plugin-lookup-fail/184081 "2019-06-04T03:15:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Izek](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/izek/32/37113_2.png) [@Izek](https://discuss.elastic.co/u/Izek)\
**Post date:** [June 4, 2019, 3:15am UTC](https://discuss.elastic.co/t/not-add-field-if-jdbc-static-plugin-lookup-fail/184081/1 "2019-06-04T03:15:37Z")

</div>

I have a jdbc\_static config as below.  
How can I not add field if lookup failed or don't have matched record.  
Can I specify if else statement for add fields?

```json
  jdbc_static {
    loaders => [
      {
        id => "host_info"
        query => "SELECT [Name],[ShortDescription],[Description],[OwnerName],[Site],[Model] ,[Status] FROM [MonitoringPortal].[dbo].[tb_RemedyServerListing] WITH (nolock)"
        local_table => "host_info"
      }
    ]
    local_db_objects => [
      {
        name => "host_info"
        index_columns => ["Name"]
        columns => [
          ["Name", "varchar(50)"],
          ["ShortDescription", "varchar(256)"],
          ["Description", "varchar(256)"],
          ["OwnerName", "varchar(256)"],
          ["Site", "varchar(50)"],
          ["Model", "varchar(50)"],
          ["Status", "varchar(50)"]
        ]
      }
    ]
    local_lookups => [
      {
        query => "select * from host_info WHERE UPPER(Name) LIKE UPPER(:host)"
        parameters => { host => "%{[monitor][host]}" }
        target => "hostname"
      }
    ]
    # using add_field here to add & rename values to the event root
    add_field => { "[sd][name]" => "%{[hostname][0][name]}"}
    add_field => { "[sd][shortdescription]" => "%{[hostname][0][shortdescription]}"}
    add_field => { "[sd][Description]" => "%{[hostname][0][description]}"}
    add_field => { "[sd][ownername]" => "%{[hostname][0][ownername]}"}
    add_field => { "[sd][site]" => "%{[hostname][0][site]}"}
    add_field => { "[sd][model]" => "%{[hostname][0][model]}"}
    add_field => { "[sd][status]" => "%{[hostname][0][status]}"}

    remove_field => ["hostname"]
    staging_directory => "/tmp/import_data"
    loader_schedule => "5 * * * *" # run loaders every 2 hours
    jdbc_driver_class => "com.microsoft.sqlserver.jdbc.SQLServerDriver"
    jdbc_driver_library => "mssql-jdbc-7.0.0.jre8.jar"

```

Empty result:

```json
    "sd": {
      "Description": "%{[hostname][0][description]}",
      "site": "%{[hostname][0][site]}",
      "shortdescription": "%{[hostname][0][shortdescription]}",
      "model": "%{[hostname][0][model]}",
      "ownername": "%{[hostname][0][ownername]}",
      "name": "%{[hostname][0][name]}",
      "status": "%{[hostname][0][status]}"
    }

```

---

<div class="post-metadata">

**Author:** ![pastechecker](https://avatars.discourse-cdn.com/v4/letter/p/0ea827/32.png) [@pastechecker](https://discuss.elastic.co/u/pastechecker)\
**Post date:** [June 4, 2019, 9:15am UTC](https://discuss.elastic.co/t/not-add-field-if-jdbc-static-plugin-lookup-fail/184081/2 "2019-06-04T09:15:10Z")

</div>

Maybe you can choose one of those examples to process your mutate section if field exists or contain a certain value:

```
if [sd]{
#enters here if the sd contains data
}

if [sd][name]{
#enters if the sd.name exists
}

if [sd][name] and [sd][site] or [sd][model]{
#enters there if two objects exists and one optional
}

if [sd][name] =~ /yourvalue/{
#enters if sd.name contains yourvalue
}
```

---

<div class="post-metadata">

**Author:** ![Izek](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/izek/32/37113_2.png) [@Izek](https://discuss.elastic.co/u/Izek)\
**Post date:** [June 5, 2019, 2:33am UTC](https://discuss.elastic.co/t/not-add-field-if-jdbc-static-plugin-lookup-fail/184081/3 "2019-06-05T02:33:55Z")

</div>

Seems like you cannot use if statement for add\_field inside jdbc\_static filter plugin

---

<div class="post-metadata">

**Author:** ![pastechecker](https://avatars.discourse-cdn.com/v4/letter/p/0ea827/32.png) [@pastechecker](https://discuss.elastic.co/u/pastechecker)\
**Post date:** [June 5, 2019, 10:55am UTC](https://discuss.elastic.co/t/not-add-field-if-jdbc-static-plugin-lookup-fail/184081/4 "2019-06-05T10:55:10Z")

</div>

I was aiming to match those fields outside of the plugin.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 3, 2019, 10:55am UTC](https://discuss.elastic.co/t/not-add-field-if-jdbc-static-plugin-lookup-fail/184081/5 "2019-07-03T10:55:54Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
