{"id":800,"date":"2018-07-17T06:36:00","date_gmt":"2018-07-17T06:36:00","guid":{"rendered":"https:\/\/www.aiproblog.com\/index.php\/2018\/07\/17\/remotely-send-r-and-python-execution-to-sql-server-from-jupyter-notebooks\/"},"modified":"2018-07-17T06:36:00","modified_gmt":"2018-07-17T06:36:00","slug":"remotely-send-r-and-python-execution-to-sql-server-from-jupyter-notebooks","status":"publish","type":"post","link":"https:\/\/www.aiproblog.com\/index.php\/2018\/07\/17\/remotely-send-r-and-python-execution-to-sql-server-from-jupyter-notebooks\/","title":{"rendered":"Remotely Send R and Python Execution to SQL Server from Jupyter Notebooks"},"content":{"rendered":"<p>Author: Kyle Weller<\/p>\n<div>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/kywe665\/MLServicesTutorials\/master\/images\/RemoteCompute.png\" alt=\"Importpng\" title=\"New Jupyter Notebook\" width=\"678\" height=\"382\"><\/p>\n<h1>Introduction<\/h1>\n<p>Did you know that you can execute R and Python code remotely in SQL Server from Jupyter Notebooks or any IDE? <a href=\"https:\/\/aka.ms\/SQLMLOverview\">Machine Learning Services<\/a> in SQL Server eliminates the need to move data around. Instead of transferring large and sensitive data over the network or losing accuracy on ML training with sample csv files, you can have your R\/Python code execute within your database. You can work in Jupyter Notebooks, RStudio, PyCharm, VSCode, Visual Studio, wherever you want, and then send function execution to SQL Server bringing intelligence to where your data lives.<\/p>\n<p>This tutorial will show you an example of how you can send your python code from Juptyter notebooks to execute within SQL Server. The same principles apply to R and any other IDE as well. If you prefer to learn through videos, this tutorial is also published on YouTube.<\/p>\n<p><a href=\"https:\/\/youtu.be\/D5erljpJDjE\">https:\/\/youtu.be\/D5erljpJDjE<\/a><\/p>\n<p><a href=\"http:\/\/www.youtube.com\/watch?v=D5erljpJDjE\"><img decoding=\"async\" src=\"http:\/\/img.youtube.com\/vi\/D5erljpJDjE\/0.jpg\" alt=\"httpsyoutubeD5erljpJDjE\"><\/a><\/p>\n<h1>Environment Setup Prerequisites<\/h1>\n<h4>1. Install ML Services on SQL Server<\/h4>\n<p>In order for R or Python to execute within SQL, you first need the Machine Learning Services feature installed and configured. See this <a href=\"https:\/\/aka.ms\/SetupMLServices\">how-to guide<\/a>.<\/p>\n<h4>2. Install RevoscalePy via Microsoft&#8217;s Python Client<\/h4>\n<p>In order to send Python execution to SQL from Jupyter Notebooks, you need to use Microsoft&#8217;s RevoscalePy package. To get RevoscalePy, download and install Microsoft&#8217;s ML Services Python Client. <a href=\"https:\/\/docs.microsoft.com\/en-us\/machine-learning-server\/install\/python-libraries-interpreter\">Documentation Page<\/a> or <a href=\"https:\/\/aka.ms\/mls93-py\">Direct Download Link<\/a> (for Windows).<\/p>\n<p>After downloading, open powershell as an administrator and navigate to the download folder. Start the installation with this command (feel free to customize the install folder):<\/p>\n<p><code><em>.Install-PyForMLS.ps1 -InstallFolder \"C:Program FilesMicrosoftPythonClient\"<\/em><\/code><\/p>\n<p>Be patient while the installation can take a little while. Once installed navigate to the new path you installed in. Let&#8217;s make an empty folder and open Jupyter Notebooks:<\/p>\n<p><code><em>mkdir JupyterNotebooks; cd JupyterNotebooks; ..Scriptsjupyter-notebook<\/em><\/code><\/p>\n<p>Create a new notebook with the Python 3 interpreter:<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0<img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/kywe665\/MLServicesTutorials\/master\/images\/JupyterNew.png\" alt=\"NewNotebookpng\" title=\"New Jupyter Notebook\"><\/p>\n<p>To test if everything is setup, import revoscalepy in the first cell and execute. If there are no error messages you are ready to move forward.<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0<img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/kywe665\/MLServicesTutorials\/master\/images\/importrevopy.png\" alt=\"Importpng\" title=\"New Jupyter Notebook\"><\/p>\n<h1>Database Setup (Required for this tutorial only)<\/h1>\n<p>For the rest of the tutorial you can clone <a href=\"https:\/\/aka.ms\/RemoteExecJupyter\">this Jupyter Notebook from Github<\/a> if you don&#8217;t want to copy paste all of the code. This database setup is a one time step to ensure you have the same data as this tutorial. You don&#8217;t need to perform any of these setup steps to use your own data.<\/p>\n<h4>1. Create a database<\/h4>\n<p>Modify the connection string for your server and use pyodbc to create a new database.<\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0000aa;\">import<\/span> <span style=\"color: #00aaaa; text-decoration: underline;\">pyodbc<\/span>  <br><span style=\"color: #aaaaaa; font-style: italic;\"># creating a new db to load Iris sample in <br><\/span>new_db_name = <span style=\"color: #aa5500;\">\"MLRemoteExec\"<\/span> connection_string = <span style=\"color: #aa5500;\">\"Driver=SQL Server;Server=localhostMSSQLSERVER2017;Database={0};Trusted_Connection=Yes;\"<\/span> <br>\ncnxn = pyodbc.connect(connection_string.format(<span style=\"color: #aa5500;\">\"master\"<\/span>), autocommit=<span style=\"color: #00aaaa;\">True<\/span>) <br>\ncnxn.cursor().execute(<span style=\"color: #aa5500;\">\"IF EXISTS(SELECT * FROM sys.databases WHERE [name] = '{0}') DROP DATABASE {0}\"<\/span>.format(new_db_name)) <br>\ncnxn.cursor().execute(<span style=\"color: #aa5500;\">\"CREATE DATABASE \"<\/span> + new_db_name)<br>\ncnxn.close()<br><span style=\"color: #0000aa;\">print<\/span>(<span style=\"color: #aa5500;\">\"Database created\"<\/span>) <\/pre>\n<\/div>\n<h4>2. Import Iris sample from SkLearn<\/h4>\n<p>Iris is a popular dataset for beginner data science tutorials. It is included by default in sklearn package.<\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0000aa;\">from<\/span> <span style=\"color: #00aaaa; text-decoration: underline;\">sklearn<\/span> <span style=\"color: #0000aa;\">import<\/span> datasets<span style=\"color: #0000aa;\">import<\/span> <span style=\"color: #00aaaa; text-decoration: underline;\">pandas<\/span> <span style=\"color: #0000aa;\">as<\/span> <span style=\"color: #00aaaa; text-decoration: underline;\">pd<br><\/span><span style=\"color: #aaaaaa; font-style: italic;\"># SkLearn has the Iris sample dataset built in to the package<\/span>iris = datasets.load_iris()<br>df = pd.DataFrame(iris.data, columns=iris.feature_names)<\/pre>\n<\/div>\n<h4>3. Use RecoscalePy APIs to create a table and load the Iris data<\/h4>\n<p><em>(You can also do this with pyodbc, sqlalchemy or other packages)<\/em><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0000aa;\">from<\/span> <span style=\"color: #00aaaa; text-decoration: underline;\">revoscalepy<\/span> <span style=\"color: #0000aa;\">import<\/span> RxSqlServerData, rx_data_step<br><span style=\"color: #aaaaaa; font-style: italic;\"># Example of using RX APIs to load data into SQL table. You can also do this with pyodbc<br><\/span>table_ref = RxSqlServerData(connection_string=connection_string.format(new_db_name), table=<span style=\"color: #aa5500;\">\"Iris\"<\/span>)rx_data_step(input_data = df, output_file = table_ref, overwrite = <span style=\"color: #00aaaa;\">True<\/span>)<span style=\"color: #0000aa;\">print<\/span>(<span style=\"color: #aa5500;\">\"New Table Created: Iris\"<\/span>)\n<span style=\"color: #0000aa;\">print<\/span>(<span style=\"color: #aa5500;\">\"Sklearn Iris sample loaded into Iris table\"<\/span>)<br><\/pre>\n<\/div>\n<h1>Define a Function to Send to SQL Server<\/h1>\n<p>Write any python code you want to execute in SQL. In this example we are creating a scatter matrix on the iris dataset and only returning the bytestream of the .png back to Jupyter Notebooks to render on our client.<\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0000aa;\">def<\/span> <span style=\"color: #00aa00;\">send_this_func_to_sql<\/span>():<br><span style=\"color: #0000aa;\">    from<\/span> <span style=\"color: #00aaaa; text-decoration: underline;\">revoscalepy<\/span> <span style=\"color: #0000aa;\">import<\/span> RxSqlServerData, rx_import<br><span style=\"color: #0000aa;\">from<\/span> <span style=\"color: #00aaaa; text-decoration: underline;\">pandas.tools.plotting<\/span> <span style=\"color: #0000aa;\">import<\/span> scatter_matrix<br><span style=\"color: #0000aa;\">import<\/span> <span style=\"color: #00aaaa; text-decoration: underline;\">matplotlib.pyplot<\/span> <span style=\"color: #0000aa;\">as<\/span> <span style=\"color: #00aaaa; text-decoration: underline;\">plt<\/span>    <span style=\"color: #0000aa;\">import<\/span> <span style=\"color: #00aaaa; text-decoration: underline;\">io<\/span>    <span style=\"color: #aaaaaa; font-style: italic;\"># remember the scope of the variables in this func are within our SQL Server Python Runtime<\/span><br>\n    connection_string = <span style=\"color: #aa5500;\">\"Driver=SQL Server;Server=localhostMSSQLSERVER2017;Database=MLRemoteExec;Trusted_Connection=Yes;\"<br><\/span><span style=\"color: #aaaaaa; font-style: italic;\">    # specify a query and load into pandas dataframe df<\/span><br>\n    sql_query = RxSqlServerData(connection_string=connection_string, sql_query = <span style=\"color: #aa5500;\">\"select * from Iris\"<\/span>)<br>\ndf = rx_import(sql_query)<br>\nscatter_matrix(df)<br><span style=\"color: #aaaaaa; font-style: italic;\"># return bytestream of image created by scatter_matrix<\/span><br>\nbuf = io.BytesIO()<br>\nplt.savefig(buf, format=<span style=\"color: #aa5500;\">\"png\"<\/span>)<br>\nbuf.seek(<span style=\"color: #009999;\">0<\/span>)<br><span style=\"color: #0000aa;\">return<\/span> buf.getvalue()<br><\/pre>\n<\/div>\n<h1>Send execution to SQL<\/h1>\n<p>Now that we are finally set up, check out how easy sending remote execution really is! First, <code>import revoscalepy<\/code>. Create a <code>sql_compute_context<\/code>, and then send the execution of any function seamlessly to SQL Server with <code>RxExec<\/code>. No raw data had to be transferred from SQL to the Jupyter Notebook. All computation happened within the database and only the image file was returned to be displayed.<\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0000aa;\">from<\/span> <span style=\"color: #00aaaa; text-decoration: underline;\">IPython<\/span> <span style=\"color: #0000aa;\">import<\/span> display<br><span style=\"color: #0000aa;\">import<\/span> <span style=\"color: #00aaaa; text-decoration: underline;\">matplotlib.pyplot<\/span> <span style=\"color: #0000aa;\">as<\/span> <span style=\"color: #00aaaa; text-decoration: underline;\">plt<\/span> <br><span style=\"color: #0000aa;\">from<\/span> <span style=\"color: #00aaaa; text-decoration: underline;\">revoscalepy<\/span> <span style=\"color: #0000aa;\">import<\/span> RxInSqlServer, rx_exec<span style=\"color: #aaaaaa; font-style: italic;\"># create a remote compute context with connection to SQL Server<\/span><br>\nsql_compute_context = RxInSqlServer(connection_string=connection_string.format(new_db_name))<br><span style=\"color: #aaaaaa; font-style: italic;\"># use rx_exec to send the function execution to SQL Server<\/span><br>\nimage = rx_exec(send_this_func_to_sql, compute_context=sql_compute_context)[<span style=\"color: #009999;\">0<\/span>]<br><span style=\"color: #aaaaaa; font-style: italic;\"># only an image was returned to my jupyter client. All data remained secure and was manipulated in my db.<\/span><br>\ndisplay.Image(data=image)<\/pre>\n<\/div>\n<p>While this example is trivial with the Iris dataset, imagine the additional scale, performance, and security capabilities that you now unlocked. You can use any of the latest open source R\/Python packages to build Deep Learning and AI applications on large amounts of data in SQL Server. We also offer <a href=\"https:\/\/cloudblogs.microsoft.com\/sqlserver\/2016\/10\/11\/1000000-predictions-per-second\/\">leading edge<\/a>, high-performance algorithms in Microsoft&#8217;s <a href=\"https:\/\/docs.microsoft.com\/en-us\/machine-learning-server\/r-reference\/revoscaler\/revoscaler\">RevoScaleR<\/a> and <a href=\"https:\/\/docs.microsoft.com\/en-us\/sql\/advanced-analytics\/python\/what-is-revoscalepy?view=sql-server-2017\">RevoScalePy<\/a> APIs. Using these with the latest innovations in the open source world allows you to bring unparalleled selection, performance, and scale to your applications.<\/p>\n<h1>Learn More<\/h1>\n<p>Check out <a href=\"https:\/\/aka.ms\/SQLMLDocs\">SQL Machine Learning Services Documentation<\/a> to learn how you can easily deploy your R\/Python code with SQL stored procedures making them accessible in your ETL processes or to any application. Train and store machine learning models in your database bringing intelligence to where your data lives.<\/p>\n<p>Basic R and Python Execution in SQL Server: <a href=\"https:\/\/aka.ms\/BasicMLServicesExecution\"><\/a><a href=\"https:\/\/aka.ms\/BasicMLServicesExecution\">https:\/\/aka.ms\/BasicMLServicesExecution<\/a><\/p>\n<p>Set up Machine Learning Services in SQL Server: <a href=\"https:\/\/aka.ms\/SetupMLServices\"><\/a><a href=\"https:\/\/aka.ms\/SetupMLServices\">https:\/\/aka.ms\/SetupMLServices<\/a><\/p>\n<p>End-to-end tutorial solutions on Github: <a href=\"https:\/\/microsoft.github.io\/sql-ml-tutorials\/\"><\/a><a href=\"https:\/\/microsoft.github.io\/sql-ml-tutorials\/\">https:\/\/microsoft.github.io\/sql-ml-tutorials\/<\/a><\/p>\n<\/p>\n<p>Other YouTube Tutorials:<\/p>\n<p>How to Install SQL Server Machine Learning Services: <a href=\"https:\/\/www.youtube.com\/redirect?v=kwudtriqLcA&#038;redir_token=Jn7Yp3BkUdO2cePtZURpC4suvb98MTUyODMyNDM2M0AxNTI4MjM3OTYz&#038;event=video_description&#038;q=https%3A%2F%2Faka.ms%2FInstallMLServices\"><\/a><a href=\"https:\/\/aka.ms\/InstallMLServices\">https:\/\/aka.ms\/InstallMLServices<\/a><\/p>\n<p>How to Enable SQL Server Machine Learning Services: <a href=\"https:\/\/www.youtube.com\/redirect?v=kwudtriqLcA&#038;redir_token=Jn7Yp3BkUdO2cePtZURpC4suvb98MTUyODMyNDM2M0AxNTI4MjM3OTYz&#038;event=video_description&#038;q=https%3A%2F%2Faka.ms%2FEnableMLServices\"><\/a><a href=\"https:\/\/aka.ms\/EnableMLServices\">https:\/\/aka.ms\/EnableMLServices<\/a><\/p>\n<p>Basics of R and Python Execution in SQL: <a href=\"https:\/\/www.youtube.com\/redirect?v=kwudtriqLcA&#038;redir_token=Jn7Yp3BkUdO2cePtZURpC4suvb98MTUyODMyNDM2M0AxNTI4MjM3OTYz&#038;event=video_description&#038;q=https%3A%2F%2Faka.ms%2FExecuteMLServices\"><\/a><a href=\"https:\/\/aka.ms\/ExecuteMLServices\">https:\/\/aka.ms\/ExecuteMLServices<\/a><\/p>\n<\/p>\n<p>Keywords: #MachineLearning #DataScience #Python #R #Jupyter #JupyterNotebook #JupyterNotebooks #RStudio #SQL #SQLServer #BigData #DataAnalytics #ScikitLearn #Data #AI #ArtificialIntelligence<\/p>\n<\/div>\n<p><a href=\"https:\/\/www.datasciencecentral.com\/xn\/detail\/6448529:BlogPost:744052\">Go to Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Author: Kyle Weller Introduction Did you know that you can execute R and Python code remotely in SQL Server from Jupyter Notebooks or any IDE? [&hellip;] <span class=\"read-more-link\"><a class=\"read-more\" href=\"https:\/\/www.aiproblog.com\/index.php\/2018\/07\/17\/remotely-send-r-and-python-execution-to-sql-server-from-jupyter-notebooks\/\">Read More<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":801,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"footnotes":""},"categories":[26],"tags":[],"_links":{"self":[{"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/posts\/800"}],"collection":[{"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/comments?post=800"}],"version-history":[{"count":0,"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/posts\/800\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/media\/801"}],"wp:attachment":[{"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/media?parent=800"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/categories?post=800"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/tags?post=800"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}