【问题标题】:Import data from mysql with json column to elastic search使用 json 列从 mysql 导入数据到弹性搜索
【发布时间】:2018-11-21 20:53:57
【问题描述】:

我在 MySQL 中有一个列,其中一个列中有 json,我必须使用多个键在该列上实现搜索。我尝试使用 log stash 使用 Mysql 创建索引。

这是我的日志存储配置。 Info 是文本形式的文本类型和 json 对的列

input {
  jdbc {
    jdbc_connection_string => "jdbc:mysql://localhost:3306/dbname"
    # The user we wish to execute our statement as
    jdbc_user => "user"
    jdbc_password => "password"
    # The path to our downloaded jdbc driver
    jdbc_driver_library => "/usr/share/java/mysql-connector-java-5.1.38.jar"
    jdbc_driver_class => "com.mysql.jdbc.Driver"
    # our query
    statement => "SELECT info FROM organization"
    }
  }
output {
  stdout { codec => json_lines }
  elasticsearch {
  "hosts" => "localhost:9200"
  "index" => "new_index"
  "document_type" => "doc"
  }
}

我尝试创建索引映射并将其中一个字段设置为嵌套在映射中,但没有任何内容上传到我的索引。从 MySQL 到索引的原始更新将我的 json 视为文本,这使得搜索变得更加困难。 任何人都有更好的解决方案将 json 列更新为索引,以便我可以从键中搜索。

输出。

{
  "check_index" : {
    "aliases" : { },
    "mappings" : {
      "doc" : {
        "properties" : {
          "@timestamp" : {
            "type" : "date"
          },
          "@version" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "info" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          }
        }
      }
    },
    "settings" : {
      "index" : {
        "creation_date" : "1528870439037",
        "number_of_shards" : "5",
        "number_of_replicas" : "1",
        "uuid" : "MkNrBMD8S8GYfDtxRyOFfg",
        "version" : {
          "created" : "6020499"
        },
        "provided_name" : "check_index"
      }
    }
  }
}

有信息是我的 JSON 字符串。在其中我有许多键值,例如:json 中的地址、名称等,因此我没有为此类字段创建一个单独的列,而是为其创建了一个 json 并将其添加到列中。但我无法搜索那个json。

【问题讨论】:

  • 你的输出现在看起来怎么样?
  • 它将我的 JSON 字符串作为文本字段。根据我的说法,它应该把它们当作嵌套的,所以我可以用键搜索它们。
  • 能否请您添加有问题的整个输出?
  • 我的输出中有信息字段作为类型文本。我想搜索该信息中的键。

标签: python mysql json elasticsearch logstash


【解决方案1】:

我想你要找的是JSONfilter。只需在 JSON 过滤器中添加您的列名,即 JSON 类型。假设如果数据类型为JSON 的列是info,您的过滤器将如下所示。

filter {
  json {
    source => "info"
    }
}

如果您有多个具有JSON 数据类型的列,您可以在filter 中重复您的json 字典。所以对于JSONinfo,您的最终logstash 配置将如下所示。

input {
  jdbc {
      jdbc_connection_string => "jdbc:mysql://localhost:3306/dbname"
      # The user we wish to execute our statement as
      jdbc_user => "user"
      jdbc_password => "password"
      # The path to our downloaded jdbc driver
      jdbc_driver_library => "/usr/share/java/mysql-connector-java-5.1.38.jar"
      jdbc_driver_class => "com.mysql.jdbc.Driver"
      # our query
      statement => "SELECT info FROM organization"
  }
} 
filter {
  json {
    source => "info"
    }
}
output {
  elasticsearch {
  "hosts" => "localhost:9200"
  "index" => "new_index"
  "document_type" => "doc"
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-01
    • 1970-01-01
    • 2019-01-08
    • 2016-05-29
    • 2014-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多