次の方法で共有


geoip_fl()

geoip_fl() は、ip アドレス 地理情報を取得するユーザー定義関数 です。

Note

前提条件

構文

T | invoke geoip_fl(ip_col, country_col, state_col, city_col, longitude_col, latitude_col)

構文規則について詳しく知る。

パラメーター

件名 タイプ Required 説明
ip_col string ✔️ 解決する IP アドレスを含む列の名前。
country_col string ✔️ 取得した国を格納する列の名前。
state_col string ✔️ 取得した状態を格納する列の名前。
city_col string ✔️ 取得した市区町村を格納する列の名前。
longitude_col real ✔️ 取得した経度を格納する列の名前。
latitude_col real ✔️ 取得した緯度を格納する列の名前。

関数定義

関数を定義するには、次のようにコードをクエリ定義関数として埋め込むか、データベースに格納された関数として作成します。

次の let ステートメントを使用して関数を定義。 権限は必要ありません。

重要

let ステートメント単独では実行できません。 その後に 表形式の式ステートメントが続く必要がありますgeoip_fl()の動作例を実行するには、Exampleを参照してください。

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 city 緯度 緯度
20.103.85.33 オランダ North Holland (北ホラント) アムステルダム 4.8883 52.3716
20.53.203.50 オーストラリア ニュー サウス ウェールズ州 シドニー 151.2006 -33.8715
20.81.111.85 米国 バージニア州 Tappahannock -76.8545 37.9273
20.84.181.62 米国 アイオワ州 Des Moines -93.6124 41.6021
205.251.242.103 米国 バージニア州 アッシュバーン -77.4903 39.0469
8.8.8.8 米国 カリフォルニア Los Angeles -118.2441 34.0544

この機能はサポートされていません。