MaxCompute prend en charge deux types de fonctions : les fonctions intégrées et les fonctions définies par l'utilisateur (UDF). Les fonctions intégrées sont disponibles immédiatement, sans configuration. Vous devez enregistrer les UDF avant de pouvoir les appeler dans des instructions SQL.
Cette rubrique décrit les opérations courantes de gestion des UDF : création, suppression et consultation des fonctions.
Aperçu des opérations
| Opération | Autorisation requise | Plateformes |
|---|---|---|
| Créer une UDF | Autorisation d'écriture sur les fonctions | Client MaxCompute, console DataWorks, MaxCompute Studio |
| Supprimer une UDF | Autorisation de suppression sur les fonctions | — |
| Afficher une UDF | Autorisation de lecture sur les fonctions | — |
| Lister toutes les UDF | Autorisation de liste sur les objets du projet | — |
| Lister toutes les fonctions intégrées | Autorisation de liste sur les objets du projet | — |
Prérequis
Avant de commencer, assurez-vous d'avoir :
Une ressource JAR ou Python contenant le code de l'UDF, chargée dans MaxCompute
Les autorisations requises pour l'opération (voir le tableau ci-dessus)
Créer une UDF
Enregistre une UDF dans un projet MaxCompute afin de pouvoir l'appeler dans des instructions SQL.
Limitations
Les noms de fonction doivent être uniques au sein d'un projet. Il est interdit de créer une fonction portant le même nom qu'une fonction existante.
Les UDF ne peuvent pas remplacer les fonctions intégrées. Seul le propriétaire du projet peut enregistrer une UDF partageant un nom avec une fonction intégrée. Si vous appelez une telle UDF, un avertissement apparaît dans le résumé Logview après l'exécution du travail SQL.
Syntaxe
create function <function_name> as '<package_to_class>' using '<resource_list>';
Paramètres
| Paramètre | Requis | Description |
|---|---|---|
function_name |
Oui | Nom de l'UDF à créer. Il doit être unique dans le projet. |
package_to_class |
Oui | Chemin de classe complet de l'UDF. La casse est significative. Placez la valeur entre guillemets simples ('). Pour une UDF Java, spécifiez le chemin complet depuis le nom du package de niveau supérieur jusqu'au nom de la classe UDF. Pour une UDF Python, utilisez le format script_name.ClassName. |
resource_list |
Oui | Liste de noms de ressources séparés par des virgules, placée entre guillemets simples ('). Elle doit inclure la ressource contenant le code de l'UDF. Si l'UDF appelle l'API Distributed Cache pour lire des fichiers de ressources, incluez également ces fichiers. Pour référencer une ressource provenant d'un autre projet, utilisez le format <project_name>/resources/<resource_name>. |
Les noms de ressources Python ne sont pas sensibles à la casse au niveau sous-jacent. Le nom de ressource sous-jacent est déterminé par le nom utilisé lors du premier chargement. Par exemple, si vous chargez d'abord pyudf_test.py puis que vous le renommez ultérieurement en PYUDF_TEST.py, le nom de ressource sous-jacent reste pyudf_test.py. Lors de l'enregistrement de l'UDF, le nom de la classe doit correspondre au nom sous-jacent : pyudf_test.SampleUDF. Exécutez list resources; pour vérifier les noms sous-jacents de toutes les ressources.
Exemples
Exemple 1 : UDF Java
Créez la fonction my_lower en utilisant la classe Java org.alidata.odps.udf.examples.Lower située dans my_lower.jar.
create function my_lower as 'org.alidata.odps.udf.examples.Lower' using 'my_lower.jar';
Exemple 2 : UDF Python provenant d'un autre projet
Créez la fonction my_lower en utilisant la classe Python MyLower présente dans le script pyudf_test.py, qui est une ressource du projet test_project.
create function my_lower as 'pyudf_test.MyLower' using 'test_project/resources/pyudf_test.py';
Exemple 3 : Fonction table-valued user-defined (UDTF) Java avec plusieurs ressources
Créez test_udtf en utilisant la classe com.aliyun.odps.examples.udf.UDTFResource située dans udtfexample1.jar. La fonction dépend également d'une ressource fichier, d'une ressource table et d'une ressource archive.
create function test_udtf as 'com.aliyun.odps.examples.udf.UDTFResource' using 'udtfexample1.jar, file_resource.txt, table_resource1, test_archive.zip';
Supprimer une UDF
Supprime une UDF existante d'un projet MaxCompute.
Syntaxe
drop function <function_name>;
Paramètres
| Paramètre | Requis | Description |
|---|---|---|
function_name |
Oui | Nom de l'UDF à supprimer. |
Exemple
-- Delete the my_lower function.
drop function my_lower;
Afficher une UDF
Renvoie les métadonnées d'une UDF spécifique : son nom, son propriétaire, sa date de création, sa classe et sa liste de ressources.
Syntaxe
desc function <function_name>;
Paramètres
| Paramètre | Requis | Description |
|---|---|---|
function_name |
Oui | Nom de l'UDF à inspecter. |
Champs de sortie
| Champ | Description |
|---|---|
Name |
Nom de l'UDF. |
Owner |
Compte ayant enregistré l'UDF. |
Created Time |
Date de création de l'UDF. |
Class |
Chemin de la classe de l'UDF. La casse est significative. |
Resources |
Liste des ressources associées à l'UDF. |
Exemple
-- View details of the my_lower function.
desc function my_lower;
Sortie :
Name my_lower
Owner ALIYUN$****
Created Time 2020-06-18 15:50:19
Class org.alidata.odps.udf.examples.Lower
Resources project_name/my_lower.jar
Lister toutes les UDF
Renvoie toutes les UDF enregistrées dans un projet MaxCompute.
Syntaxe
Utilisez l'une des commandes suivantes :
list functions [-p <project_name>];
show functions;
Paramètres
| Paramètre | Requis | Description |
|---|---|---|
project_name |
Non | Nom du projet MaxCompute à interroger. S'il est omis, le projet actuel est utilisé. Pris en charge uniquement avec list functions. |
Exemple
list functions;
Sortie :
Name Owner Create Time Class Resources
ipv4_ipv6_aton ALIYUN$****@aliyun.com 2021-11-15 13:42:14 com.aliyun.odps.udf.udfFunction.IpLocation ipv4.txt,ipv6.txt,udf-1.0-SNAPSHOT.jar
Lower_test ALIYUN$****@aliyun.com 2021-08-25 15:51:22 com.aliyun.odps.udf.example.Lower udf-1.0-SNAPSHOT.jar
my_add ALIYUN$****@aliyun.com 2021-05-08 11:26:02
my_index ALIYUN$****@aliyun.com 2021-08-25 12:01:05 com.aliyun.odps.examples.udf.UdfArray udf-1.0-SNAPSHOT.jar
my_sum ALIYUN$****@aliyun.com 2021-05-08 10:24:58
my_udtf ALIYUN$****@aliyun.com 2021-02-23 11:37:30 com.aliyun.odps.examples.udf.UDTFResource udf-1.0-SNAPSHOT.jar
numpy ALIYUN$****@aliyun.com 2020-11-11 14:12:50 numpy.TryImport numpy.py,numpy-1.19.4-cp37-cp37m-manylinux1_x86_64.zip
ST_Aggr_ConvexHull ALIYUN$****@aliyun.com 2021-03-18 17:06:29 com.esri.hadoop.hive.ST_Aggr_ConvexHull esri-geometry-api.jar,spatial-sdk-hive.jar
ST_Aggr_Intersection ALIYUN$****@aliyun.com 2021-03-18 17:06:29 com.esri.hadoop.hive.ST_Aggr_Intersection esri-geometry-api.jar,spatial-sdk-hive.jar
ST_Aggr_Union ALIYUN$****@aliyun.com 2021-03-18 17:06:30 com.esri.hadoop.hive.ST_Aggr_Union esri-geometry-api.jar,spatial-sdk-hive.jar
ST_Area ALIYUN$****@aliyun.com 2021-03-18 17:06:30 com.esri.hadoop.hive.ST_Area esri-geometry-api.jar,spatial-sdk-hive.jar
ST_AsBinary ALIYUN$****@aliyun.com 2021-03-18 17:06:30 com.esri.hadoop.hive.ST_AsBinary esri-geometry-api.jar,spatial-sdk-hive.jar
ST_AsGeoJson ALIYUN$****@aliyun.com 2021-03-18 17:06:49 com.esri.hadoop.hive.ST_AsGeoJson esri-geometry-api.jar,spatial-sdk-hive.jar
ST_AsJson ALIYUN$****@aliyun.com 2021-03-18 17:06:50 com.esri.hadoop.hive.ST_AsJson esri-geometry-api.jar,spatial-sdk-hive.jar
ST_AsShape ALIYUN$****@aliyun.com 2021-03-18 17:06:50 com.esri.hadoop.hive.ST_AsShape esri-geometry-api.jar,spatial-sdk-hive.jar
ST_AsText ALIYUN$****@aliyun.com 2021-03-18 17:06:50 com.esri.hadoop.hive.ST_AsText esri-geometry-api.jar,spatial-sdk-hive.jar
ST_Bin ALIYUN$****@aliyun.com 2021-03-18 17:06:50 com.esri.hadoop.hive.ST_Bin esri-geometry-api.jar,spatial-sdk-hive.jar
ST_BinEnvelope ALIYUN$****@aliyun.com 2021-03-18 17:07:01 com.esri.hadoop.hive.ST_BinEnvelope esri-geometry-api.jar,spatial-sdk-hive.jar
ST_Boundary ALIYUN$****@aliyun.com 2021-03-18 17:07:01 com.esri.hadoop.hive.ST_Boundary esri-geometry-api.jar,spatial-sdk-hive.jar
ST_Buffer ALIYUN$****@aliyun.com 2021-03-18 17:07:01 com.esri.hadoop.hive.ST_Buffer esri-geometry-api.jar,spatial-sdk-hive.jar
ST_Centroid ALIYUN$****@aliyun.com 2021-03-18 17:07:01 com.esri.hadoop.hive.ST_Centroid esri-geometry-api.jar,spatial-sdk-hive.jar
ST_Contains ALIYUN$****@aliyun.com 2021-03-18 17:07:01 com.esri.hadoop.hive.ST_Contains esri-geometry-api.jar,spatial-sdk-hive.jar
ST_ConvexHull ALIYUN$****@aliyun.com 2021-03-18 17:07:13 com.esri.hadoop.hive.ST_ConvexHull esri-geometry-api.jar,spatial-sdk-hive.jar
ST_CoordDim ALIYUN$****@aliyun.com 2021-03-18 17:07:14 com.esri.hadoop.hive.ST_CoordDim esri-geometry-api.jar,spatial-sdk-hive.jar
ST_Crosses ALIYUN$****@aliyun.com 2021-03-18 17:07:14 com.esri.hadoop.hive.ST_Crosses esri-geometry-api.jar,spatial-sdk-hive.jar
ST_Difference ALIYUN$****@aliyun.com 2021-03-18 17:07:14 com.esri.hadoop.hive.ST_Difference esri-geometry-api.jar,spatial-sdk-hive.jar
ST_Dimension ALIYUN$****@aliyun.com 2021-03-18 17:07:14 com.esri.hadoop.hive.ST_Dimension esri-geometry-api.jar,spatial-sdk-hive.jar
ST_Disjoint ALIYUN$****@aliyun.com 2021-03-18 17:07:31 com.esri.hadoop.hive.ST_Disjoint esri-geometry-api.jar,spatial-sdk-hive.jar
ST_Distance ALIYUN$****@aliyun.com 2021-03-18 17:07:31 com.esri.hadoop.hive.ST_Distance esri-geometry-api.jar,spatial-sdk-hive.jar
ST_EndPoint ALIYUN$****@aliyun.com 2021-03-18 17:07:31 com.esri.hadoop.hive.ST_EndPoint esri-geometry-api.jar,spatial-sdk-hive.jar
ST_Envelope ALIYUN$****@aliyun.com 2021-03-18 17:07:32 com.esri.hadoop.hive.ST_Envelope esri-geometry-api.jar,spatial-sdk-hive.jar
ST_EnvIntersects ALIYUN$****@aliyun.com 2021-03-18 17:07:32 com.esri.hadoop.hive.ST_EnvIntersects esri-geometry-api.jar,spatial-sdk-hive.jar
ST_Equals ALIYUN$****@aliyun.com 2021-03-18 17:07:44 com.esri.hadoop.hive.ST_Equals esri-geometry-api.jar,spatial-sdk-hive.jar
ST_ExteriorRing ALIYUN$****@aliyun.com 2021-03-18 17:07:44 com.esri.hadoop.hive.ST_ExteriorRing esri-geometry-api.jar,spatial-sdk-hive.jar
ST_GeodesicLengthWGS84 ALIYUN$****@aliyun.com 2021-03-18 17:07:44 com.esri.hadoop.hive.ST_GeodesicLengthWGS84 esri-geometry-api.jar,spatial-sdk-hive.jar
ST_GeomCollection ALIYUN$****@aliyun.com 2021-03-18 17:07:44 com.esri.hadoop.hive.ST_GeomCollection esri-geometry-api.jar,spatial-sdk-hive.jar
ST_Geometry ALIYUN$****@aliyun.com 2021-03-18 17:07:44 com.esri.hadoop.hive.ST_Geometry esri-geometry-api.jar,spatial-sdk-hive.jar
ST_GeometryN ALIYUN$****@aliyun.com 2021-03-18 17:07:55 com.esri.hadoop.hive.ST_GeometryN esri-geometry-api.jar,spatial-sdk-hive.jar
ST_GeometryType ALIYUN$****@aliyun.com 2021-03-18 17:07:55 com.esri.hadoop.hive.ST_GeometryType esri-geometry-api.jar,spatial-sdk-hive.jar
ST_GeomFromGeoJson ALIYUN$****@aliyun.com 2021-03-18 17:07:55 com.esri.hadoop.hive.ST_GeomFromGeoJson esri-geometry-api.jar,spatial-sdk-hive.jar
ST_GeomFromJson ALIYUN$****@aliyun.com 2021-03-18 17:07:55 com.esri.hadoop.hive.ST_GeomFromJson esri-geometry-api.jar,spatial-sdk-hive.jar
ST_GeomFromShape ALIYUN$****@aliyun.com 2021-03-18 17:07:56 com.esri.hadoop.hive.ST_GeomFromShape esri-geometry-api.jar,spatial-sdk-hive.jar
ST_GeomFromText ALIYUN$****@aliyun.com 2021-03-18 17:08:10 com.esri.hadoop.hive.ST_GeomFromText esri-geometry-api.jar,spatial-sdk-hive.jar
ST_GeomFromWKB ALIYUN$****@aliyun.com 2021-03-18 17:08:10 com.esri.hadoop.hive.ST_GeomFromWKB esri-geometry-api.jar,spatial-sdk-hive.jar
Les colonnes de sortie sont les suivantes :
| Colonne | Description |
|---|---|
Name |
Nom de l'UDF. |
Owner |
Compte ayant enregistré l'UDF. |
Create Time |
Date de création de l'UDF. |
Class |
Chemin de la classe de l'UDF. Vide si les informations de classe ne sont pas disponibles. |
Resources |
Ressources associées à l'UDF. Vide si aucune ressource n'est enregistrée. |
Lister toutes les fonctions intégrées
Renvoie des informations sur les fonctions intégrées disponibles dans MaxCompute, y compris les signatures de fonction et les types de données pris en charge.
Cette commande nécessite le client MaxCompute version 43.0 ou ultérieure.
Syntaxe
show builtin functions [<function_name>];
Paramètres
| Paramètre | Requis | Description |
|---|---|---|
function_name |
Non | Nom d'une fonction intégrée spécifique à rechercher. S'il est omis, toutes les fonctions intégrées sont renvoyées. |
Exemple
show builtin functions;
Sortie (partielle) :
ID = 20230307081023424gef2hwowr1
::ABS SCALAR DECIMAL(?,?) ABS(DECIMAL(?,?) arg0),DOUBLE ABS(DOUBLE arg0),BIGINT ABS(BIGINT arg0),INT ABS(INT arg0)
::ACOS SCALAR DOUBLE ACOS(DOUBLE arg0),DOUBLE ACOS(DECIMAL(?,?) arg0)
::ADD_MONTHS SCALAR STRING ADD_MONTHS(DATE arg0, BIGINT arg1),STRING ADD_MONTHS(TIMESTAMP arg0, BIGINT arg1),STRING ADD_MONTHS(STRING arg0, BIGINT arg1)
::ALL_MATCH SCALAR BOOLEAN ALL_MATCH(ARRAY<T> arg0, java.util.function.Function<T, java.lang.Boolean> arg1)
::ANY_MATCH SCALAR BOOLEAN ANY_MATCH(ARRAY<T> arg0, java.util.function.Function<T, java.lang.Boolean> arg1)
::ANY_VALUE AGGREGATOR T ANY_VALUE([DISTINCT] T arg1)
::APPROX_DISTINCT AGGREGATOR BIGINT APPROX_DISTINCT([DISTINCT] P arg1, DOUBLE arg2),BIGINT APPROX_DISTINCT([DISTINCT] P arg1)
::ARG_MAX AGGREGATOR R ARG_MAX([DISTINCT] T arg1, R arg2)
::ARG_MIN AGGREGATOR R ARG_MIN([DISTINCT] T arg1, R arg2)
::ARRAY SCALAR ARRAY<STRING> ARRAY(),ARRAY<T> ARRAY(T arg0...)
::ARRAYS_OVERLAP SCALAR BOOLEAN ARRAYS_OVERLAP(ARRAY<T> arg0, ARRAY<T> arg1)
::ARRAYS_ZIP SCALAR null
::ARRAY_CONTAINS SCALAR BOOLEAN ARRAY_CONTAINS(ARRAY<T> arg0, T arg1)
::ARRAY_DISTINCT SCALAR ARRAY<T> ARRAY_DISTINCT(ARRAY<T> arg0)
::ARRAY_EXCEPT SCALAR ARRAY<T> ARRAY_EXCEPT(ARRAY<T> arg0, ARRAY<T> arg1)
::ARRAY_INTERSECT SCALAR null
::ARRAY_JOIN SCALAR STRING ARRAY_JOIN(ARRAY<STRING> arg0, STRING arg1, STRING arg2),STRING ARRAY_JOIN(ARRAY<STRING> arg0, STRING arg1)
::ARRAY_MAX SCALAR T ARRAY_MAX(ARRAY<T> arg0)
::ARRAY_MIN SCALAR T ARRAY_MIN(ARRAY<T> arg0)
::ARRAY_NORMALIZE SCALAR ARRAY<FLOAT> ARRAY_NORMALIZE(ARRAY<FLOAT> arg0, FLOAT arg1),ARRAY<DOUBLE> ARRAY_NORMALIZE(ARRAY<DOUBLE> arg0, DOUBLE arg1)
::ARRAY_POSITION SCALAR BIGINT ARRAY_POSITION(ARRAY<T> arg0, T arg1, BIGINT arg2),BIGINT ARRAY_POSITION(ARRAY<T> arg0, T arg1)
::ARRAY_REDUCE SCALAR OUT ARRAY_REDUCE(ARRAY<IN> arg0, BUF arg1, java.util.function.BiFunction<BUF, IN, BUF> arg2, java.util.function.Function<BUF, OUT> arg3)
::ARRAY_REMOVE SCALAR ARRAY<T> ARRAY_REMOVE(ARRAY<T> arg0, T arg1)
::ARRAY_REPEAT SCALAR ARRAY<T> ARRAY_REPEAT(T arg0, BIGINT arg1)
::ARRAY_SORT SCALAR ARRAY<T> ARRAY_SORT(ARRAY<T> arg0, java.util.function.BiFunction<T, T, java.lang.Long> arg1)
::ARRAY_UNION SCALAR ARRAY<T> ARRAY_UNION(ARRAY<T> arg0, ARRAY<T> arg1)
::ASCII SCALAR BIGINT ASCII(STRING arg0)
::ASIN SCALAR DOUBLE ASIN(DOUBLE arg0),DOUBLE ASIN(DECIMAL(?,?) arg0)
::ATAN SCALAR DOUBLE ATAN(DECIMAL(?,?) arg0),DOUBLE ATAN(DOUBLE arg0)
::ATAN2 SCALAR DOUBLE ATAN2(DECIMAL(?,?) arg0, DECIMAL(?,?) arg1),DOUBLE ATAN2(DOUBLE arg0, DOUBLE arg1)
::AVG AGGREGATOR DECIMAL(?,?) AVG([DISTINCT] DECIMAL(?,?) arg1),DOUBLE AVG([DISTINCT] DOUBLE arg1)
::AVG WINDOW DOUBLE AVG([DISTINCT] DOUBLE arg0),DECIMAL(?,?) AVG([DISTINCT] DECIMAL(?,?) arg0)
::BASE64 SCALAR STRING BASE64(BINARY arg0)
::BIN SCALAR STRING BIN(BIGINT arg0)
::BITAND SCALAR BIGINT BITAND(BIGINT arg0, BIGINT arg1)
::BITNOT SCALAR BIGINT BITNOT(BIGINT arg0)
::BITOR SCALAR BIGINT BITOR(BIGINT arg0, BIGINT arg1)
::BITWISE_AND_AGG AGGREGATOR BIGINT BITWISE_AND_AGG([DISTINCT] BIGINT arg1)
::BITWISE_OR_AGG AGGREGATOR BIGINT BITWISE_OR_AGG([DISTINCT] BIGINT arg1)
::BITXOR SCALAR BIGINT BITXOR(BIGINT arg0, BIGINT arg1)
::BROUND SCALAR DOUBLE BROUND(DOUBLE arg0, BIGINT arg1),DOUBLE BROUND(DOUBLE arg0)
......
Chaque ligne de la sortie suit le format : function_name function_type signature(s). Le type de fonction est l'un des suivants : SCALAR, AGGREGATOR ou WINDOW.