<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>Question Re: SAP Datasphere CLI &amp;amp; Python: Exporting Modeling Objects to CSV Files for Each Artifact in Technology Q&amp;A</title>
    <link>https://community.sap.com/t5/technology-q-a/sap-datasphere-cli-amp-python-exporting-modeling-objects-to-csv-files-for/qaa-p/14087481#M4911984</link>
    <description>Great post</description>
    <pubDate>Sun, 27 Apr 2025 08:07:31 GMT</pubDate>
    <dc:creator>vishal_parmar2</dc:creator>
    <dc:date>2025-04-27T08:07:31Z</dc:date>
    <item>
      <title>SAP Datasphere CLI &amp; Python: Exporting Modeling Objects to CSV Files for Each Artifact</title>
      <link>https://community.sap.com/t5/technology-q-a/sap-datasphere-cli-amp-python-exporting-modeling-objects-to-csv-files-for/qaq-p/14087055</link>
      <description>&lt;H1 id="toc-hId-1382837983"&gt;Introduction&lt;/H1&gt;&lt;P&gt;In this blog post, we'll explore how to use Python alongside SAP Datasphere CLI to extract modeling objects and export them to CSV files. The script allows users to handle artifacts such as remote tables, views, replication flows, and more, for each space in SAP Datasphere.&lt;BR /&gt;This solution is particularly useful for automating repetitive tasks and ensuring structured data handling across different modeling objects&lt;/P&gt;&lt;P&gt;&lt;FONT size="5" color="#000000"&gt;&lt;STRONG&gt;Prerequisites&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;Steps to install SAP Datasphere CLI:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="https://help.sap.com/docs/SAP_DATASPHERE/d0ecd6f297ac40249072a44df0549c1a/f7d5eddf20a34a1aa48d8e2c68a44e28.html/" target="_blank" rel="noopener"&gt;https://help.sap.com/docs/SAP_DATASPHERE/d0ecd6f297ac40249072a44df0549c1a/f7d5eddf20a34a1aa48d8e2c68a44e28.html/&lt;/A&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="https://community.sap.com/t5/technology-blogs-by-sap/sap-datasphere-external-access-overview-apis-cli-and-sql/bc-p/14086942#M180986/" target="_blank" rel="noopener"&gt;https://community.sap.com/t5/technology-blogs-by-sap/sap-datasphere-external-access-overview-apis-cli-and-sql/bc-p/14086942#M180986/&lt;/A&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT size="5" color="#000000"&gt;&lt;STRONG&gt;Step-by-Step Process&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Step 1: Prepare Login.Json file&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;Create OAuth Client with Purpose as Interactive Usage and Redirect URL as &lt;A href="http://localhost:8080/" target="_blank" rel="noopener nofollow noreferrer"&gt;&lt;EM&gt;http://localhost:8080&lt;/EM&gt;&lt;/A&gt;&lt;/P&gt;&lt;P&gt;Get the value of all below fields from the OAuth Client and prepare the Login.json file.&lt;/P&gt;&lt;LI-CODE lang="json"&gt;{
"client_id": "",
"client_secret": "",
"authorization_url": "",
"token_url": "",
"access_token": "",
"refresh_token": ""
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;STRONG&gt;Step 2: Create Model_Object.py file with below code&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;dsp host&lt;/STRONG&gt; : give URL of Datasphere Tenant.&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;secrets_file&lt;/STRONG&gt; : Give Path of Login.json file.&lt;/P&gt;&lt;LI-CODE lang="abap"&gt;import subprocess
import pandas as pd
import sys

def manage_Modeling_Object(Modeling_Object):
    # Step 1: Login to Datasphere using host and secrets file
    dsp_host = '&amp;lt;URL of Datasphere&amp;gt;'
    secrets_file = '&amp;lt;path&amp;gt;/Login.json'
    command = f'datasphere login --host {dsp_host} --secrets-file {secrets_file}'
    subprocess.run(command, shell=True)  # Execute the login command
    
    # Step 2: Retrieve a list of all spaces in JSON format
    command = ['datasphere', 'spaces', 'list', '--json']
    result_spaces = subprocess.run(command, capture_output=True, shell=True, text=True)  # Run the command and capture output
    
    # Step 3: Parse the list of spaces from the command's output
    spaces = result_spaces.stdout.splitlines()  # Split output into individual lines
    
    ModelingObject_data = []  # Initialize a list to store Modeling Object data
    
    # Step 4: Check if the Modeling Object is 'spaces'
    if Modeling_Object == 'spaces':
        for space in spaces:
            if space == "[" or space == "]":
                continue  # Skip brackets in the JSON output
            space_id = space.strip()  # Extract space ID
            
            # Add space details to the data list
            ModelingObject_data.append({
                'Space ID': space_id.replace('"', '').replace(',', ''),
                'Technical Name': space_id.replace('"', '').replace(',', ''),
                'TYPE': Modeling_Object[:-1].upper()  # Set the TYPE as uppercase version of the input Modeling Object name
            })
    
    # Step 5: Process Modeling Objects for each space
    else:
        for space in spaces:
            if space == "[" or space == "]":
                continue  # Skip brackets in the JSON output
            space_id = space.strip()  # Extract space ID
            
            # Step 6: Retrieve Modeling Objects for the current space
            command = ['datasphere', 'objects', Modeling_Object, 'list', '--space', space_id.replace('"', '').replace(',', '')]
            result_ModelingObject = subprocess.run(command, capture_output=True, shell=True, text=True)  # Run the command
            
            # Step 7: Parse the Modeling Object data from the output
            ModelingObject_info = result_ModelingObject.stdout.splitlines()  # Split output into individual lines
            print("Checking "+Modeling_Object.upper()+" for space : "+space_id.replace('"', '').replace(',', ''))  # Log the space being checked
            
            # Step 8: Process each Modeling Object
            if len(ModelingObject_info) &amp;gt; 1:
                for flow in ModelingObject_info:
                    if '{' in flow or '}' in flow or '[' in flow or ']' in flow:
                        continue  # Skip brackets or braces in the output
                    cleaned_flow = flow.replace('"technicalName":', '').replace('"', '').strip()  # Clean up the output
                    
                    # Step 9: Add Modeling Object details to the data list
                    ModelingObject_data.append({
                        'Space ID': space_id.replace('"', '').replace(',', ''),
                        'Technical Name': cleaned_flow,
                        'TYPE': Modeling_Object[:-1].upper()  # Set the TYPE as uppercase version of the input Modeling Object name
                    })
    
    # Step 10: Write the collected data into a CSV file
    if ModelingObject_data:
        df = pd.DataFrame(ModelingObject_data)  # Create a DataFrame from the data list
        df.to_csv(Modeling_Object.upper()+'.csv', index=False)  # Save the DataFrame to a CSV file without the index
        print("Space vise all "+Modeling_Object.upper()+" have been written to "+Modeling_Object.upper()+".csv.")  # Log success message
    else:
        print("No Modeling Objects found.")  # Log message if no data was collected
    
    print('------------------------------------------------------------------------------------------------------------------------------------')  # Separator for readability
        
if __name__ == "__main__":
    # Check if an argument is provided via the command line
    if len(sys.argv) &amp;gt; 1:
        # Pass the first argument to the method
        manage_Modeling_Object(sys.argv[1])
    else:
        print("Please provide a Modeling Object name as an argument.")  # Log error message if no argument is provided
        
# Execute for predefined Modeling Objects
manage_Modeling_Object('remote-tables')
manage_Modeling_Object('local-tables')
manage_Modeling_Object('views')
manage_Modeling_Object('intelligent-lookups')
manage_Modeling_Object('data-flows')
manage_Modeling_Object('replication-flows')
manage_Modeling_Object('transformation-flows')
manage_Modeling_Object('task-chains')
manage_Modeling_Object('analytic-models')
manage_Modeling_Object('data-access-controls')&lt;/LI-CODE&gt;&lt;P&gt;&lt;STRONG&gt;Step 3: Open command prompt and execute the Model_Objects.py file&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="vikasparmar88_0-1745646785952.png" style="width: 400px;"&gt;&lt;img src="https://community.sap.com/t5/image/serverpage/image-id/255123i23E96AEF92C443CA/image-size/medium?v=v2&amp;amp;px=400" role="button" title="vikasparmar88_0-1745646785952.png" alt="vikasparmar88_0-1745646785952.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Once the program execution is done it will generate CSV files for all the Datasphere artifactes mention in python code&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="vikasparmar88_1-1745646877909.png" style="width: 400px;"&gt;&lt;img src="https://community.sap.com/t5/image/serverpage/image-id/255124i2D0343903B2ECFC7/image-size/medium?v=v2&amp;amp;px=400" role="button" title="vikasparmar88_1-1745646877909.png" alt="vikasparmar88_1-1745646877909.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;each CSV file will have 3 columns :&amp;nbsp;&lt;/P&gt;&lt;P&gt;1) Space ID : Name of the space&lt;/P&gt;&lt;P&gt;2) Technical Name : Exact Technical Name of Object&amp;nbsp;&lt;/P&gt;&lt;P&gt;3) Type : Type of Object&amp;nbsp;( i.e view, local-table, remote-table, replication flw etc)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;H1 id="toc-hId-1382837983"&gt;Conclusion&lt;/H1&gt;&lt;P&gt;This script demonstrates how Python and SAP Datasphere CLI can collaborate to streamline artifact management and export data systematically. By following the steps provided, users can extend or adapt the code to suit their requirements.&lt;/P&gt;</description>
      <pubDate>Sat, 26 Apr 2025 06:25:43 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/sap-datasphere-cli-amp-python-exporting-modeling-objects-to-csv-files-for/qaq-p/14087055</guid>
      <dc:creator>vikasparmar88</dc:creator>
      <dc:date>2025-04-26T06:25:43Z</dc:date>
    </item>
    <item>
      <title>Re: SAP Datasphere CLI &amp; Python: Exporting Modeling Objects to CSV Files for Each Artifact</title>
      <link>https://community.sap.com/t5/technology-q-a/sap-datasphere-cli-amp-python-exporting-modeling-objects-to-csv-files-for/qaa-p/14087481#M4911984</link>
      <description>Great post</description>
      <pubDate>Sun, 27 Apr 2025 08:07:31 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/sap-datasphere-cli-amp-python-exporting-modeling-objects-to-csv-files-for/qaa-p/14087481#M4911984</guid>
      <dc:creator>vishal_parmar2</dc:creator>
      <dc:date>2025-04-27T08:07:31Z</dc:date>
    </item>
    <item>
      <title>Re: SAP Datasphere CLI &amp; Python: Exporting Modeling Objec...</title>
      <link>https://community.sap.com/t5/technology-q-a/sap-datasphere-cli-amp-python-exporting-modeling-objects-to-csv-files-for/qaa-p/14088077#M4912070</link>
      <description>&lt;P&gt;Thank you for sharing. Have you considered going via APIs (instead of managing CLI commands) as an alternative?&lt;/P&gt;&lt;P&gt;&lt;A href="https://api.sap.com/api/Catalog/resource/SAP_Data_Warehouse_Cloud_Consumption_Catalog" target="_blank"&gt;https://api.sap.com/api/Catalog/resource/SAP_Data_Warehouse_Cloud_Consumption_Catalog&lt;/A&gt;&lt;/P&gt;&lt;P&gt;I haven't tried; therefore, I am interested to hear your opinion.&lt;/P&gt;</description>
      <pubDate>Mon, 28 Apr 2025 09:15:27 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/sap-datasphere-cli-amp-python-exporting-modeling-objects-to-csv-files-for/qaa-p/14088077#M4912070</guid>
      <dc:creator>Vitaliy-R</dc:creator>
      <dc:date>2025-04-28T09:15:27Z</dc:date>
    </item>
    <item>
      <title>Re: SAP Datasphere CLI &amp; Python: Exporting Modeling Objects to CSV Files for Each Artifact</title>
      <link>https://community.sap.com/t5/technology-q-a/sap-datasphere-cli-amp-python-exporting-modeling-objects-to-csv-files-for/qaa-p/14088081#M4912073</link>
      <description>Great tutorial on automating SAP Datasphere object extraction!</description>
      <pubDate>Mon, 28 Apr 2025 09:18:32 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/sap-datasphere-cli-amp-python-exporting-modeling-objects-to-csv-files-for/qaa-p/14088081#M4912073</guid>
      <dc:creator>michal_majer</dc:creator>
      <dc:date>2025-04-28T09:18:32Z</dc:date>
    </item>
    <item>
      <title>Re: SAP Datasphere CLI &amp; Python: Exporting Modeling Objec...</title>
      <link>https://community.sap.com/t5/technology-q-a/sap-datasphere-cli-amp-python-exporting-modeling-objects-to-csv-files-for/qaa-p/14088124#M4912087</link>
      <description>Nope I haven't tried via api still but surely check this</description>
      <pubDate>Mon, 28 Apr 2025 09:50:45 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/sap-datasphere-cli-amp-python-exporting-modeling-objects-to-csv-files-for/qaa-p/14088124#M4912087</guid>
      <dc:creator>vikasparmar88</dc:creator>
      <dc:date>2025-04-28T09:50:45Z</dc:date>
    </item>
  </channel>
</rss>

