【问题标题】:INET_ATON equivalent in AWS RedshiftAWS Redshift 中的 INET_ATON 等效项
【发布时间】:2014-10-06 21:45:43
【问题描述】:

我在 Web 上找不到任何文档,无论 AWS Redshift 中是否存在类似 INET_ATON 的内容。所以我想它还没有,但我想知道是否有某种解决方法。顺便说一句,我正在使用 max_mind 数据。

【问题讨论】:

    标签: amazon-redshift maxmind


    【解决方案1】:

    您可以使用以下解决方法:

    SELECT ipAddr, 
       SPLIT_PART(ipAddr, '.', 1)* 16777216::bigint +
       SPLIT_PART(ipAddr, '.', 2)* 65536::bigint +
       SPLIT_PART(ipAddr, '.', 3)* 256::bigint +
       SPLIT_PART(ipAddr, '.', 4)::bigint AS addressRange       
    FROM <your_table) LIMIT 5
    

    addressRange 应该匹配到 maxmind startIpNum

    【讨论】:

      【解决方案2】:

      我最终在 Python(这是我使用的主要语言)中创建了一个包装器,它将 IP 字符串转换为数字,然后使用它。

      【讨论】:

        【解决方案3】:

        我在同名表中加载了国家块和位置 CSV,并加入了以下查询

        INSERT INTO dim.geoip_country
        SELECT
                SPLIT_PART(first_ip, '.', 1) * 16777216::BIGINT
                + SPLIT_PART(first_ip, '.', 2) * 65536::BIGINT
                + SPLIT_PART(first_ip, '.', 3) * 256::BIGINT
                + SPLIT_PART(first_ip, '.', 4)::BIGINT AS ip_inf,
                SPLIT_PART(first_ip, '.', 1) * 16777216::BIGINT
                + SPLIT_PART(first_ip, '.', 2) * 65536::BIGINT
                + SPLIT_PART(first_ip, '.', 3) * 256::BIGINT
                + SPLIT_PART(first_ip, '.', 4)::BIGINT
                + POW(2, 32 - mask_bits)::BIGINT AS ip_sup,
                network,
                isocode2,
                name,
                continent_code,
                continent_name,
                is_anonymous_proxy,
                is_satellite_provider
        FROM (
                SELECT
                        b.network,
                        SPLIT_PART(b.network, '/', 1) AS first_ip,
                        SPLIT_PART(b.network, '/', 2)::INTEGER AS mask_bits,
                        l.country_iso_code AS isocode2,
                        l.country_name AS name,
                        l.continent_code AS continent_code,
                        l.continent_name AS continent_name,
                        b.is_anonymous_proxy::BOOLEAN AS is_anonymous_proxy,
                        b.is_satellite_provider::BOOLEAN AS is_satellite_provider
                FROM ext.geoip2_country_blocks_ipv4 b
                JOIN ext.geoip2_country_locations_en l
                        ON b.geoname_id = l.geoname_id
        )
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2010-11-25
          • 2010-11-19
          • 1970-01-01
          • 2018-07-23
          • 1970-01-01
          • 2021-11-16
          • 1970-01-01
          • 2017-04-05
          相关资源
          最近更新 更多