try_ip_as_string
S’applique à : Databricks Runtime 18.2 et versions ultérieures
info
Bêta
Cette fonctionnalité est en version bêta.
Retourne la représentation canonique sous forme de chaîne d'une adresse IP ou d'un bloc CIDR. Retourne None au lieu de lever une erreur si l'entrée est invalide.
Pour la fonction SQL correspondante, consultez la fonctiontry_ip_as_string.
Syntaxe
Python
from pyspark.databricks.sql import functions as dbf
dbf.try_ip_as_string(col=<col>)
parameter
parameter | Type | Description |
|---|---|---|
|
| Une valeur STRING ou BINARY représentant une adresse IPv4 ou IPv6 ou un bloc CIDR valide. |
Exemples
Exemple 1 : Convertir une adresse IPv4 en chaîne de caractères.
Python
from pyspark.databricks.sql import functions as dbf
df = spark.createDataFrame([('192.168.1.1',)], ['ip'])
df.select(dbf.try_ip_as_string('ip').alias('result')).collect()
Output
[Row(result='192.168.1.1')]
Exemple 2 : Convertir une adresse IPv6 en chaîne de caractères.
Python
from pyspark.databricks.sql import functions as dbf
df = spark.createDataFrame([('2001:db8::1',)], ['ip'])
df.select(dbf.try_ip_as_string('ip').alias('result')).collect()
Output
[Row(result='2001:db8::1')]
Exemple 3 : Convertir un bloc CIDR en chaîne.
Python
from pyspark.databricks.sql import functions as dbf
df = spark.createDataFrame([('192.168.1.5/24',)], ['cidr'])
df.select(dbf.try_ip_as_string('cidr').alias('result')).collect()
Output
[Row(result='192.168.1.0/24')]
Exemple 4 : L'entrée non valide renvoie None.
Python
from pyspark.databricks.sql import functions as dbf
df = spark.createDataFrame([('invalid.ip',)], ['ip'])
df.select(dbf.try_ip_as_string('ip').alias('result')).collect()
Output
[Row(result=None)]
**Exemple 5** : None l'entrée None renvoie.
Python
from pyspark.databricks.sql import functions as dbf
df = spark.createDataFrame([(None,)], 'ip: string')
df.select(dbf.try_ip_as_string('ip').alias('result')).collect()
Output
[Row(result=None)]