다음을 통해 공유


geoip_fl()

geoip_fl() 는 IP 주소의 지리적 정보를 검색하는 사용자 정의 함수 입니다.

참고

  • 이 문서에 설명된 함수 대신 네이티브 함수 geo_info_from_ip_address() 를 사용합니다. 네이티브 함수는 동일한 기능을 제공하며 성능 및 확장성에 더 적합합니다. 이 문서는 참조 목적으로만 제공됩니다.
  • 이 함수는 에서 사용할 수 있는 MaxMind에서 만든 GeoLite2 데이터에서 http://www.maxmind.com지리적 데이터를 검색했습니다. GeoLite2 최종 사용자 사용권 계약을 검토하세요.

사전 요구 사항

  • 클러스터에서 Python 플러그 인 을 사용하도록 설정해야 합니다. 함수에 사용되는 인라인 Python에 필요합니다.
  • 데이터베이스에서 Python 플러그 인 을 사용하도록 설정해야 합니다. 함수에 사용되는 인라인 Python에 필요합니다.

Syntax

T | invoke geoip_fl(, ip_col, country_col, state_col, city_col, longitude_collatitude_col)

구문 규칙에 대해 자세히 알아보세요.

매개 변수

이름 형식 필수 Description
ip_col string ✔️ resolve IP 주소를 포함하는 열의 이름입니다.
country_col string ✔️ 검색된 국가를 저장할 열의 이름입니다.
state_col string ✔️ 검색된 상태를 저장할 열의 이름입니다.
city_col string ✔️ 검색된 도시를 저장할 열의 이름입니다.
longitude_col real ✔️ 검색된 경도를 저장할 열의 이름입니다.
latitude_col real ✔️ 검색된 위도를 저장할 열의 이름입니다.

함수 정의

다음과 같이 해당 코드를 쿼리 정의 함수로 포함하거나 데이터베이스에 저장된 함수로 만들어 함수를 정의할 수 있습니다.

다음 let 문을 사용하여 함수를 정의합니다. 사용 권한이 필요 없습니다.

중요

let 문은 자체적으로 실행할 수 없습니다. 그 뒤에 테이블 형식 식 문이 있어야 합니다. 의 geoip_fl()작업 예제를 실행하려면 예제를 참조 하세요.

let geoip_fl=(tbl:(*), ip_col:string, country_col:string, state_col:string, city_col:string, longitude_col:string, latitude_col:string)
{
    let kwargs = bag_pack('ip_col', ip_col, 'country_col', country_col, 'state_col', state_col, 'city_col', city_col, 'longitude_col', longitude_col, 'latitude_col', latitude_col);
    let code= ```if 1:
        from sandbox_utils import Zipackage
        Zipackage.install('geoip2.zip')
        import geoip2.database

        ip_col = kargs['ip_col']
        country_col = kargs['country_col']
        state_col = kargs['state_col']
        city_col = kargs['city_col']
        longitude_col = kargs['longitude_col']
        latitude_col = kargs['latitude_col']
        result=df
        reader = geoip2.database.Reader(r'C:\\Temp\\GeoLite2-City.mmdb')

        def geodata(ip):
            try:
                gd = reader.city(ip)
                geo = pd.Series((gd.country.name, gd.subdivisions.most_specific.name, gd.city.name, gd.location.longitude, gd.location.latitude))
            except:
                geo = pd.Series((None, None, None, None, None))
            return geo

        result[[country_col, state_col, city_col, longitude_col, latitude_col]] = result[ip_col].apply(geodata)

    ```;
    tbl
    | evaluate python(typeof(*), code, kwargs,
        external_artifacts =
        pack('geoip2.zip', 'https://artifactswestus.blob.core.windows.net/public/geoip2-4.6.0.zip',
             'GeoLite2-City.mmdb', 'https://artifactswestus.blob.core.windows.net/public/GeoLite2-City-20230221.mmdb')
        )
};
// Write your query to use the function here.

예제

다음 예제에서는 invoke 연산자를 사용하여 함수를 실행합니다.

쿼리 정의 함수를 사용하려면 포함된 함수 정의 다음에 호출합니다.

let geoip_fl=(tbl:(*), ip_col:string, country_col:string, state_col:string, city_col:string, longitude_col:string, latitude_col:string)
{
    let kwargs = bag_pack('ip_col', ip_col, 'country_col', country_col, 'state_col', state_col, 'city_col', city_col, 'longitude_col', longitude_col, 'latitude_col', latitude_col);
    let code= ```if 1:
        from sandbox_utils import Zipackage
        Zipackage.install('geoip2.zip')
        import geoip2.database

        ip_col = kargs['ip_col']
        country_col = kargs['country_col']
        state_col = kargs['state_col']
        city_col = kargs['city_col']
        longitude_col = kargs['longitude_col']
        latitude_col = kargs['latitude_col']
        result=df
        reader = geoip2.database.Reader(r'C:\\Temp\\GeoLite2-City.mmdb')

        def geodata(ip):
            try:
                gd = reader.city(ip)
                geo = pd.Series((gd.country.name, gd.subdivisions.most_specific.name, gd.city.name, gd.location.longitude, gd.location.latitude))
            except:
                geo = pd.Series((None, None, None, None, None))
            return geo

        result[[country_col, state_col, city_col, longitude_col, latitude_col]] = result[ip_col].apply(geodata)

    ```;
    tbl
    | evaluate python(typeof(*), code, kwargs,
        external_artifacts =
        pack('geoip2.zip', 'https://artifactswestus.blob.core.windows.net/public/geoip2-4.6.0.zip',
             'GeoLite2-City.mmdb', 'https://artifactswestus.blob.core.windows.net/public/GeoLite2-City-20230221.mmdb')
        )
};
datatable(ip:string) [
'8.8.8.8',
'20.53.203.50',
'20.81.111.85',
'20.103.85.33',
'20.84.181.62',
'205.251.242.103',
]
| extend country='', state='', city='', longitude=real(null), latitude=real(null)
| invoke geoip_fl('ip','country', 'state', 'city', 'longitude', 'latitude')

출력

ip country state city longitude latitude
20.103.85.33 네덜란드 North Holland 암스테르담 4.8883 52.3716
20.53.203.50 오스트레일리아 뉴사우스웨일스 시드니 151.2006 -33.8715
20.81.111.85 미국 버지니아 타파한녹 -76.8545 37.9273
20.84.181.62 미국 아이오와 디모인 -93.6124 41.6021
205.251.242.103 미국 버지니아 애슈번 -77.4903 39.0469
8.8.8.8 미국 캘리포니아 Los Angeles -118.2441 34.0544

이 기능은 지원되지 않습니다.