Published on :
Administration CREATION_INTERNE

Add an Oracle Caslib

This code is also available in: Deutsch Español Français
Awaiting validation
Attention : This code requires administrator privileges.
The `table.addCaslib` action from the `Table` action set is used to create an Oracle caslib. Essential parameters include the caslib name, the data source type (`srctype="oracle"`), authentication information (username and password), and the Oracle database connection path (format `//<db_host>:<port>/<db_name>`). Once the caslib is added, Oracle tables can be loaded into CAS memory for distributed analytics.
Data Analysis

Type : CREATION_INTERNE


Examples use generated data (datalines) or SASHELP and simulated connection information for the Oracle database.

1 Code Block
PROC CAS / table.addCaslib
Explanation :
This example shows the simplest way to add an Oracle caslib. It specifies the caslib name ('orlib'), the source type ('oracle'), connection credentials, and the database access path. It attempts to delete the caslib if it already exists before creating it to ensure a clean environment.
Copied!
1/* Démarrer une session CAS si ce n'est pas déjà fait */
2options cashost="cloud.example.com" casport=5570;
3cas casauto;
4 
5PROC CAS;
6 SESSION casauto;
7 /* Supprimer la caslib si elle existe déjà pour éviter les erreurs */
8 SIMPLE.dropCaslib / caslib='orlib';
9 
10 /* Ajouter une caslib Oracle avec les informations de connexion minimales */
11 TABLE.addCaslib RESULT=r /
12 caslib="orlib",
13 datasource={
14 srctype="oracle",
15 username="myuser",
16 password="mypass",
17 path="//oradb.example.com:1521/mydb"
18 };
19
20 PRINT r;
21RUN;
22QUIT;
23 
2 Code Block
PROC CAS / table.addCaslib
Explanation :
This example extends the basic case by adding common options. 'globals=TRUE' makes the caslib accessible to all CAS sessions, and 'subdirs=TRUE' allows the caslib to access subdirectories of the specified database path. A description is also added for better documentation.
Copied!
1/* Démarrer une session CAS si ce n'est pas déjà fait */
2options cashost="cloud.example.com" casport=5570;
3cas casauto;
4 
5PROC CAS;
6 SESSION casauto;
7 /* Supprimer la caslib si elle existe déjà */
8 SIMPLE.dropCaslib / caslib='global_orlib';
9 
10 /* Ajouter une caslib Oracle, la rendant globale et permettant l'accès aux sous-répertoires */
11 TABLE.addCaslib RESULT=r /
12 caslib="global_orlib",
13 description="Caslib Oracle globale avec sous-répertoires",
14 path="//oradb.example.com:1521/mydb",
15 datasource={
16 srctype="oracle",
17 username="myuser",
18 password="mypass"
19 },
20 globals=TRUE,
21 subdirs=TRUE;
22 
23 PRINT r;
24 
25 /* Lister les caslibs pour vérifier la nouvelle entrée */
26 TABLE.caslibInfo RESULT=r_info;
27 PRINT r_info;
28RUN;
29QUIT;
30 
3 Code Block
PROC CAS / table.addCaslib
Explanation :
This advanced example integrates error checking and handling logic. It first verifies the existence of the caslib and deletes it if necessary. Then, it attempts to add the caslib and displays a success or failure message based on the severity of the CAS action result, which is crucial for automated scripts or production environments. The caslib name is dynamically defined.
Copied!
1/* Démarrer une session CAS si ce n'est pas déjà fait */
2options cashost="cloud.example.com" casport=5570;
3cas casauto;
4 
5PROC CAS;
6 SESSION casauto;
7 
8 /* Nom dynamique pour la caslib */
9 caslib_name = 'orlib_dynamic';
10 
11 /* Vérifier si la caslib existe déjà avant de tenter de l'ajouter */
12 TABLE.caslibinfo RESULT=caslib_status / caslib=caslib_name;
13 
14 IF (caslib_status.caslibinfo.Name = caslib_name) THEN DO;
15 PRINT 'NOTE: Caslib ' || caslib_name || ' existe déjà. Suppression en cours...';
16 SIMPLE.dropCaslib / caslib=caslib_name;
17 PRINT 'NOTE: Caslib ' || caslib_name || ' supprimée.';
18 END;
19 
20 /* Ajouter la caslib Oracle avec gestion d'erreurs et affichage du statut */
21 TABLE.addCaslib RESULT=r /
22 caslib=caslib_name,
23 datasource={
24 srctype="oracle",
25 username="myuser",
26 password="mypass",
27 path="//oradb.example.com:1521/mydb"
28 };
29 
30 IF (r.severity = 0) THEN DO;
31 PRINT 'NOTE: Caslib ' || caslib_name || ' ajoutée avec succès.';
32 END ELSE DO;
33 PRINT 'ERREUR: Échec de l''ajout de la caslib ' || caslib_name;
34 PRINT r;
35 END;
36 
37RUN;
38QUIT;
39 
4 Code Block
PROC CAS / table.addCaslib / table.loadtable Data
Explanation :
This example highlights a more advanced integration with SAS Viya by using an authentication domain (`authdomain`) pre-configured on the CAS server. This centralizes credential management and improves security. The example then simulates an Oracle table and demonstrates how to load this table into CAS memory via the newly created caslib, then displays a data preview. Cleanup is included to remove created objects.
Copied!
1/* Démarrer une session CAS si ce n'est pas déjà fait */
2options cashost="cloud.example.com" casport=5570;
3cas casauto;
4 
5PROC CAS;
6 SESSION casauto;
7 
8 /* Création de données d'exemple pour simuler une table Oracle */
9 DATA mydata;
10 INPUT ID $ Name $ Value;
11 DATALINES;
12 101 Apple 25
13 102 Banana 30
14 103 Orange 45
15 ;
16 RUN;
17 
18 /* Supprimer la caslib si elle existe déjà */
19 SIMPLE.dropCaslib / caslib='orlib_ad';
20 
21 /* Ajouter une caslib Oracle en utilisant un domaine d'authentification (authdomain) */
22 /* Note: L'authdomain 'oracle_auth' doit être préconfiguré sur le serveur CAS */
23 TABLE.addCaslib RESULT=r /
24 caslib="orlib_ad",
25 datasource={
26 srctype="oracle",
27 authdomain="oracle_auth", /* Utilise un domaine d'authentification Viya */
28 path="//oradb.example.com:1521/mydb"
29 };
30 PRINT r;
31 
32 /* Si la caslib a été ajoutée avec succès, tenter de charger une table */
33 IF (r.severity = 0) THEN DO;
34 /* Simuler une table existante dans Oracle en créant une table temporaire CAS */
35 DATA casuser.my_oracle_table;
36 SET mydata;
37 RUN;
38 
39 /* Charger une table depuis la caslib Oracle (simulée) vers la mémoire CAS */
40 TABLE.loadtable RESULT=lr /
41 caslib="orlib_ad",
42 path="MY_ORACLE_TABLE", /* Nom de la table dans la base de données Oracle */
43 casout={
44 name="my_cas_table",
45 replace=TRUE
46 };
47 PRINT lr;
48 
49 /* Afficher les 5 premières lignes de la table chargée */
50 TABLE.fetch casout.my_cas_table / to=5;
51 END;
52 
53 /* Nettoyage: Supprimer la table chargée et la caslib */
54 TABLE.droptable / caslib='casuser' name='my_cas_table';
55 SIMPLE.dropCaslib / caslib='orlib_ad';
56 
57RUN;
58QUIT;
59 
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.