How to Retrieve Tableau Server User Information and Data Sources
Written on
Introduction to Tableau Server
Tableau Server is a robust platform that enables users to utilize Tableau's features without the need to download or open workbooks in Tableau Desktop. Similar to any other server, it provides secure storage and fosters a collaborative workspace. When Tableau users want to share their established data connections, they publish data sources to the server. Understanding how to retrieve user details and data sources from a plethora of data sources is crucial.
Understanding Tableau Server Client (TSC)
The Tableau Server Client (TSC) is a Python library that interfaces with the Tableau Server REST API. You can read more about TSC in the official documentation. In environments where numerous users—potentially thousands—publish a vast array of data sources, it can become chaotic if there’s no clarity regarding who published a specific source or which sources are linked to a particular user.
Each data source on the server has a unique identifier (id) alongside an owner identifier (owner_id), which reveals the user who published the data source, corresponding to the user identifier (user_id).
Before we proceed, ensure that you have Python 3 installed. If not, consider downloading Anaconda, which comes with essential Python packages. In this guide, you will learn how to extract user details and data sources from Tableau Server using the TSC library.
Step 1: Establish a Connection to Tableau Server
Begin by connecting to the Tableau Server with the TSC library. Input the following lines of code, replacing 'Username' and 'Password' with your credentials:
import tableauserverclient as TSC
import pandas as pd
tableau_auth = TSC.TableauAuth('Username', 'Password')
server = TSC.Server('http://3.227.165.186')
request_options = TSC.RequestOptions(pagesize=1000)
with server.auth.sign_in(tableau_auth):
Step 2: Retrieve the List of Users
Now, fetch the list of all users on the Tableau Server. This command will store each user’s ID and name in a dictionary format:
all_users = list(TSC.Pager(server.users, request_options))
userinfo = [{user.id: user.name} for user in all_users]
Step 3: Gather All Data Sources
Next, collect information on all data sources and their unique identifiers available on the server:
all_datasources = list(TSC.Pager(server.datasources, request_options))
datasource_ids = [datasource.id for datasource in all_datasources]
Step 4: Utilize Pandas for Data Handling
Using the Pandas library, you can organize user IDs, user names, data source IDs, and data source owner IDs into a DataFrame for easier manipulation. First, input the following code to create a DataFrame with user details:
datasource_owner_id = [{datasource.name: datasource.owner_id} for datasource in all_datasources]
print('The datasource name and the corresponding datasource owner_id for the default site is as follows:', datasource_owner_id)
server.auth.sign_out()
Next, prepare to build a DataFrame with user information by creating lists for user IDs and names:
key22, value22 = [], []
for userdiction in userinfo:
for key2, value2 in userdiction.items():
key22.append(key2)
value22.append(value2)
df = pd.DataFrame({"datasource_owner_id": key22, "Name": value22})
Now, loop through the datasource_owner_id dictionary to create a second DataFrame containing datasource names and their respective owner IDs:
key1, value1 = [], []
for data in datasource_owner_id:
for key, value in data.items():
key1.append(key)
value1.append(value)
df2 = pd.DataFrame({"datasource_name": key1, "datasource_owner_id": value1})
At this stage, you have two DataFrames: df for user IDs and names, and df2 for data sources and their owners.
Step 5: Map Users to Their Published Data Sources
Now, merge the two DataFrames to associate users with the data sources they have published:
result = pd.merge(df, df2[['datasource_name', 'datasource_owner_id']], on='datasource_owner_id')
Step 6: Export the Results to a CSV File
Finally, display the resulting table and save it as a CSV file:
print(result)
result.to_csv('output.csv')
The resulting table will organize users with their respective published data sources, making it easier to navigate through the data.
Conclusion
Thank you for reading! If you found this guide useful, please leave a comment and share it with your peers.
Learn how to publish data sources to Tableau Server and Tableau Cloud in this beginner-friendly tutorial.
This video provides a step-by-step guide on connecting to various data sources in Tableau, aimed at beginners.