<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
  version="2.0">
  <channel>
    <title>Carl's Blog</title>
    <atom:link
      href="https://docs.microsoft.com/archive/blogs/carlnol/feed.xml"
      rel="self"
      type="application/rss+xml" />
    <link>https://docs.microsoft.com/archive/blogs/carlnol/feed.xml</link>
    <description>Carl Nolan&amp;#039;s ramblings on development and data processing</description>
    <lastBuildDate>Tue, 16 Apr 2019 04:51:18 GMT</lastBuildDate>
    <language>en-US</language>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    <item>
      <title>Pushing Hadoop Cluster Configuration Changes using PowerShell</title>
      <link>https://docs.microsoft.com/archive/blogs/carlnol/pushing-hadoop-cluster-configuration-changes-using-powershell</link>
      <pubDate>Wed, 02 Apr 2014 10:53:17 GMT</pubDate>
      <dc:creator><![CDATA[Carl Nolan]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.cloudplatform.qa.msdn.wdslab.com/carlnol/2014/04/02/pushing-hadoop-cluster-configuration-changes-using-powershell/</guid>
      <description><![CDATA[In my previous post I talked about Implementing and Deploying Rack Awareness using PowerShell....]]></description>
      <content:encoded><![CDATA[<p>In my previous post I talked about <a href="http://blogs.msdn.com/b/carlnol/archive/2014/03/14/implementing-hadoop-rack-awareness-with-powershell.aspx" target="_blank">Implementing</a> and <a href="http://blogs.msdn.com/b/carlnol/archive/2014/03/19/deploying-hadoop-rack-awareness-with-powershell.aspx" target="_blank">Deploying</a> Rack Awareness using PowerShell. However PowerShell is a great tool for not only managing things like Rack Awareness but for installing and managing the Hadoop cluster; especially for managing configuration changes, the focus of this post.</p>  <p>All the files relating to post can be found <a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/76/6786.hadoop-configuration.zip" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-76/6786.hadoop_2D00_configuration.zip" target="_blank">here</a>, along with the script for rack awareness.</p>  <p>So what would be involved in pushing out a set of configuration changes to all nodes in the cluster? </p>  <p>If you consider each node in the cluster you would need:</p>  <ul>   <li>A script that modifies a property name/value pair for a given configuration file on the cluster node itself</li>    <li>A script, along with corresponding configuration specifications, for making all the required configuration changes that are required on the cluster node</li> </ul>  <p>So lets tackle these first.</p>  <p>As a recap, within each configuration file (source) the property names and values have the following format:</p>  <pre style="background: white; color: black; font-family: consolas;"><span style="color: blue;">&lt;</span><span style="color: rgb(163, 21, 21);">property</span><span style="color: blue;">&gt;</span><br /><span style="color: blue;">&#160; &lt;</span><span style="color: rgb(163, 21, 21);">name</span><span style="color: blue;">&gt;</span>topology.script.file.name<span style="color: blue;">&lt;/</span><span style="color: rgb(163, 21, 21);">name</span><span style="color: blue;">&gt;</span><br /><span style="color: blue;">&#160; &lt;</span><span style="color: rgb(163, 21, 21);">value</span><span style="color: blue;">&gt;</span>hadoop-rack-configuration.cmd<span style="color: blue;">&lt;/</span><span style="color: rgb(163, 21, 21);">value</span><span style="color: blue;">&gt;</span><br /><span style="color: blue;">&lt;/</span><span style="color: rgb(163, 21, 21);">property</span><span style="color: blue;">&gt;</span></pre>

<p>Thus, the starting point for managing configuration changes would be a set of configuration changes that one would like to make to the cluster nodes. In this implementation the configuration file would look like the following:</p>

<pre style="background: white; color: black; font-family: consolas;"><strong>configure-cluster-properties.txt</strong></pre>

<pre style="background: white; color: black; font-family: consolas;"># YARN Configuration<br />yarn-site.xml, yarn.nodemanager.resource.memory-mb, 8192<br />yarn-site.xml, yarn.scheduler.minimum-allocation-mb, 2048<br /> <br /># MR Configurations<br />mapred-site.xml, mapreduce.map.memory.mb, 2048<br />mapred-site.xml, mapreduce.reduce.memory.mb, 4096<br />mapred-site.xml, mapreduce.map.java.opts, -Xmx1536m<br />mapred-site.xml, mapreduce.reduce.java.opts, -Xmx3072m</pre>

<p>This is basically a list of the configuration source, property name, and property values, comma separated, for all the required changes to be made to each node.</p>

<p>Based on these three values as inputs to make a single configuration change the PowerShell script would be as follows:</p>

<pre style="background: white; color: black; font-family: consolas;"><strong>configure-cluster-property.ps1</strong></pre>

<pre style="background: white; color: black; font-family: consolas;"><span style="color: blue;">param</span>([<span style="color: rgb(43, 145, 175);">string</span>] $config_file_name, [<span style="color: rgb(43, 145, 175);">string</span>] $config_property_name, [<span style="color: rgb(43, 145, 175);">string</span>] $config_property_value)<br /> <br /><span style="color: blue;">if</span>(<span style="color: rgb(43, 145, 175);">Test-Path</span> -Path $config_file_name -PathType Leaf) {<br />&#160;&#160;&#160; $conf_doc = [<span style="color: rgb(43, 145, 175);">System.Xml.XmlDocument</span>](<span style="color: rgb(43, 145, 175);">Get-Content</span> $config_file_name)<br /> <br />&#160;&#160;&#160; $property_node = ($conf_doc.DocumentElement.property | <span style="color: rgb(43, 145, 175);">Where-Object</span> {$_.name -eq $config_property_name})<br />&#160;&#160;&#160; <span style="color: blue;">If</span> ($property_node) {<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green;"># Element found so ensure the property is correctly set</span><br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(43, 145, 175);">write-host</span>&#160;<span style="color: rgb(163, 21, 21);">&quot;</span>$config_property_name<span style="color: rgb(163, 21, 21);"> Element Found, so updating value to </span>$config_property_value<span style="color: rgb(163, 21, 21);">...&quot;</span><br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; $property_node.Value = $config_property_value<br />&#160;&#160;&#160; } <span style="color: blue;">else</span> {<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green;"># No Element found so add a new one to the document</span><br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(43, 145, 175);">write-host</span>&#160;<span style="color: rgb(163, 21, 21);">&quot;</span>$config_property_name<span style="color: rgb(163, 21, 21);"> Element Not Present, adding new element...&quot;</span><br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; $property_element = $conf_doc.CreateElement(<span style="color: rgb(163, 21, 21);">&quot;property&quot;</span>)<br /> <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; $property_element_name = $conf_doc.CreateElement(<span style="color: rgb(163, 21, 21);">&quot;name&quot;</span>)<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; $property_element_name.AppendChild($conf_doc.CreateTextNode($config_property_name))<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; $property_element.AppendChild($property_element_name)<br /> <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; $property_element_value = $conf_doc.CreateElement(<span style="color: rgb(163, 21, 21);">&quot;value&quot;</span>)<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; $property_element_value.AppendChild($conf_doc.CreateTextNode($config_property_value))<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; $property_element.AppendChild($property_element_value)<br /> <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; $conf_doc.DocumentElement.AppendChild($property_element)<br />&#160;&#160;&#160; }<br /> <br />&#160;&#160;&#160; $conf_doc.Save($config_file_name)<br />} <span style="color: blue;">else</span> {<br />&#160;&#160;&#160; <span style="color: rgb(43, 145, 175);">Write-Error</span>&#160;<span style="color: rgb(163, 21, 21);">&quot;Configuration File </span>$config_file_name<span style="color: rgb(163, 21, 21);"> cannot be found&quot;</span><br />}</pre>

<p>This script opens the configuration document as an XmlDocument and then locates the property Element, where the name Element matches the one requiring modification. If the corresponding property Element is found then its value is changed. If the Element is not found then the property Element is added to the document.</p>

<p>To use this script the input file of configuration changes just needs to be parsed and for each configuration line a call is made to the property change modification script:</p>

<pre style="background: white; color: black; font-family: consolas;"><strong>configure-cluster-properties.ps1</strong></pre>

<pre style="background: white; color: black; font-family: consolas;"><span style="color: blue;">param</span>([<span style="color: rgb(43, 145, 175);">string</span>] $working_path, [<span style="color: rgb(43, 145, 175);">string</span>] $configuration_file)<br /> <br />$hadoop_config_dir = $env:HADOOP_CONF_DIR<br />$configurationFile = <span style="color: rgb(163, 21, 21);">&quot;</span>$working_path<span style="color: rgb(163, 21, 21);">\</span>$configuration_file<span style="color: rgb(163, 21, 21);">&quot;</span>;<br /> <br /><span style="color: blue;">function</span> Get-PropertiesTable {<br /> <br />&#160;&#160;&#160; $properties = @(); <br /> <br />&#160;&#160;&#160; <span style="color: blue;">if</span> (<span style="color: rgb(43, 145, 175);">Test-Path</span> -Path $configurationFile -PathType Leaf) {&#160;&#160;&#160; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; $propertylines = <span style="color: rgb(43, 145, 175);">Get-Content</span> $configurationFile<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue;">foreach</span> ($propertyline <span style="color: blue;">in</span> $propertylines)<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; {<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; $propertyline = $propertyline.Trim()<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue;">if</span> (($propertyline) -and (-not $propertyline.StartsWith(<span style="color: rgb(163, 21, 21);">&quot;#&quot;</span>)))<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; $propertyline_values = ,@($propertyline -Split <span style="color: rgb(163, 21, 21);">&quot;,|\t&quot;</span>) | <span style="color: rgb(43, 145, 175);">%</span> {$_.Trim()}<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue;">if</span> ($propertyline_values.length -eq 3) {<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; $hdp_file = $propertyline_values[0]<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; $properties += <span style="color: rgb(43, 145, 175);">New-Object</span> PSObject –Property @{ConfigFile=<span style="color: rgb(163, 21, 21);">&quot;</span>$hadoop_config_dir<span style="color: rgb(163, 21, 21);">\</span>$hdp_file<span style="color: rgb(163, 21, 21);">&quot;</span>; PropertyName=$propertyline_values[1]; PropertyValue=$propertyline_values[2]}<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; }&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br />&#160;&#160;&#160; } <span style="color: blue;">else</span> {<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(43, 145, 175);">Write-Error</span>&#160;<span style="color: rgb(163, 21, 21);">&quot;Configuration File </span>$configurationFile<span style="color: rgb(163, 21, 21);"> cannot be found...&quot;</span><br />&#160;&#160;&#160; }<br /> <br />&#160;&#160;&#160; <span style="color: blue;">return</span> $properties;<br />}<br /> <br /><span style="color: rgb(43, 145, 175);">Get-PropertiesTable</span> | <span style="color: rgb(43, 145, 175);">%</span> {. $working_path<span style="color: rgb(43, 145, 175);">\configure-cluster-property.ps1</span> -config_file_name $_.ConfigFile -config_property_name $_.PropertyName -config_property_value $_.PropertyValue }</pre>

<p>Calling this script on a local node is then easily achieved using a command line file:</p>

<pre style="background: white; color: black; font-family: consolas;">set working_path=%~dp0<br />if %working_path:~-1%==\ set working_path=%working_path:~0,-1%<br /> <br />set script_path=%working_path%\configure-cluster-properties.ps1<br />set configuration_file=configure-cluster-properties.txt<br /> <br />PowerShell -NoProfile -ExecutionPolicy Bypass -Command &quot;&amp; '%script_path%' -working_path '%working_path%' -configuration_file '%configuration_file%'&quot;<br /> <br />pause</pre>

<p>However having to do this on possibly hundreds of remote machines is not feasible; especially if your cluster consists of hundreds of nodes.</p>

<p>Once again PowerShell provides an easy solution. A script can be created that can be used to push these configuration files to all the cluster nodes and then to execute the configuration change script remotely on all the nodes:</p>

<pre style="background: white; color: black; font-family: consolas;"><strong>push_configure_cluster_properties.ps1</strong></pre>

<pre style="background: white; color: black; font-family: consolas;"><span style="color: blue;">param</span>( [<span style="color: rgb(43, 145, 175);">string</span>] $source_path, [<span style="color: rgb(43, 145, 175);">string</span>] $target_path, [<span style="color: rgb(43, 145, 175);">string</span>] $files_list, [<span style="color: rgb(43, 145, 175);">string</span>] $nodes_list, [<span style="color: rgb(43, 145, 175);">int</span>] $batch_size, [<span style="color: rgb(43, 145, 175);">string</span>] $cmd_word ) <span style="color: green;"># followed by $cmd_args</span><br /> <br />$cmd_args = $args&#160; <span style="color: green;"># capture this right away so can use it in functions</span><br /> <br /><span style="color: blue;">function</span> ListSplitUnique($values)<br />{<br />&#160;&#160;&#160; <span style="color: blue;">return</span> ,@((($values -Split <span style="color: rgb(163, 21, 21);">&quot;,&quot;</span>) | <span style="color: rgb(43, 145, 175);">%</span> {$_.Trim()} | <span style="color: rgb(43, 145, 175);">where-object</span> {$_ -ne <span style="color: rgb(163, 21, 21);">&quot;&quot;</span>} ) | <span style="color: rgb(43, 145, 175);">Sort-Object</span> -Unique)<br />}<br /> <br /><span style="color: blue;">function</span> GetRequiredNodes($nodelistpath)<br />{<br />&#160;&#160;&#160; $required_nodes = @()<br />&#160;&#160;&#160; <span style="color: rgb(43, 145, 175);">write-host</span>&#160;<span style="color: rgb(163, 21, 21);">&quot;</span>$nodelistpath<span style="color: rgb(163, 21, 21);">&quot;</span><br />&#160;&#160;&#160; <span style="color: blue;">if</span> ($nodelistpath -notlike <span style="color: rgb(163, 21, 21);">&quot;skip&quot;</span>) {<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue;">if</span> (<span style="color: rgb(43, 145, 175);">Test-Path</span> -Path $nodelistpath -PathType Leaf){<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green;"># // File exists</span><br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; (<span style="color: rgb(43, 145, 175);">get-content</span> $nodelistpath) | <span style="color: rgb(43, 145, 175);">foreach-object</span> {$required_nodes += <span style="color: rgb(43, 145, 175);">ListSplitUnique</span>($_)}<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; } <span style="color: blue;">else</span> {<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green;"># // File does not exist</span><br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; $required_nodes = <span style="color: rgb(43, 145, 175);">ListSplitUnique</span>($nodelistpath)<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; }<br />&#160;&#160;&#160; }<br /> <br />&#160;&#160;&#160; <span style="color: blue;">return</span> $required_nodes;<br />}<br /> <br /><span style="color: blue;">function</span> GetIPAddress($hostname)<br />{<br />&#160;&#160;&#160; <span style="color: blue;">try</span><br />&#160;&#160;&#160; {<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; [<span style="color: rgb(43, 145, 175);">System.Net.Dns</span>]::GetHostAddresses($hostname) | <span style="color: rgb(43, 145, 175);">ForEach-Object</span> { <span style="color: blue;">if</span> ($_.AddressFamily -eq <span style="color: rgb(163, 21, 21);">&quot;InterNetwork&quot;</span>) { $_.IPAddressToString } }<br />&#160;&#160;&#160; }<br />&#160;&#160;&#160; <span style="color: blue;">catch</span><br />&#160;&#160;&#160; {<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue;">throw</span>&#160;<span style="color: rgb(163, 21, 21);">&quot;Error resolving IPAddress for host '</span>$hostname<span style="color: rgb(163, 21, 21);">'&quot;</span><br />&#160;&#160;&#160; }<br />}<br /> <br /><span style="color: blue;">function</span> IsSameHost(<br />&#160;&#160;&#160; [<span style="color: rgb(43, 145, 175);">string</span>] [<span style="color: rgb(43, 145, 175);">parameter</span>( Position=0, Mandatory=$true )] $host1,<br />&#160;&#160;&#160; [<span style="color: rgb(43, 145, 175);">array</span>] [<span style="color: rgb(43, 145, 175);">parameter</span>( Position=1, Mandatory=$false )] $host2ips = ((<span style="color: rgb(43, 145, 175);">GetIPAddress</span> $env:COMPUTERNAME) -as [<span style="color: rgb(43, 145, 175);">array</span>]))<br />{<br />&#160;&#160;&#160; $host1ips = ((<span style="color: rgb(43, 145, 175);">GetIPAddress</span> $host1) -as [<span style="color: rgb(43, 145, 175);">array</span>])<br />&#160;&#160;&#160; $heq = <span style="color: rgb(43, 145, 175);">Compare-Object</span> $host1ips $host2ips -ExcludeDifferent -IncludeEqual<br />&#160;&#160;&#160; <span style="color: blue;">return</span> ($heq -ne $null)<br />}<br /> <br /><span style="color: green;">#convert $files_list to an array</span><br />$all_files = @()<br /><span style="color: blue;">if</span> ($files_list -ne $null) {<br />&#160;&#160;&#160; $all_files = <span style="color: rgb(43, 145, 175);">ListSplitUnique</span>($files_list)<br />}<br /> <br /><span style="color: green;">#convert $nodes_list to an array</span><br />$required_nodes = <span style="color: rgb(43, 145, 175);">GetRequiredNodes</span>($nodes_list);<br /> <br /><span style="color: green;"># define some global values</span><br />$all_nodes = @()<br />$nl = [<span style="color: rgb(43, 145, 175);">Environment</span>]::NewLine<br />$current_host = <span style="color: rgb(43, 145, 175);">gc</span> env:computername<br />$ips = ((<span style="color: rgb(43, 145, 175);">GetIPAddress</span> $env:COMPUTERNAME) -as [<span style="color: rgb(43, 145, 175);">array</span>])<br /> <br /> <br /><span style="color: blue;">function</span> Check-Files <br />{&#160;&#160;&#160; <br />&#160;&#160;&#160; <span style="color: green;"># check for missing arguments</span><br />&#160;&#160;&#160; <span style="color: blue;">if</span> (($all_files.Count -lt 2) -or ($cmd_word = $null)) {<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(43, 145, 175);">write-error</span>&#160;<span style="color: rgb(163, 21, 21);">&quot;Usage: push_configure_hdp_dependencies.ps1 &lt;source_path&gt; &lt;target_path&gt; &lt;files_list&gt; &lt;cmd_line...&gt;<br />Files_list is a string containing a comma-delimited list of simple<br />file names, to be found in source_path and copied to target_path on<br />each node.&#160; Cmd_line is a sequence of command line tokens constituting <br />a valid PowerShell execution.&quot;</span><br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue;">Exit</span> -1;<br />&#160;&#160;&#160; }<br />&#160;&#160;&#160; <br />&#160;&#160;&#160; <span style="color: green;"># validate that $target_path is an absolute path including volume name</span><br />&#160;&#160;&#160; $path_pieces = $target_path.Split(<span style="color: rgb(163, 21, 21);">&quot;:&quot;</span>)<br />&#160;&#160;&#160; <span style="color: blue;">if</span> ($path_pieces.Length -lt 2) {<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(43, 145, 175);">write-error</span>&#160;<span style="color: rgb(163, 21, 21);">&quot;</span>$target_path<span style="color: rgb(163, 21, 21);"> path does not include volume name with colon.&#160; Exiting.&quot;</span><br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue;">Exit</span> -1;<br />&#160;&#160;&#160; }<br />&#160;&#160;&#160; <span style="color: blue;">if</span> ($path_pieces.Length -gt 2) {<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(43, 145, 175);">write-error</span>&#160;<span style="color: rgb(163, 21, 21);">&quot;</span>$target_path<span style="color: rgb(163, 21, 21);"> path has multiple colons.&#160; Exiting.&quot;</span><br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue;">Exit</span> -1;<br />&#160;&#160;&#160; }<br />&#160;&#160;&#160; <br />&#160;&#160;&#160; <span style="color: green;"># validate existence and accessibility of files in the source_path</span><br />&#160;&#160;&#160; <span style="color: blue;">if</span> (! (<span style="color: rgb(43, 145, 175);">Test-Path</span> $source_path)) {<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(43, 145, 175);">write-error</span>&#160;<span style="color: rgb(163, 21, 21);">&quot;</span>$source_path<span style="color: rgb(163, 21, 21);"> is not accessible from install master server.&#160; Exiting.&quot;</span><br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue;">Exit</span> -1;<br />&#160;&#160;&#160; }<br />&#160;&#160;&#160; <span style="color: rgb(43, 145, 175);">cd</span>&#160;<span style="color: rgb(163, 21, 21);">&quot;</span>$source_path<span style="color: rgb(163, 21, 21);">&quot;</span><br />&#160;&#160;&#160; <span style="color: rgb(43, 145, 175);">write-output</span>&#160;<span style="color: rgb(163, 21, 21);">&quot;</span>$($nl)<span style="color: rgb(163, 21, 21);">Push install files: &quot;</span><br />&#160;&#160;&#160; <span style="color: rgb(43, 145, 175);">ls</span> $all_files<br />&#160;&#160;&#160; <span style="color: blue;">if</span> (! $?) {<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(43, 145, 175);">write-error</span>&#160;<span style="color: rgb(163, 21, 21);">&quot;Some requested files are missing from </span>$source_path<span style="color: rgb(163, 21, 21);">.&#160; Exiting.&quot;</span><br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue;">Exit</span> -1<br />&#160;&#160;&#160; }<br />&#160;&#160;&#160; <span style="color: rgb(43, 145, 175);">write-output</span>&#160;<span style="color: rgb(163, 21, 21);">&quot;</span>$nl<span style="color: rgb(163, 21, 21);">&quot;</span> <br />}<br /> <br /><span style="color: blue;">function</span> Summarize-Results($results_set)<br />{<br />&#160;&#160;&#160; $remoteErrors = @()<br />&#160;&#160;&#160; $remoteSuccessCount = 0<br />&#160;&#160;&#160; $remoteFailureCount = 0<br />&#160;&#160;&#160; $remoteOutputResult = 0<br />&#160;&#160;&#160; <br />&#160;&#160;&#160; <span style="color: blue;">foreach</span> ( $result <span style="color: blue;">in</span> $results_set ) {<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue;">if</span> ( $result -is [<span style="color: rgb(43, 145, 175);">System.Management.Automation.ErrorRecord</span>] ) {<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; $remoteFailureCount++<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; }<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue;">ElseIf</span> ($result -eq <span style="color: rgb(163, 21, 21);">&quot;Done.&quot;</span>) {<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; $remoteSuccessCount++ <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; } <span style="color: blue;">Else</span> {<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; $remoteOutputResult++<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; }<br />&#160;&#160;&#160; }<br />&#160;&#160;&#160; <br />&#160;&#160;&#160; <span style="color: blue;">return</span>&#160;<span style="color: rgb(43, 145, 175);">New-Object</span> psobject -Property @{<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; remoteErrors = $results_set;<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; remoteSuccessCount = $remoteSuccessCount;<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; remoteFailureCount = $remoteFailureCount;<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; remoteOutputResult = $remoteOutputResult<br />&#160;&#160;&#160; }<br />}<br /> <br /><span style="color: blue;">function</span> Report-Results($summary_results) {<br />&#160;&#160;&#160; <span style="color: green;"># Report summarized results to user</span><br />&#160;&#160;&#160; <span style="color: green;"># $summary_results must be the output of Summarize-Results()</span>&#160;&#160;&#160; <br />&#160;&#160;&#160; <span style="color: rgb(43, 145, 175);">write-output</span>&#160;<span style="color: rgb(163, 21, 21);">&quot;Summary:&quot;</span><br />&#160;&#160;&#160; <span style="color: rgb(43, 145, 175);">write-output</span> (<span style="color: rgb(163, 21, 21);">&quot;&quot;</span> + $results.remoteSuccessCount + <span style="color: rgb(163, 21, 21);">&quot; nodes successfully completed&quot;</span>)<br />&#160;&#160;&#160; <br />&#160;&#160;&#160; <span style="color: blue;">if</span> ($results.remoteFailureCount -gt 0) {<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(43, 145, 175);">write-output</span> (<span style="color: rgb(163, 21, 21);">&quot;&quot;</span> + $results.remoteFailureCount + <span style="color: rgb(163, 21, 21);">&quot; failure messages.&quot;</span>)<br />&#160;&#160;&#160; }<br />&#160;&#160;&#160; <br />&#160;&#160;&#160; <span style="color: blue;">if</span> ($results.remoteOutputResult -gt 0) {<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(43, 145, 175);">write-output</span> (<span style="color: rgb(163, 21, 21);">&quot;&quot;</span> + $results.remoteOutputResult + <span style="color: rgb(163, 21, 21);">&quot; Output Lines.&quot;</span>)<br />&#160;&#160;&#160; }<br />}<br /> <br /><span style="color: blue;">function</span> CopyInstall-Files($node)<br />{<br />&#160;&#160;&#160; <span style="color: blue;">if</span> (-not ((($node -ieq $current_host) -or (<span style="color: rgb(43, 145, 175);">IsSameHost</span> $node $ips)) -and ($source_path -ieq $target_path)))&#160; {<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green;"># convert $target_path into the corresponding admin share path, so we can push the files to the node</span><br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; $tgtDir = <span style="color: rgb(163, 21, 21);">'\\'</span> + ($node) + <span style="color: rgb(163, 21, 21);">'\'</span> +&#160; $target_path.ToString().Replace(<span style="color: rgb(163, 21, 21);">':'</span>, <span style="color: rgb(163, 21, 21);">'$'</span>)<br /> <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green;"># attempt to create the target install directory on the remote node if it doesn't already exist</span><br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue;">if</span>(! (<span style="color: rgb(43, 145, 175);">Test-Path</span>&#160;<span style="color: rgb(163, 21, 21);">&quot;</span>$tgtDir<span style="color: rgb(163, 21, 21);">&quot;</span>)) { $r = <span style="color: rgb(43, 145, 175);">mkdir</span>&#160;<span style="color: rgb(163, 21, 21);">&quot;</span>$tgtDir<span style="color: rgb(163, 21, 21);">&quot;</span> }<br /> <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green;"># validate that the $tgtDir admin share exists and is accessible on the remote node</span><br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue;">if</span> (! (<span style="color: rgb(43, 145, 175);">Test-Path</span>&#160;<span style="color: rgb(163, 21, 21);">&quot;</span>$tgtDir<span style="color: rgb(163, 21, 21);">&quot;</span>) ) {<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(43, 145, 175);">write-error</span>&#160;<span style="color: rgb(163, 21, 21);">&quot;</span>$target_path<span style="color: rgb(163, 21, 21);"> on </span>$node<span style="color: rgb(163, 21, 21);"> is not accessible by admin share.&#160; Skipping.&quot;</span><br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue;">return</span> $false<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; }<br /> <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green;"># push the files to each node.&#160; Skip node if any errors.</span><br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(43, 145, 175);">cd</span>&#160;<span style="color: rgb(163, 21, 21);">&quot;</span>$source_path<span style="color: rgb(163, 21, 21);">&quot;</span><br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(43, 145, 175);">cp</span> $all_files <span style="color: rgb(163, 21, 21);">&quot;</span>$tgtDir<span style="color: rgb(163, 21, 21);">&quot;</span> -Force -ErrorAction Stop<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue;">if</span> (! $?) {<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(43, 145, 175);">write-error</span>&#160;<span style="color: rgb(163, 21, 21);">&quot;Some files could not be pushed to </span>$node<span style="color: rgb(163, 21, 21);">.&#160; Skipping.&quot;</span><br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue;">return</span> $false<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; }<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue;">return</span> $true<br />&#160;&#160;&#160; }<br />&#160;&#160;&#160; <span style="color: blue;">else</span> {<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue;">return</span> $true<br />&#160;&#160;&#160; }<br />}<br /> <br /><span style="color: blue;">function</span> PushInstall-Files($nodes)<br />{<br />&#160;&#160;&#160; $copied_nodes = @()<br />&#160;&#160;&#160; <br />&#160;&#160;&#160; <span style="color: blue;">foreach</span> ($node <span style="color: blue;">in</span> $nodes) {<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green;"># tag output with node name:</span><br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(43, 145, 175);">write-output</span> (<span style="color: rgb(163, 21, 21);">&quot;Copying files for node </span>$node<span style="color: rgb(163, 21, 21);">&#160;</span>$nl<span style="color: rgb(163, 21, 21);">&quot;</span>)&#160;&#160;&#160; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green;"># Copy the install files and build list of those needed an install</span><br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue;">if</span> (<span style="color: rgb(43, 145, 175);">CopyInstall-Files</span>($node)) {<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue;">if</span> (($node -ieq $current_host) -or (<span style="color: rgb(43, 145, 175);">IsSameHost</span> $node $ips)){<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(43, 145, 175);">write-output</span>&#160;<span style="color: rgb(163, 21, 21);">&quot;Skipping </span>$node<span style="color: rgb(163, 21, 21);"> because it is current node </span>$nl<span style="color: rgb(163, 21, 21);">&quot;</span><br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue;">else</span> {<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(43, 145, 175);">write-output</span> (<span style="color: rgb(163, 21, 21);">&quot;Copied files to node </span>$node<span style="color: rgb(163, 21, 21);">&#160;</span>$nl<span style="color: rgb(163, 21, 21);">&quot;</span>)&#160;&#160;&#160; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; $copied_nodes += $node<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; }<br />&#160;&#160;&#160; }<br />&#160;&#160;&#160; <br />&#160;&#160;&#160; <span style="color: blue;">if</span> ($copied_nodes.length -gt 0) {<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green;"># invoke the install, and wait for it to complete</span><br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(43, 145, 175);">write-output</span> (<span style="color: rgb(163, 21, 21);">&quot;Pushing to nodes: &quot;</span> + ($copied_nodes -join <span style="color: rgb(163, 21, 21);">&quot; &quot;</span>) + <span style="color: rgb(163, 21, 21);">&quot;</span>$nl<span style="color: rgb(163, 21, 21);">&quot;</span>)<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(43, 145, 175);">write-output</span>&#160;<span style="color: rgb(163, 21, 21);">&quot;With command : </span>$cmd_word<span style="color: rgb(163, 21, 21);">&#160;</span>$cmd_args<span style="color: rgb(163, 21, 21);">&quot;</span><br /> <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; $arg_list = @(<span style="color: rgb(163, 21, 21);">&quot;-file&quot;</span>, <span style="color: rgb(163, 21, 21);">&quot;</span>$cmd_word<span style="color: rgb(163, 21, 21);">&quot;</span>) + $cmd_args<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(43, 145, 175);">Invoke-Command</span> -ComputerName $copied_nodes -ScriptBlock {<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green;"># everything in this scriptblock runs local to the node&#160;&#160;&#160; </span><br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; $node = $env:COMPUTERNAME<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green;"># launch the ps1 file</span><br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; $proc_record = <span style="color: rgb(43, 145, 175);">Start-Process</span> powershell.exe -ArgumentList $using:arg_list -PassThru -Wait -Verb <span style="color: rgb(163, 21, 21);">&quot;RunAs&quot;</span><br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue;">if</span> (! $?) {<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(43, 145, 175);">write-error</span>&#160;<span style="color: rgb(163, 21, 21);">&quot;Start-Process call failed for </span>$node<span style="color: rgb(163, 21, 21);">.&#160; Error </span>$($error[0])<span style="color: rgb(163, 21, 21);">.&#160; Skipping.&quot;</span><br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue;">return</span><br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue;">if</span> ($proc_record.ExitCode -ne 0) {<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(43, 145, 175);">write-error</span>&#160;<span style="color: rgb(163, 21, 21);">&quot;</span>$using:cmd_word<span style="color: rgb(163, 21, 21);"> failed for </span>$node<span style="color: rgb(163, 21, 21);">.&#160; Error code </span>$($proc_record.ExitCode)<span style="color: rgb(163, 21, 21);">.&#160; Skipping.&quot;</span><br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(43, 145, 175);">write-error</span>&#160;<span style="color: rgb(163, 21, 21);">&quot;For error code meanings, see http://msdn.microsoft.com/en-us/library/windows/desktop/aa390890(v=vs.85).aspx&quot;</span><br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue;">return</span><br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(43, 145, 175);">write-output</span>&#160;<span style="color: rgb(163, 21, 21);">&quot;Installed files to node </span>$node<span style="color: rgb(163, 21, 21);">&#160;</span>$nl<span style="color: rgb(163, 21, 21);">&quot;</span><br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(43, 145, 175);">write-output</span>&#160;<span style="color: rgb(163, 21, 21);">&quot;Done.&quot;</span>&#160; <span style="color: green;">#getting to this line indicates success</span><br /> <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; } 2&gt;&amp;1<br />&#160;&#160;&#160; }&#160;&#160;&#160; <br />&#160;&#160;&#160; <span style="color: rgb(43, 145, 175);">write-output</span>&#160;<span style="color: rgb(163, 21, 21);">&quot;</span>$nl<span style="color: rgb(163, 21, 21);">&quot;</span><br />}<br /> <br /><span style="color: blue;">function</span> Install-HDPDependencies( )<br />{<br />&#160;&#160;&#160; <span style="color: green;"># validate the input files</span><br />&#160;&#160;&#160; <span style="color: rgb(43, 145, 175);">Check-Files</span><br />&#160;&#160;&#160; <br />&#160;&#160;&#160; <span style="color: green;"># Do the install.</span><br />&#160;&#160;&#160; $all_results = @()<br />&#160;&#160;&#160; <span style="color: rgb(43, 145, 175);">write-output</span> (<span style="color: rgb(163, 21, 21);">&quot;Nodes to install (parallel): &quot;</span> + ($required_nodes -join <span style="color: rgb(163, 21, 21);">&quot; &quot;</span>) + <span style="color: rgb(163, 21, 21);">&quot;</span>$nl<span style="color: rgb(163, 21, 21);">&quot;</span>)<br />&#160;&#160;&#160; <br />&#160;&#160;&#160; <span style="color: green;"># bucket the nodes for parallel installation</span><br />&#160;&#160;&#160; $required_buckets = @()<br />&#160;&#160;&#160; <span style="color: blue;">if</span> ($batch_size -le 1) { $batch_size = 1 }<br />&#160;&#160;&#160; <span style="color: blue;">if</span> ($required_nodes.length -le $batch_size) {<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; $required_buckets= ,@($required_nodes)<br />&#160;&#160;&#160; } <span style="color: blue;">else</span> {<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; $required_buckets= <span style="color: blue;">for</span>($i=0; $i -lt $required_nodes.length; $i+=$batch_size) { , $required_nodes[$i..($i+$batch_size-1)]}<br />&#160;&#160;&#160; }<br />&#160;&#160;&#160; <br />&#160;&#160;&#160; <span style="color: blue;">foreach</span> ($required_bucket <span style="color: blue;">in</span> $required_buckets) {<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(43, 145, 175);">write-output</span> (<span style="color: rgb(163, 21, 21);">&quot;Pushing to nodes: &quot;</span> + ($required_bucket -join <span style="color: rgb(163, 21, 21);">&quot; &quot;</span>) + <span style="color: rgb(163, 21, 21);">&quot;...&quot;</span>)<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; $push_results = <span style="color: rgb(43, 145, 175);">PushInstall-Files</span>($required_bucket) 2&gt;&amp;1<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue;">if</span> ($push_results) {<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(43, 145, 175);">write-output</span>&#160;<span style="color: rgb(163, 21, 21);">&quot;output: </span>$push_results<span style="color: rgb(163, 21, 21);">&quot;</span><br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; $all_results += $push_results<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; $push_results<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; }<br />&#160;&#160;&#160; }&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br /> <br />&#160;&#160;&#160; <span style="color: rgb(43, 145, 175);">write-output</span>&#160;<span style="color: rgb(163, 21, 21);">&quot;</span>$nl$nl<span style="color: rgb(163, 21, 21);">&quot;</span><br />&#160;&#160;&#160; <br />&#160;&#160;&#160; <span style="color: green;"># parse $all_results for failure messages and alert user</span><br />&#160;&#160;&#160; $results = <span style="color: rgb(43, 145, 175);">Summarize-Results</span> $all_results<br />&#160;&#160;&#160; <span style="color: rgb(43, 145, 175);">Report-Results</span> $results<br />}&#160;&#160;&#160; <br /> <br /><span style="color: green;"># Invoke the functionality of the script</span><br /><span style="color: rgb(43, 145, 175);">Install-HDPDependencies</span></pre>

<p>I will leave it up to the reader to look over what this script is doing. In short this script takes a series of files and deploys them to all nodes in the cluster. It then remotely executes a specified PowerShell script file on each node. This script forms the basis of not only deploying the configuration changes but can also be used for deploying dependency software to cluster nodes, and deploying rack awareness.</p>

<p>Calling this script is a bit more involved but can be managed through the following command file:</p>

<pre style="background: white; color: black; font-family: consolas;">set working_path=%~dp0<br />if %working_path:~-1%==\ set working_path=%working_path:~0,-1%<br /> <br />set script_path=%working_path%\push_configure_cluster_properties.ps1<br />set source_path=%working_path%<br /> <br />set target_root=E:\hdp-installassets<br />set target_path=%target_root%\configuration<br /> <br />set files_list=configure-cluster-property.ps1,configure-cluster-properties.ps1,configure-cluster-properties.cmd,configure-cluster-properties.txt<br />set nodes_list=%working_path%\clusternodes.txt<br /> <br />set configuration_file=configure-cluster-properties.txt<br /> <br />set cmd_word=%target_path%\configure-cluster-properties.ps1<br />set cmd_args=-working_path '%target_path%' -configuration_file '%configuration_file%'<br /> <br />PowerShell -NoProfile -ExecutionPolicy Bypass -Command &quot;&amp; '%script_path%' -source_path '%source_path%' -target_path '%target_path%' -files_list '%files_list%' -nodes_list '%nodes_list%' -batch_size 4 -cmd_word '%cmd_word%' %cmd_args%&quot;<br /> <br />pause</pre>

<p>The items one will need to specify are the remote location where the configuration files can be deployed to, and the list of nodes forming the cluster. The list of cluster nodes can be a file of the names, such as the one below, or just a simple comma separated list:</p>

<pre style="background: white; color: black; font-family: consolas;">HDP-NN1.ACME.LAB,HDP-NN2.ACME.LAB,HDP-MGMT1.ACME.LAB<br />HDP-DATA01.ACME.LAB,HDP-DATA02.ACME.LAB,HDP-DATA03.ACME.LAB,HDP-DATA04.ACME.LAB,HDP-DATA05.ACME.LAB</pre>

<p>One last thing worth mentioning about the deployment script is that it does group the cluster nodes and make the changes to the node in parallel for each group. This parallel grouping is especially important when using the script to manage dependency software installations.</p>]]></content:encoded>
    </item>
    <item>
      <title>Deploying Hadoop Rack Awareness with PowerShell</title>
      <link>https://docs.microsoft.com/archive/blogs/carlnol/deploying-hadoop-rack-awareness-with-powershell</link>
      <pubDate>Wed, 19 Mar 2014 10:22:01 GMT</pubDate>
      <dc:creator><![CDATA[Carl Nolan]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.cloudplatform.qa.msdn.wdslab.com/carlnol/2014/03/19/deploying-hadoop-rack-awareness-with-powershell/</guid>
      <description><![CDATA[In a previous post I talked about Implementing Hadoop Rack Awareness with PowerShell. One thing I...]]></description>
      <content:encoded><![CDATA[<p>In a previous post I talked about <a href="http://blogs.msdn.com/b/carlnol/archive/2014/03/14/implementing-hadoop-rack-awareness-with-powershell.aspx" target="_blank">Implementing Hadoop Rack Awareness with PowerShell</a>. One thing I skimmed over in this post was how to deploy the necessary files to the cluster and make the configuration file changes. Once again PowerShell is your friend.</p>  <p>Deploying this solution involves two processes. Firstly copying the necessary files to the correct Hadoop folder on each node, and secondly making the necessary modifications to the core-site.xml configuration file.</p>  <p>If the necessary deployment files are copied to any folder on a node in the cluster (easily managed from a central location) an additional PowerShell file can be used to manage the actual deployment on the cluster node:</p>  <pre style="background: white; color: black; font-family: consolas;"><span style="color: blue;">param</span>([<span style="color: rgb(43, 145, 175);">string</span>] $working_path, [<span style="color: rgb(43, 145, 175);">string</span>] $script_filename, [<span style="color: rgb(43, 145, 175);">string</span>] $files_list)<br /> <br /><span style="color: blue;">function</span> ListSplitUnique($values)<br />{<br />&#160;&#160;&#160; <span style="color: blue;">return</span> ,@((($values -Split <span style="color: rgb(163, 21, 21);">&quot;,&quot;</span>) | <span style="color: rgb(43, 145, 175);">%</span> {$_.Trim()} | <span style="color: rgb(43, 145, 175);">where-object</span> {$_ -ne <span style="color: rgb(163, 21, 21);">&quot;&quot;</span>} ) | <span style="color: rgb(43, 145, 175);">Sort-Object</span> -Unique)<br />}<br /> <br /><span style="color: green;"># Define the Hadoop files and paths</span><br />$hadoop_home = <span style="color: rgb(43, 145, 175);">resolve-path</span> $env:HADOOP_HOME<br />$hadoop_bin = <span style="color: rgb(163, 21, 21);">&quot;</span>$hadoop_home<span style="color: rgb(163, 21, 21);">\bin&quot;</span><br />$hadoop_conf = <span style="color: rgb(43, 145, 175);">resolve-path</span> $env:HADOOP_CONF_DIR<br />$hadoop_conf_file = <span style="color: rgb(163, 21, 21);">&quot;</span>$hadoop_conf<span style="color: rgb(163, 21, 21);">\core-site.xml&quot;</span><br /> <br />$topology_property_name = <span style="color: rgb(163, 21, 21);">&quot;topology.script.file.name&quot;</span><br />$topology_property_value = $script_filename<br /> <br /><span style="color: green;"># First copy the files to the Hadoop Bin directory</span><br /><span style="color: green;"># Convert $files_list to an array</span><br />$all_files = @()<br /><span style="color: blue;">if</span> ($files_list -ne $null) {<br />&#160;&#160;&#160; $all_files = <span style="color: rgb(43, 145, 175);">ListSplitUnique</span>($files_list + <span style="color: rgb(163, 21, 21);">&quot;, </span>$script_filename<span style="color: rgb(163, 21, 21);">&quot;</span>)<br />}<br /> <br />$all_files | <span style="color: rgb(43, 145, 175);">%</span> { <span style="color: rgb(43, 145, 175);">Copy-Item</span> -Path $working_path\$_ -Destination $hadoop_bin -Force }<br /> <br /><span style="color: green;"># Next Update the core-site-topology.xml file</span><br /><span style="color: green;"># With topology.script.file.name Property</span><br />$conf_doc = [<span style="color: rgb(43, 145, 175);">System.Xml.XmlDocument</span>](<span style="color: rgb(43, 145, 175);">Get-Content</span> $hadoop_conf_file)<br /> <br />$topo_node = ($conf_doc.DocumentElement.property | <span style="color: rgb(43, 145, 175);">Where-Object</span> {$_.name -eq $topology_property_name})<br /><span style="color: blue;">If</span> ($topo_node) {<br />&#160;&#160;&#160; <span style="color: green;"># Element found so ensure the property is correctly set</span><br />&#160;&#160;&#160; <span style="color: rgb(43, 145, 175);">write-host</span>&#160;<span style="color: rgb(163, 21, 21);">&quot;</span>$topology_property_name<span style="color: rgb(163, 21, 21);"> Element Found, so updating value...&quot;</span><br />&#160;&#160;&#160; $topo_node.Value = $topology_property_value<br />} <span style="color: blue;">else</span> {<br />&#160;&#160;&#160; <span style="color: green;"># No Element found so add a new one to the document</span><br />&#160;&#160;&#160; <span style="color: rgb(43, 145, 175);">write-host</span>&#160;<span style="color: rgb(163, 21, 21);">&quot;</span>$topology_property_name<span style="color: rgb(163, 21, 21);"> Element Not Present, adding new element...&quot;</span><br />&#160;&#160;&#160; $rack_property = $conf_doc.CreateElement(<span style="color: rgb(163, 21, 21);">&quot;property&quot;</span>)<br /> <br />&#160;&#160;&#160; $rack_property_name = $conf_doc.CreateElement(<span style="color: rgb(163, 21, 21);">&quot;name&quot;</span>)<br />&#160;&#160;&#160; $rack_property_name.AppendChild($conf_doc.CreateTextNode($topology_property_name))<br />&#160;&#160;&#160; $rack_property.AppendChild($rack_property_name)<br /> <br />&#160;&#160;&#160; $rack_property_value = $conf_doc.CreateElement(<span style="color: rgb(163, 21, 21);">&quot;value&quot;</span>)<br />&#160;&#160;&#160; $rack_property_value.AppendChild($conf_doc.CreateTextNode($topology_property_value))<br />&#160;&#160;&#160; $rack_property.AppendChild($rack_property_value)<br /> <br />&#160;&#160;&#160; $conf_doc.DocumentElement.AppendChild($rack_property)<br />}<br /> <br />$conf_doc.Save($hadoop_conf_file)</pre>

<p>This PowerShell script basically takes in the name of the <font size="2" face="Consolas">topology.script.file.name</font> property and a list of supporting files. It then copies these files to the necessary Hadoop directory and makes the necessary Xml configuration file changes.</p>

<p>The Xml file configuration changes are smart enough that if a previous <font size="2" face="Consolas">topology.script.file.name</font> property has been created then it will just update the current value with the newly specified one.</p>

<p>Calling this script can be achieved through a simple CMD file:</p>

<pre style="background: white; color: black; font-family: consolas;">set working_path=%~dp0<br />if %working_path:~-1%==\ set working_path=%working_path:~0,-1%<br /> <br />set script_path=%working_path%\hadoop-rack-configuration-deployment.ps1<br />set files_list=hadoop-rack-configuration.cmd,hadoop-rack-configuration.ps1,hadoop-rack-configurations.txt<br />set script_filename=hadoop-rack-configuration.cmd<br /> <br />PowerShell -NoProfile -ExecutionPolicy Bypass -Command &quot;&amp; '%script_path%' -working_path '%working_path%' -script_filename '%script_filename%' -files_list '%files_list%'<br /> <br />pause</pre>

<p>Hopefully these script will make deploying the necessary rack awareness assets to the cluster a much simpler and error free process.</p>]]></content:encoded>
    </item>
    <item>
      <title>Implementing Hadoop Rack Awareness with PowerShell</title>
      <link>https://docs.microsoft.com/archive/blogs/carlnol/implementing-hadoop-rack-awareness-with-powershell</link>
      <pubDate>Fri, 14 Mar 2014 09:28:20 GMT</pubDate>
      <dc:creator><![CDATA[Carl Nolan]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.cloudplatform.qa.msdn.wdslab.com/carlnol/2014/03/14/implementing-hadoop-rack-awareness-with-powershell/</guid>
      <description><![CDATA[This post walks-through building a PowerShell script for enabling Rack Awareness in Hadoop. While...]]></description>
      <content:encoded><![CDATA[<p>This post walks-through building a PowerShell script for enabling Rack Awareness in Hadoop. While several example scripts can be found online for Linux, samples building a script for Windows is less common.</p>  <p>Hadoop divides the data into multiple file blocks and stores them on different machines. By default all machines are deemed to be on the same rack, and thus if rack awareness is not configured there the possibility that all replicated copies of a block end up in same rack. Although rare, this could result in data loss when that rack fails. This can be avoided by explicitly configuring Rack Awareness.</p>  <p>Rack Awareness is implemented using the property “topology.script.file.name” in the core-site.xml configuration file. The script accepts a list of IP addresses and return the corresponding list of rack identifiers.</p>  <p>If this “topology.script.file.name” is not configured the default value “/default-rack” is returned for any IP address; thus all nodes considered to be on the same rack.</p>  <p>This sample implementation uses PowerShell to relate a machine name to a predefined rack identifier. One can of course implement any logic one needs. The implementation consists of three files:</p>  <ul>   <li><strong>hadoop-rack-configuration.cmd</strong> – Stub CMD file called by Hadoop </li>    <li><strong>hadoop-rack-configuration.ps1</strong> – PowerShell file called by the CMD stub </li>    <li><strong>hadoop-rack-configurations.txt</strong> – Mapping file from machine names to rack identifiers </li> </ul>  <p>Each of these files will now be defined.</p>  <p><strong>hadoop-rack-configuration.cmd</strong></p>  <p>The stub CMD file that calls a PS1 file, as follows:</p>  <pre style="background: white; color: black; font-family: consolas; font-size: 13px;">@echo off<br /> <br />set cmd_args=%*<br />set root_dir=%HADOOP_HOME%\bin<br /> <br />set script_path=%root_dir%\hadoop-rack-configuration.ps1<br />set config_path=%root_dir%\hadoop-rack-configurations.txt<br /> <br />PowerShell -NoProfile -ExecutionPolicy Bypass -Command &quot;&amp; '%script_path%' -config_path '%config_path%' %cmd_args%&quot;</pre>

<p>This file acts as a stub to pass in the IP addresses and a mapping file, which in this case is a mapping from machine name to rack, to the PowerShell file that implements the necessary mapping logic. </p>

<p>The @echo off commands are needed to ensure that the output is limited to the write-output commands.</p>

<p><strong>hadoop-rack-configurations.txt</strong></p>

<p>This sample implements a mapping of IP address to a rack identifier using the actual machine name through a mapping file. A sample file line would be:</p>

<pre><font face="Consolas" size="2">HDP-DATA01.hdp.lab	/hdp/rack1
HDP-DATA02.hdp.lab	/hdp/rack2
HDP-DATA03.hdp.lab	/hdp/rack1
HDP-DATA04.hdp.lab	/hdp/rack2</font></pre>

<p>The machine name and rack identifier are just separated with a TAB. In this implementation the machines are part of a domain “hdp.lab” and as such fully qualified names are used in the mapping.</p>

<p><strong>hadoop-rack-configuration.ps1</strong> </p>

<p>The PowerShell file can then use the mapping file to map the IP address to a rack definition. </p>

<p>A sample PowerShell file that uses the configuration mapping file would be as follows:</p>

<pre style="background: white; color: black; font-family: consolas; font-size: 13px;"><span style="color: blue;">param</span>([<span style="color: rgb(43, 145, 175);">string</span>] $config_path ) <span style="color: green;"># followed by $cmd_args</span><br /> <br />$listofIPs = $args | <span style="color: rgb(43, 145, 175);">%</span> {$_.Trim()};<br />$configurationFile = $config_path;<br /> <br />$DEFAULT_RACK = <span style="color: rgb(163, 21, 21);">&quot;/default/rack&quot;</span>;<br /> <br /><span style="color: blue;">function</span> Get-LookupTable {<br /> <br />&#160;&#160;&#160; $hash = <span style="color: rgb(43, 145, 175);">New-Object</span> Collections.Hashtable([<span style="color: rgb(43, 145, 175);">StringComparer</span>]::InvariantCultureIgnoreCase);<br /> <br />&#160;&#160;&#160; <span style="color: blue;">if</span> (<span style="color: rgb(43, 145, 175);">Test-Path</span> $configurationFile) {<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(43, 145, 175);">Get-Content</span> $configurationFile | <span style="color: rgb(43, 145, 175);">ForEach-Object</span> { $split = $_ -Split <span style="color: rgb(163, 21, 21);">&quot;\s+|\t+&quot;</span>; $hash[$split[0].Trim()] = $split[1].Trim() };<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue;">Trap</span> { <span style="color: blue;">Continue</span>; }<br />&#160;&#160;&#160; }<br /> <br />&#160;&#160;&#160; <span style="color: blue;">return</span> $hash;<br />}<br /> <br /><span style="color: blue;">function</span> Get-ComputerNameByIP {<br />&#160;&#160;&#160; <span style="color: blue;">param</span>($ipAddress = $null)<br />&#160;&#160;&#160; <br />&#160;&#160;&#160; <span style="color: blue;">begin</span> { }<br /> <br />&#160;&#160;&#160; <span style="color: blue;">process</span> {<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue;">if</span> (($ipAddress -and $_) -or (!$ipAddress -and !$_)) {<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue;">throw</span>&#160;<span style="color: rgb(163, 21, 21);">&quot;Please use either pipeline or input parameter&quot;</span>;<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue;">break</span>;<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; } <span style="color: blue;">else</span> {<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; $result = $null;<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; $lookup = <span style="color: blue;">if</span> ($ipAddress) { $ipAddress.Trim(); } <span style="color: blue;">else</span> { $_.Trim(); }<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue;">if</span> ($lookup -as [<span style="color: rgb(43, 145, 175);">ipaddress</span>]) {<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; $result = [<span style="color: rgb(43, 145, 175);">System.Net.Dns</span>]::GetHostbyAddress($lookup);<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue;">Trap</span> { <span style="color: blue;">Continue</span>; }<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue;">if</span> ($result) <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; { <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [<span style="color: rgb(43, 145, 175);">string</span>]$result.HostName;<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; } <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue;">else</span><br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; $lookup;<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; }<br />&#160;&#160;&#160; }<br /> <br />&#160;&#160;&#160; <span style="color: blue;">end</span>&#160; { }<br />}<br /> <br /><span style="color: blue;">function</span> Get-ComputerRack<br />{<br />&#160;&#160;&#160; <span style="color: blue;">begin</span><br />&#160;&#160;&#160; {<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; $lookupTable = <span style="color: rgb(43, 145, 175);">Get-LookupTable</span>;&#160; <br />&#160;&#160;&#160; }<br /> <br />&#160;&#160;&#160; <span style="color: blue;">process</span><br />&#160;&#160;&#160; {<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; $lookup = ($_).trim();<br /> <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue;">if</span> ($lookupTable.ContainsKey($lookup)) {<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; $lookupTable[$lookup];<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; } <span style="color: blue;">else</span> {<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; $DEFAULT_RACK;<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; }<br />&#160;&#160;&#160; }<br /> <br />&#160;&#160;&#160; <span style="color: blue;">end</span> { }<br />}<br /> <br /> <br />$listofNames = $listofIPs | <span style="color: rgb(43, 145, 175);">Get-ComputerNameByIP</span>;<br />$listofRacks = $listofNames | <span style="color: rgb(43, 145, 175);">Get-ComputerRack</span><br /><span style="color: rgb(43, 145, 175);">write-output</span> ($listofRacks -join <span style="color: rgb(163, 21, 21);">&quot; &quot;</span>)<br /></pre>

<p>Of course this does mean one can implement any logic you desire now in a PS1 file.</p>

<p><strong>Deploying</strong></p>

<p>Once you have written your PowerShell implementation the final step is to deploy to you cluster.</p>

<p>As mentioned, the core-site.xml is updated to include this additional node:</p>

<pre style="background: white; line-height: normal; margin-top: 13px; margin-bottom: 13px; list-style-type: disc;"><font face="Consolas"><font size="2"><span><font color="#0000ff">&lt;</font></span><span><font color="#a31515">property</font></span><span><font color="#0000ff">&gt;</font></span><font color="#000000">
</font><span><font color="#0000ff">	&lt;</font></span><span><font color="#a31515">name</font></span><span><font color="#0000ff">&gt;</font></span><font color="#000000">topology.script.file.name</font><span><font color="#0000ff">&lt;/</font></span><span><font color="#a31515">name</font></span><span><font color="#0000ff">&gt;</font></span><font color="#000000">
</font><span><font color="#0000ff">	&lt;</font></span><span><font color="#a31515">value</font></span><span><font color="#0000ff">&gt;</font></span><font color="#000000">hadoop-rack-configuration.cmd</font><span><font color="#0000ff">&lt;/</font></span><span><font color="#a31515">value</font></span><span><font color="#0000ff">&gt;</font></span><font color="#000000">
</font><span><font color="#0000ff">&lt;/</font></span><span><font color="#a31515">property</font></span><span><font color="#0000ff">&gt;</font></span></font></font></pre>

<p>Finally the files need to be deployed to the cluster. One has to copy the CMD and PS1 files to the <font size="2" face="Consolas">%HADOOOP_BIN_PATH%</font> directory.</p>

<p>Once deployed the nodes can then query the script to find out their non-default rack identifier.</p>]]></content:encoded>
    </item>
    <item>
      <title>Managing Your HDInsight Cluster using PowerShell – Update</title>
      <link>https://docs.microsoft.com/archive/blogs/carlnol/managing-your-hdinsight-cluster-using-powershell-update</link>
      <pubDate>Mon, 16 Dec 2013 07:36:50 GMT</pubDate>
      <dc:creator><![CDATA[Carl Nolan]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.cloudplatform.qa.msdn.wdslab.com/carlnol/2013/12/16/managing-your-hdinsight-cluster-using-powershell-update/</guid>
      <description><![CDATA[Since writing my last post Managing Your HDInsight Cluster and .Net Job Submissions using...]]></description>
      <content:encoded><![CDATA[<p>Since writing my last post <a href="http://blogs.msdn.com/b/carlnol/archive/2013/12/02/managing-your-hdinsight-cluster-and-net-job-submissions.aspx" target="_blank">Managing Your HDInsight Cluster and .Net Job Submissions using PowerShell</a>, there have been some useful modifications to the Azure PowerShell Tools.</p>  <p>The HDInsight cmdlets no longer exist as these have now been integrated into the latest release of the Windows Azure Powershell Tools. This integration means:</p>  <ul>   <li>You don’t need to specify Subscription parameter</li>    <li>If needed, you can use AAD authentication to Azure instead of certificates</li> </ul>  <p>Also, the cmdlets are fully backwards compatible meaning you don’t need to change your current scripts. The Subscription parameter is now optional but if it specified then it is honoured.</p>  <p>As such the cluster creation script will now be:</p>  <div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:b1ae7655-1bcf-475a-8c80-e5520f82fba5" class="wlWriterEditableSmartContent" style="margin: 0px; padding: 0px; float: none; display: inline;"> <div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"> <div style="background: #fff; overflow: auto"> <ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px; white-space: nowrap"> <li><span style="background:#ffffff;color:#0000ff">Param</span><span style="background:#ffffff;color:#000000">($Hosts = 4, [</span><span style="background:#ffffff;color:#2b91af">string</span><span style="background:#ffffff;color:#000000">] $Cluster = $(</span><span style="background:#ffffff;color:#0000ff">throw</span><span style="background:#ffffff;color:#000000"> </span><span style="background:#ffffff;color:#a31515">&quot;Cluster Name Required.&quot;</span><span style="background:#ffffff;color:#000000">), [</span><span style="background:#ffffff;color:#2b91af">string</span><span style="background:#ffffff;color:#000000">] $StorageContainer = </span><span style="background:#ffffff;color:#a31515">&quot;hadooproot&quot;</span><span style="background:#ffffff;color:#000000">)</span></li> <li style="background: #f3f3f3">&nbsp;</li> <li><span style="background:#ffffff;color:#008000"># Get the subscription information and set variables</span></li> <li style="background: #f3f3f3"><span style="background:#ffffff;color:#000000">$subscriptionInfo = </span><span style="background:#ffffff;color:#2b91af">Get-AzureSubscription</span><span style="background:#ffffff;color:#000000"> -Default</span></li> <li><span style="background:#ffffff;color:#000000">$subName = $subscriptionInfo | </span><span style="background:#ffffff;color:#2b91af">%{</span><span style="background:#ffffff;color:#000000"> $_.SubscriptionName }</span></li> <li style="background: #f3f3f3"><span style="background:#ffffff;color:#000000">$subId = $subscriptionInfo | </span><span style="background:#ffffff;color:#2b91af">%{</span><span style="background:#ffffff;color:#000000"> $_.SubscriptionId }</span></li> <li><span style="background:#ffffff;color:#000000">$cert = $subscriptionInfo | </span><span style="background:#ffffff;color:#2b91af">%{</span><span style="background:#ffffff;color:#000000"> $_.Certificate }</span></li> <li style="background: #f3f3f3"><span style="background:#ffffff;color:#000000">$storeAccount = $subscriptionInfo | </span><span style="background:#ffffff;color:#2b91af">%{</span><span style="background:#ffffff;color:#000000"> $_.CurrentStorageAccountName }</span></li> <li>&nbsp;</li> <li style="background: #f3f3f3"><span style="background:#ffffff;color:#2b91af">Select-AzureSubscription</span><span style="background:#ffffff;color:#000000"> -SubscriptionName $subName</span></li> <li>&nbsp;</li> <li style="background: #f3f3f3"><span style="background:#ffffff;color:#000000">$key = </span><span style="background:#ffffff;color:#2b91af">Get-AzureStorageKey</span><span style="background:#ffffff;color:#000000"> $storeAccount | </span><span style="background:#ffffff;color:#2b91af">%{</span><span style="background:#ffffff;color:#000000"> $_.Primary }</span></li> <li>&nbsp;</li> <li style="background: #f3f3f3"><span style="background:#ffffff;color:#000000">$storageAccountInfo = </span><span style="background:#ffffff;color:#2b91af">Get-AzureStorageAccount</span><span style="background:#ffffff;color:#000000"> $storeAccount</span></li> <li><span style="background:#ffffff;color:#000000">$location = $storageAccountInfo | </span><span style="background:#ffffff;color:#2b91af">%{</span><span style="background:#ffffff;color:#000000"> $_.Location }</span></li> <li style="background: #f3f3f3">&nbsp;</li> <li><span style="background:#ffffff;color:#000000">$hadoopUsername = </span><span style="background:#ffffff;color:#a31515">&quot;Hadoop&quot;</span></li> <li style="background: #f3f3f3"><span style="background:#ffffff;color:#000000">$clusterUsername = </span><span style="background:#ffffff;color:#a31515">&quot;Admin&quot;    </span></li> <li><span style="background:#ffffff;color:#000000">$clusterPassword = </span><span style="background:#ffffff;color:#a31515">&quot;myclusterpassword&quot;</span></li> <li style="background: #f3f3f3">&nbsp;</li> <li><span style="background:#ffffff;color:#000000">$secpasswd = </span><span style="background:#ffffff;color:#2b91af">ConvertTo-SecureString</span><span style="background:#ffffff;color:#000000"> $clusterPassword -AsPlainText -Force</span></li> <li style="background: #f3f3f3"><span style="background:#ffffff;color:#000000">$clusterCreds = </span><span style="background:#ffffff;color:#2b91af">New-Object</span><span style="background:#ffffff;color:#000000"> System.Management.Automation.PSCredential($clusterUsername, $secpasswd)</span></li> <li>&nbsp;</li> <li style="background: #f3f3f3"><span style="background:#ffffff;color:#000000">$clusterName = $Cluster</span></li> <li><span style="background:#ffffff;color:#000000">$numberNodes = $Hosts</span></li> <li style="background: #f3f3f3"><span style="background:#ffffff;color:#000000">$containerDefault = $StorageContainer</span></li> <li><span style="background:#ffffff;color:#000000">$blobStorage = </span><span style="background:#ffffff;color:#a31515">&quot;</span><span style="background:#ffffff;color:#000000">$storeAccount</span><span style="background:#ffffff;color:#a31515">.blob.core.windows.net&quot;</span></li> <li style="background: #f3f3f3">&nbsp;</li> <li><span style="background:#ffffff;color:#008000"># tidyup the root to ensure empty</span></li> <li style="background: #f3f3f3"><span style="background:#ffffff;color:#008000"># Remove-AzureStorageContainer Name $containerDefault -Force</span></li> <li><span style="background:#ffffff;color:#2b91af">Write-Host</span><span style="background:#ffffff;color:#000000"> </span><span style="background:#ffffff;color:#a31515">&quot;Deleting old storage container contents: </span><span style="background:#ffffff;color:#000000">$containerDefault</span><span style="background:#ffffff;color:#a31515">&quot;</span><span style="background:#ffffff;color:#000000"> -f yellow</span></li> <li style="background: #f3f3f3"><span style="background:#ffffff;color:#000000">$blobs = </span><span style="background:#ffffff;color:#2b91af">Get-AzureStorageBlob</span><span style="background:#ffffff;color:#000000"> -Container $containerDefault</span></li> <li><span style="background:#ffffff;color:#0000ff">foreach</span><span style="background:#ffffff;color:#000000">($blob </span><span style="background:#ffffff;color:#0000ff">in</span><span style="background:#ffffff;color:#000000"> $blobs)</span></li> <li style="background: #f3f3f3"><span style="background:#ffffff;color:#000000">{</span></li> <li>    <span style="background:#ffffff;color:#000000"></span><span style="background:#ffffff;color:#2b91af">Remove-AzureStorageBlob</span><span style="background:#ffffff;color:#000000"> -Container $containerDefault -Blob ($blob.Name)</span></li> <li style="background: #f3f3f3"><span style="background:#ffffff;color:#000000">}</span></li> <li>&nbsp;</li> <li style="background: #f3f3f3"><span style="background:#ffffff;color:#008000"># Create the cluster</span></li> <li><span style="background:#ffffff;color:#2b91af">Write-Host</span><span style="background:#ffffff;color:#000000"> </span><span style="background:#ffffff;color:#a31515">&quot;Creating &#39;</span><span style="background:#ffffff;color:#000000">$numberNodes</span><span style="background:#ffffff;color:#a31515">&#39; Node Cluster named: </span><span style="background:#ffffff;color:#000000">$clusterName</span><span style="background:#ffffff;color:#a31515">&quot;</span><span style="background:#ffffff;color:#000000"> -f yellow</span></li> <li style="background: #f3f3f3"><span style="background:#ffffff;color:#2b91af">Write-Host</span><span style="background:#ffffff;color:#000000"> </span><span style="background:#ffffff;color:#a31515">&quot;Storage Account &#39;</span><span style="background:#ffffff;color:#000000">$storeAccount</span><span style="background:#ffffff;color:#a31515">&#39; and Container &#39;</span><span style="background:#ffffff;color:#000000">$containerDefault</span><span style="background:#ffffff;color:#a31515">&#39;&quot;</span><span style="background:#ffffff;color:#000000"> -f yellow</span></li> <li><span style="background:#ffffff;color:#2b91af">Write-Host</span><span style="background:#ffffff;color:#000000"> </span><span style="background:#ffffff;color:#a31515">&quot;User &#39;</span><span style="background:#ffffff;color:#000000">$clusterUsername</span><span style="background:#ffffff;color:#a31515">&#39; Password &#39;</span><span style="background:#ffffff;color:#000000">$clusterPassword</span><span style="background:#ffffff;color:#a31515">&#39;&quot;</span><span style="background:#ffffff;color:#000000"> -f green</span></li> <li style="background: #f3f3f3">&nbsp;</li> <li><span style="background:#ffffff;color:#2b91af">New-AzureHDInsightCluster</span><span style="background:#ffffff;color:#000000"> -Certificate $cert -Name $clusterName -Location $location -DefaultStorageAccountName $blobStorage -DefaultStorageAccountKey $key -DefaultStorageContainerName $containerDefault -Credential $clusterCreds -ClusterSizeInNodes $numberNodes</span></li> <li style="background: #f3f3f3">&nbsp;</li> <li><span style="background:#ffffff;color:#2b91af">Write-Host</span><span style="background:#ffffff;color:#000000"> </span><span style="background:#ffffff;color:#a31515">&quot;Created &#39;</span><span style="background:#ffffff;color:#000000">$numberNodes</span><span style="background:#ffffff;color:#a31515">&#39; Node Cluster: </span><span style="background:#ffffff;color:#000000">$clusterName</span><span style="background:#ffffff;color:#a31515">&quot;</span><span style="background:#ffffff;color:#000000"> -f yellow</span></li> </ol> </div> </div> </div>  <p>The only changes are the selection of the subscription and the removal of the Subscription option when creating the cluster. Of course all the other scripts can easily be modified along the same lines; such as the cluster deletion script:</p>  <div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:548a3224-a702-4da8-9990-be9007d0eba9" class="wlWriterEditableSmartContent" style="margin: 0px; padding: 0px; float: none; display: inline;"> <div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"> <div style="background: #fff; overflow: auto"> <ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px; white-space: nowrap"> <li><span style="background:#ffffff;color:#0000ff">Param</span><span style="background:#ffffff;color:#000000">($Cluster = $(</span><span style="background:#ffffff;color:#0000ff">throw</span><span style="background:#ffffff;color:#000000"> </span><span style="background:#ffffff;color:#a31515">&quot;Cluster Name Required.&quot;</span><span style="background:#ffffff;color:#000000">))</span></li> <li style="background: #f3f3f3">&nbsp;</li> <li><span style="background:#ffffff;color:#008000"># Get the subscription information and set variables</span></li> <li style="background: #f3f3f3"><span style="background:#ffffff;color:#000000">$subscriptionInfo = </span><span style="background:#ffffff;color:#2b91af">Get-AzureSubscription</span><span style="background:#ffffff;color:#000000"> -Default</span></li> <li><span style="background:#ffffff;color:#000000">$subName = $subscriptionInfo | </span><span style="background:#ffffff;color:#2b91af">%{</span><span style="background:#ffffff;color:#000000"> $_.SubscriptionName }</span></li> <li style="background: #f3f3f3"><span style="background:#ffffff;color:#000000">$subId = $subscriptionInfo | </span><span style="background:#ffffff;color:#2b91af">%{</span><span style="background:#ffffff;color:#000000"> $_.SubscriptionId }</span></li> <li><span style="background:#ffffff;color:#000000">$cert = $subscriptionInfo | </span><span style="background:#ffffff;color:#2b91af">%{</span><span style="background:#ffffff;color:#000000"> $_.Certificate }</span></li> <li style="background: #f3f3f3"><span style="background:#ffffff;color:#000000">$storeAccount = $subscriptionInfo | </span><span style="background:#ffffff;color:#2b91af">%{</span><span style="background:#ffffff;color:#000000"> $_.CurrentStorageAccountName }</span></li> <li>&nbsp;</li> <li style="background: #f3f3f3"><span style="background:#ffffff;color:#2b91af">Select-AzureSubscription</span><span style="background:#ffffff;color:#000000"> -SubscriptionName $subName</span></li> <li>&nbsp;</li> <li style="background: #f3f3f3"><span style="background:#ffffff;color:#000000">$clusterName = $Cluster</span></li> <li>&nbsp;</li> <li style="background: #f3f3f3"><span style="background:#ffffff;color:#008000"># Delete the cluster</span></li> <li><span style="background:#ffffff;color:#2b91af">Write-Host</span><span style="background:#ffffff;color:#000000"> </span><span style="background:#ffffff;color:#a31515">&quot;Deleting Cluster named: </span><span style="background:#ffffff;color:#000000">$clusterName</span><span style="background:#ffffff;color:#a31515">&quot;</span><span style="background:#ffffff;color:#000000"> -f yellow</span></li> <li style="background: #f3f3f3">&nbsp;</li> <li><span style="background:#ffffff;color:#2b91af">Remove-AzureHDInsightCluster</span><span style="background:#ffffff;color:#000000"> $clusterName -Subscription $subId -Certificate $cert</span></li> <li style="background: #f3f3f3">&nbsp;</li> <li><span style="background:#ffffff;color:#2b91af">Write-Host</span><span style="background:#ffffff;color:#000000"> </span><span style="background:#ffffff;color:#a31515">&quot;Deleted Cluster </span><span style="background:#ffffff;color:#000000">$clusterName</span><span style="background:#ffffff;color:#a31515">&quot;</span><span style="background:#ffffff;color:#000000"> -f yellow</span></li> </ol> </div> </div> </div>  <p>This cluster creation script also contains an additional section to ensure that the default storage container does not container any leftover files, from previous cluster creations:</p>  <div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:6ae66685-30e8-4de8-b78b-de99e5c444c7" class="wlWriterEditableSmartContent" style="margin: 0px; padding: 0px; float: none; display: inline;"> <div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"> <div style="background: #fff; overflow: auto"> <ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px; white-space: nowrap"> <li><span style="background:#ffffff;color:#2b91af">$blobs = Get-AzureSt</span><span style="background:#ffffff;color:#000000">orageBlob -Container $container</span><span style="background:#ffffff;color:#0000ff">Default</span></li> <li style="background: #f3f3f3"><span style="background:#ffffff;color:#000000">forea</span><span style="background:#ffffff;color:#0000ff">ch</span><span style="background:#ffffff;color:#000000">($blob in $blobs)</span></li> <li><span style="background:#ffffff;color:#2b91af">{</span></li> <li style="background: #f3f3f3">    <span style="background:#ffffff;color:#2b91af">Remove-AzureSt</span><span style="background:#ffffff;color:#000000">orageBlob -Container $containerDefault -Blob ($blob.Nam</span><span style="background:#ffffff;color:#008000">e)</span></li> <li><span style="background:#ffffff;color:#008000">}</span></li> </ol> </div> </div> </div>  <p>The rationale behind this is to ensure that any files that may be left over from previous clusters creations/deletions are removed.</p>]]></content:encoded>
    </item>
    <item>
      <title>Managing Your HDInsight Cluster and .Net Job Submissions using PowerShell</title>
      <link>https://docs.microsoft.com/archive/blogs/carlnol/managing-your-hdinsight-cluster-and-net-job-submissions-using-powershell</link>
      <pubDate>Mon, 02 Dec 2013 01:56:00 GMT</pubDate>
      <dc:creator><![CDATA[Carl Nolan]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.cloudplatform.qa.msdn.wdslab.com/carlnol/2013/12/02/managing-your-hdinsight-cluster-and-net-job-submissions-using-powershell/</guid>
      <description><![CDATA[This post explains how best to manage an HDInsight cluster using a management console and Windows...]]></description>
      <content:encoded><![CDATA[<p><span lang="EN-US"><span style="font-size: 11pt;">This post explains how best to manage an HDInsight cluster using a management console and Windows PowerShell. The goal is to outline how to create a simple cluster, provide a mechanism for managing an elastic service, and demonstrate how to customize the cluster creation.</span></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><span style="font-size: 11pt;">Before provisioning a cluster one need to ensure the Azure subscription has been correctly configured and a management station has been setup.</span></span></p>
<h1 style="margin: 24pt 0cm 18pt; line-height: normal; list-style-type: disc;"><a name="_Toc373503054"></a><span lang="EN-US"><span style="font-size: 18pt;">Managing Your HDInsight Cluster</span></span></h1>
<h2 style="margin: 18pt 0cm; line-height: normal; list-style-type: disc;"><a name="_Toc373503055"></a><a name="_Toc359333205"></a><span style="mso-bookmark: _toc373503055;"><span style="mso-ansi-language: en;" lang="EN"><span style="font-size: 16pt;">Prepare Azure Subscription Storage</span></span></span></h2>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span style="mso-ansi-language: en;" lang="EN"><span style="font-size: 11pt;">Under the subscription in which the HDInsight is going to be running one needs a storage account defined, and a management certificate uploaded.</span></span></p>
<h3 style="margin: 18pt 0cm 12pt; line-height: normal; list-style-type: disc;"><a name="_Toc373503056"></a><a name="_Toc359333206"></a><span style="mso-bookmark: _toc373503056;"><span style="mso-ansi-language: en;" lang="EN"><span style="font-size: 14pt;">Create a Storage Account</span></span></span></h3>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/76/metablogapi/3124.image_03C8BE32.png" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-76-metablogapi/3124.image_5F00_03C8BE32.png"><img style="border: 0px currentcolor; display: inline; background-image: none;" title="image" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/76/metablogapi/3835.image_thumb_33AB1206.png" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-76-metablogapi/3835.image_5F00_thumb_5F00_33AB1206.png" alt="image" width="400" height="310" border="0" /></a></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span style="mso-ansi-language: en;" lang="EN"><span style="font-size: 11pt;">Next, under the storage account management create a new default container:</span></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/76/metablogapi/3107.image_778467BF.png" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-76-metablogapi/3107.image_5F00_778467BF.png"><img style="border: 0px currentcolor; display: inline; background-image: none;" title="image" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/76/metablogapi/6175.image_thumb_37DF2C8F.png" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-76-metablogapi/6175.image_5F00_thumb_5F00_37DF2C8F.png" alt="image" width="400" height="325" border="0" /></a></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span style="mso-ansi-language: en;" lang="EN"><span style="font-size: 11pt;">At this point the Azure subscription is ready.</span></span></p>
<h2 style="margin: 18pt 0cm; line-height: normal; list-style-type: disc;"><a name="_Toc373503057"></a><a name="_Toc359333207"></a><a name="_Ref356565353"></a><span style="mso-bookmark: _toc359333207;"><span style="mso-bookmark: _toc373503057;"><span lang="EN-US"><span style="font-size: 16pt;">Setting Up Your Environment</span></span></span></span></h2>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><span style="font-size: 11pt;">Once the Azure subscription has been configured, the next step is to configure your management environment.</span></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><span style="font-size: 11pt;">The first step is to install and configure Windows Azure PowerShell. </span></span><span style="mso-ansi-language: en;" lang="EN"><span style="font-size: 11pt;">Windows Azure PowerShell is the scripting environment that allows you to control and automate the deployment and management of your workloads in Windows Azure. This includes provisioning virtual machines, setting up virtual networks and cross-premises networks, and managing cloud services in Windows Azure.</span></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span style="mso-ansi-language: en;" lang="EN"><span style="font-size: 11pt;">To use the cmdlets in Windows Azure PowerShell you will need to download and import the modules, as well as import and configure information that provides connectivity to Windows Azure through your subscription. For instructions, see </span></span><span style="font-size: 11pt;"><span lang="EN-US"><a href="http://msdn.microsoft.com/en-us/library/windowsazure/jj554332.aspx"><span style="mso-ansi-language: en;" lang="EN">Get Started with Windows Azure Cmdlets</span></a></span></span><span style="mso-ansi-language: en;" lang="EN"><span style="font-size: 11pt;">.</span></span></p>
<h3 style="margin: 18pt 0cm 12pt; line-height: normal; list-style-type: disc;"><a name="_Toc373503058"></a><a name="_Toc359333208"></a><span style="mso-bookmark: _toc373503058;"><span lang="EN-US"><span style="font-size: 14pt;">Prepare the Work Environment</span></span></span></h3>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><span style="font-size: 11pt;">The basic process for running the </span></span><span style="mso-ansi-language: en;" lang="EN"><span style="font-size: 11pt;">cmdlets by using the Windows Azure PowerShell command shell is as follows:</span></span></p>
<p class="MsoListParagraph" style="margin: 0cm 0cm 0pt 36pt; line-height: normal; text-indent: -18pt; list-style-type: disc; mso-list: l3 level1 lfo3;"><span style="mso-fareast-font-family: symbol; mso-bidi-font-family: symbol;" lang="EN-US"><span style="mso-list: ignore;"><span style="font-size: 11pt;">&middot;</span><span style="line-height: normal; font-size-adjust: none; font-stretch: normal;"><span style="font-size: 7pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span></span><span lang="EN-US"><span style="font-size: 11pt;">Upload a Management Certificate. </span></span><span style="font-size: 11pt;"><span style="mso-ansi-language: en;" lang="EN">For more information about how to create and upload a management certificate, see </span><span lang="EN-US"><a href="http://msdn.microsoft.com/en-us/library/windowsazure/gg551721.aspx"><span style="mso-ansi-language: en;" lang="EN">How to: Manage Management Certificates in Windows Azure</span></a></span><span style="mso-ansi-language: en;" lang="EN"> and the section entitled &ldquo;</span><span style="mso-ansi-language: en;" lang="EN">Create a Certificate</span></span><span style="mso-ansi-language: en;" lang="EN"><span style="font-size: 11pt;">&rdquo;.</span></span></p>
<p class="MsoListParagraph" style="margin: 0cm 0cm 0pt 36pt; line-height: normal; text-indent: -18pt; list-style-type: disc; mso-list: l3 level1 lfo3;"><span style="mso-fareast-font-family: symbol; mso-bidi-font-family: symbol; mso-ansi-language: en;" lang="EN"><span style="mso-list: ignore;"><span style="font-size: 11pt;">&middot;</span><span style="line-height: normal; font-size-adjust: none; font-stretch: normal;"><span style="font-size: 7pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span></span><span style="mso-ansi-language: en;" lang="EN"><span style="font-size: 11pt;">Install the Windows Azure module for Windows PowerShell. The install can be found on the </span></span><span style="font-size: 11pt;"><span lang="EN-US"><a href="http://www.windowsazure.com/en-us/downloads/?fb=en-us"><span style="mso-ansi-language: en;" lang="EN">Windows Azure Downloads</span></a></span></span><span style="mso-ansi-language: en;" lang="EN"><span style="font-size: 11pt;"> page, under Windows.</span></span></p>
<p class="MsoListParagraph" style="margin: 0cm 0cm 0pt 36pt; line-height: normal; text-indent: -18pt; list-style-type: disc; mso-list: l3 level1 lfo3;"><span style="mso-fareast-font-family: symbol; mso-bidi-font-family: symbol;" lang="EN-US"><span style="mso-list: ignore;"><span style="font-size: 11pt;">&middot;</span><span style="line-height: normal; font-size-adjust: none; font-stretch: normal;"><span style="font-size: 7pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span></span><span lang="EN-US"><span style="font-size: 11pt;">Finally </span></span><span style="mso-ansi-language: en;" lang="EN"><span style="font-size: 11pt;">set the Windows PowerShell execution policy. To achieve this run the <strong style="mso-bidi-font-weight: normal;">Windows Azure PowerShell </strong>as an Administrator and execute the following command:</span></span></p>
<p class="CodeLine" style="margin: 3pt 0cm 3pt 36pt; line-height: 11pt; list-style-type: disc; mso-add-space: auto;"><span style="font-size: 10pt;">Set-ExecutionPolicy RemoteSigned</span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span style="mso-ansi-language: en;" lang="EN"><span style="font-size: 11pt;">The Windows PowerShell execution policy determines the conditions under which configuration files and scripts are run. The Windows Azure PowerShell cmdlets need the execution policy set to a value of RemoteSigned, Unrestricted, or Bypass.</span></span></p>
<h3 style="margin: 18pt 0cm 12pt; line-height: normal; list-style-type: disc;"><a name="_Toc373503059"></a><a name="_Toc359333209"></a><a name="_Ref356564342"></a><span style="mso-bookmark: _toc359333209;"><span style="mso-bookmark: _toc373503059;"><span style="mso-ansi-language: en;" lang="EN"><span style="font-size: 14pt;">Create a Certificate</span></span></span></span></h3>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span style="mso-ansi-language: en;" lang="EN"><span style="font-size: 11pt;">From a Visual Studio Command Prompt run the following:</span></span></p>
<p class="CodeLine" style="margin: 3pt 0cm 3pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="mso-fareast-language: en-gb;"><span style="font-size: 10pt;">makecert -sky exchange -r -n "CN=&lt;CertificateName&gt;" -pe -a sha1 -len 2048 -ss My "&lt;CertificateName&gt;.cer"</span></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span style="mso-ansi-language: en;" lang="EN"><span style="font-size: 11pt;">&nbsp;</span></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span style="mso-ansi-language: en;" lang="EN"><span style="font-size: 11pt;">Where&lt; CertificateName&gt; is the name that you want to use for the certificate. It must have a .cer extension. The command loads the private key into your user store, indicated by the &ldquo;-ss My&rdquo; switch.<span style="mso-spacerun: yes;">&nbsp;</span>In the certmgr.msc, it appears in the path of Certificates -Current User\Personal\Certificates.</span></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span style="mso-ansi-language: en;" lang="EN"><span style="font-size: 11pt;">Upload the new certification to Azure:</span></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/76/metablogapi/6712.image_1684A855.png" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-76-metablogapi/6712.image_5F00_1684A855.png"><img style="border: 0px currentcolor; display: inline; background-image: none;" title="image" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/76/metablogapi/4174.image_thumb_19520F5B.png" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-76-metablogapi/4174.image_5F00_thumb_5F00_19520F5B.png" alt="image" width="400" height="310" border="0" /></a></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span style="mso-ansi-language: en;" lang="EN"><span style="font-size: 11pt;">If one needs to connect from several machines the same certificate should be installed into the user store on that machine also.</span></span></p>
<h3 style="margin: 18pt 0cm 12pt; line-height: normal; list-style-type: disc;"><a name="_Toc373503060"></a><a name="_Toc359333210"></a><a name="_Ref358016929"></a><span style="mso-bookmark: _toc359333210;"><span style="mso-bookmark: _toc373503060;"><span lang="EN-US"><span style="font-size: 14pt;">Configure Connectivity</span></span></span></span></h3>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span style="mso-ansi-language: en;" lang="EN"><span style="font-size: 11pt;">There are two ways one can configure connectivity between your workstation and Windows Azure. Either manually by configuring the management certificate and subscription details with the <strong><span style="mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi;">Set-AzureSubscription</span></strong> and <strong><span style="mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi;">Select-AzureSubscription</span></strong> cmdlets, or automatically by downloading the PublishSettings file from Windows Azure and importing it. The settings for Windows Azure PowerShell are stored in: <strong><span style="mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi;">&lt;user&gt;\AppData\Roaming\Windows Azure PowerShell</span></strong>.</span></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span style="mso-ansi-language: en;" lang="EN"><span style="font-size: 11pt;">The PublishSettings file method works well when you are responsible for a limited number of subscriptions, but it adds a certificate to any subscription that you are an administrator or co-administrator for. This can be achieved by:</span></span></p>
<p class="MsoListParagraph" style="margin: 0cm 0cm 0pt 36pt; line-height: normal; text-indent: -18pt; list-style-type: disc; mso-list: l2 level1 lfo4;"><span style="mso-fareast-font-family: symbol; mso-bidi-font-family: symbol; mso-ansi-language: en;" lang="EN"><span style="mso-list: ignore;"><span style="font-size: 11pt;">&middot;</span><span style="line-height: normal; font-size-adjust: none; font-stretch: normal;"><span style="font-size: 7pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span></span><span style="mso-ansi-language: en;" lang="EN"><span style="font-size: 11pt;">Execute the following command:</span></span></p>
<p class="CodeLine" style="margin: 3pt 0cm 3pt 36pt; line-height: 11pt; list-style-type: disc; mso-add-space: auto;"><span style="font-size: 10pt;">Get-AzurePublishSettingsFile</span></p>
<p class="MsoListParagraph" style="margin: 0cm 0cm 0pt 36pt; line-height: normal; text-indent: -18pt; list-style-type: disc; mso-list: l2 level1 lfo4;"><span style="mso-fareast-font-family: symbol; mso-bidi-font-family: symbol; mso-ansi-language: en;" lang="EN"><span style="mso-list: ignore;"><span style="font-size: 11pt;">&middot;</span><span style="line-height: normal; font-size-adjust: none; font-stretch: normal;"><span style="font-size: 7pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span></span><span style="mso-ansi-language: en;" lang="EN"><span style="font-size: 11pt;">Save the .publishSettings file locally.</span></span></p>
<p class="MsoListParagraph" style="margin: 0cm 0cm 0pt 36pt; line-height: normal; text-indent: -18pt; list-style-type: disc; mso-list: l2 level1 lfo4;"><span style="mso-fareast-font-family: symbol; mso-bidi-font-family: symbol; mso-ansi-language: en;" lang="EN"><span style="mso-list: ignore;"><span style="font-size: 11pt;">&middot;</span><span style="line-height: normal; font-size-adjust: none; font-stretch: normal;"><span style="font-size: 7pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span></span><span style="mso-ansi-language: en;" lang="EN"><span style="font-size: 11pt;">Import the downloaded .publishSettings file by running the PowerShell command:</span></span></p>
<p class="CodeLine" style="margin: 3pt 0cm 3pt 36pt; line-height: 11pt; list-style-type: disc; mso-add-space: auto;"><span style="font-size: 10pt;">Import-AzurePublishSettingsFile&lt; publishSettings-file&gt; </span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span style="mso-ansi-language: en;" lang="EN"><span style="font-size: 11pt;">Security note: This file contains an encoded management certificate that will serve as your credentials to administer all aspects of your subscriptions and related services. Store this file in a secure location, or delete it after you follow these steps.</span></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span style="mso-ansi-language: en;" lang="EN"><span style="font-size: 11pt;">In complex or shared development environments, it might be desirable to manually configure the publish settings and subscription information, including any management certificates, for your Windows Azure subscriptions. This is achieved by running the following script:</span></span></p>
<p class="CodeLineCxSpFirst" style="margin: 3pt 0cm 0pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">$mySubID = "&lt;subscritionID&gt;"</span></p>
<p class="CodeLineCxSpMiddle" style="margin: 0cm 0cm 0pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">$certThumbprint = "&lt;Thumbprint&gt;"</span></p>
<p class="CodeLineCxSpMiddle" style="margin: 0cm 0cm 0pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">$myCert = Get-Item cert:\CurrentUser\My\$certThumbprint</span></p>
<p class="CodeLineCxSpMiddle" style="margin: 0cm 0cm 0pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">$mySubName = "&lt;SubscriptionName&gt;"</span></p>
<p class="CodeLineCxSpMiddle" style="margin: 0cm 0cm 0pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">$myStoreAcct = "&lt;StorageAccount&gt;"</span></p>
<p class="CodeLineCxSpMiddle" style="margin: 0cm 0cm 0pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">&nbsp;</span></p>
<p class="CodeLineCxSpMiddle" style="margin: 0cm 0cm 0pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">Set-AzureSubscription -SubscriptionName $mySubName -Certificate $myCert -SubscriptionID $mySubID</span></p>
<p class="CodeLineCxSpMiddle" style="margin: 0cm 0cm 0pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">&nbsp;</span></p>
<p class="CodeLineCxSpMiddle" style="margin: 0cm 0cm 0pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">Select-AzureSubscription -Default $mySubName</span></p>
<p class="CodeLineCxSpMiddle" style="margin: 0cm 0cm 0pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">Set-AzureSubscription &ndash;SubscriptionName $mySubName -CurrentStorageAccount $myStoreAcct</span></p>
<p class="CodeLineCxSpLast" style="margin: 0cm 0cm 3pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">&nbsp;</span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><span style="font-size: 11pt;">You can view the details of the Azure Subscription run the command:</span></span></p>
<p class="CodeLine" style="margin: 3pt 0cm 3pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">Get-AzureSubscription &ndash;Default</span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><span style="font-size: 11pt;">Or:</span></span></p>
<p class="CodeLine" style="margin: 3pt 0cm 3pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">Get-AzureSubscription -Current</span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span style="mso-ansi-language: en;" lang="EN"><span style="font-size: 11pt;">This script uses the <strong style="mso-bidi-font-weight: normal;">Set-AzureSubscription</strong> cmdlet. This cmdlet can be used to configure a default Windows Azure subscription, set a default storage account, or a custom Windows Azure service endpoint:</span></span></p>
<p class="CodeLine" style="margin: 3pt 0cm 3pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">Set-AzureSubscription -DefaultSubscription $mySubName</span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><span style="font-size: 11pt;">You can also just select a subscription for the current session using:</span></span></p>
<p class="CodeLine" style="margin: 3pt 0cm 3pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">Select-AzureSubscription -SubscriptionName $mySubName</span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><span style="font-size: 11pt;">When managing your cluster and you have multiple subscription it is advisable to set the default subscription.</span></span></p>
<h3 style="margin: 18pt 0cm 12pt; line-height: normal; list-style-type: disc;"><a name="_Toc373503061"></a><a name="_Toc359333211"></a><a name="_Ref356565468"></a><span style="mso-bookmark: _toc359333211;"><span style="mso-bookmark: _toc373503061;"><span lang="EN-US"><span style="font-size: 14pt;">Install HDInsight cmdlets</span></span></span></span></h3>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><span style="font-size: 11pt;">Ensure the HDInsight binaries are accessible and loaded. They can be downloaded from:</span></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><a href="http://www.microsoft.com/en-gb/download/details.aspx?id=40724"><span style="font-size: 11pt;">http://www.microsoft.com/en-gb/download/details.aspx?id=40724</span></a><span style="mso-spacerun: yes;"><span style="font-size: 11pt;">&nbsp;</span></span></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><span style="font-size: 11pt;">Once downloaded and installed </span></span><span style="mso-ansi-language: en;" lang="EN"><span style="font-size: 11pt;">they are ready to be loaded into Azure PowerShell environment:</span></span></p>
<p class="CodeLine" style="margin: 3pt 0cm 3pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">Import-Module "C:\Program Files (x86)\Windows Azure HDInsight PowerShell\Microsoft.WindowsAzure.Management.HDInsight.Cmdlet\Microsoft.WindowsAzure.Management.HDInsight.Cmdlet.dll"</span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><span style="font-size: 11pt;">Assuming the Windows Azure HDInsight PowerShell Tools were installed to the default directory.</span></span></p>
<h2 style="margin: 18pt 0cm; line-height: normal; list-style-type: disc;"><a name="_Toc373503062"></a><a name="_Toc359333212"></a><a name="_Ref356565162"></a><span style="mso-bookmark: _toc359333212;"><span style="mso-bookmark: _toc373503062;"><span style="mso-ansi-language: en;" lang="EN"><span style="font-size: 16pt;">Cluster Provisioning</span></span></span></span></h2>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span style="mso-ansi-language: en;" lang="EN"><span style="font-size: 11pt;">There are various ways one can provision a new cluster. If using the Management Portal there is a simple UX process once can follow:</span></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/76/metablogapi/1526.image_70D84EA8.png" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-76-metablogapi/1526.image_5F00_70D84EA8.png"><img style="border: 0px currentcolor; display: inline; background-image: none;" title="image" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/76/metablogapi/1172.image_thumb_33D93E78.png" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-76-metablogapi/1172.image_5F00_thumb_5F00_33D93E78.png" alt="image" width="400" height="172" border="0" /></a></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span style="mso-ansi-language: en;" lang="EN"><span style="font-size: 11pt;">However for an automated process it is recommended that PowerShell is utilized. In running the script to be presented one has to remember load the HDInsight management binaries, as outlined in the &ldquo;</span></span><span style="font-size: 11pt;"><span lang="EN-US">Install HDInsight cmdlets</span></span><span style="mso-ansi-language: en;" lang="EN"><span style="font-size: 11pt;">&rdquo; step.</span></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span style="mso-ansi-language: en;" lang="EN"><span style="font-size: 11pt;">The following sections present a series of scripts for automating cluster management. Further examples can also be found at </span></span><span lang="EN-US"><span style="font-size: 11pt;"><a href="http://hadoopsdk.codeplex.com/wikipage?title=PowerShell%20Cmdlets%20for%20Cluster%20Management&amp;referringTitle=Cluster%20Management">Microsoft .Net SDK for Hadoop - PowerShell Cmdlets for Cluster Management</a>.</span></span></p>
<h3 style="margin: 18pt 0cm 12pt; line-height: normal; list-style-type: disc;"><a name="_Toc373503063"></a><a name="_Toc359333213"></a><span style="mso-bookmark: _toc373503063;"><span style="mso-ansi-language: en;" lang="EN"><span style="font-size: 14pt;">Provision Base Cluster</span></span></span></h3>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span style="mso-ansi-language: en;" lang="EN"><span style="font-size: 11pt;">Firstly one need access to the Subscription Id and Certificate, for the current Azure Subscription, which was registered in the &ldquo;</span></span><span style="font-size: 11pt;"><span lang="EN-US">Setting Up Your Environment</span></span><span style="mso-ansi-language: en;" lang="EN"><span style="font-size: 11pt;">&rdquo; step.</span></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span style="mso-ansi-language: en;" lang="EN"><span style="font-size: 11pt;">When running these scripts the italicized variables will first need to be defined.</span></span></p>
<p class="MsoListParagraph" style="margin: 0cm 0cm 0pt 36pt; line-height: normal; text-indent: -18pt; list-style-type: disc; mso-list: l1 level1 lfo5;"><span style="mso-fareast-font-family: 'Segoe UI'; mso-bidi-font-family: 'Segoe UI'; mso-ansi-language: en;" lang="EN"><span style="mso-list: ignore;"><span style="font-size: 11pt;">1.</span><span style="line-height: normal; font-size-adjust: none; font-stretch: normal;"><span style="font-size: 7pt;">&nbsp;&nbsp;&nbsp; </span></span></span></span><span style="mso-ansi-language: en;" lang="EN"><span style="font-size: 11pt;">In this version you need to explicitly specify subscription information for cmdlets. The plan is after the cmdlets are integrated with Azure PowerShell tools this step won&rsquo;t be necessary. Once can get the Default or Current subscription:</span></span></p>
<p class="CodeLine" style="margin: 3pt 0cm 3pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="mso-spacerun: yes;"><span style="font-size: 10pt;">&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 10pt;">$subscriptionInfo = Get-AzureSubscription -Default</span></p>
<p class="MsoListParagraph" style="margin: 0cm 0cm 0pt 36pt; line-height: normal; text-indent: -18pt; list-style-type: disc; mso-list: l1 level1 lfo5;"><span style="mso-fareast-font-family: 'Segoe UI'; mso-bidi-font-family: 'Segoe UI'; mso-ansi-language: en;" lang="EN"><span style="mso-list: ignore;"><span style="font-size: 11pt;">2.</span><span style="line-height: normal; font-size-adjust: none; font-stretch: normal;"><span style="font-size: 7pt;">&nbsp;&nbsp;&nbsp; </span></span></span></span><span style="mso-ansi-language: en;" lang="EN"><span style="font-size: 11pt;">Once the Subscription Information has been obtained the properties of the Subscription are accessible:</span></span></p>
<p class="CodeLineCxSpFirst" style="margin: 3pt 0cm 0pt 36pt; line-height: 11pt; list-style-type: disc; mso-add-space: auto;"><span style="font-size: 10pt;">$subName = $subscriptionInfo | %{ $_.SubscriptionName }</span></p>
<p class="CodeLineCxSpMiddle" style="margin: 0cm 0cm 0pt 36pt; line-height: 11pt; list-style-type: disc; mso-add-space: auto;"><span style="font-size: 10pt;">$subId = $subscriptionInfo | %{ $_.SubscriptionId }</span></p>
<p class="CodeLineCxSpMiddle" style="margin: 0cm 0cm 0pt 36pt; line-height: 11pt; list-style-type: disc; mso-add-space: auto;"><span style="font-size: 10pt;">$cert = $subscriptionInfo | %{ $_.Certificate }</span></p>
<p class="CodeLineCxSpLast" style="margin: 0cm 0cm 3pt 36pt; line-height: 11pt; list-style-type: disc; mso-add-space: auto;"><span style="font-size: 10pt;">$storeAccount = $subscriptionInfo | %{ $_.CurrentStorageAccountName }</span></p>
<p class="MsoListParagraph" style="margin: 0cm 0cm 0pt 36pt; line-height: normal; text-indent: -18pt; list-style-type: disc; mso-list: l1 level1 lfo5;"><span style="mso-fareast-font-family: 'Segoe UI'; mso-bidi-font-family: 'Segoe UI'; mso-ansi-language: en;" lang="EN"><span style="mso-list: ignore;"><span style="font-size: 11pt;">3.</span><span style="line-height: normal; font-size-adjust: none; font-stretch: normal;"><span style="font-size: 7pt;">&nbsp;&nbsp;&nbsp; </span></span></span></span><span style="mso-ansi-language: en;" lang="EN"><span style="font-size: 11pt;">Get the Storage Account Key so you can use it later.</span></span></p>
<p class="CodeLine" style="margin: 3pt 0cm 3pt 36pt; line-height: 11pt; list-style-type: disc; mso-add-space: auto;"><span style="font-size: 10pt;">$key = Get-AzureStorageKey $storeAccount | %{ $_.Primary }</span></p>
<p class="MsoListParagraph" style="margin: 0cm 0cm 0pt 36pt; line-height: normal; text-indent: -18pt; list-style-type: disc; mso-list: l1 level1 lfo5;"><span style="mso-fareast-font-family: 'Segoe UI'; mso-bidi-font-family: 'Segoe UI'; mso-ansi-language: en;" lang="EN"><span style="mso-list: ignore;"><span style="font-size: 11pt;">4.</span><span style="line-height: normal; font-size-adjust: none; font-stretch: normal;"><span style="font-size: 7pt;">&nbsp;&nbsp;&nbsp; </span></span></span></span><span style="mso-ansi-language: en;" lang="EN"><span style="font-size: 11pt;">Next get the Storage Account information:</span></span></p>
<p class="CodeLineCxSpFirst" style="margin: 3pt 0cm 0pt 36pt; line-height: 11pt; list-style-type: disc; mso-add-space: auto;"><span style="font-size: 10pt;">$storageAccountInfo = Get-AzureStorageAccount $storeAccount</span></p>
<p class="CodeLineCxSpLast" style="margin: 0cm 0cm 3pt 36pt; line-height: 11pt; list-style-type: disc; mso-add-space: auto;"><span style="font-size: 10pt;">$location = $storageAccountInfo | %{ $_.Location }</span></p>
<p class="MsoListParagraph" style="margin: 0cm 0cm 0pt 36pt; line-height: normal; text-indent: -18pt; list-style-type: disc; mso-list: l1 level1 lfo5;"><span style="mso-fareast-font-family: 'Segoe UI'; mso-bidi-font-family: 'Segoe UI'; mso-ansi-language: en;" lang="EN"><span style="mso-list: ignore;"><span style="font-size: 11pt;">5.</span><span style="line-height: normal; font-size-adjust: none; font-stretch: normal;"><span style="font-size: 7pt;">&nbsp;&nbsp;&nbsp; </span></span></span></span><span style="mso-ansi-language: en;" lang="EN"><span style="font-size: 11pt;">Define the PS Credentials to be used when creating the cluster:</span></span></p>
<p class="CodeLineCxSpFirst" style="margin: 3pt 0cm 0pt 36pt; line-height: 11pt; list-style-type: disc; mso-add-space: auto;"><span style="font-size: 10pt;">$hadoopUsername = "Hadoop"</span></p>
<p class="CodeLineCxSpMiddle" style="margin: 0cm 0cm 0pt 36pt; line-height: 11pt; list-style-type: disc; mso-add-space: auto;"><span style="font-size: 10pt;">$clusterUsername = "Admin"</span></p>
<p class="CodeLineCxSpMiddle" style="margin: 0cm 0cm 0pt 36pt; line-height: 11pt; list-style-type: disc; mso-add-space: auto;"><span style="font-size: 10pt;">$clusterPassword = "<em style="mso-bidi-font-style: normal;">MyPassword</em>"</span></p>
<p class="CodeLineCxSpMiddle" style="margin: 0cm 0cm 0pt 36pt; line-height: 11pt; list-style-type: disc; mso-add-space: auto;"><span style="font-size: 10pt;">$secpasswd = ConvertTo-SecureString $clusterPassword -AsPlainText -Force</span></p>
<p class="CodeLineCxSpLast" style="margin: 0cm 0cm 3pt 36pt; line-height: 11pt; list-style-type: disc; mso-add-space: auto;"><span style="font-size: 10pt;">$clusterCreds = New-Object System.Management.Automation.PSCredential($clusterUsername, $secpasswd)</span></p>
<p class="MsoListParagraph" style="margin: 0cm 0cm 0pt 36pt; line-height: normal; text-indent: -18pt; list-style-type: disc; mso-list: l1 level1 lfo5;"><span style="mso-fareast-font-family: 'Segoe UI'; mso-bidi-font-family: 'Segoe UI'; mso-ansi-language: en;" lang="EN"><span style="mso-list: ignore;"><span style="font-size: 11pt;">6.</span><span style="line-height: normal; font-size-adjust: none; font-stretch: normal;"><span style="font-size: 7pt;">&nbsp;&nbsp;&nbsp; </span></span></span></span><span style="mso-ansi-language: en;" lang="EN"><span style="font-size: 11pt;">Define the final cluster creation parameters:</span></span></p>
<p class="CodeLineCxSpFirst" style="margin: 3pt 0cm 0pt 36pt; line-height: 11pt; list-style-type: disc; mso-add-space: auto;"><span style="font-size: 10pt;">$clusterName = </span><em style="mso-bidi-font-style: normal;"><span style="font-size: 10pt;">$Cluster</span></em></p>
<p class="CodeLineCxSpMiddle" style="margin: 0cm 0cm 0pt 36pt; line-height: 11pt; list-style-type: disc; mso-add-space: auto;"><span style="font-size: 10pt;">$numberNodes = </span><em style="mso-bidi-font-style: normal;"><span style="font-size: 10pt;">$Hosts</span></em></p>
<p class="CodeLineCxSpMiddle" style="margin: 0cm 0cm 0pt 36pt; line-height: 11pt; list-style-type: disc; mso-add-space: auto;"><span style="font-size: 10pt;">$containerDefault = $StorageContainer</span></p>
<p class="CodeLineCxSpLast" style="margin: 0cm 0cm 3pt 36pt; line-height: 11pt; list-style-type: disc; mso-add-space: auto;"><span style="font-size: 10pt;">$blobStorage = "$storeAccount.blob.core.windows.net"</span></p>
<p class="MsoListParagraph" style="margin: 0cm 0cm 0pt 36pt; line-height: normal; text-indent: -18pt; list-style-type: disc; mso-list: l1 level1 lfo5;"><span style="mso-fareast-font-family: 'Segoe UI'; mso-bidi-font-family: 'Segoe UI'; mso-ansi-language: en;" lang="EN"><span style="mso-list: ignore;"><span style="font-size: 11pt;">7.</span><span style="line-height: normal; font-size-adjust: none; font-stretch: normal;"><span style="font-size: 7pt;">&nbsp;&nbsp;&nbsp; </span></span></span></span><span style="mso-ansi-language: en;" lang="EN"><span style="font-size: 11pt;">Finally create new cluster. The cmdlet will return Cluster object in about 10-15 minutes when cluster is finished provisioning:</span></span></p>
<p class="CodeLine" style="margin: 3pt 0cm 3pt 36pt; line-height: 11pt; list-style-type: disc; mso-add-space: auto;"><span style="font-size: 10pt;">New-AzureHDInsightCluster -Subscription $subId -Certificate $cert -Name $clusterName -Location $location -DefaultStorageAccountName $blobStorage -DefaultStorageAccountKey $key -DefaultStorageContainerName $containerDefault -Credential $clusterCreds -ClusterSizeInNodes $numberNodes</span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><span style="font-size: 11pt;">Once the cluster has been created you will see the results displayed on the screen:</span></span></p>
<p class="CodeLineCxSpFirst" style="margin: 3pt 0cm 0pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">Name<span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>: carlhdinsighttest</span></p>
<p class="CodeLineCxSpMiddle" style="margin: 0cm 0cm 0pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">ConnectionUrl<span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp; </span>: https://carlhdinsighttest.azurehdinsight.net</span></p>
<p class="CodeLineCxSpMiddle" style="margin: 0cm 0cm 0pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">State<span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>: Running</span></p>
<p class="CodeLineCxSpMiddle" style="margin: 0cm 0cm 0pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">CreateDate<span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>: 07/11/2013 14:16:37</span></p>
<p class="CodeLineCxSpMiddle" style="margin: 0cm 0cm 0pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">UserName<span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>: Username</span></p>
<p class="CodeLineCxSpMiddle" style="margin: 0cm 0cm 0pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">Location<span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>: Location</span></p>
<p class="CodeLineCxSpLast" style="margin: 0cm 0cm 3pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">ClusterSizeInNodes : 4</span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><span style="font-size: 11pt;">You will also be able to see the cluster in the management portal:</span></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/76/metablogapi/0131.image_3BF2FB7E.png" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-76-metablogapi/0131.image_5F00_3BF2FB7E.png"><img style="margin: 0px; border: 0px currentcolor; display: inline; background-image: none;" title="image" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/76/metablogapi/4073.image_thumb_0F93984F.png" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-76-metablogapi/4073.image_5F00_thumb_5F00_0F93984F.png" alt="image" width="400" height="256" border="0" /></a></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><span style="font-size: 11pt;">If the subscription one is managing is not the configured default, before creating the cluster, one should execute the following command:</span></span></p>
<p class="CodeLine" style="margin: 3pt 0cm 3pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">Set-AzureSubscription -DefaultSubscription $mySubName</span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><span style="font-size: 11pt;">To create a script that enables one to pass in parameters, such as the number of required hosts, you can define a Param statement as the first line of the script:</span></span></p>
<p class="CodeLine" style="margin: 3pt 0cm 3pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">Param($Hosts = 4, [string] $Cluster = $(throw "Cluster Name Required."), [string] $StorageContainer = "hadooproot")</span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><span style="font-size: 11pt;">The complete script would thus be as follows:</span></span></p>
<div style="padding: 1pt 4pt; border: 1pt solid windowtext; line-height: normal; list-style-type: disc; mso-border-alt: solid windowtext .5pt; mso-element: para-border-div;">
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">Param($Hosts = 4, [string] $Cluster = $(throw "Cluster Name Required."), [string] $StorageContainer = "hadooproot")</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;"># Import the management module and assemblies</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">Import-Module "C:\Program Files (x86)\Windows Azure HDInsight PowerShell\Microsoft.WindowsAzure.Management.HDInsight.Cmdlet\Microsoft.WindowsAzure.Management.HDInsight.Cmdlet.dll"</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">Add-Type &ndash;AssemblyName System.Web</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;"># Get the subscription information and set variables</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$subscriptionInfo = Get-AzureSubscription -Default</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$subName = $subscriptionInfo | %{ $_.SubscriptionName }</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$subId = $subscriptionInfo | %{ $_.SubscriptionId }</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$cert = $subscriptionInfo | %{ $_.Certificate }</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$storeAccount = $subscriptionInfo | %{ $_.CurrentStorageAccountName }</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$key = Get-AzureStorageKey $storeAccount | %{ $_.Primary }</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$storageAccountInfo = Get-AzureStorageAccount $storeAccount</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$location = $storageAccountInfo | %{ $_.Location }</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$hadoopUsername = "Hadoop"</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$clusterUsername = "Admin"</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$clusterPassword = "mypassword"</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$secpasswd = ConvertTo-SecureString $clusterPassword -AsPlainText -Force</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$clusterCreds = New-Object System.Management.Automation.PSCredential($clusterUsername, $secpasswd)</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$clusterName = $Cluster</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$numberNodes = $Hosts</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$containerDefault = $StorageContainer</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$blobStorage = "$storeAccount.blob.core.windows.net"</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;"># Create the cluster</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">Write-Host "Creating '$numbernodes' Node Cluster named: $clusterName" -f yellow</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">Write-Host "Storage Account '$storeAccount' and Container '$containerDefault'" -f yellow</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">Write-Host "User '$clusterUsername' Password '$clusterPassword'" -f green</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">New-AzureHDInsightCluster -Subscription $subId -Certificate $cert -Name $clusterName -Location $location -DefaultStorageAccountName $blobStorage -DefaultStorageAccountKey $key -DefaultStorageContainerName $containerDefault -Credential $clusterCreds -ClusterSizeInNodes $numberNodes</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">Write-Host "Created '$numberNodes' Node Cluster: $clusterName" -f yellow</span></span></p>
</div>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><span style="font-size: 11pt;">The script can then be executed as follows:</span></span></p>
<p class="CodeLine" style="margin: 3pt 0cm 3pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">. "C:\Scripts\ClusterCreate.ps1" -Hosts 4 -Cluster "carlhdinsighttest" -StorageContainer "hadooproot"</span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><span style="font-size: 11pt;">Of course other variables can also be passed in.</span></span></p>
<h3 style="margin: 18pt 0cm 12pt; line-height: normal; list-style-type: disc;"><a name="_Toc373503064"></a><a name="_Toc359333214"></a><span style="mso-bookmark: _toc373503064;"><span lang="EN-US"><span style="font-size: 14pt;">Manage Cluster</span></span></span></h3>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span style="mso-ansi-language: en;" lang="EN"><span style="font-size: 11pt;">One can view your current clusters within the current subscription using the following command:</span></span></p>
<p class="CodeLine" style="margin: 3pt 0cm 3pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">Get-AzureHDInsightCluster -Subscription $subId -Certificate $cert</span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span style="mso-ansi-language: en; mso-fareast-language: en-gb;" lang="EN"><span style="font-size: 11pt;">To view the details of a specific cluster use the following command:</span></span></p>
<p class="CodeLine" style="margin: 3pt 0cm 3pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="mso-fareast-language: en-gb;"><span style="font-size: 10pt;">Get-AzureHDInsightCluster $clusterName -Subscription $subId -Certificate $cert</span></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><span style="font-size: 11pt;">To delete the cluster run the command:</span></span></p>
<p class="CodeLine" style="margin: 3pt 0cm 3pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="mso-fareast-language: en-gb;"><span style="font-size: 10pt;">Remove-AzureHDInsightCluster $clusterName -Subscription $subId -Certificate $cert</span></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><span style="font-size: 11pt;">Thus a script one could use to delete clusters would be:</span></span></p>
<div style="padding: 1pt 4pt; border: 1pt solid windowtext; line-height: normal; list-style-type: disc; mso-border-alt: solid windowtext .5pt; mso-element: para-border-div;">
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">Param($Cluster = $(throw "Cluster Name Required."))</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;"># Import the management module</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">Import-Module "C:\Program Files (x86)\Windows Azure HDInsight PowerShell\Microsoft.WindowsAzure.Management.HDInsight.Cmdlet\Microsoft.WindowsAzure.Management.HDInsight.Cmdlet.dll"</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;"># Get the subscription information and set variables</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$subscriptionInfo = Get-AzureSubscription -Default</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$subName = $subscriptionInfo | %{ $_.SubscriptionName }</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$subId = $subscriptionInfo | %{ $_.SubscriptionId }</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$cert = $subscriptionInfo | %{ $_.Certificate }</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$storeAccount = $subscriptionInfo | %{ $_.CurrentStorageAccountName }</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$clusterName = $Cluster</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;"># Delete the cluster</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">Write-Host "Deleting Cluster named: $clusterName" -f yellow</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">Remove-AzureHDInsightCluster $clusterName -Subscription $subId -Certificate $cert</span></span></p>
</div>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><span style="font-size: 11pt;">The script can then be executed as follows:</span></span></p>
<p class="CodeLine" style="margin: 3pt 0cm 3pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">. "C:\Scripts\ClusterDelete.ps1" -Cluster "carlhdinsighttest"</span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><span style="font-size: 11pt;">There are few word that need to be mentioned around versioning and cluster name.</span></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span class="MsoSubtleEmphasis"><span lang="EN-US"><span style="font-size: 11pt;">When a cluster is currently provisioned you will always get the latest build. However if there are breaking changes between your current build and latest build currently executing code could break. Future versions of the cluster provisioning may allow for a version number to be specified.</span></span></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span class="MsoSubtleEmphasis"><span lang="EN-US"><span style="font-size: 11pt;">Also, when you delete a cluster there is no guarantee that when you recreate the cluster the name will be available. To ensure that your name is not going to be used abstract cluster names should be used.</span></span></span></p>
<h3 style="margin: 18pt 0cm 12pt; line-height: normal; list-style-type: disc;"><a name="_Toc373503065"></a><a name="_Toc359333215"></a><a name="_Ref359312012"></a><span style="mso-bookmark: _toc359333215;"><span style="mso-bookmark: _toc373503065;"><span style="mso-ansi-language: en;" lang="EN"><span style="font-size: 14pt;">Provision Customized Cluster</span></span></span></span></h3>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span style="mso-ansi-language: en;" lang="EN"><span style="font-size: 11pt;">You can also provision cluster and configure it to connect to more than one Azure Blob storage or custom Hive and Oozie meta-stores.</span></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><em><span style="mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi;" lang="EN-US"><span style="font-size: 11pt;">This advanced feature allows you to separate the lifetime of your data and metadata from the lifetime of the cluster. This not only includes job submission details but also allows one to retain Hive metadata, and hence table definitions, as long as Hive External tables are used.</span></span></em></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span style="mso-ansi-language: en;" lang="EN"><span style="font-size: 11pt;">Using the same principal of defining the italicized variables first, the first step is to get the subscription details and the storage key of the storage accounts you want to connect your cluster to, in this case 2 storage accounts:</span></span></p>
<p class="CodeLineCxSpFirst" style="margin: 3pt 0cm 0pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">$subscriptionInfo = Get-AzureSubscription -Default</span></p>
<p class="CodeLineCxSpMiddle" style="margin: 0cm 0cm 0pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">&nbsp;</span></p>
<p class="CodeLineCxSpMiddle" style="margin: 0cm 0cm 0pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">$subName = $subscriptionInfo | %{ $_.SubscriptionName }</span></p>
<p class="CodeLineCxSpMiddle" style="margin: 0cm 0cm 0pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">$subId = $subscriptionInfo | %{ $_.SubscriptionId }</span></p>
<p class="CodeLineCxSpMiddle" style="margin: 0cm 0cm 0pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">$cert = $subscriptionInfo | %{ $_.Certificate }</span></p>
<p class="CodeLineCxSpMiddle" style="margin: 0cm 0cm 0pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">$storeAccount = $subscriptionInfo | %{ $_.CurrentStorageAccountName }</span></p>
<p class="CodeLineCxSpMiddle" style="margin: 0cm 0cm 0pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">$storeAccountBck = $storeAccount + "<em style="mso-bidi-font-style: normal;">bck</em>" # Assume same name with bck suffix</span></p>
<p class="CodeLineCxSpMiddle" style="margin: 0cm 0cm 0pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">&nbsp;</span></p>
<p class="CodeLineCxSpMiddle" style="margin: 0cm 0cm 0pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">$key = Get-AzureStorageKey $storeAccount | %{ $_.Primary }</span></p>
<p class="CodeLineCxSpMiddle" style="margin: 0cm 0cm 0pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">$keyBck = Get-AzureStorageKey $storeAccountBck | %{ $_.Primary }</span></p>
<p class="CodeLineCxSpMiddle" style="margin: 0cm 0cm 0pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">&nbsp;</span></p>
<p class="CodeLineCxSpMiddle" style="margin: 0cm 0cm 0pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">$storageAccountInfo = Get-AzureStorageAccount $storeAccount</span></p>
<p class="CodeLineCxSpMiddle" style="margin: 0cm 0cm 0pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">$location = $storageAccountInfo | %{ $_.Location }</span></p>
<p class="CodeLineCxSpMiddle" style="margin: 0cm 0cm 0pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">&nbsp;</span></p>
<p class="CodeLineCxSpMiddle" style="margin: 0cm 0cm 0pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">$hadoopUsername = "Hadoop"</span></p>
<p class="CodeLineCxSpMiddle" style="margin: 0cm 0cm 0pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">$clusterUsername = "Admin"</span><span style="mso-tab-count: 1;"><span style="font-size: 10pt;">&nbsp;&nbsp;&nbsp;&nbsp; </span></span></p>
<p class="CodeLineCxSpMiddle" style="margin: 0cm 0cm 0pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">$clusterPassword = "<em style="mso-bidi-font-style: normal;">Password</em>"</span></p>
<p class="CodeLineCxSpMiddle" style="margin: 0cm 0cm 0pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">&nbsp;</span></p>
<p class="CodeLineCxSpMiddle" style="margin: 0cm 0cm 0pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">$secpasswd = ConvertTo-SecureString $clusterPassword -AsPlainText -Force</span></p>
<p class="CodeLineCxSpMiddle" style="margin: 0cm 0cm 0pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">$clusterCreds = New-Object System.Management.Automation.PSCredential($clusterUsername, $secpasswd)</span></p>
<p class="CodeLineCxSpMiddle" style="margin: 0cm 0cm 0pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">&nbsp;</span></p>
<p class="CodeLineCxSpMiddle" style="margin: 0cm 0cm 0pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">$clusterName = $Cluster</span></p>
<p class="CodeLineCxSpMiddle" style="margin: 0cm 0cm 0pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">$numberNodes = $Hosts</span></p>
<p class="CodeLineCxSpMiddle" style="margin: 0cm 0cm 0pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">$containerDefault = $StorageContainer</span></p>
<p class="CodeLineCxSpMiddle" style="margin: 0cm 0cm 0pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">$blobStorage = "$storeAccount.blob.core.windows.net"</span></p>
<p class="CodeLineCxSpLast" style="margin: 0cm 0cm 3pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">$blobStorageBck = "$storeAccountBck.blob.core.windows.net"</span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><span style="font-size: 11pt;">In this instance I have assumed that the secondary (backup) storage account has the same name as the original with a &ldquo;bck&rdquo; prefix. This however can be configured for your environment.</span></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><span style="font-size: 11pt;">The next step is to create your custom SQL Azure Database and Server following this <a href="http://blogs.msdn.com/b/windowsazure/archive/2013/02/07/windows-azure-sql-database-management-with-powershell.aspx">blog post</a> or get credentials to the existing one</span></span><span style="line-height: 11pt; mso-bidi-font-family: 'Segoe UI'; mso-ansi-language: en;" lang="EN"><span style="font-size: 9.5pt;">.</span></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span style="mso-ansi-language: en;" lang="EN"><span style="font-size: 11pt;">To create a SQL Database one can easily use the management portal:</span></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/76/metablogapi/0564.image_5294881E.png" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-76-metablogapi/0564.image_5F00_5294881E.png"><img style="border: 0px currentcolor; display: inline; background-image: none;" title="image" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/76/metablogapi/4645.image_thumb_68ECBE14.png" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-76-metablogapi/4645.image_5F00_thumb_5F00_68ECBE14.png" alt="image" width="400" height="295" border="0" /></a><span style="mso-fareast-font-family: wingdings; mso-bidi-font-family: wingdings; mso-ansi-language: en; mso-bidi-font-size: 9.5pt;" lang="EN"><span style="mso-list: ignore;"><img src="file:///C:/Users/Carl/Technical Library/BigDataHadoop/ManagingHDInsightCluster/PicExportError" alt="*" width="10" height="10" /><span style="line-height: normal; font-size-adjust: none; font-stretch: normal;"><span style="font-size: 7pt;">&nbsp; </span></span></span></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span style="mso-ansi-language: en;" lang="EN"><span style="font-size: 11pt;">Using this connectivity information you can create cluster config object, pipe in additional storage and meta-stores configuration, and then finally pipe this config object into the New-AzureHDInsightCluster cmdlet to create a cluster based on this custom configuration.</span></span></p>
<p class="CodeLineCxSpFirst" style="margin: 3pt 0cm 0pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">New-AzureHDInsightClusterConfig -ClusterSizeInNodes $numberNodes</span></p>
<p class="CodeLineCxSpMiddle" style="margin: 0cm 0cm 0pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="mso-spacerun: yes;"><span style="font-size: 10pt;">&nbsp;</span></span><span style="font-size: 10pt;"><span style="mso-spacerun: yes;">&nbsp;</span>| Set-AzureHDInsightDefaultStorage -StorageAccountName $blobStorage -StorageAccountKey $key -StorageContainerName $containerDefault</span></p>
<p class="CodeLineCxSpMiddle" style="margin: 0cm 0cm 0pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="mso-spacerun: yes;"><span style="font-size: 10pt;">&nbsp;</span></span><span style="font-size: 10pt;"><span style="mso-spacerun: yes;">&nbsp;</span>| Add-AzureHDInsightStorage -StorageAccountName $blobStorageBck -StorageAccountKey $keyBck</span></p>
<p class="CodeLineCxSpMiddle" style="margin: 0cm 0cm 0pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="mso-spacerun: yes;"><span style="font-size: 10pt;">&nbsp;</span></span><span style="font-size: 10pt;"><span style="mso-spacerun: yes;">&nbsp;</span>| Add-AzureHDInsightMetastore -SqlAzureServerName $sqlServer -DatabaseName $databaseOozie -Credential $databaseCreds -MetastoreType OozieMetastore</span></p>
<p class="CodeLineCxSpMiddle" style="margin: 0cm 0cm 0pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="mso-spacerun: yes;"><span style="font-size: 10pt;">&nbsp;</span></span><span style="font-size: 10pt;"><span style="mso-spacerun: yes;">&nbsp;</span>| Add-AzureHDInsightMetastore -SqlAzureServerName $sqlServer -DatabaseName $databaseHive -Credential $databaseCreds -MetastoreType HiveMetastore</span></p>
<p class="CodeLineCxSpMiddle" style="margin: 0cm 0cm 0pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="mso-spacerun: yes;"><span style="font-size: 10pt;">&nbsp;</span></span><span style="font-size: 10pt;"><span style="mso-spacerun: yes;">&nbsp;</span>| New-AzureHDInsightCluster -Subscription $subId -Certificate $cert -Credential $clusterCreds -Name $clusterName -Location $location</span></p>
<p class="CodeLineCxSpLast" style="margin: 0cm 0cm 3pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">&nbsp;</span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><span style="font-size: 11pt;">In this instance I have again assumed that the Oozie and Hive database servers are the same and that the credentials are shared. S before this can easily be configured for your environment.</span></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><span style="font-size: 11pt;">As before one can use the PowerShell Param option to create a script that enables passing in parameters; such as number hosts, cluster name, etc.</span></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><span style="font-size: 11pt;">A complete configurable script would be as follows:</span></span></p>
<div style="padding: 1pt 4pt; border: 1pt solid windowtext; line-height: normal; list-style-type: disc; mso-border-alt: solid windowtext .5pt; mso-element: para-border-div;">
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">Param($Hosts = 4, [string] $Cluster = $(throw "Cluster Name Required."), [string] $StorageContainer = "hadooproot")</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;"># Import the management module and assemblies</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">Import-Module "C:\Program Files (x86)\Windows Azure HDInsight PowerShell\Microsoft.WindowsAzure.Management.HDInsight.Cmdlet\Microsoft.WindowsAzure.Management.HDInsight.Cmdlet.dll"</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">Add-Type &ndash;AssemblyName System.Web</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;"># Get the subscription information and set variables</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$subscriptionInfo = Get-AzureSubscription -Default</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$subName = $subscriptionInfo | %{ $_.SubscriptionName }</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$subId = $subscriptionInfo | %{ $_.SubscriptionId }</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$cert = $subscriptionInfo | %{ $_.Certificate }</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$storeAccount = $subscriptionInfo | %{ $_.CurrentStorageAccountName }</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$storeAccountBck = $storeAccount + "bck" # Assume same name with bck suffix</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$key = Get-AzureStorageKey $storeAccount | %{ $_.Primary }</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$keyBck = Get-AzureStorageKey $storeAccountBck | %{ $_.Primary }</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$storageAccountInfo = Get-AzureStorageAccount $storeAccount</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$location = $storageAccountInfo | %{ $_.Location }</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$hadoopUsername = "Hadoop"</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$clusterUsername = "Admin"</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$clusterPassword = "Password"</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$secpasswd = ConvertTo-SecureString $clusterPassword -AsPlainText -Force</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$clusterCreds = New-Object System.Management.Automation.PSCredential($clusterUsername, $secpasswd)</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$clusterName = $Cluster</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$numberNodes = $Hosts</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$containerDefault = $StorageContainer</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$blobStorage = "$storeAccount.blob.core.windows.net"</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$blobStorageBck = "$storeAccountBck.blob.core.windows.net"</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;"># Define variables for the SQL database connectivity</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$sqlServer = "servername.database.windows.net"</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$sqlUsername = "SqlUsername"</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$sqlPassword = "SqlPassword"</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$sqlpasswd = ConvertTo-SecureString $sqlPassword -AsPlainText -Force</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$databaseCreds = New-Object System.Management.Automation.PSCredential($sqlUsername, $sqlpasswd)</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$databaseHive = "HiveDatabaseName"</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$databaseOozie = "OozieDatabaseName"</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;"># Create the cluster</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">Write-Host "Creating '$numberNodes' Node Cluster named: $clusterName" -f yellow</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">Write-Host "Storage Account '$storeAccount' and Container '$containerDefault'" -f yellow</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">Write-Host "Storage Account Backup '$storeAccountBck'" -f yellow</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">Write-Host "User '$clusterUsername' Password '$clusterPassword'" -f green</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">New-AzureHDInsightClusterConfig -ClusterSizeInNodes $numberNodes | Set-AzureHDInsightDefaultStorage -StorageAccountName $blobStorage -StorageAccountKey $key -StorageContainerName $containerDefault | Add-AzureHDInsightStorage -StorageAccountName $blobStorageBck -StorageAccountKey $keyBck | Add-AzureHDInsightMetastore -SqlAzureServerName $sqlServer -DatabaseName $databaseOozie -Credential $databaseCreds -MetastoreType OozieMetastore | Add-AzureHDInsightMetastore -SqlAzureServerName $sqlServer -DatabaseName $databaseHive -Credential $databaseCreds -MetastoreType HiveMetastore | New-AzureHDInsightCluster -Subscription $subId -Certificate $cert -Credential $clusterCreds -Name $clusterName -Location $location</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">Write-Host "Created '$numberNodes' Node Cluster: $clusterName" -f yellow</span></span></p>
</div>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><span style="font-size: 11pt;">The script can then be executed as follows:</span></span></p>
<p class="CodeLine" style="margin: 3pt 0cm 3pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">. "C:\Scripts\ClusterCreateCustom.ps1" -Hosts 4 -Cluster "carlhdinsighttest" -StorageContainer "hadooproot"</span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><span style="font-size: 11pt;">Of course other variables can also be passed in.</span></span></p>
<h1 style="margin: 24pt 0cm 18pt; line-height: normal; list-style-type: disc;"><a name="_Toc373503066"></a><a name="_Toc359333216"></a><span style="mso-bookmark: _toc373503066;"><span lang="EN-US"><span style="font-size: 18pt;">Job Submission</span></span></span></h1>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><span style="font-size: 11pt;">In addition to managing the HDInsight cluster the Microsoft .Net SDK for Hadoop also allows one to write MapReduce jobs utilizing the .Net Framework. Under the covers this is employing the Hadoop Streaming interface. The documentation for creating such jobs can be found at:</span></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><a href="http://hadoopsdk.codeplex.com/wikipage?title=Map%2fReduce"><span style="font-size: 11pt;">http://hadoopsdk.codeplex.com/wikipage?title=Map%2fReduce</span></a></span></p>
<h2 style="margin: 18pt 0cm; line-height: normal; list-style-type: disc;"><a name="_Toc373503067"></a><a name="_Toc359333217"></a><span style="mso-bookmark: _toc373503067;"><span lang="EN-US"><span style="font-size: 16pt;">MRRunner Support</span></span></span></h2>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><span style="font-size: 11pt;">To submit such jobs there is a command-line utility called MRRunner that should be utilized. To support the MRRunner utility you should have an assembly (a .net DLL or EXE) that defines at least one implementation of HadoopJob&lt;&gt;.</span></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><span style="font-size: 11pt;">If the Dll contains only one implementation of Hadoop&lt;&gt;, you can run the job with:</span></span></p>
<p class="CodeLine" style="margin: 3pt 0cm 3pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">MRRunner -dll MyDll </span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><span style="font-size: 11pt;">If the Dll contains multiple implementations of HadoopJob&lt;&gt;, then you need to indicate the one you wish to run:</span></span></p>
<p class="CodeLine" style="margin: 3pt 0cm 3pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">MRRunner -dll MyDll -class MyClass </span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><span style="font-size: 11pt;">To supply options to your job, pass them as trailing arguments on the command-line, after a double-hyphen:</span></span></p>
<p class="CodeLine" style="margin: 3pt 0cm 3pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">MRRunner -dll MyDll -class MyClass -- extraArg1 extraArg2 </span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><span style="font-size: 11pt;">These additional arguments are provided to your job via a context object that is available to all methods on HadoopJob&lt;&gt;.</span></span></p>
<h2 style="margin: 18pt 0cm; line-height: normal; list-style-type: disc;"><a name="_Toc373503068"></a><a name="_Toc359333218"></a><span style="mso-bookmark: _toc373503068;"><span lang="EN-US"><span style="font-size: 16pt;">Local PowerShell Submission</span></span></span></h2>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><span style="font-size: 11pt;">Of course one can submit job directly from a .Net executable and thus PowerShell scripts.</span></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><span style="font-size: 11pt;">MapReduceJob is a property on Hadoop connection object. It provides access to implementation of IStreamingJobExecutor interface which handles the creation and execution of Hadoop Streaming jobs. Depending on how the connection object was created it can execute jobs locally using Hadoop command-line or against remote cluster using WebHCat client library. </span></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><span style="font-size: 11pt;">Under the covers the MRRunner utility will invoke LocalStreamingJobExecutor implementation on your behalf.</span></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><span style="font-size: 11pt;">Alternatively one can invoke the executor directly and request it execute a Hadoop Job:</span></span></p>
<p class="CodeLineCxSpFirst" style="margin: 3pt 0cm 0pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="mso-spacerun: yes;"><span style="font-size: 10pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 10pt;">var hadoop = Hadoop.Connect();</span></p>
<p class="CodeLineCxSpLast" style="margin: 0cm 0cm 3pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="mso-spacerun: yes;"><span style="font-size: 10pt;">&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 10pt;">hadoop.MapReduceJob.ExecuteJob&lt;JobType&gt;(arguments);</span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><span style="font-size: 11pt;">Utilizing this approach one can thus integrate Job Submission into cluster management scripts:</span></span></p>
<div style="padding: 1pt 4pt; border: 1pt solid windowtext; line-height: normal; list-style-type: disc; mso-border-alt: solid windowtext .5pt; mso-element: para-border-div;">
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;"># Define Base Paths</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$BasePath = "C:\Users\Carl\Projects"</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$dllPath = $BasePath + "\WordCountSampleApplication\Debug"</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$submitterDll = $dllPath + "\WordCount.dll"</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$hadoopcmd = $env:HADOOP_HOME + "/bin/hadoop.cmd"</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;"># Add submission file references</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">Add-Type -Path ($dllPath + "\microsoft.hadoop.mapreduce.dll")</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;"># Define the Type for the Job</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$submitterAssembly = [System.Reflection.Assembly]::LoadFile($submitterDll)</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">[Type] $jobType = $submitterAssembly.GetType("WordCountSampleApplication.WordCount", 1)</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;"># Connect and Run the Job</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$hadoop = [Microsoft.Hadoop.MapReduce.Hadoop]::Connect()</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$job = $hadoop.MapReduceJob</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$result = $job.ExecuteJob($jobType)</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">Write-Host "Job Run Information"</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">Write-Host "Job Id: " $result.Id</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">Write-Host "Exit Code: " $result.Info.ExitCode</span></span></p>
</div>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><span style="font-size: 11pt;">The only challenge becomes managing the generic objects within PowerShell.</span></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><span style="font-size: 11pt;">In this case I have used a version of the WordCount sample that comes with the SDK. I have removed the Driver entry point and compiled the assembly into a DLL rather than an EXE.</span></span></p>
<h2 style="margin: 18pt 0cm; line-height: normal; list-style-type: disc;"><a name="_Toc373503069"></a><a name="_Toc359333219"></a><span style="mso-bookmark: _toc373503069;"><span lang="EN-US"><span style="font-size: 16pt;">Remote PowerShell Submission</span></span></span></h2>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><span style="font-size: 11pt;">Of course the ultimate goal is to submit the job from your management console. The process for this is very similar to the Local submission, with the inclusion of the connection information for the Azure cluster.</span></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><span style="font-size: 11pt;">To provide flexibility in the job configuration allowing for a configuration object to be defined in the PowerShell script and for the mappers and reducers to be defined is desirable. This the full submission script would be as follows:</span></span></p>
<div style="padding: 1pt 4pt; border: 1pt solid windowtext; line-height: normal; list-style-type: disc; mso-border-alt: solid windowtext .5pt; mso-element: para-border-div;">
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">Param($Cluster = $(throw "Cluster Name Required."), [string] $StorageContainer = "hadooproot", [string] $InputPath = $(throw "Input Path Required."), [string] $OutputFolder = $(throw "Output Path Required."), [string] $Dll = $(throw "MapReduce Dll Path Required."), [string] $Mapper = $(throw "Mapper Type Required."), [string] $Reducer = $Null, [string] $Combiner = $Null)</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;"># Define all local variable names for script execution</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$submitterDll = $Dll</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$dllPath = Split-Path $submitterDll -parent</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$dllFile = Split-Path $submitterDll -leaf</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;"># Import the management module and assemblies</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">Import-Module "C:\Program Files (x86)\Windows Azure HDInsight PowerShell\Microsoft.WindowsAzure.Management.HDInsight.Cmdlet\Microsoft.WindowsAzure.Management.HDInsight.Cmdlet.dll"</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">Add-Type -Path ($dllPath + "\microsoft.hadoop.mapreduce.dll")</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">Add-Type -Path ($dllPath + "\microsoft.hadoop.webclient.dll")</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;"># Get the subscription information and set variables</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$subscriptionInfo = Get-AzureSubscription -Default</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$subName = $subscriptionInfo | %{ $_.SubscriptionName }</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$subId = $subscriptionInfo | %{ $_.SubscriptionId }</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$cert = $subscriptionInfo | %{ $_.Certificate }</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$storeAccount = $subscriptionInfo | %{ $_.CurrentStorageAccountName }</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$key = Get-AzureStorageKey $storeAccount | %{ $_.Primary }</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$storageAccountInfo = Get-AzureStorageAccount $storeAccount</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$clusterUsername = "Admin"</span><span style="mso-tab-count: 7;"><span style="font-size: 8pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$clusterPassword = "Password"</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$hadoopUsername = "Hadoop"</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$containerDefault = $StorageContainer</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$clusterName = $Cluster</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$clusterHttp = "https://$clusterName.azurehdinsight.net"</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$blobStorage = "$storeAccount.blob.core.windows.net"</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;"># Define the Types for the Job (Mapper. Reducer, Combiner)</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$submitterAssembly = [System.Reflection.Assembly]::LoadFile($submitterDll)</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">[Type] $mapperType = $submitterAssembly.GetType($Mapper, 1)</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">[Type] $reducerType = $Null</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">if ($Reducer) { $reducerType = $submitterAssembly.GetType($Reducer, 1) }</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">[Type] $combinerType = $Null</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">if ($Combiner) { $combinerType = $submitterAssembly.GetType($Combiner, 1) }</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;"># Define the configuration properties</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$config = New-Object Microsoft.Hadoop.MapReduce.HadoopJobConfiguration</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">[Boolean] $config.Verbose = $True</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$config.InputPath = $InputPath</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$config.OutputFolder = $OutputFolder</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;"># Define the connection properties</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">[Boolean] $createContinerIfNotExist = $True</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">[Uri] $clusterUri = New-Object System.Uri $clusterHttp</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;"># Connect and Run the Job</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">Write-Host "Executing Job Dll '$dllFile' on Cluster $clusterName" -f yellow</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$hadoop = [Microsoft.Hadoop.MapReduce.Hadoop]::Connect($clusterUri, $clusterUsername, $hadoopUsername, $clusterPassword, $blobStorage, $key, $containerDefault, $createContinerIfNotExist)</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$job = $hadoop.MapReduceJob</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$result = $job.Execute($mapperType, $reducerType, $combinerType, $config)</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">Write-Host "Job Run Information" -f yellow</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">Write-Host "Job Id: " $result.Id</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">Write-Host "Exit Code: " $result.Info.ExitCode</span></span></p>
</div>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><span style="font-size: 11pt;">The HadoopJobConfiguration allows for the definition of input and output files, along with other job parameters.</span></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><span style="font-size: 11pt;">To execute the job one has to provide the types for the Mapper, Reducer, and Combiner. If a Reducer or Combiner are not needed then the parameter value of $Null can be used:</span></span></p>
<p class="CodeLine" style="margin: 3pt 0cm 3pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">. "C:\Scripts\JobSubmissionAzure.ps1" -Cluster "carlhdinsighttest" -StorageContainer "hadooproot" -InputPath "/wordcount/input" -OutputFolder "/wordcount/output" -Dll "C:\Projects\WordCountSampleApplication\ \Debug\WordCount.dll" -Mapper "WordCountSampleApplication.WordCountMapper" -Reducer "WordCountSampleApplication.WordCountReducer" -Combiner "WordCountSampleApplication.WordCountReducer"</span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><span style="font-size: 11pt;">One has to remember in this case as we are submitting the job to an Azure cluster the default file system will be WASB.</span></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><span style="font-size: 11pt;">To run this sample I uploaded some text files to the appropriate Azure storage location:</span></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/76/metablogapi/6761.image_5EBB23A7.png" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-76-metablogapi/6761.image_5F00_5EBB23A7.png"><img style="margin: 0px; border: 0px currentcolor; display: inline; background-image: none;" title="image" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/76/metablogapi/7752.image_thumb_71067B1A.png" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-76-metablogapi/7752.image_5F00_thumb_5F00_71067B1A.png" alt="image" width="400" height="106" border="0" /></a></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><span style="font-size: 11pt;">The output then will show up in the wordcount/output container.</span></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><span style="font-size: 11pt;">After running this job, from within the management portal, you should see the metrics for the number of mapper and reducers executed be updated:</span></span><span style="mso-ansi-language: en-gb; mso-fareast-language: en-gb; mso-no-proof: yes;"><span style="font-size: 11pt;">&nbsp;</span></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/76/metablogapi/8787.image_00A916DD.png" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-76-metablogapi/8787.image_5F00_00A916DD.png"><img style="margin: 0px; border: 0px currentcolor; display: inline; background-image: none;" title="image" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/76/metablogapi/0410.image_thumb_020D39AF.png" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-76-metablogapi/0410.image_5F00_thumb_5F00_020D39AF.png" alt="image" width="400" height="199" border="0" /></a></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span class="MsoSubtleEmphasis"><span lang="EN-US"><span style="font-size: 11pt;">Unfortunately at this point in time you will not see your job within the job history when managing the cluster. For this you will have to use the standard Hadoop UI interfaces; accessible by connecting to the head node of the cluster.</span></span></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><span style="font-size: 11pt;">As an alternative to his approach one could define a HadoopJob class configuration within the code; in addition to the mappers and reducers, such as:</span></span></p>
<p class="MsoNormal" style="background: white; margin: 0cm 0cm 0pt; line-height: normal; list-style-type: disc; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="mso-fareast-font-family: 'Times New Roman'; mso-ansi-language: en-gb; mso-fareast-language: en-gb;"><span style="font-size: 10pt;">class</span></span><span style="font-size: 10pt;"><span style="mso-fareast-font-family: 'Times New Roman'; mso-ansi-language: en-gb; mso-fareast-language: en-gb;">&nbsp;</span><span style="mso-fareast-font-family: 'Times New Roman'; mso-ansi-language: en-gb; mso-fareast-language: en-gb;">WordCount</span><span style="mso-fareast-font-family: 'Times New Roman'; mso-ansi-language: en-gb; mso-fareast-language: en-gb;"> : </span><span style="mso-fareast-font-family: 'Times New Roman'; mso-ansi-language: en-gb; mso-fareast-language: en-gb;">HadoopJob</span><span style="mso-fareast-font-family: 'Times New Roman'; mso-ansi-language: en-gb; mso-fareast-language: en-gb;">&lt;</span><span style="mso-fareast-font-family: 'Times New Roman'; mso-ansi-language: en-gb; mso-fareast-language: en-gb;">WordCountMapper</span><span style="mso-fareast-font-family: 'Times New Roman'; mso-ansi-language: en-gb; mso-fareast-language: en-gb;">, </span><span style="mso-fareast-font-family: 'Times New Roman'; mso-ansi-language: en-gb; mso-fareast-language: en-gb;">WordCountReducer</span><span style="mso-fareast-font-family: 'Times New Roman'; mso-ansi-language: en-gb; mso-fareast-language: en-gb;">, </span><span style="mso-fareast-font-family: 'Times New Roman'; mso-ansi-language: en-gb; mso-fareast-language: en-gb;">WordCountReducer</span></span><span style="mso-fareast-font-family: 'Times New Roman'; mso-ansi-language: en-gb; mso-fareast-language: en-gb;"><span style="font-size: 10pt;">&gt;</span></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><span style="font-size: 11pt;">To execute this job one would just need to define the type for the HadoopJob, which provides the necessary configuration details:</span></span></p>
<p class="CodeLineCxSpFirst" style="margin: 3pt 0cm 0pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">[Type] $jobType = $submitterAssembly.GetType("WordCountSampleApplication.WordCount", 1)</span></p>
<p class="CodeLineCxSpMiddle" style="margin: 0cm 0cm 0pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">...</span></p>
<p class="CodeLineCxSpLast" style="margin: 0cm 0cm 3pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">$result = $job.Execute($jobType)</span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><span style="font-size: 11pt;">The code in either case is very similar but the former approach is probably more flexible. </span></span></p>
<h2 style="margin: 18pt 0cm; line-height: normal; list-style-type: disc;"><a name="_Toc373503070"></a><a name="_Toc359333223"></a><span style="mso-bookmark: _toc373503070;"><span lang="EN-US"><span style="font-size: 16pt;">Specifying WASB Paths</span></span></span></h2>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><span style="font-size: 11pt;">As mentioned the default schema when submitting MapReduce jobs is Azure Blob Storage. This means the default container specified when creating the cluster is used as the root. </span></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><span style="font-size: 11pt;">In the example above the path &ldquo;/wordcount/input&rdquo; actually equates to:</span></span></p>
<p class="CodeLineCxSpFirst" style="margin: 3pt 0cm 0pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">wasb://hadooproot@mystorageaccount.blob.core.windows.net/wordcount/input</span></p>
<p class="CodeLineCxSpLast" style="margin: 0cm 0cm 3pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">&nbsp;</span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><span style="font-size: 11pt;">Thus if one wanted to operate out of a different default container such as &ldquo;data&rdquo; fully-qualified names could be specified such as:</span></span></p>
<p class="CodeLineCxSpFirst" style="margin: 3pt 0cm 0pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">-InputPath "wasb://data@ mystorageaccount.blob.core.windows.net/books"</span></p>
<p class="CodeLineCxSpLast" style="margin: 0cm 0cm 3pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">-OutputFolder "wasb://data@mystorageaccount.blob.core.windows.net/wordcount/output"</span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><span style="font-size: 11pt;">This would equate to the &ldquo;data&rdquo; container:</span></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/76/metablogapi/1423.image_6D8A7AE1.png" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-76-metablogapi/1423.image_5F00_6D8A7AE1.png"><img style="margin: 0px; border: 0px currentcolor; display: inline; background-image: none;" title="image" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/76/metablogapi/5557.image_thumb_48208773.png" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-76-metablogapi/5557.image_5F00_thumb_5F00_48208773.png" alt="image" width="400" height="110" border="0" /></a></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><span style="font-size: 11pt;">The containers being referenced just need to be under the same storage account.</span></span></p>
<h2 style="margin: 18pt 0cm; line-height: normal; list-style-type: disc;"><a name="_Toc373503071"></a><a name="_Toc359333225"></a><span style="mso-bookmark: _toc373503071;"><span lang="EN-US"><span style="font-size: 16pt;">Elastic MapReduce </span></span></span><span style="mso-bookmark: _toc373503071;"><span lang="EN-US"><span style="font-size: 16pt;">Submissions</span></span></span></h2>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><span style="font-size: 11pt;">The idea behind an Elastic Service is that the cluster can be brought up, with the necessary hosts, when job execution is necessary. To achieve this, with the provided scripts, one can use the following approach:</span></span></p>
<div style="padding: 1pt 4pt; border: 1pt solid windowtext; line-height: normal; list-style-type: disc; mso-border-alt: solid windowtext .5pt; mso-element: para-border-div;">
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">Param($Hosts = 4, $Cluster = $(throw "Cluster Name Required."), [string] $StorageContainer = "hadooproot", [string] $InputPath = $(throw "Input Path Required."), [string] $OutputFolder = $(throw "Output Path Required."), [string] $Dll = $(throw "MapReduce Dll Path Required."), [string] $Mapper = $(throw "Mapper Type Required."), [string] $Reducer = $Null, [string] $Combiner = $Null)</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;"># Create the Cluster</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">. ".\ClusterCreate.ps1" -Hosts $Hosts -Cluster $Cluster &ndash;StorageContainer $StorageContainer</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;"># Execute the Job</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">. ".\JobSubmissionAzure.ps1" -Cluster "carlhdinsighttest" -StorageContainer = "hadooproot" -InputPath "/wordcount/input" -OutputFolder "/wordcount/output" -Dll "C:\Projects\WordCountSampleApplication\ \Debug\WordCount.dll" -Mapper "WordCountSampleApplication.WordCountMapper" -Reducer "WordCountSampleApplication.WordCountReducer" -Combiner "WordCountSampleApplication.WordCountReducer"</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;"># Delete the Cluster</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">. ".\ClusterDelete.ps1" -Cluster Cluster</span></span></p>
</div>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><span style="font-size: 11pt;">To execute the script one just has to execute:</span></span></p>
<p class="CodeLineCxSpFirst" style="margin: 3pt 0cm 0pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">. "C:\Scripts\JobSubmissionElastic.ps1" &ndash;Hosts 4 -Cluster "carlhdinsighttest" -StorageContainer = "hadooproot" -InputPath "/wordcount/input" -OutputFolder "/wordcount/output" -Dll "C:\Projects\WordCountSampleApplication\ \Debug\WordCount.dll" -Mapper "WordCountSampleApplication.WordCountMapper" -Reducer "WordCountSampleApplication.WordCountReducer" -Combiner "WordCountSampleApplication.WordCountReducer"</span></p>
<p class="CodeLineCxSpLast" style="margin: 0cm 0cm 3pt 17.85pt; line-height: 11pt; list-style-type: disc;"><span style="font-size: 10pt;">&nbsp;</span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><span style="font-size: 11pt;">This will create the cluster, run the required job, and then delete the cluster. During the creation the cluster name and number of hosts is specified, and during the job submission the input and output paths are specified.</span></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><span style="font-size: 11pt;">One could of course roll all the script elements into a single reusable script, as follows:</span></span></p>
<div style="padding: 1pt 4pt; border: 1pt solid windowtext; line-height: normal; list-style-type: disc; mso-border-alt: solid windowtext .5pt; mso-element: para-border-div;">
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">Param($Hosts = 4, $Cluster = $(throw "Cluster Name Required."), [string] $StorageContainer = "hadooproot", [string] $InputPath = $(throw "Input Path Required."), [string] $OutputFolder = $(throw "Output Path Required."), [string] $Dll = $(throw "MapReduce Dll Path Required."), [string] $Mapper = $(throw "Mapper Type Required."), [string] $Reducer = $Null, [string] $Combiner = $Null, $MapTasks = $Null)</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;"># Define all local variable names for script execution</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$submitterDll = $Dll</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$dllPath = Split-Path $submitterDll -parent</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$dllFile = Split-Path $submitterDll -leaf</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;"># Import the management module and assemblies</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">Import-Module "C:\Program Files (x86)\Windows Azure HDInsight PowerShell\Microsoft.WindowsAzure.Management.HDInsight.Cmdlet\Microsoft.WindowsAzure.Management.HDInsight.Cmdlet.dll"</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">Add-Type &ndash;AssemblyName System.Web</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">Add-Type -Path ($dllPath + "\microsoft.hadoop.mapreduce.dll")</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">Add-Type -Path ($dllPath + "\microsoft.hadoop.webclient.dll")</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;"># Get the subscription information and set variables</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$subscriptionInfo = Get-AzureSubscription -Default</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$subName = $subscriptionInfo | %{ $_.SubscriptionName }</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$subId = $subscriptionInfo | %{ $_.SubscriptionId }</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$cert = $subscriptionInfo | %{ $_.Certificate }</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$storeAccount = $subscriptionInfo | %{ $_.CurrentStorageAccountName }</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$storeAccountBck = $storeAccount + "bck" # Assume same name with bck suffix</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$key = Get-AzureStorageKey $storeAccount | %{ $_.Primary }</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$keyBck = Get-AzureStorageKey $storeAccountBck | %{ $_.Primary }</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$storageAccountInfo = Get-AzureStorageAccount $storeAccount</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$location = $storageAccountInfo | %{ $_.Location }</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$hadoopUsername = "Hadoop"</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$clusterUsername = "Admin"</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$clusterPassword = [System.Web.Security.Membership]::GeneratePassword(20, 5)</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$secpasswd = ConvertTo-SecureString $clusterPassword -AsPlainText -Force</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$clusterCreds = New-Object System.Management.Automation.PSCredential($clusterUsername, $secpasswd)</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$clusterName = $Cluster</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$numberNodes = $Hosts</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$containerDefault = $StorageContainer</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$clusterHttp = "https://$clusterName.azurehdinsight.net"</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$blobStorage = "$storeAccount.blob.core.windows.net"</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$blobStorageBck = "$storeAccountBck.blob.core.windows.net"</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;"># Define variables for the SQL database connectivity</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$sqlServer = "mydatabaseserver.database.windows.net"</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$sqlUsername = "SqlUsername"</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$sqlPassword = "SqlPassword"</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$sqlpasswd = ConvertTo-SecureString $sqlPassword -AsPlainText -Force</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$databaseCreds = New-Object System.Management.Automation.PSCredential($sqlUsername, $sqlpasswd)</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$databaseHive = "HiveDatabaseCarl"</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$databaseOozie = "OozieDatabaseCarl"</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;"># Create the cluster</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">Write-Host "Creating '$numberNodes' Node Cluster named: $clusterName" -f yellow</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">Write-Host "Storage Account '$storeAccount' and Container '$containerDefault'" -f yellow</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">Write-Host "Storage Account Backup '$storeAccountBck'" -f yellow</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">Write-Host "User '$clusterUsername' Password '$clusterPassword'" -f green</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">New-AzureHDInsightClusterConfig -ClusterSizeInNodes $numberNodes | Set-AzureHDInsightDefaultStorage -StorageAccountName $blobStorage -StorageAccountKey $key -StorageContainerName $containerDefault | Add-AzureHDInsightStorage -StorageAccountName $blobStorageBck -StorageAccountKey $keyBck | Add-AzureHDInsightMetastore -SqlAzureServerName $sqlServer -DatabaseName $databaseOozie -Credential $databaseCreds -MetastoreType OozieMetastore | Add-AzureHDInsightMetastore -SqlAzureServerName $sqlServer -DatabaseName $databaseHive -Credential $databaseCreds -MetastoreType HiveMetastore | New-AzureHDInsightCluster -Subscription $subId -Certificate $cert -Credential $clusterCreds -Name $clusterName -Location $location</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">Write-Host "Created '$numberNodes' Node Cluster: $clusterName" -f yellow</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;"># Define the Types for the Job (Mapper. Reducer, Combiner)</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$submitterAssembly = [System.Reflection.Assembly]::LoadFile($submitterDll)</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">[Type] $mapperType = $submitterAssembly.GetType($Mapper, 1)</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">[Type] $reducerType = $Null</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">if ($Reducer) { $reducerType = $submitterAssembly.GetType($Reducer, 1) }</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">[Type] $combinerType = $Null</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">if ($Combiner) { $combinerType = $submitterAssembly.GetType($Combiner, 1) }</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;"># Define the configuration properties</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$config = New-Object Microsoft.Hadoop.MapReduce.HadoopJobConfiguration</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">[Boolean] $config.Verbose = $True</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$config.InputPath = $InputPath</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$config.OutputFolder = $OutputFolder</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">if ($MapTasks) {$config.AdditionalGenericArguments.Add("-Dmapred.map.tasks=$MapTasks")}</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;"># Define the connection properties</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">[Boolean] $createContinerIfNotExist = $True</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">[Uri] $clusterUri = New-Object System.Uri $clusterHttp</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;"># Connect and Run the Job</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">Write-Host "Executing Job Dll '$dllFile' on Cluster $clusterName" -f yellow</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$hadoop = [Microsoft.Hadoop.MapReduce.Hadoop]::Connect($clusterUri, $clusterUsername, $hadoopUsername, $clusterPassword, $blobStorage, $key, $containerDefault, $createContinerIfNotExist)</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$job = $hadoop.MapReduceJob</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">$result = $job.Execute($mapperType, $reducerType, $combinerType, $config)</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">Write-Host "Job Run Information" -f yellow</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">Write-Host "Job Id: " $result.Id</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">Write-Host "Exit Code: " $result.Info.ExitCode</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">&nbsp;</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;"># Delete the cluster</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">Write-Host "Deleting Cluster named: $clusterName" -f yellow</span></span></p>
<p class="CodeBlock" style="margin: 1pt 0cm; padding: 0cm; line-height: 9pt; page-break-after: avoid;"><span lang="EN-US"><span style="font-size: 8pt;">Remove-AzureHDInsightCluster $clusterName -Subscription $subId -Certificate $cert</span></span></p>
</div>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><span style="font-size: 11pt;">In this instance the cluster password is randomly generated, as it only needs to exist for the lifetime of the job execution. In reality one could also randomly generate the name of the cluster.</span></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><span style="font-size: 11pt;">Of course one could also customize these scripts to including additional parameters such as additional job arguments, etc. The script presented here does take into consideration the number of mappers; specified with the MapTasks parameter.</span></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><span style="font-size: 11pt;">This Elastic Job Submission process is possible because the storage used for input and output is Azure Blob Storage. As such the cluster is only needed for compute operations and not storage. In addition the job history is persisted, across cluster creations, in the specified SQL databases.</span></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><span style="font-size: 11pt;">Of course other parameters could be included in this model to make the PowerShell script more applicable to more job types; such as number of mappers, additional job arguments, etc.</span></span></p>
<h1 style="margin: 24pt 0cm 18pt; line-height: normal; list-style-type: disc;"><a name="_Toc260989127"></a><a name="_Toc373503072"></a><span style="mso-bookmark: _toc260989127;"><span lang="EN-US"><span style="font-size: 18pt;">References</span></span></span></h1>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><a href="http://msdn.microsoft.com/en-us/library/windowsazure/jj156055.aspx"><span style="mso-bookmark: _toc260989127;"><span style="mso-ansi-language: en-gb;"><span style="font-size: 11pt;">Windows Azure PowerShell</span></span></span></a></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><a href="http://msdn.microsoft.com/en-us/library/windowsazure/jj554332.aspx"><span style="mso-bookmark: _toc260989127;"><span style="mso-ansi-language: en;" lang="EN"><span style="font-size: 11pt;">Get Started with Windows Azure Cmdlets</span></span></span></a></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><a href="http://hadoopsdk.codeplex.com/wikipage?title=PowerShell%20Cmdlets%20for%20Cluster%20Management&amp;referringTitle=Cluster%20Management"><span style="mso-bookmark: _toc260989127;"><span style="mso-ansi-language: en-gb;"><span style="font-size: 11pt;">Microsoft .Net SDK for Hadoop - PowerShell Cmdlets for Cluster Management</span></span></span></a></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><a href="http://msdn.microsoft.com/en-us/library/windowsazure/gg551721.aspx"><span style="mso-bookmark: _toc260989127;"><span style="mso-ansi-language: en;" lang="EN"><span style="font-size: 11pt;">How to: Manage Management Certificates in Windows Azure</span></span></span></a></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><a href="http://www.windowsazure.com/en-us/downloads/?fb=en-us"><span style="mso-bookmark: _toc260989127;"><span style="mso-ansi-language: en;" lang="EN"><span style="font-size: 11pt;">Windows Azure Downloads</span></span></span></a></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><a href="https://hadoopsdk.codeplex.com/downloads/get/671067"><span style="mso-bookmark: _toc260989127;"><span style="mso-ansi-language: en;" lang="EN"><span style="font-size: 11pt;">HDInsight cmdlets</span></span></span></a></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><a href="http://blogs.msdn.com/b/windowsazure/archive/2013/02/07/windows-azure-sql-database-management-with-powershell.aspx"><span style="mso-bookmark: _toc260989127;"><span style="mso-ansi-language: en;" lang="EN"><span style="font-size: 11pt;">Windows Azure SQL Database Management with PowerShell</span></span></span></a></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><a href="http://hadoopsdk.codeplex.com/wikipage?title=Map%2fReduce"><span style="mso-bookmark: _toc260989127;"><span style="mso-ansi-language: en;" lang="EN"><span style="font-size: 11pt;">Microsoft .Net SDK for Hadoop - Map/Reduce</span></span></span></a></p>
<h1 style="margin: 24pt 0cm 18pt; line-height: normal; list-style-type: disc;"><span style="mso-bookmark: _toc260989127;"><a name="_Toc373503073"></a><span lang="EN-US"><span style="font-size: 18pt;">Conclusion</span></span></span></h1>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><span style="font-size: 11pt;">This document hopefully explains how best to manage an HDInsight cluster and submit .Net MapReduce jobs using a management console and Windows PowerShell. The ultimate goal was to provide an approach one could use for managing Elastic Job Submissions where the cluster only exists for the lifetime of the job.</span></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm; line-height: 13pt; list-style-type: disc;"><span lang="EN-US"><span style="font-size: 11pt;">This Elastic approach to job submissions is possible when using Windows Azure Storage Blobs for data retention and Windows Azure SQL Database for job history. Configuring the cluster such that there is a separate of compute and storage processes allows for the cluster to exist when and only when it is needed for computational processing.</span></span></p>]]></content:encoded>
    </item>
    <item>
      <title>Implementing LOB Storage in Memory Optimized Tables</title>
      <link>https://docs.microsoft.com/archive/blogs/carlnol/implementing-lob-storage-in-memory-optimized-tables</link>
      <pubDate>Mon, 16 Sep 2013 10:08:55 GMT</pubDate>
      <dc:creator><![CDATA[Carl Nolan]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.cloudplatform.qa.msdn.wdslab.com/carlnol/2013/09/16/implementing-lob-storage-in-memory-optimized-tables/</guid>
      <description><![CDATA[Memory optimized tables do not have off-row or large object (LOB) storage, and the row size is...]]></description>
      <content:encoded><![CDATA[<p><font face="Calibri"><font style="font-size: 11pt;" color="#000000">Memory optimized tables do not have off-row or large object (LOB) storage, and the row size is limited to 8060 bytes. Thus, storing large binary or character string values can be done in one of two ways:</font></font></p>  <p class="MsoListParagraphCxSpFirst" style="margin: 0cm 0cm 0pt 54pt; line-height: 12pt; text-indent: -36pt; mso-add-space: auto; mso-list: l0 level1 lfo2;"><font color="#000000"><span style="mso-ascii-font-family: calibri; mso-fareast-font-family: calibri; mso-hansi-font-family: calibri; mso-bidi-font-family: calibri;"><span style="mso-list: ignore;"><font face="Calibri"><font style="font-size: 11pt;">•</font></font><span style="line-height: normal; font-family:;"><font face="Times New Roman"><font style="font-size: 7pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span></span></span><font face="Calibri"><font style="font-size: 11pt;">Split the LOB values into multiple rows</font></font></font></p>  <p class="MsoListParagraphCxSpLast" style="margin: 0cm 0cm 8pt 54pt; line-height: 12pt; text-indent: -36pt; mso-add-space: auto; mso-list: l0 level1 lfo2;"><font color="#000000"><span style="mso-ascii-font-family: calibri; mso-fareast-font-family: calibri; mso-hansi-font-family: calibri; mso-bidi-font-family: calibri;"><span style="mso-list: ignore;"><font face="Calibri"><font style="font-size: 11pt;">•</font></font><span style="line-height: normal; font-family:;"><font face="Times New Roman"><font style="font-size: 7pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span></span></span><font face="Calibri"><font style="font-size: 11pt;">Store the LOB values in a regular non-memory optimized (file based) table</font></font></font></p>  <p class="MsoNormal" style="margin: 0cm 0cm 8pt; line-height: 12pt;"><font face="Calibri"><font style="font-size: 11pt;" color="#000000">This document will provide a mechanism for implementing the split of LOB values into multiple rows, and outline several mechanisms for retrieving the data.</font></font></p>  <p class="MsoNormal" style="margin: 0cm 0cm 8pt; line-height: 12pt;"><font face="Calibri"><font style="font-size: 11pt;" color="#000000">Associated with this post is a set of scripts covering all the TSQL code presented, along with some simple testing scripts, which can be downloaded from <a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/76/1070.HekatonBinaryMgmt.zip" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-76/1070.HekatonBinaryMgmt.zip" target="_blank">here</a>.</font></font></p>  <h2 style="margin: 2pt 0cm 0pt; text-transform: none; line-height: 14pt;"><font face="Calibri Light"><font style="font-size: 13pt;" color="#2e74b5">Approach Overview</font></font></h2>  <p class="MsoNormal" style="margin: 0cm 0cm 8pt; line-height: 12pt;"><font face="Calibri"><font style="font-size: 11pt;" color="#000000">The approach to be taken will allow for the processing of LOB data such that when the data length is greater than a predefined upper limit, segment size, it is broken down into smaller chunks. </font></font></p>  <p class="MsoNormal" style="margin: 0cm 0cm 8pt; line-height: 12pt;"><font face="Calibri"><font style="font-size: 11pt;" color="#000000">If the data fits into a single segment then the process will default to operating on just a primary table with a single row for the key identifier. In this instance reading the data means just returning the data solely from the primary table.</font></font></p>  <p class="MsoNormal" style="margin: 0cm 0cm 8pt; line-height: 12pt;"><font face="Calibri"><font style="font-size: 11pt;" color="#000000">If the data is too large for a single segment then it will be broken down into multiple segments and each part saved as a row with an associated part number; linked to the primary element via the key identifier. In this instance, reading the data then becomes a determination of how many segments the data is stored in, and either concatenating them into a single field or return the individual parts through a SELECT statement.</font></font></p>  <p class="MsoNormal" style="margin: 0cm 0cm 8pt; line-height: 12pt;"><font face="Calibri"><font style="font-size: 11pt;" color="#000000">The procedures to support this approach would be as follows:</font></font></p>  <p class="MsoListParagraphCxSpFirst" style="margin: 0cm 0cm 0pt 36pt; line-height: 12pt; text-indent: -18pt; mso-list: l2 level1 lfo3;"><font color="#000000"><span style="font-family:; mso-fareast-font-family: symbol; mso-bidi-font-family: symbol;"><span style="mso-list: ignore;"><font face="Symbol"><font style="font-size: 11pt;">·</font></font><span style="line-height: normal; font-family:;"><font face="Times New Roman"><font style="font-size: 7pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span></span></span><font face="Calibri"><font style="font-size: 11pt;">Insert a LOB column when the data length is less than a predefined segment size</font></font></font></p>  <p class="MsoListParagraphCxSpMiddle" style="margin: 0cm 0cm 0pt 36pt; line-height: 12pt; text-indent: -18pt; mso-list: l2 level1 lfo3;"><font color="#000000"><span style="font-family:; mso-fareast-font-family: symbol; mso-bidi-font-family: symbol;"><span style="mso-list: ignore;"><font face="Symbol"><font style="font-size: 11pt;">·</font></font><span style="line-height: normal; font-family:;"><font face="Times New Roman"><font style="font-size: 7pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span></span></span><font face="Calibri"><font style="font-size: 11pt;">Insert a LOB column when the data length is greater than the predefined segment size and a (max) column definition is needed</font></font></font></p>  <p class="MsoListParagraphCxSpMiddle" style="margin: 0cm 0cm 0pt 36pt; line-height: 12pt; text-indent: -18pt; mso-list: l2 level1 lfo3;"><font color="#000000"><span style="font-family:; mso-fareast-font-family: symbol; mso-bidi-font-family: symbol;"><span style="mso-list: ignore;"><font face="Symbol"><font style="font-size: 11pt;">·</font></font><span style="line-height: normal; font-family:;"><font face="Times New Roman"><font style="font-size: 7pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span></span></span><font face="Calibri"><font style="font-size: 11pt;">Provide Upsert operations that allow an entry to be made after an insert has previously been made; or to insert data if the current state is not known</font></font></font></p>  <p class="MsoListParagraphCxSpMiddle" style="margin: 0cm 0cm 0pt 36pt; line-height: 12pt; text-indent: -18pt; mso-list: l2 level1 lfo3;"><font color="#000000"><span style="font-family:; mso-fareast-font-family: symbol; mso-bidi-font-family: symbol;"><span style="mso-list: ignore;"><font face="Symbol"><font style="font-size: 11pt;">·</font></font><span style="line-height: normal; font-family:;"><font face="Times New Roman"><font style="font-size: 7pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span></span></span><font face="Calibri"><font style="font-size: 11pt;">Provide a SELECT process that returns the collection of LOB segments; taking into consideration the fact the data may not be segmented</font></font></font></p>  <p class="MsoListParagraphCxSpMiddle" style="margin: 0cm 0cm 0pt 36pt; line-height: 12pt; text-indent: -18pt; mso-list: l2 level1 lfo3;"><font color="#000000"><span style="font-family:; mso-fareast-font-family: symbol; mso-bidi-font-family: symbol;"><span style="mso-list: ignore;"><font face="Symbol"><font style="font-size: 11pt;">·</font></font><span style="line-height: normal; font-family:;"><font face="Times New Roman"><font style="font-size: 7pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span></span></span><font face="Calibri"><font style="font-size: 11pt;">Read a LOB column when the size is less than a predefined segment size</font></font></font></p>  <p class="MsoListParagraphCxSpLast" style="margin: 0cm 0cm 8pt 36pt; line-height: 12pt; text-indent: -18pt; mso-list: l2 level1 lfo3;"><font color="#000000"><span style="font-family:; mso-fareast-font-family: symbol; mso-bidi-font-family: symbol;"><span style="mso-list: ignore;"><font face="Symbol"><font style="font-size: 11pt;">·</font></font><span style="line-height: normal; font-family:;"><font face="Times New Roman"><font style="font-size: 7pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span></span></span><font face="Calibri"><font style="font-size: 11pt;">Read a LOB column when the size is greater than the predefined segment size, returning the data in a (max) datatype definition</font></font></font></p>  <p class="MsoNormal" style="margin: 0cm 0cm 8pt; line-height: 12pt;"><font face="Calibri"><font style="font-size: 11pt;" color="#000000">When implementing your own solution consideration should be given to the predefined segment size. The goal should be such that this size is as small as possible but big enough that a large percentage of the applications data can possibly fit into a single segment. For small transient data, such as session data and small encrypted data elements, adjusting this value will impact the overall performance.</font></font></p>  <p class="MsoNormal" style="margin: 0cm 0cm 8pt; line-height: 12pt;"><font face="Calibri"><font style="font-size: 11pt;" color="#000000">The other added advantage of having the data fit into a single segment is that the data handling Stored Procedures can all be natively compiled. In this case the application would need to know the size of the data it is processing, which may or may not be possible.</font></font></p>  <p class="MsoNormal" style="margin: 0cm 0cm 8pt; line-height: 12pt;"><font face="Calibri"><font style="font-size: 11pt;" color="#000000">Of course, if you are saving MBs of data this optimization will not be feasible so the size should probably be set to 8000 bytes.</font></font></p>  <p class="MsoNormal" style="margin: 0cm 0cm 8pt; line-height: 12pt;"><font face="Calibri"><font style="font-size: 11pt;" color="#000000">However, the primary approach outlined may not be suitable for storing very large quantities of LOB data which are accessed infrequently; say document or images. In this instance a standard file based table approach may be more suitable. However for highly transient data such as session data, encrypted authentication codes, etc. using this approach can dramatically speed up access.</font></font></p>  <h2 style="margin: 2pt 0cm 0pt; text-transform: none; line-height: 14pt;"><font face="Calibri Light"><font style="font-size: 13pt;" color="#2e74b5">Implementation</font></font></h2>  <p class="MsoNormal" style="margin: 0cm 0cm 8pt; line-height: 12pt;"><font face="Calibri"><font style="font-size: 11pt;" color="#000000">For this implementation I have created a new database, called [MemBinaryStore]. In reality the object definitions below would be placed into your own application database:</font></font></p>  <p class="CodeBlockCxSpFirst" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span><font color="#0000ff"><font style="font-size: 9pt;">CREATE</font></font></span><font style="font-size: 9pt;"><font color="#000000"> </font><span><font color="#0000ff">DATABASE</font></span><font color="#000000"> [MemBinaryStore] </font></font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font color="#000000"><span style="mso-tab-count: 1;"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></span><font style="font-size: 9pt;">CONTAINMENT </font></font><font style="font-size: 9pt;"><span><font color="#808080">=</font></span><font color="#000000"> </font><span><font color="#0000ff">NONE</font></span></font><span style="mso-tab-count: 2;"><font style="font-size: 9pt;" color="#000000">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">ON</font></span><font color="#000000"> </font><span><font color="#0000ff">PRIMARY </font></span><span><font color="#808080">(</font></span><font color="#000000">NAME </font><span><font color="#808080">=</font></span><font color="#000000"> [MemBinaryStore_Primary]</font><span><font color="#808080">,</font></span><font color="#000000"> </font><span><font color="#0000ff">FILENAME</font></span><font color="#000000"> </font><span><font color="#808080">=</font></span><font color="#000000"> </font><span><font color="#ff0000">'C:\MSSQL\DATA\MemBinaryStore_Prim.mdf'</font></span><span><font color="#808080">,</font></span><font color="#000000"> SIZE </font><span><font color="#808080">=</font></span><font color="#000000"> 10 MB</font><span><font color="#808080">,</font></span><font color="#000000"> FILEGROWTH </font><span><font color="#808080">=</font></span><font color="#000000"> 10 MB</font></font><span><font style="font-size: 9pt;" color="#808080">),</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">FILEGROUP</font></span><font color="#000000"> [MemBinaryStore_Mod] </font><span><font color="#ff00ff">CONTAINS</font></span><font color="#000000"> MEMORY_OPTIMIZED_DATA</font><span><font color="#0000ff"> </font></span><span><font color="#808080">(</font></span><font color="#000000">NAME </font><span><font color="#808080">=</font></span><font color="#000000"> [MemBinaryStore_Mod]</font><span><font color="#808080">,</font></span><font color="#000000"> </font><span><font color="#0000ff">FILENAME</font></span><font color="#000000"> </font><span><font color="#808080">=</font></span><font color="#000000"> </font><span><font color="#ff0000">'C:\MSSQL\DATA\MemBinaryStore.dir'</font></span></font><span><font style="font-size: 9pt;" color="#808080">),</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">FILEGROUP</font></span><font color="#000000"> [MemBinaryStore_Data]</font><span><font color="#0000ff"> </font></span><span><font color="#808080">(</font></span><font color="#000000">NAME </font><span><font color="#808080">=</font></span><font color="#000000"> [MemBinaryStore_Data1]</font><span><font color="#808080">,</font></span><font color="#000000"> </font><span><font color="#0000ff">FILENAME</font></span><font color="#000000"> </font><span><font color="#808080">=</font></span><font color="#000000"> </font><span><font color="#ff0000">'C:\MSSQL\DATA\MemBinaryStore_Data1.ndf'</font></span><span><font color="#808080">,</font></span><font color="#000000"> SIZE </font><span><font color="#808080">=</font></span><font color="#000000"> 10 MB</font><span><font color="#808080">,</font></span><font color="#000000"> FILEGROWTH </font><span><font color="#808080">=</font></span><font color="#000000"> 10 MB</font></font><span><font style="font-size: 9pt;" color="#808080">),</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span><span style="mso-tab-count: 2;"><font color="#0000ff"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span></span><font style="font-size: 9pt;"><span><font color="#808080">(</font></span><font color="#000000">NAME </font><span><font color="#808080">=</font></span><font color="#000000"> [MemBinaryStore_Data2]</font><span><font color="#808080">,</font></span><font color="#000000"> </font><span><font color="#0000ff">FILENAME</font></span><font color="#000000"> </font><span><font color="#808080">=</font></span><font color="#000000"> </font><span><font color="#ff0000">'C:\MSSQL\DATA\MemBinaryStore_Data2.ndf'</font></span><span><font color="#808080">,</font></span><font color="#000000"> SIZE </font><span><font color="#808080">=</font></span><font color="#000000"> 10 MB</font><span><font color="#808080">,</font></span><font color="#000000"> FILEGROWTH </font><span><font color="#808080">=</font></span><font color="#000000"> 10 MB</font></font><span><font style="font-size: 9pt;" color="#808080">)</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#ff00ff">LOG</font></span><font color="#000000"> </font><span><font color="#0000ff">ON </font></span><span><font color="#808080">(</font></span><font color="#000000">NAME </font><span><font color="#808080">=</font></span><font color="#000000"> [MemBinaryStore_Log1]</font><span><font color="#808080">,</font></span><font color="#000000"> </font><span><font color="#0000ff">FILENAME</font></span><font color="#000000"> </font><span><font color="#808080">=</font></span><font color="#000000"> </font><span><font color="#ff0000">'C:\MSSQL\DATA\MemBinaryStore_Log1.ldf'</font></span><span><font color="#808080">,</font></span><font color="#000000"> SIZE </font><span><font color="#808080">=</font></span><font color="#000000"> 5 MB</font><span><font color="#808080">,</font></span><font color="#000000"> FILEGROWTH </font><span><font color="#808080">=</font></span><font color="#000000"> 5 MB</font></font><span><font style="font-size: 9pt;" color="#808080">)</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#ff00ff">COLLATE</font></span><font color="#000000"> Latin1_General_100_CI_AS</font></font><span><font style="font-size: 9pt;" color="#808080">;</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><span><font face="Consolas"><font style="font-size: 9pt;" color="#0000ff">GO</font></font></span></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font style="font-size: 9pt;" color="#000000">&#160;</font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span><font color="#0000ff"><font style="font-size: 9pt;">ALTER</font></font></span><font style="font-size: 9pt;"><font color="#000000"> </font><span><font color="#0000ff">DATABASE</font></span><font color="#000000"> [MemBinaryStore]</font></font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-spacerun: yes;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">MODIFY</font></span><font color="#000000"> </font><span><font color="#0000ff">FILEGROUP</font></span><font color="#000000"> [MemBinaryStore_Data] </font><span><font color="#0000ff">DEFAULT</font></span></font><span><font style="font-size: 9pt;" color="#808080">;</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><span><font face="Consolas"><font style="font-size: 9pt;" color="#0000ff">GO</font></font></span></p>  <p class="CodeBlockCxSpLast" style="margin: 0cm 0cm 6pt; line-height: normal;"><font face="Consolas"><font style="font-size: 9pt;" color="#000000">&#160;</font></font></p>  <p class="MsoNormal" style="margin: 0cm 0cm 8pt; line-height: 12pt;"><font face="Calibri"><font style="font-size: 11pt;" color="#000000">One must remember Memory Optimized tables have a restriction on the code page for (var)char columns. These columns must use code page 1252, hence the reason the collation has been specified.</font></font></p>  <h3 style="margin: 2pt 0cm 0pt; line-height: 13pt;"><font face="Calibri Light"><font style="font-size: 12pt;" color="#1f4d78"><font style="font-weight: normal;">Table Objects</font></font></font></h3>  <p class="MsoNormal" style="margin: 0cm 0cm 8pt; line-height: 12pt;"><font face="Calibri"><font style="font-size: 11pt;" color="#000000">This implementation uses two base tables. A Primary table that holds the base binary information and also all the necessary data when the LOB data can fit into a single segment. An Extended table that holds any necessary LOB data segments; multiple records existing for each primary entry when the data is split into segments.</font></font></p>  <p class="CodeBlockCxSpFirst" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span><font color="#0000ff"><font style="font-size: 9pt;">CREATE</font></font></span><font style="font-size: 9pt;"><font color="#000000"> </font><span><font color="#0000ff">TABLE</font></span><font color="#000000"> [dbo]</font><span><font color="#808080">.</font></span><font color="#000000">[BinaryItem] </font></font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><span><font face="Consolas"><font style="font-size: 9pt;" color="#808080">(</font></font></span></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font color="#000000"><span style="mso-tab-count: 1;"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></span><font style="font-size: 9pt;">[BinaryItemId]<span style="mso-tab-count: 3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></font></font><font style="font-size: 9pt;"><span><font color="#0000ff">binary</font></span><span><font color="#808080">(</font></span><font color="#000000">16</font><span><font color="#808080">)</font></span><span style="mso-tab-count: 2;"><font color="#000000">&#160;&#160;&#160;&#160;&#160; </font></span><span><font color="#808080">NOT</font></span><font color="#000000"> </font><span><font color="#808080">NULL</font></span></font><span style="mso-tab-count: 2;"><font style="font-size: 9pt;" color="#000000">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font color="#000000"><span style="mso-tab-count: 1;"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></span><font style="font-size: 9pt;"><span style="mso-tab-count: 1;">&#160;&#160;&#160;&#160; </span></font></font><font style="font-size: 9pt;"><span><font color="#0000ff">CONSTRAINT</font></span><font color="#000000"> [PK_BinaryItem] </font><span><font color="#0000ff">PRIMARY</font></span><font color="#000000"> </font><span><font color="#0000ff">KEY</font></span><font color="#000000"> </font><span><font color="#0000ff">NONCLUSTERED</font></span><font color="#000000"> </font></font><span><font style="font-size: 9pt;" color="#0000ff">HASH</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span><font color="#0000ff"><span style="mso-tab-count: 2;"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font style="font-size: 9pt;">WITH </font></font></span><font style="font-size: 9pt;"><span><font color="#808080">(</font></span><font color="#000000">BUCKET_COUNT </font><span><font color="#808080">=</font></span><font color="#000000"> 1000000</font></font><span><font style="font-size: 9pt;" color="#808080">),</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font color="#000000"><span style="mso-tab-count: 1;"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></span><font style="font-size: 9pt;">[ItemCreated]<span style="mso-tab-count: 3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></font></font><font style="font-size: 9pt;"><span><font color="#0000ff">datetime</font></span><span style="mso-tab-count: 2;"><font color="#000000">&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><span><font color="#808080">NOT</font></span><font color="#000000"> </font></font><span><font style="font-size: 9pt;" color="#808080">NULL,</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font color="#000000"><span style="mso-tab-count: 1;"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></span><font style="font-size: 9pt;">[ItemAccessed]<span style="mso-tab-count: 3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></font></font><font style="font-size: 9pt;"><span><font color="#0000ff">datetime</font></span><span style="mso-tab-count: 2;"><font color="#000000">&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><span><font color="#808080">NOT</font></span><font color="#000000"> </font></font><span><font style="font-size: 9pt;" color="#808080">NULL,</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font color="#000000"><span style="mso-tab-count: 1;"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></span><font style="font-size: 9pt;">[BinaryData]<span style="mso-tab-count: 3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></font></font><font style="font-size: 9pt;"><span><font color="#0000ff">varbinary</font></span><span><font color="#808080">(</font></span><font color="#000000">6000</font><span><font color="#808080">)</font></span><span style="mso-tab-count: 1;"><font color="#000000"> </font></span><span><font color="#808080">NULL,</font></span><font color="#000000"> </font></font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font color="#000000"><span style="mso-tab-count: 1;"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></span><font style="font-size: 9pt;">[BinaryExParts]<span style="mso-tab-count: 2;">&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="mso-tab-count: 1;">&#160;&#160;&#160;&#160; </span></font></font><font style="font-size: 9pt;"><span><font color="#0000ff">int</font></span><span style="mso-tab-count: 3;"><font color="#000000">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><span><font color="#808080">NOT</font></span><font color="#000000"> </font></font><span><font style="font-size: 9pt;" color="#808080">NULL</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><span><font face="Consolas"><font style="font-size: 9pt;" color="#808080">)</font></font></span></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span><font color="#0000ff"><font style="font-size: 9pt;">WITH </font></font></span><font style="font-size: 9pt;"><span><font color="#808080">(</font></span><font color="#000000">MEMORY_OPTIMIZED </font><span><font color="#808080">=</font></span><font color="#000000"> </font><span><font color="#0000ff">ON</font></span><span><font color="#808080">,</font></span><font color="#000000"> DURABILITY </font><span><font color="#808080">=</font></span><font color="#000000"> SCHEMA_AND_DATA</font></font><span><font style="font-size: 9pt;" color="#808080">);</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><span><font face="Consolas"><font style="font-size: 9pt;" color="#0000ff">GO</font></font></span></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font style="font-size: 9pt;" color="#000000">&#160;</font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span><font color="#0000ff"><font style="font-size: 9pt;">CREATE</font></font></span><font style="font-size: 9pt;"><font color="#000000"> </font><span><font color="#0000ff">TABLE</font></span><font color="#000000"> [dbo]</font><span><font color="#808080">.</font></span><font color="#000000">[BinaryItemEx] </font></font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><span><font face="Consolas"><font style="font-size: 9pt;" color="#808080">(</font></font></span></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font color="#000000"><span style="mso-tab-count: 1;"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></span><font style="font-size: 9pt;">[BinaryItemId]<span style="mso-tab-count: 3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></font></font><font style="font-size: 9pt;"><span><font color="#0000ff">binary</font></span><span><font color="#808080">(</font></span><font color="#000000">16</font><span><font color="#808080">)</font></span><span style="mso-tab-count: 2;"><font color="#000000">&#160;&#160;&#160;&#160;&#160; </font></span><span><font color="#808080">NOT</font></span><font color="#000000"> </font></font><span><font style="font-size: 9pt;" color="#808080">NULL</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 2;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">INDEX</font></span><font color="#000000"> IDX_BINARYITEMEX_ID </font></font><span><font style="font-size: 9pt;" color="#0000ff">HASH</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span><font color="#0000ff"><span style="mso-tab-count: 2;"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font style="font-size: 9pt;">WITH </font></font></span><font style="font-size: 9pt;"><span><font color="#808080">(</font></span><font color="#000000">BUCKET_COUNT </font><span><font color="#808080">=</font></span><font color="#000000"> 1000000</font></font><span><font style="font-size: 9pt;" color="#808080">),</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font color="#000000"><span style="mso-tab-count: 1;"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></span><font style="font-size: 9pt;">[BinaryPart]<span style="mso-tab-count: 3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></font></font><font style="font-size: 9pt;"><span><font color="#0000ff">int</font></span><span style="mso-tab-count: 3;"><font color="#000000">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><span><font color="#808080">NOT</font></span><font color="#000000"> </font></font><span><font style="font-size: 9pt;" color="#808080">NULL,</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font color="#000000"><span style="mso-tab-count: 1;"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></span><font style="font-size: 9pt;">[BinaryDataEx]<span style="mso-tab-count: 3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></font></font><font style="font-size: 9pt;"><span><font color="#0000ff">varbinary</font></span><span><font color="#808080">(</font></span><font color="#000000">6000</font><span><font color="#808080">)</font></span><span style="mso-tab-count: 1;"><font color="#000000"> </font></span><span><font color="#808080">NOT</font></span><font color="#000000"> </font></font><span><font style="font-size: 9pt;" color="#808080">NULL,</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font color="#000000"><span style="mso-tab-count: 1;"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></span><font style="font-size: 9pt;"><span style="mso-tab-count: 1;">&#160;&#160;&#160;&#160; </span></font></font><font style="font-size: 9pt;"><span><font color="#0000ff">CONSTRAINT</font></span><font color="#000000"> [PK_BinaryItemEx] </font><span><font color="#0000ff">PRIMARY</font></span><font color="#000000"> </font><span><font color="#0000ff">KEY</font></span><font color="#000000"> </font><span><font color="#0000ff">NONCLUSTERED</font></span><font color="#000000"> </font></font><span><font style="font-size: 9pt;" color="#0000ff">HASH</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span><font color="#808080"><span style="mso-tab-count: 2;"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font style="font-size: 9pt;">(</font></font></span><font style="font-size: 9pt;"><font color="#000000">[BinaryItemId]</font><span><font color="#808080">,</font></span><font color="#000000"> [BinaryPart]</font></font><span><font style="font-size: 9pt;" color="#808080">)</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span><font color="#0000ff"><span style="mso-tab-count: 2;"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font style="font-size: 9pt;">WITH </font></font></span><font style="font-size: 9pt;"><span><font color="#808080">(</font></span><font color="#000000">BUCKET_COUNT </font><span><font color="#808080">=</font></span><font color="#000000"> 20000000</font></font><span><font style="font-size: 9pt;" color="#808080">)</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><span><font face="Consolas"><font style="font-size: 9pt;" color="#808080">)</font></font></span></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span><font color="#0000ff"><font style="font-size: 9pt;">WITH </font></font></span><font style="font-size: 9pt;"><span><font color="#808080">(</font></span><font color="#000000">MEMORY_OPTIMIZED </font><span><font color="#808080">=</font></span><font color="#000000"> </font><span><font color="#0000ff">ON</font></span><span><font color="#808080">,</font></span><font color="#000000"> DURABILITY </font><span><font color="#808080">=</font></span><font color="#000000"> SCHEMA_AND_DATA</font></font><span><font style="font-size: 9pt;" color="#808080">);</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><span><font face="Consolas"><font style="font-size: 9pt;" color="#0000ff">GO</font></font></span></p>  <p class="CodeBlockCxSpLast" style="margin: 0cm 0cm 6pt; line-height: normal;"><font face="Consolas"><font style="font-size: 9pt;" color="#000000">&#160;</font></font></p>  <p class="MsoNormal" style="margin: 0cm 0cm 8pt; line-height: 12pt;"><font face="Calibri"><font style="font-size: 11pt;" color="#000000">When defining your tables care should be taken in defining the correct bucket count. As a very rough guideline, the bucket count should be set to 1-2X the maximum expected cardinality of the table.</font></font></p>  <p class="MsoNormal" style="margin: 0cm 0cm 8pt; line-height: 12pt;"><font face="Calibri"><font style="font-size: 11pt;" color="#000000">In this instance I have chosen a key type of binary(16), rather than a varchar() or uniqueidentifier datatype. This is mostly to avoid specifying a binary collation that is needed for the primary key and index specifications; such as “nvarchar(34) collate Latin1_General_100_BIN2”.</font></font></p>  <p class="MsoNormal" style="margin: 0cm 0cm 8pt; line-height: 12pt;"><font face="Calibri"><font style="font-size: 11pt;" color="#000000">This collation requirement is necessary because indexes on (n)(var)char columns can only be specified with BIN2 collations.</font></font></p>  <h3 style="margin: 2pt 0cm 0pt; line-height: 13pt;"><font face="Calibri Light"><font style="font-size: 12pt;" color="#1f4d78"><font style="font-weight: normal;">Write Procedures</font></font></font></h3>  <p class="MsoNormal" style="margin: 0cm 0cm 8pt; line-height: 12pt;"><font face="Calibri"><font style="font-size: 11pt;" color="#000000">The process for inserting or updating LOB items is dependent on the length of the LOB data. Calling application should determine what procedure to call based on the size of the data being passed in.</font></font></p>  <p class="MsoNormal" style="margin: 0cm 0cm 8pt; line-height: 12pt;"><font face="Calibri"><font style="font-size: 11pt;" color="#000000">If the data is less than the selected segment size then it is just a simple INSERT operation, which can be performed using a natively compiled Stored Procedure:</font></font></p>  <p class="CodeBlockCxSpFirst" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span><font color="#0000ff"><font style="font-size: 9pt;">CREATE</font></font></span><font style="font-size: 9pt;"><font color="#000000"> </font><span><font color="#0000ff">PROCEDURE</font></span><font color="#000000"> [dbo]</font><span><font color="#808080">.</font></span><font color="#000000">[InsertBinaryItem]</font></font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><span><font face="Consolas"><font style="font-size: 9pt;" color="#808080">(</font></font></span></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font color="#000000"><span style="mso-spacerun: yes;"><font style="font-size: 9pt;">&#160;&#160;&#160; </font></span><font style="font-size: 9pt;">@BinaryItemId<span style="mso-tab-count: 3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></font></font><font style="font-size: 9pt;"><span><font color="#0000ff">uniqueidentifier</font></span></font><span><font style="font-size: 9pt;" color="#808080">,</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font color="#000000"><span style="mso-spacerun: yes;"><font style="font-size: 9pt;">&#160;&#160;&#160; </font></span><font style="font-size: 9pt;">@BinaryData<span style="mso-tab-count: 3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></font></font><font style="font-size: 9pt;"><span><font color="#0000ff">varbinary</font></span><span><font color="#808080">(</font></span><font color="#000000">6000</font></font><span><font style="font-size: 9pt;" color="#808080">)</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><span><font face="Consolas"><font style="font-size: 9pt;" color="#808080">)</font></font></span></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span><font color="#0000ff"><font style="font-size: 9pt;">WITH</font></font></span><font style="font-size: 9pt;"><font color="#000000"> NATIVE_COMPILATION</font><span><font color="#808080">,</font></span><font color="#000000"> </font><span><font color="#0000ff">SCHEMABINDING</font></span><span><font color="#808080">,</font></span><font color="#000000"> </font><span><font color="#0000ff">EXECUTE</font></span><font color="#000000"> </font><span><font color="#0000ff">AS</font></span><font color="#000000"> </font></font><span><font style="font-size: 9pt;" color="#0000ff">OWNER</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><span><font face="Consolas"><font style="font-size: 9pt;" color="#0000ff">AS</font></font></span></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span><font color="#0000ff"><font style="font-size: 9pt;">BEGIN</font></font></span><font style="font-size: 9pt;"><font color="#000000"> ATOMIC </font><span><font color="#0000ff">WITH</font></span><font color="#000000"> </font></font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><span><font face="Consolas"><font style="font-size: 9pt;" color="#808080">(</font></font></span></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">TRANSACTION</font></span><font color="#000000"> </font><span><font color="#0000ff">ISOLATION</font></span><font color="#000000"> </font><span><font color="#0000ff">LEVEL</font></span><font color="#000000"> </font><span><font color="#808080">=</font></span><font color="#000000"> </font><span><font color="#0000ff">SNAPSHOT</font></span></font><span><font style="font-size: 9pt;" color="#808080">,</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">LANGUAGE</font></span><font color="#000000"> </font><span><font color="#808080">=</font></span><font color="#000000"> </font></font><span><font style="font-size: 9pt;" color="#ff0000">N'English'</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><span><font face="Consolas"><font style="font-size: 9pt;" color="#808080">)</font></font></span></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-spacerun: yes;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span style="mso-tab-count: 1;"></span><span><font color="#0000ff">DECLARE</font></span><font color="#000000"> @now </font><span><font color="#0000ff">datetime</font></span><font color="#000000"> </font><span><font color="#808080">=</font></span><font color="#000000"> </font><span><font color="#ff00ff">GETUTCDATE</font></span></font><span><font style="font-size: 9pt;" color="#808080">();</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">DECLARE</font></span><font color="#000000"> @binaryId </font><span><font color="#0000ff">binary</font></span><span><font color="#808080">(</font></span><font color="#000000">16</font><span><font color="#808080">)</font></span><font color="#000000"> </font><span><font color="#808080">=</font></span><font color="#000000"> </font><span><font color="#ff00ff">CAST</font></span><span><font color="#808080">(</font></span><font color="#000000">@BinaryItemId </font><span><font color="#0000ff">AS</font></span><font color="#000000"> </font><span><font color="#0000ff">binary</font></span><span><font color="#808080">(</font></span><font color="#000000">16</font></font><span><font style="font-size: 9pt;" color="#808080">));</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><span style="mso-spacerun: yes;"><font face="Consolas"><font style="font-size: 9pt;" color="#000000">&#160;&#160;&#160; </font></font></span></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">INSERT</font></span><font color="#000000"> </font><span><font color="#0000ff">INTO</font></span><font color="#000000"> [dbo]</font><span><font color="#808080">.</font></span><font color="#000000">[BinaryItem]</font></font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#808080">(</font></span><font color="#000000">[BinaryItemId]</font><span><font color="#808080">,</font></span><font color="#000000"> [ItemCreated]</font><span><font color="#808080">,</font></span><font color="#000000"> [ItemAccessed]</font><span><font color="#808080">,</font></span><font color="#000000"> [BinaryData]</font><span><font color="#808080">,</font></span><font color="#000000"> [BinaryExParts]</font></font><span><font style="font-size: 9pt;" color="#808080">)</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">VALUES</font></span><span><font color="#808080">(</font></span><font color="#000000">@binaryId</font><span><font color="#808080">,</font></span><font color="#000000"> @now</font><span><font color="#808080">,</font></span><font color="#000000"> @now</font><span><font color="#808080">,</font></span><font color="#000000"> @BinaryData</font><span><font color="#808080">,</font></span><font color="#000000"> 0</font></font><span><font style="font-size: 9pt;" color="#808080">);</font></span></font></p>  <p class="CodeBlockCxSpLast" style="margin: 0cm 0cm 6pt; line-height: normal;"><span><font face="Consolas"><font style="font-size: 9pt;" color="#0000ff">END</font></font></span></p>  <p class="MsoNormal" style="margin: 0cm 0cm 8pt; line-height: 12pt;"><font face="Calibri"><font style="font-size: 11pt;" color="#000000">You can of course define the primary key to be whatever datatype is applicable for your application.</font></font></p>  <p class="MsoNormal" style="margin: 0cm 0cm 8pt; line-height: 12pt;"><font face="Calibri"><font style="font-size: 11pt;" color="#000000">In the instance that the data length is larger than the defined segment size you have to pass in the data using a (max) datatype definition. In this case the procedure will have to chunk the data down and INSERT it into the extensions table:</font></font></p>  <p class="CodeBlockCxSpFirst" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span><font color="#0000ff"><font style="font-size: 9pt;">CREATE</font></font></span><font style="font-size: 9pt;"><font color="#000000"> </font><span><font color="#0000ff">PROCEDURE</font></span><font color="#000000"> [dbo]</font><span><font color="#808080">.</font></span><font color="#000000">[InsertBinaryItemEx]</font></font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><span><font face="Consolas"><font style="font-size: 9pt;" color="#808080">(</font></font></span></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font color="#000000"><span style="mso-tab-count: 1;"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></span><font style="font-size: 9pt;">@BinaryItemId<span style="mso-tab-count: 2;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></font></font><font style="font-size: 9pt;"><span><font color="#0000ff">uniqueidentifier</font></span></font><span><font style="font-size: 9pt;" color="#808080">,</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font color="#000000"><span style="mso-tab-count: 1;"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></span><font style="font-size: 9pt;">@BinaryData<span style="mso-tab-count: 3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></font></font><font style="font-size: 9pt;"><span><font color="#0000ff">varbinary</font></span><span><font color="#808080">(</font></span><span><font color="#ff00ff">max</font></span></font><span><font style="font-size: 9pt;" color="#808080">)</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><span><font face="Consolas"><font style="font-size: 9pt;" color="#808080">)</font></font></span></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><span><font face="Consolas"><font style="font-size: 9pt;" color="#0000ff">AS</font></font></span></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><span><font face="Consolas"><font style="font-size: 9pt;" color="#0000ff">BEGIN</font></font></span></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-spacerun: yes;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span style="mso-tab-count: 1;"></span><span><font color="#0000ff">DECLARE</font></span><font color="#000000"> @now </font><span><font color="#0000ff">datetime</font></span><font color="#000000"> </font><span><font color="#808080">=</font></span><font color="#000000"> </font><span><font color="#ff00ff">GETUTCDATE</font></span></font><span><font style="font-size: 9pt;" color="#808080">();</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">DECLARE</font></span><font color="#000000"> @binaryId </font><span><font color="#0000ff">binary</font></span><span><font color="#808080">(</font></span><font color="#000000">16</font><span><font color="#808080">)</font></span><font color="#000000"> </font><span><font color="#808080">=</font></span><font color="#000000"> </font><span><font color="#ff00ff">CAST</font></span><span><font color="#808080">(</font></span><font color="#000000">@BinaryItemId </font><span><font color="#0000ff">AS</font></span><font color="#000000"> </font><span><font color="#0000ff">binary</font></span><span><font color="#808080">(</font></span><font color="#000000">16</font></font><span><font style="font-size: 9pt;" color="#808080">));</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font style="font-size: 9pt;" color="#000000">&#160;</font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">DECLARE</font></span><font color="#000000"> @maxSegmentLength </font><span><font color="#0000ff">int</font></span><font color="#000000"> </font><span><font color="#808080">=</font></span><font color="#000000"> 6000</font></font><span><font style="font-size: 9pt;" color="#808080">;</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-spacerun: yes;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span style="mso-tab-count: 1;"></span><span><font color="#0000ff">DECLARE</font></span><font color="#000000"> @binaryDataLength </font><span><font color="#0000ff">int</font></span><font color="#000000"> </font><span><font color="#808080">=</font></span><font color="#000000"> </font><span><font color="#ff00ff">DATALENGTH</font></span><span><font color="#808080">(</font></span><font color="#000000">@BinaryData</font></font><span><font style="font-size: 9pt;" color="#808080">);</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">DECLARE</font></span><font color="#000000"> @maxSegment </font><span><font color="#0000ff">int</font></span><font color="#000000"> </font><span><font color="#808080">=</font></span><font color="#000000"> </font><span><font color="#ff00ff">CEILING</font></span><span><font color="#808080">(</font></span><font color="#000000">@binaryDataLength </font><span><font color="#808080">/</font></span><font color="#000000"> 1.0 </font><span><font color="#808080">/</font></span><font color="#000000"> @maxSegmentLength</font></font><span><font style="font-size: 9pt;" color="#808080">);</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">DECLARE</font></span><font color="#000000"> @segmentValue </font><span><font color="#0000ff">varbinary</font></span><span><font color="#808080">(</font></span><font color="#000000">6000</font></font><span><font style="font-size: 9pt;" color="#808080">);</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font style="font-size: 9pt;" color="#000000">&#160;</font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><span><font style="font-size: 9pt;" color="#008000">-- Insert the header primary</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">INSERT</font></span><font color="#000000"> </font><span><font color="#0000ff">INTO</font></span><font color="#000000"> [dbo]</font><span><font color="#808080">.</font></span><font color="#000000">[BinaryItem] </font><span><font color="#0000ff">WITH </font></span><span><font color="#808080">(</font></span><span><font color="#0000ff">SNAPSHOT</font></span></font><span><font style="font-size: 9pt;" color="#808080">)</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span><span style="mso-tab-count: 2;"><font color="#0000ff"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span></span><font style="font-size: 9pt;"><span><font color="#808080">(</font></span><font color="#000000">[BinaryItemId]</font><span><font color="#808080">,</font></span><font color="#000000"> [ItemCreated]</font><span><font color="#808080">,</font></span><font color="#000000"> [ItemAccessed]</font><span><font color="#808080">,</font></span><font color="#000000"> [BinaryData]</font><span><font color="#808080">,</font></span><font color="#000000"> [BinaryExParts]</font></font><span><font style="font-size: 9pt;" color="#808080">)</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 2;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">VALUES</font></span><span><font color="#808080">(</font></span><font color="#000000">@binaryId</font><span><font color="#808080">,</font></span><font color="#000000"> @now</font><span><font color="#808080">,</font></span><font color="#000000"> @now</font><span><font color="#808080">,</font></span><font color="#000000"> </font><span><font color="#808080">NULL,</font></span><font color="#000000"> @maxSegment</font></font><span><font style="font-size: 9pt;" color="#808080">);</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font style="font-size: 9pt;" color="#000000">&#160;</font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><span><font style="font-size: 9pt;" color="#008000">-- Now insert the chunked binary data</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">DECLARE</font></span><font color="#000000"> @part </font><span><font color="#0000ff">int</font></span><font color="#000000"> </font><span><font color="#808080">=</font></span><font color="#000000"> 1</font></font><span><font style="font-size: 9pt;" color="#808080">;</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">WHILE </font></span><span><font color="#808080">(</font></span><font color="#000000">@part </font><span><font color="#808080">&lt;=</font></span><font color="#000000"> @maxSegment</font></font><span><font style="font-size: 9pt;" color="#808080">)</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><span><font style="font-size: 9pt;" color="#0000ff">BEGIN</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 2;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">SET</font></span><font color="#000000"> @segmentValue </font><span><font color="#808080">=</font></span><font color="#000000"> </font><span><font color="#ff00ff">SUBSTRING</font></span><span><font color="#808080">(</font></span><font color="#000000">@BinaryData</font><span><font color="#808080">,</font></span><span><font color="#0000ff"> </font></span><span><font color="#808080">((</font></span><font color="#000000">@part </font><span><font color="#808080">-</font></span><font color="#000000">1</font><span><font color="#808080">)</font></span><font color="#000000"> </font><span><font color="#808080">*</font></span><font color="#000000"> @maxSegmentLength </font><span><font color="#808080">+</font></span><font color="#000000"> 1</font><span><font color="#808080">),</font></span><font color="#000000"> @maxSegmentLength</font></font><span><font style="font-size: 9pt;" color="#808080">);</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font style="font-size: 9pt;" color="#000000">&#160;</font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 2;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">INSERT</font></span><font color="#000000"> [dbo]</font><span><font color="#808080">.</font></span><font color="#000000">[BinaryItemEx] </font><span><font color="#0000ff">WITH </font></span><span><font color="#808080">(</font></span><span><font color="#0000ff">SNAPSHOT</font></span></font><span><font style="font-size: 9pt;" color="#808080">)</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span><span style="mso-tab-count: 3;"><font color="#0000ff"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span></span><font style="font-size: 9pt;"><span><font color="#808080">(</font></span><font color="#000000">[BinaryItemId]</font><span><font color="#808080">,</font></span><font color="#000000"> [BinaryPart]</font><span><font color="#808080">,</font></span><font color="#000000"> [BinaryDataEx]</font></font><span><font style="font-size: 9pt;" color="#808080">)</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 3;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">VALUES </font></span><span><font color="#808080">(</font></span><font color="#000000">@binaryId</font><span><font color="#808080">,</font></span><font color="#000000"> @part</font><span><font color="#808080">,</font></span><font color="#000000"> @segmentValue</font></font><span><font style="font-size: 9pt;" color="#808080">);</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font style="font-size: 9pt;" color="#000000">&#160;</font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 2;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">SET</font></span><font color="#000000"> @part </font><span><font color="#808080">+=</font></span><font color="#000000"> 1</font></font><span><font style="font-size: 9pt;" color="#808080">;</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">END</font></span></font><span style="mso-spacerun: yes;"><font style="font-size: 9pt;" color="#000000">&#160;&#160;&#160; </font></span></font></p>  <p class="CodeBlockCxSpLast" style="margin: 0cm 0cm 6pt; line-height: normal;"><span><font face="Consolas"><font style="font-size: 9pt;" color="#0000ff">END</font></font></span></p>  <p class="MsoNormal" style="margin: 0cm 0cm 8pt; line-height: 12pt;"><font face="Calibri"><font style="font-size: 11pt;" color="#000000">In this case the binary header element is still written but the binary block is NULL. Also the part count, [BinaryExParts], is defined as the number of segments the data has been split into.</font></font></p>  <p class="MsoNormal" style="margin: 0cm 0cm 8pt; line-height: 12pt;"><font face="Calibri"><font style="font-size: 11pt;" color="#000000">Breaking down the LOB column is merely a simple case of using the SUBSTRING, which can be used on binary data, to define each segment.</font></font></p>  <p class="MsoNormal" style="margin: 0cm 0cm 8pt; line-height: 12pt;"><font face="Calibri"><font style="font-size: 11pt;" color="#000000">For the INSERT/UPDATE, upsert operations, the process follows the same pattern. In the case of the data being less than the selected segment size an UPDATE operation is first performed. If no entry is found then an INSERT operation is performed:</font></font></p>  <p class="CodeBlockCxSpFirst" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span><font color="#0000ff"><font style="font-size: 9pt;">CREATE</font></font></span><font style="font-size: 9pt;"><font color="#000000"> </font><span><font color="#0000ff">PROCEDURE</font></span><font color="#000000"> [dbo]</font><span><font color="#808080">.</font></span><font color="#000000">[UpsertBinaryItem]</font></font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><span><font face="Consolas"><font style="font-size: 9pt;" color="#808080">(</font></font></span></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font color="#000000"><span style="mso-tab-count: 1;"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></span><font style="font-size: 9pt;">@BinaryItemId<span style="mso-tab-count: 2;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></font></font><font style="font-size: 9pt;"><span><font color="#0000ff">uniqueidentifier</font></span></font><span><font style="font-size: 9pt;" color="#808080">,</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font color="#000000"><span style="mso-tab-count: 1;"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></span><font style="font-size: 9pt;">@BinaryData<span style="mso-tab-count: 3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></font></font><font style="font-size: 9pt;"><span><font color="#0000ff">varbinary</font></span><span><font color="#808080">(</font></span><font color="#000000">6000</font></font><span><font style="font-size: 9pt;" color="#808080">)</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><span><font face="Consolas"><font style="font-size: 9pt;" color="#808080">)</font></font></span></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span><font color="#0000ff"><font style="font-size: 9pt;">WITH</font></font></span><font style="font-size: 9pt;"><font color="#000000"> NATIVE_COMPILATION</font><span><font color="#808080">,</font></span><font color="#000000"> </font><span><font color="#0000ff">SCHEMABINDING</font></span><span><font color="#808080">,</font></span><font color="#000000"> </font><span><font color="#0000ff">EXECUTE</font></span><font color="#000000"> </font><span><font color="#0000ff">AS</font></span><font color="#000000"> </font></font><span><font style="font-size: 9pt;" color="#0000ff">OWNER</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><span><font face="Consolas"><font style="font-size: 9pt;" color="#0000ff">AS</font></font></span></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span><font color="#0000ff"><font style="font-size: 9pt;">BEGIN</font></font></span><font style="font-size: 9pt;"><font color="#000000"> ATOMIC </font><span><font color="#0000ff">WITH</font></span><font color="#000000"> </font></font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><span><font face="Consolas"><font style="font-size: 9pt;" color="#808080">(</font></font></span></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">TRANSACTION</font></span><font color="#000000"> </font><span><font color="#0000ff">ISOLATION</font></span><font color="#000000"> </font><span><font color="#0000ff">LEVEL</font></span><font color="#000000"> </font><span><font color="#808080">=</font></span><font color="#000000"> </font><span><font color="#0000ff">SNAPSHOT</font></span></font><span><font style="font-size: 9pt;" color="#808080">,</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">LANGUAGE</font></span><font color="#000000"> </font><span><font color="#808080">=</font></span><font color="#000000"> </font></font><span><font style="font-size: 9pt;" color="#ff0000">N'English'</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><span><font face="Consolas"><font style="font-size: 9pt;" color="#808080">)</font></font></span></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">DECLARE</font></span><font color="#000000"> @now </font><span><font color="#0000ff">datetime</font></span><font color="#000000"> </font><span><font color="#808080">=</font></span><font color="#000000"> </font><span><font color="#ff00ff">GETUTCDATE</font></span></font><span><font style="font-size: 9pt;" color="#808080">();</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">DECLARE</font></span><font color="#000000"> @binaryId </font><span><font color="#0000ff">binary</font></span><span><font color="#808080">(</font></span><font color="#000000">16</font><span><font color="#808080">)</font></span><font color="#000000"> </font><span><font color="#808080">=</font></span><font color="#000000"> </font><span><font color="#ff00ff">CAST</font></span><span><font color="#808080">(</font></span><font color="#000000">@BinaryItemId </font><span><font color="#0000ff">AS</font></span><font color="#000000"> </font><span><font color="#0000ff">binary</font></span><span><font color="#808080">(</font></span><font color="#000000">16</font></font><span><font style="font-size: 9pt;" color="#808080">));</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">DECLARE</font></span><font color="#000000"> @itemIdFound </font><span><font color="#0000ff">binary</font></span><span><font color="#808080">(</font></span><font color="#000000">16</font><span><font color="#808080">)</font></span><font color="#000000"> </font><span><font color="#808080">=</font></span><font color="#000000"> </font></font><span><font style="font-size: 9pt;" color="#808080">NULL;</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><span style="mso-spacerun: yes;"><font face="Consolas"><font style="font-size: 9pt;" color="#000000">&#160;&#160;&#160; </font></font></span></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><span><font style="font-size: 9pt;" color="#008000">-- Delete any extended data</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">DELETE</font></span><font color="#000000"> </font><span><font color="#0000ff">FROM</font></span><font color="#000000"> [dbo]</font><span><font color="#808080">.</font></span><font color="#000000">[BinaryItemEx]</font></font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">WHERE</font></span><font color="#000000"> [BinaryItemId] </font><span><font color="#808080">=</font></span><font color="#000000"> @binaryId</font></font><span><font style="font-size: 9pt;" color="#808080">;</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font style="font-size: 9pt;" color="#000000">&#160;</font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><span><font style="font-size: 9pt;" color="#008000">-- First try an update as the primary may already exist</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#ff00ff">UPDATE</font></span><font color="#000000"> [dbo]</font><span><font color="#808080">.</font></span><font color="#000000">[BinaryItem]</font></font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">SET</font></span><font color="#000000"><span style="mso-tab-count: 1;">&#160; </span>@itemIdFound </font><span><font color="#808080">=</font></span><font color="#000000"> [BinaryItemId]</font></font><span><font style="font-size: 9pt;" color="#808080">,</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font color="#000000"><span style="mso-tab-count: 2;"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font style="font-size: 9pt;">[ItemAccessed] </font></font><font style="font-size: 9pt;"><span><font color="#808080">=</font></span><font color="#000000"> @now</font></font><span><font style="font-size: 9pt;" color="#808080">,</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font color="#000000"><span style="mso-tab-count: 2;"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font style="font-size: 9pt;">[BinaryData] </font></font><font style="font-size: 9pt;"><span><font color="#808080">=</font></span><font color="#000000"> @BinaryData</font></font><span><font style="font-size: 9pt;" color="#808080">,</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font color="#000000"><span style="mso-tab-count: 2;"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font style="font-size: 9pt;">[BinaryExParts] </font></font><font style="font-size: 9pt;"><span><font color="#808080">=</font></span><font color="#000000"> 0</font></font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">WHERE</font></span><font color="#000000"> [BinaryItemId] </font><span><font color="#808080">=</font></span><font color="#000000"> @binaryId</font></font><span><font style="font-size: 9pt;" color="#808080">;</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font style="font-size: 9pt;" color="#000000">&#160;</font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><span><font style="font-size: 9pt;" color="#008000">-- If no primary found the perform the insert</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">IF </font></span><span><font color="#808080">(</font></span><font color="#000000">@itemIdFound </font><span><font color="#808080">IS</font></span><font color="#000000"> </font></font><span><font style="font-size: 9pt;" color="#808080">NULL)</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><span><font style="font-size: 9pt;" color="#0000ff">BEGIN</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 2;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">INSERT</font></span><font color="#000000"> </font><span><font color="#0000ff">INTO</font></span><font color="#000000"> [dbo]</font><span><font color="#808080">.</font></span><font color="#000000">[BinaryItem]</font></font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 2;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#808080">(</font></span><font color="#000000">[BinaryItemId]</font><span><font color="#808080">,</font></span><font color="#000000"> [ItemCreated]</font><span><font color="#808080">,</font></span><font color="#000000"> [ItemAccessed]</font><span><font color="#808080">,</font></span><font color="#000000"> [BinaryData]</font><span><font color="#808080">,</font></span><font color="#000000"> [BinaryExParts]</font></font><span><font style="font-size: 9pt;" color="#808080">)</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 2;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">VALUES</font></span><span><font color="#808080">(</font></span><font color="#000000">@binaryId</font><span><font color="#808080">,</font></span><font color="#000000"> @now</font><span><font color="#808080">,</font></span><font color="#000000"> @now</font><span><font color="#808080">,</font></span><font color="#000000"> @BinaryData</font><span><font color="#808080">,</font></span><font color="#000000"> 0</font></font><span><font style="font-size: 9pt;" color="#808080">);</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><span><font style="font-size: 9pt;" color="#0000ff">END</font></span></font></p>  <p class="CodeBlockCxSpLast" style="margin: 0cm 0cm 6pt; line-height: normal;"><span><font face="Consolas"><font style="font-size: 9pt;" color="#0000ff">END</font></font></span></p>  <p class="MsoNormal" style="margin: 0cm 0cm 8pt; line-height: 12pt;"><font face="Calibri"><font style="font-size: 11pt;" color="#000000">In this case one has to remember to ensure that any spurious extended binary segments are first deleted before the new values are upserted.</font></font></p>  <p class="MsoNormal" style="margin: 0cm 0cm 8pt; line-height: 12pt;"><font face="Calibri"><font style="font-size: 11pt;" color="#000000">For the case of the data being larger than the selected segment size the process is very similar, except that the data is chunked as in the case of an insert operation:</font></font></p>  <p class="CodeBlockCxSpFirst" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span><font color="#0000ff"><font style="font-size: 9pt;">CREATE</font></font></span><font style="font-size: 9pt;"><font color="#000000"> </font><span><font color="#0000ff">PROCEDURE</font></span><font color="#000000"> [dbo]</font><span><font color="#808080">.</font></span><font color="#000000">[UpsertBinaryItemEx]</font></font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><span><font face="Consolas"><font style="font-size: 9pt;" color="#808080">(</font></font></span></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font color="#000000"><span style="mso-tab-count: 1;"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></span><font style="font-size: 9pt;">@BinaryItemId<span style="mso-tab-count: 2;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></font></font><font style="font-size: 9pt;"><span><font color="#0000ff">uniqueidentifier</font></span></font><span><font style="font-size: 9pt;" color="#808080">,</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font color="#000000"><span style="mso-tab-count: 1;"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></span><font style="font-size: 9pt;">@BinaryData<span style="mso-tab-count: 3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></font></font><font style="font-size: 9pt;"><span><font color="#0000ff">varbinary</font></span><span><font color="#808080">(</font></span><span><font color="#ff00ff">max</font></span></font><span><font style="font-size: 9pt;" color="#808080">)</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><span><font face="Consolas"><font style="font-size: 9pt;" color="#808080">)</font></font></span></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><span><font face="Consolas"><font style="font-size: 9pt;" color="#0000ff">AS</font></font></span></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><span><font face="Consolas"><font style="font-size: 9pt;" color="#0000ff">BEGIN</font></font></span></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">DECLARE</font></span><font color="#000000"> @now </font><span><font color="#0000ff">datetime</font></span><font color="#000000"> </font><span><font color="#808080">=</font></span><font color="#000000"> </font><span><font color="#ff00ff">GETUTCDATE</font></span></font><span><font style="font-size: 9pt;" color="#808080">();</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">DECLARE</font></span><font color="#000000"> @binaryId </font><span><font color="#0000ff">binary</font></span><span><font color="#808080">(</font></span><font color="#000000">16</font><span><font color="#808080">)</font></span><font color="#000000"> </font><span><font color="#808080">=</font></span><font color="#000000"> </font><span><font color="#ff00ff">CAST</font></span><span><font color="#808080">(</font></span><font color="#000000">@BinaryItemId </font><span><font color="#0000ff">AS</font></span><font color="#000000"> </font><span><font color="#0000ff">binary</font></span><span><font color="#808080">(</font></span><font color="#000000">16</font></font><span><font style="font-size: 9pt;" color="#808080">));</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">DECLARE</font></span><font color="#000000"> @itemIdFound </font><span><font color="#0000ff">binary</font></span><span><font color="#808080">(</font></span><font color="#000000">16</font><span><font color="#808080">)</font></span><font color="#000000"> </font><span><font color="#808080">=</font></span><font color="#000000"> </font></font><span><font style="font-size: 9pt;" color="#808080">NULL;</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font style="font-size: 9pt;" color="#000000">&#160;</font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">DECLARE</font></span><font color="#000000"> @maxSegmentLength </font><span><font color="#0000ff">int</font></span><font color="#000000"> </font><span><font color="#808080">=</font></span><font color="#000000"> 6000</font></font><span><font style="font-size: 9pt;" color="#808080">;</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">DECLARE</font></span><font color="#000000"> @binaryDataLength </font><span><font color="#0000ff">int</font></span><font color="#000000"> </font><span><font color="#808080">=</font></span><font color="#000000"> </font><span><font color="#ff00ff">DATALENGTH</font></span><span><font color="#808080">(</font></span><font color="#000000">@BinaryData</font></font><span><font style="font-size: 9pt;" color="#808080">);</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">DECLARE</font></span><font color="#000000"> @maxSegment </font><span><font color="#0000ff">int</font></span><font color="#000000"> </font><span><font color="#808080">=</font></span><font color="#000000"> </font><span><font color="#ff00ff">CEILING</font></span><span><font color="#808080">(</font></span><font color="#000000">@binaryDataLength </font><span><font color="#808080">/</font></span><font color="#000000"> 1.0 </font><span><font color="#808080">/</font></span><font color="#000000"> @maxSegmentLength</font></font><span><font style="font-size: 9pt;" color="#808080">);</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">DECLARE</font></span><font color="#000000"> @segmentValue </font><span><font color="#0000ff">varbinary</font></span><span><font color="#808080">(</font></span><font color="#000000">6000</font></font><span><font style="font-size: 9pt;" color="#808080">);</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font style="font-size: 9pt;" color="#000000">&#160;</font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">IF </font></span><span><font color="#808080">(</font></span><font color="#000000">@binaryDataLength </font><span><font color="#808080">&lt;=</font></span><font color="#000000"> 6000</font></font><span><font style="font-size: 9pt;" color="#808080">)</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><span><font style="font-size: 9pt;" color="#0000ff">BEGIN</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 2;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">SET</font></span><font color="#000000"> @segmentValue </font><span><font color="#808080">=</font></span><font color="#000000"> </font><span><font color="#ff00ff">SUBSTRING</font></span><span><font color="#808080">(</font></span><font color="#000000">@BinaryData</font><span><font color="#808080">,</font></span><font color="#000000"> 1</font><span><font color="#808080">,</font></span><font color="#000000"> @binaryDataLength</font></font><span><font style="font-size: 9pt;" color="#808080">);</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 2;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">EXEC</font></span><font color="#000000"> [dbo]</font><span><font color="#808080">.</font></span><font color="#000000">[UpsertBinaryItem]</font><span><font color="#0000ff"> </font></span><font color="#000000">@BinaryItemId</font><span><font color="#808080">,</font></span><font color="#000000"> @segmentValue</font></font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><span><font style="font-size: 9pt;" color="#0000ff">END</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><span><font style="font-size: 9pt;" color="#0000ff">ELSE</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><span><font style="font-size: 9pt;" color="#0000ff">BEGIN</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font style="font-size: 9pt;" color="#000000">&#160;</font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 2;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><span><font style="font-size: 9pt;" color="#008000">-- Delete any existing binary segments</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 2;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">DELETE</font></span><font color="#000000"> </font><span><font color="#0000ff">FROM</font></span><font color="#000000"> [dbo]</font><span><font color="#808080">.</font></span><font color="#000000">[BinaryItemEx] </font><span><font color="#0000ff">WITH </font></span><span><font color="#808080">(</font></span><span><font color="#0000ff">SNAPSHOT</font></span></font><span><font style="font-size: 9pt;" color="#808080">)</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 2;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">WHERE</font></span><font color="#000000"> [BinaryItemId] </font><span><font color="#808080">=</font></span><font color="#000000"> @binaryId</font></font><span><font style="font-size: 9pt;" color="#808080">;</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font style="font-size: 9pt;" color="#000000">&#160;</font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 2;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><span><font style="font-size: 9pt;" color="#008000">-- Update the header</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 2;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#ff00ff">UPDATE</font></span><font color="#000000"> [dbo]</font><span><font color="#808080">.</font></span><font color="#000000">[BinaryItem] </font><span><font color="#0000ff">WITH </font></span><span><font color="#808080">(</font></span><span><font color="#0000ff">SNAPSHOT</font></span></font><span><font style="font-size: 9pt;" color="#808080">)</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 2;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">SET</font></span><font color="#000000"><span style="mso-tab-count: 1;">&#160; </span>@itemIdFound </font><span><font color="#808080">=</font></span><font color="#000000"> [BinaryItemId]</font></font><span><font style="font-size: 9pt;" color="#808080">,</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font color="#000000"><span style="mso-tab-count: 3;"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font style="font-size: 9pt;">[ItemAccessed] </font></font><font style="font-size: 9pt;"><span><font color="#808080">=</font></span><font color="#000000"> @now</font></font><span><font style="font-size: 9pt;" color="#808080">,</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font color="#000000"><span style="mso-tab-count: 3;"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font style="font-size: 9pt;">[BinaryData] </font></font><font style="font-size: 9pt;"><span><font color="#808080">=</font></span><font color="#000000"> </font></font><span><font style="font-size: 9pt;" color="#808080">NULL,</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font color="#000000"><span style="mso-tab-count: 3;"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font style="font-size: 9pt;">[BinaryExParts] </font></font><font style="font-size: 9pt;"><span><font color="#808080">=</font></span><font color="#000000"> @maxSegment</font></font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 2;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">WHERE</font></span><font color="#000000"> [BinaryItemId] </font><span><font color="#808080">=</font></span><font color="#000000"> @binaryId</font></font><span><font style="font-size: 9pt;" color="#808080">;</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font style="font-size: 9pt;" color="#000000">&#160;</font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 2;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><span><font style="font-size: 9pt;" color="#008000">-- If no header found then create one</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 2;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">IF </font></span><span><font color="#808080">(</font></span><font color="#000000">@itemIdFound </font><span><font color="#808080">IS</font></span><font color="#000000"> </font></font><span><font style="font-size: 9pt;" color="#808080">NULL)</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 2;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><span><font style="font-size: 9pt;" color="#0000ff">BEGIN</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 3;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">INSERT</font></span><font color="#000000"> </font><span><font color="#0000ff">INTO</font></span><font color="#000000"> [dbo]</font><span><font color="#808080">.</font></span><font color="#000000">[BinaryItem] </font><span><font color="#0000ff">WITH </font></span><span><font color="#808080">(</font></span><span><font color="#0000ff">SNAPSHOT</font></span></font><span><font style="font-size: 9pt;" color="#808080">)</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span><span style="mso-tab-count: 4;"><font color="#0000ff"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span></span><font style="font-size: 9pt;"><span><font color="#808080">(</font></span><font color="#000000">[BinaryItemId]</font><span><font color="#808080">,</font></span><font color="#000000"> [ItemCreated]</font><span><font color="#808080">,</font></span><font color="#000000"> [ItemAccessed]</font><span><font color="#808080">,</font></span><font color="#000000"> [BinaryData]</font><span><font color="#808080">,</font></span><font color="#000000"> [BinaryExParts]</font></font><span><font style="font-size: 9pt;" color="#808080">)</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 4;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">VALUES</font></span><span><font color="#808080">(</font></span><font color="#000000">@binaryId</font><span><font color="#808080">,</font></span><font color="#000000"> @now</font><span><font color="#808080">,</font></span><font color="#000000"> @now</font><span><font color="#808080">,</font></span><font color="#000000"> </font><span><font color="#808080">NULL,</font></span><font color="#000000"> @maxSegment</font></font><span><font style="font-size: 9pt;" color="#808080">);</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 2;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><span><font style="font-size: 9pt;" color="#0000ff">END</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font style="font-size: 9pt;" color="#000000">&#160;</font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 2;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><span><font style="font-size: 9pt;" color="#008000">-- Insert the binary segments</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 2;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">DECLARE</font></span><font color="#000000"> @part </font><span><font color="#0000ff">int</font></span><font color="#000000"> </font><span><font color="#808080">=</font></span><font color="#000000"> 1</font></font><span><font style="font-size: 9pt;" color="#808080">;</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 2;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">WHILE </font></span><span><font color="#808080">(</font></span><font color="#000000">@part </font><span><font color="#808080">&lt;=</font></span><font color="#000000"> @maxSegment</font></font><span><font style="font-size: 9pt;" color="#808080">)</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 2;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><span><font style="font-size: 9pt;" color="#0000ff">BEGIN</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 3;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">SET</font></span><font color="#000000"> @segmentValue </font><span><font color="#808080">=</font></span><font color="#000000"> </font><span><font color="#ff00ff">SUBSTRING</font></span><span><font color="#808080">(</font></span><font color="#000000">@BinaryData</font><span><font color="#808080">,</font></span><span><font color="#0000ff"> </font></span><span><font color="#808080">((</font></span><font color="#000000">@part </font><span><font color="#808080">-</font></span><font color="#000000">1</font><span><font color="#808080">)</font></span><font color="#000000"> </font><span><font color="#808080">*</font></span><font color="#000000"> @maxSegmentLength </font><span><font color="#808080">+</font></span><font color="#000000"> 1</font><span><font color="#808080">),</font></span><font color="#000000"> @maxSegmentLength</font></font><span><font style="font-size: 9pt;" color="#808080">);</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font style="font-size: 9pt;" color="#000000">&#160;</font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 3;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">INSERT</font></span><font color="#000000"> [dbo]</font><span><font color="#808080">.</font></span><font color="#000000">[BinaryItemEx] </font><span><font color="#0000ff">WITH </font></span><span><font color="#808080">(</font></span><span><font color="#0000ff">SNAPSHOT</font></span></font><span><font style="font-size: 9pt;" color="#808080">)</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span><span style="mso-tab-count: 4;"><font color="#0000ff"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span></span><font style="font-size: 9pt;"><span><font color="#808080">(</font></span><font color="#000000">[BinaryItemId]</font><span><font color="#808080">,</font></span><font color="#000000"> [BinaryPart]</font><span><font color="#808080">,</font></span><font color="#000000"> [BinaryDataEx]</font></font><span><font style="font-size: 9pt;" color="#808080">)</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 4;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">VALUES </font></span><span><font color="#808080">(</font></span><font color="#000000">@binaryId</font><span><font color="#808080">,</font></span><font color="#000000"> @part</font><span><font color="#808080">,</font></span><font color="#000000"> @segmentValue</font></font><span><font style="font-size: 9pt;" color="#808080">);</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font style="font-size: 9pt;" color="#000000">&#160;</font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 3;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">SET</font></span><font color="#000000"> @part </font><span><font color="#808080">+=</font></span><font color="#000000"> 1</font></font><span><font style="font-size: 9pt;" color="#808080">;</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 2;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">END</font></span><span style="mso-spacerun: yes;"><font color="#000000">&#160;&#160;&#160; </font></span></font><span style="mso-tab-count: 4;"><font style="font-size: 9pt;" color="#000000">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><span><font style="font-size: 9pt;" color="#0000ff">END</font></span></font></p>  <p class="CodeBlockCxSpLast" style="margin: 0cm 0cm 6pt; line-height: normal;"><span><font face="Consolas"><font style="font-size: 9pt;" color="#0000ff">END</font></font></span></p>  <p class="MsoNormal" style="margin: 0cm 0cm 8pt; line-height: 12pt;"><font face="Calibri"><font color="#000000"><font style="font-size: 11pt;">As in the case of the insert operations, it is only for when the data is less than the selected segment size that the Stored Procedures can be natively compiled. As such, ensuring that a large portion of the data fits into this selected segment size will give the best performance gains.</font><span style="mso-tab-count: 1;"><font style="font-size: 11pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span></font></font></p>  <h3 style="margin: 2pt 0cm 0pt; line-height: 13pt;"><font face="Calibri Light"><font style="font-size: 12pt;" color="#1f4d78"><font style="font-weight: normal;">Access Procedures</font></font></font></h3>  <p class="MsoNormal" style="margin: 0cm 0cm 8pt; line-height: 12pt;"><font face="Calibri"><font style="font-size: 11pt;" color="#000000">The most efficient way to access the binary data is just to return binary segments, through a SELECT statement, and have the application layer combine them back into a single element. The process to do this has to look at the primary element for the specified number of elements and return the corresponding data:</font></font></p>  <p class="CodeBlockCxSpFirst" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span><font color="#0000ff"><font style="font-size: 9pt;">CREATE</font></font></span><font style="font-size: 9pt;"><font color="#000000"> </font><span><font color="#0000ff">PROCEDURE</font></span><font color="#000000"> [dbo]</font><span><font color="#808080">.</font></span><font color="#000000">[GetBinaryDataParts]</font></font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><span><font face="Consolas"><font style="font-size: 9pt;" color="#808080">(</font></font></span></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font color="#000000"><span style="mso-tab-count: 1;"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></span><font style="font-size: 9pt;">@BinaryItemId<span style="mso-tab-count: 2;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></font></font><span><font style="font-size: 9pt;" color="#0000ff">uniqueidentifier</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><span><font face="Consolas"><font style="font-size: 9pt;" color="#808080">)</font></font></span></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span><font color="#0000ff"><font style="font-size: 9pt;">WITH</font></font></span><font style="font-size: 9pt;"><font color="#000000"> NATIVE_COMPILATION</font><span><font color="#808080">,</font></span><font color="#000000"> </font><span><font color="#0000ff">SCHEMABINDING</font></span><span><font color="#808080">,</font></span><font color="#000000"> </font><span><font color="#0000ff">EXECUTE</font></span><font color="#000000"> </font><span><font color="#0000ff">AS</font></span><font color="#000000"> </font></font><span><font style="font-size: 9pt;" color="#0000ff">OWNER</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><span><font face="Consolas"><font style="font-size: 9pt;" color="#0000ff">AS</font></font></span></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span><font color="#0000ff"><font style="font-size: 9pt;">BEGIN</font></font></span><font style="font-size: 9pt;"><font color="#000000"> ATOMIC </font><span><font color="#0000ff">WITH</font></span><font color="#000000"> </font></font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><span><font face="Consolas"><font style="font-size: 9pt;" color="#808080">(</font></font></span></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">TRANSACTION</font></span><font color="#000000"> </font><span><font color="#0000ff">ISOLATION</font></span><font color="#000000"> </font><span><font color="#0000ff">LEVEL</font></span><font color="#000000"> </font><span><font color="#808080">=</font></span><font color="#000000"> </font><span><font color="#0000ff">SNAPSHOT</font></span></font><span><font style="font-size: 9pt;" color="#808080">,</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">LANGUAGE</font></span><font color="#000000"> </font><span><font color="#808080">=</font></span><font color="#000000"> </font></font><span><font style="font-size: 9pt;" color="#ff0000">N'English'</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><span><font face="Consolas"><font style="font-size: 9pt;" color="#808080">)</font></font></span></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">DECLARE</font></span><font color="#000000"> @now </font><span><font color="#0000ff">datetime</font></span><font color="#000000"> </font><span><font color="#808080">=</font></span><font color="#000000"> </font><span><font color="#ff00ff">GETUTCDATE</font></span></font><span><font style="font-size: 9pt;" color="#808080">();</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">DECLARE</font></span><font color="#000000"> @binaryId </font><span><font color="#0000ff">binary</font></span><span><font color="#808080">(</font></span><font color="#000000">16</font><span><font color="#808080">)</font></span><font color="#000000"> </font><span><font color="#808080">=</font></span><font color="#000000"> </font><span><font color="#ff00ff">CAST</font></span><span><font color="#808080">(</font></span><font color="#000000">@BinaryItemId </font><span><font color="#0000ff">AS</font></span><font color="#000000"> </font><span><font color="#0000ff">binary</font></span><span><font color="#808080">(</font></span><font color="#000000">16</font></font><span><font style="font-size: 9pt;" color="#808080">));</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">DECLARE</font></span><font color="#000000"> @binaryData </font><span><font color="#0000ff">varbinary</font></span><span><font color="#808080">(</font></span><font color="#000000">6000</font></font><span><font style="font-size: 9pt;" color="#808080">);</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">DECLARE</font></span><font color="#000000"> @binaryParts </font><span><font color="#0000ff">int</font></span><font color="#000000"> </font><span><font color="#808080">=</font></span><font color="#000000"> 0</font></font><span><font style="font-size: 9pt;" color="#808080">;</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><span style="mso-spacerun: yes;"><font face="Consolas"><font style="font-size: 9pt;" color="#000000">&#160;&#160;&#160; </font></font></span></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><span><font style="font-size: 9pt;" color="#008000">-- Get the binary data and see if parts exists</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#ff00ff">UPDATE</font></span><font color="#000000"> [dbo]</font><span><font color="#808080">.</font></span><font color="#000000">[BinaryItem]</font></font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">SET</font></span><font color="#000000"><span style="mso-tab-count: 1;">&#160; </span>[ItemAccessed] </font><span><font color="#808080">=</font></span><font color="#000000"> @now</font></font><span><font style="font-size: 9pt;" color="#808080">,</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font color="#000000"><span style="mso-tab-count: 2;"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font style="font-size: 9pt;">@binaryData </font></font><font style="font-size: 9pt;"><span><font color="#808080">=</font></span><font color="#000000"> [BinaryData]</font></font><span><font style="font-size: 9pt;" color="#808080">,</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font color="#000000"><span style="mso-tab-count: 2;"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font style="font-size: 9pt;">@binaryParts </font></font><font style="font-size: 9pt;"><span><font color="#808080">=</font></span><font color="#000000"> [BinaryExParts]</font></font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">WHERE</font></span><font color="#000000"> [BinaryItemId] </font><span><font color="#808080">=</font></span><font color="#000000"> @binaryId</font></font><span><font style="font-size: 9pt;" color="#808080">;</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font style="font-size: 9pt;" color="#000000">&#160;</font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><span><font style="font-size: 9pt;" color="#008000">-- If parts exists then return these otherwise just return the single element</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-spacerun: yes;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span style="mso-tab-count: 1;"></span><span><font color="#0000ff">IF </font></span><span><font color="#808080">(</font></span><font color="#000000">@binaryData </font><span><font color="#808080">IS</font></span><font color="#000000"> </font><span><font color="#808080">NULL</font></span><font color="#000000"> </font><span><font color="#808080">AND</font></span><font color="#000000"> @binaryParts </font><span><font color="#808080">&gt;</font></span><font color="#000000"> 0</font></font><span><font style="font-size: 9pt;" color="#808080">)</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><span><font style="font-size: 9pt;" color="#0000ff">BEGIN</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 2;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">SELECT</font></span><font color="#000000"> </font><span><font color="#ff00ff">CAST</font></span><span><font color="#808080">(</font></span><font color="#000000">[BinaryItemId] </font><span><font color="#0000ff">AS</font></span><font color="#000000"> </font><span><font color="#0000ff">uniqueidentifier</font></span><span><font color="#808080">)</font></span><font color="#000000"> </font><span><font color="#0000ff">AS</font></span><font color="#000000"> [BinaryItemId]</font><span><font color="#808080">,</font></span><font color="#000000"> [BinaryPart]</font><span><font color="#808080">,</font></span><font color="#000000"> [BinaryDataEx] </font><span><font color="#0000ff">AS</font></span><font color="#000000"> [BinaryData]</font></font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 2;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">FROM</font></span><font color="#000000"> [dbo]</font><span><font color="#808080">.</font></span><font color="#000000">[BinaryItemEx]</font></font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 2;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">WHERE</font></span><font color="#000000"> [BinaryItemId] </font><span><font color="#808080">=</font></span><font color="#000000"> @binaryId</font></font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 2;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">ORDER</font></span><font color="#000000"> </font><span><font color="#0000ff">BY</font></span><font color="#000000"> [BinaryItemId]</font><span><font color="#808080">,</font></span><font color="#000000"> [BinaryPart]</font></font><span><font style="font-size: 9pt;" color="#808080">;</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><span><font style="font-size: 9pt;" color="#0000ff">END</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><span><font style="font-size: 9pt;" color="#0000ff">ELSE</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><span><font style="font-size: 9pt;" color="#0000ff">BEGIN</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 2;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">SELECT</font></span><font color="#000000"> @BinaryItemId </font><span><font color="#0000ff">AS</font></span><font color="#000000"> [BinaryItemId]</font><span><font color="#808080">,</font></span><font color="#000000"> 0 </font><span><font color="#0000ff">AS</font></span><font color="#000000"> [BinaryPart]</font><span><font color="#808080">,</font></span><font color="#000000"> @binaryData </font><span><font color="#0000ff">AS</font></span><font color="#000000"> [BinaryData]</font></font><span><font style="font-size: 9pt;" color="#808080">;</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><span><font style="font-size: 9pt;" color="#0000ff">END</font></span></font></p>  <p class="CodeBlockCxSpLast" style="margin: 0cm 0cm 6pt; line-height: normal;"><span><font face="Consolas"><font style="font-size: 9pt;" color="#0000ff">END</font></font></span></p>  <p class="MsoNormal" style="margin: 0cm 0cm 8pt; line-height: 12pt;"><font face="Calibri"><font style="font-size: 11pt;" color="#000000">The beauty of this approach is that the called Stored Procedure can be natively compiled.</font></font></p>  <p class="MsoNormal" style="margin: 0cm 0cm 8pt; line-height: 12pt;"><font face="Calibri"><font style="font-size: 11pt;" color="#000000">Also, the application calling this Stored Procedure then has the option of better handling the necessary memory allocations for concatenating the LOB segments into a single element.</font></font></p>  <p class="MsoNormal" style="margin: 0cm 0cm 8pt; line-height: 12pt;"><font face="Calibri"><font style="font-size: 11pt;" color="#000000">The approach I have taken here is to update the primary element with the last accessed time. This would allow for elements to be removed based on a defined life expectancy. If this is not needed one could just perform a SELECT of the primary data, rather than an update.</font></font></p>  <p class="MsoNormal" style="margin: 0cm 0cm 8pt; line-height: 12pt;"><font face="Calibri"><font style="font-size: 11pt;" color="#000000">If a SELECT process is not optimal as in the case that the application requires the data to be concatenated one can use a Stored Procedure with an OUTPUT field.</font></font></p>  <p class="MsoNormal" style="margin: 0cm 0cm 8pt; line-height: 12pt;"><font face="Calibri"><font style="font-size: 11pt;" color="#000000">In the advent that the application knows that data size is less than the specified segment size there is the option of directly calling a natively compiled Stored Procedure:</font></font></p>  <p class="MsoNormal" style="margin: 0cm 0cm 0pt; line-height: normal; mso-layout-grid-align: none;"><font face="Consolas"><span style="font-family:;"><font color="#0000ff"><font style="font-size: 9.5pt;">CREATE</font></font></span><span style="font-family:;"><font style="font-size: 9.5pt;"><font color="#000000"> </font><span><font color="#0000ff">PROCEDURE</font></span><font color="#000000"> </font><span><font color="#008080">[dbo]</font></span><span><font color="#808080">.</font></span></font><span><font style="font-size: 9.5pt;" color="#008080">[GetBinaryData]</font></span></span></font></p>  <p class="CodeBlockCxSpFirst" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font style="font-size: 9pt;" color="#000000">(</font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#008080">@BinaryItemId</font></span><span style="mso-tab-count: 2;"><font color="#000000">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><span><font color="#0000ff">uniqueidentifier</font></span><font color="#000000">,</font></font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#008080">@BinaryData</font></span><span style="mso-tab-count: 3;"><font color="#000000">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><span><font color="#0000ff">varbinary</font></span><font color="#000000">(6000) </font></font><span><font style="font-size: 9pt;" color="#0000ff">OUTPUT</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font style="font-size: 9pt;" color="#000000">)</font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span><font color="#0000ff"><font style="font-size: 9pt;">WITH</font></font></span><font style="font-size: 9pt;"><font color="#000000"> </font><span><font color="#008080">NATIVE_COMPILATION</font></span><font color="#000000">, </font><span><font color="#0000ff">SCHEMABINDING</font></span><font color="#000000">, </font><span><font color="#0000ff">EXECUTE</font></span><font color="#000000"> </font><span><font color="#0000ff">AS</font></span><font color="#000000"> </font></font><span><font style="font-size: 9pt;" color="#0000ff">OWNER</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><span><font face="Consolas"><font style="font-size: 9pt;" color="#0000ff">AS</font></font></span></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span><font color="#0000ff"><font style="font-size: 9pt;">BEGIN</font></font></span><font style="font-size: 9pt;"><font color="#000000"> </font><span><font color="#008080">ATOMIC</font></span><font color="#000000"> </font><span><font color="#0000ff">WITH</font></span><font color="#000000"> </font></font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font style="font-size: 9pt;" color="#000000">(</font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">TRANSACTION</font></span><font color="#000000"> </font><span><font color="#0000ff">ISOLATION</font></span><font color="#000000"> </font><span><font color="#0000ff">LEVEL</font></span><font color="#000000"> = </font><span><font color="#0000ff">SNAPSHOT</font></span><font color="#000000">,</font></font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">LANGUAGE</font></span><font color="#000000"> = </font></font><span><font style="font-size: 9pt;" color="#ff0000">N'English'</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font style="font-size: 9pt;" color="#000000">)</font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">DECLARE</font></span><font color="#000000"> </font><span><font color="#008080">@now</font></span><font color="#000000"> </font><span><font color="#0000ff">datetime</font></span><font color="#000000"> = </font><span><font color="#ff00ff">GETUTCDATE</font></span><font color="#000000">();</font></font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">DECLARE</font></span><font color="#000000"> </font><span><font color="#008080">@binaryId</font></span><font color="#000000"> </font><span><font color="#0000ff">binary</font></span><font color="#000000">(16) = </font><span><font color="#ff00ff">CAST</font></span><font color="#000000">(</font><span><font color="#008080">@BinaryItemId</font></span><font color="#000000"> </font><span><font color="#0000ff">AS</font></span><font color="#000000"> </font><span><font color="#0000ff">binary</font></span><font color="#000000">(16));</font></font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><span style="mso-spacerun: yes;"><font face="Consolas"><font style="font-size: 9pt;" color="#000000">&#160;&#160;&#160; </font></font></span></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><span><font style="font-size: 9pt;" color="#008000">-- Get the binary data for output</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#ff00ff">UPDATE</font></span><font color="#000000"> </font><span><font color="#008080">[dbo]</font></span><font color="#000000">.</font></font><span><font style="font-size: 9pt;" color="#008080">[BinaryItem]</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">SET</font></span><span style="mso-tab-count: 1;"><font color="#000000">&#160; </font></span><span><font color="#008080">[ItemAccessed]</font></span><font color="#000000"> = </font><span><font color="#008080">@now</font></span><font color="#000000">,</font></font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 2;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#008080">@BinaryData</font></span><font color="#000000"> = </font></font><span><font style="font-size: 9pt;" color="#008080">[BinaryData]</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">WHERE</font></span><font color="#000000"> </font><span><font color="#008080">[BinaryItemId]</font></span><font color="#000000"> = </font><span><font color="#008080">@binaryId</font></span><font color="#000000">;</font></font></font></p>  <p class="CodeBlockCxSpLast" style="margin: 0cm 0cm 6pt; line-height: normal;"><span><font face="Consolas"><font style="font-size: 9pt;" color="#0000ff">END</font></font></span></p>  <p class="MsoNormal" style="margin: 0cm 0cm 8pt; line-height: 12pt;"><font face="Calibri"><font style="font-size: 11pt;" color="#000000">In the case that the data size is unknown, or it is larger than the specified segment size, the Stored Procedure has to perform the concatenation into a max datatype, and thus cannot be natively compiled:</font></font></p>  <p class="CodeBlockCxSpFirst" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span><font color="#0000ff"><font style="font-size: 9pt;">CREATE</font></font></span><font style="font-size: 9pt;"><font color="#000000"> </font><span><font color="#0000ff">PROCEDURE</font></span><font color="#000000"> [dbo]</font><span><font color="#808080">.</font></span><font color="#000000">[GetBinaryDataEx]</font></font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><span><font face="Consolas"><font style="font-size: 9pt;" color="#808080">(</font></font></span></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font color="#000000"><span style="mso-tab-count: 1;"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></span><font style="font-size: 9pt;">@BinaryItemId<span style="mso-tab-count: 2;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></font></font><font style="font-size: 9pt;"><span><font color="#0000ff">uniqueidentifier</font></span></font><span><font style="font-size: 9pt;" color="#808080">,</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font color="#000000"><span style="mso-tab-count: 1;"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></span><font style="font-size: 9pt;">@BinaryData<span style="mso-tab-count: 3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></font></font><font style="font-size: 9pt;"><span><font color="#0000ff">varbinary</font></span><span><font color="#808080">(</font></span><span><font color="#ff00ff">max</font></span><span><font color="#808080">)</font></span><font color="#000000"> </font></font><span><font style="font-size: 9pt;" color="#0000ff">OUTPUT</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><span><font face="Consolas"><font style="font-size: 9pt;" color="#808080">)</font></font></span></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><span><font face="Consolas"><font style="font-size: 9pt;" color="#0000ff">AS</font></font></span></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><span><font face="Consolas"><font style="font-size: 9pt;" color="#0000ff">BEGIN</font></font></span></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">DECLARE</font></span><font color="#000000"> @now </font><span><font color="#0000ff">datetime</font></span><font color="#000000"> </font><span><font color="#808080">=</font></span><font color="#000000"> </font><span><font color="#ff00ff">GETUTCDATE</font></span></font><span><font style="font-size: 9pt;" color="#808080">();</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">DECLARE</font></span><font color="#000000"> @binaryId </font><span><font color="#0000ff">binary</font></span><span><font color="#808080">(</font></span><font color="#000000">16</font><span><font color="#808080">)</font></span><font color="#000000"> </font><span><font color="#808080">=</font></span><font color="#000000"> </font><span><font color="#ff00ff">CAST</font></span><span><font color="#808080">(</font></span><font color="#000000">@BinaryItemId </font><span><font color="#0000ff">AS</font></span><font color="#000000"> </font><span><font color="#0000ff">binary</font></span><span><font color="#808080">(</font></span><font color="#000000">16</font></font><span><font style="font-size: 9pt;" color="#808080">));</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font style="font-size: 9pt;" color="#000000">&#160;</font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">DECLARE</font></span><font color="#000000"> @maxSegmentLength </font><span><font color="#0000ff">int</font></span><font color="#000000"> </font><span><font color="#808080">=</font></span><font color="#000000"> 6000</font></font><span><font style="font-size: 9pt;" color="#808080">;</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">DECLARE</font></span><font color="#000000"> @blobSegment </font><span><font color="#0000ff">varbinary</font></span><span><font color="#808080">(</font></span><font color="#000000">6000</font></font><span><font style="font-size: 9pt;" color="#808080">);</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">DECLARE</font></span><font color="#000000"> @binaryParts </font><span><font color="#0000ff">int</font></span></font><span><font style="font-size: 9pt;" color="#808080">;</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">DECLARE</font></span><font color="#000000"> @count </font><span><font color="#0000ff">int</font></span><font color="#000000"> </font><span><font color="#808080">=</font></span><font color="#000000"> 0</font></font><span><font style="font-size: 9pt;" color="#808080">;</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font style="font-size: 9pt;" color="#000000">&#160;</font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">SET</font></span><font color="#000000"> @BinaryData </font><span><font color="#808080">=</font></span><font color="#000000"> </font></font><span><font style="font-size: 9pt;" color="#808080">NULL;</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font style="font-size: 9pt;" color="#000000">&#160;</font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><span><font style="font-size: 9pt;" color="#008000">-- Get the binary primary and see if parts exists</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#ff00ff">UPDATE</font></span><font color="#000000"> [dbo]</font><span><font color="#808080">.</font></span><font color="#000000">[BinaryItem] </font><span><font color="#0000ff">WITH </font></span><span><font color="#808080">(</font></span><span><font color="#0000ff">SNAPSHOT</font></span></font><span><font style="font-size: 9pt;" color="#808080">)</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">SET</font></span><font color="#000000"><span style="mso-tab-count: 1;">&#160; </span>[ItemAccessed] </font><span><font color="#808080">=</font></span><font color="#000000"> @now</font></font><span><font style="font-size: 9pt;" color="#808080">,</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font color="#000000"><span style="mso-tab-count: 2;"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font style="font-size: 9pt;">@BinaryData </font></font><font style="font-size: 9pt;"><span><font color="#808080">=</font></span><font color="#000000"> [BinaryData]</font></font><span><font style="font-size: 9pt;" color="#808080">,</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font color="#000000"><span style="mso-tab-count: 2;"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font style="font-size: 9pt;">@binaryParts </font></font><font style="font-size: 9pt;"><span><font color="#808080">=</font></span><font color="#000000"> [BinaryExParts]</font></font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">WHERE</font></span><font color="#000000"> [BinaryItemId] </font><span><font color="#808080">=</font></span><font color="#000000"> @binaryId</font></font><span><font style="font-size: 9pt;" color="#808080">;</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font style="font-size: 9pt;" color="#000000">&#160;</font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><span><font style="font-size: 9pt;" color="#008000">-- If parts exist then build the binary element</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">IF </font></span><span><font color="#808080">(</font></span><font color="#000000">@BinaryData </font><span><font color="#808080">IS</font></span><font color="#000000"> </font><span><font color="#808080">NULL</font></span><font color="#000000"> </font><span><font color="#808080">AND</font></span><font color="#000000"> @binaryParts </font><span><font color="#808080">&gt;</font></span><font color="#000000"> 0</font></font><span><font style="font-size: 9pt;" color="#808080">)</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><span><font style="font-size: 9pt;" color="#0000ff">BEGIN</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 2;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">WHILE </font></span><span><font color="#808080">(</font></span><font color="#000000">@count </font><span><font color="#808080">&lt;=</font></span><font color="#000000"> @binaryParts</font></font><span><font style="font-size: 9pt;" color="#808080">)</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 2;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><span><font style="font-size: 9pt;" color="#0000ff">BEGIN</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 3;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">SELECT</font></span><font color="#000000"> @blobSegment </font><span><font color="#808080">=</font></span><font color="#000000"> [BinaryDataEx]</font></font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 3;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">FROM</font></span><font color="#000000"> [dbo]</font><span><font color="#808080">.</font></span><font color="#000000">[BinaryItemEx] </font><span><font color="#0000ff">WITH </font></span><span><font color="#808080">(</font></span><span><font color="#0000ff">SNAPSHOT</font></span></font><span><font style="font-size: 9pt;" color="#808080">)</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 3;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">WHERE</font></span><font color="#000000"> [BinaryItemId] </font><span><font color="#808080">=</font></span><font color="#000000"> @binaryId </font><span><font color="#808080">AND</font></span><font color="#000000"> [BinaryPart] </font><span><font color="#808080">=</font></span><font color="#000000"> @count</font></font><span><font style="font-size: 9pt;" color="#808080">;</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><span style="mso-tab-count: 1;"><font face="Consolas"><font style="font-size: 9pt;" color="#000000">&#160;&#160;&#160;&#160; </font></font></span></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 3;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">IF </font></span><span><font color="#808080">(</font></span><font color="#000000">@BinaryData </font><span><font color="#808080">IS</font></span><font color="#000000"> </font></font><span><font style="font-size: 9pt;" color="#808080">NULL)</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 4;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">SET</font></span><font color="#000000"> @BinaryData </font><span><font color="#808080">=</font></span><font color="#000000"> </font><span><font color="#ff00ff">CAST</font></span><span><font color="#808080">(</font></span><font color="#000000">@blobSegment </font><span><font color="#0000ff">AS</font></span><font color="#000000"> </font><span><font color="#0000ff">varbinary</font></span><span><font color="#808080">(</font></span><span><font color="#ff00ff">max</font></span></font><span><font style="font-size: 9pt;" color="#808080">));</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 3;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><span><font style="font-size: 9pt;" color="#0000ff">ELSE</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 4;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">SET</font></span><font color="#000000"> @BinaryData </font><span><font color="#808080">=</font></span><font color="#000000"> @BinaryData </font><span><font color="#808080">+</font></span><font color="#000000"> </font><span><font color="#ff00ff">CAST</font></span><span><font color="#808080">(</font></span><font color="#000000">@blobSegment </font><span><font color="#0000ff">AS</font></span><font color="#000000"> </font><span><font color="#0000ff">varbinary</font></span><span><font color="#808080">(</font></span><span><font color="#ff00ff">max</font></span></font><span><font style="font-size: 9pt;" color="#808080">));</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font style="font-size: 9pt;" color="#000000">&#160;</font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 3;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">SET</font></span><font color="#000000"> @count </font><span><font color="#808080">+=</font></span><font color="#000000"> 1</font></font><span><font style="font-size: 9pt;" color="#808080">;</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 2;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><span><font style="font-size: 9pt;" color="#0000ff">END</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><span><font style="font-size: 9pt;" color="#0000ff">END</font></span></font></p>  <p class="CodeBlockCxSpLast" style="margin: 0cm 0cm 6pt; line-height: normal;"><span><font face="Consolas"><font style="font-size: 9pt;" color="#0000ff">END</font></font></span></p>  <p class="MsoNormal" style="margin: 0cm 0cm 8pt; line-height: 12pt;"><font face="Calibri"><font style="font-size: 11pt;" color="#000000">In this case the LOB concatenation is performing simply using the plus operator. Of course this concatenation is only necessary if extended binary segments are found.</font></font></p>  <p class="MsoNormal" style="margin: 0cm 0cm 8pt; line-height: 12pt;"><font face="Calibri"><font style="font-size: 11pt;" color="#000000">From the performance perspective it is probably better to take the original SELECT approach and have the application layer handle the necessary data concatenation; as it can better handle the memory allocations necessary to perform this operation.</font></font></p>  <h2 style="margin: 2pt 0cm 0pt; text-transform: none; line-height: 14pt;"><font face="Calibri Light"><font style="font-size: 13pt;" color="#2e74b5">Alternative Considerations</font></font></h2>  <h3 style="margin: 2pt 0cm 0pt; line-height: 13pt;"><font face="Calibri Light"><font style="font-size: 12pt;" color="#1f4d78"><font style="font-weight: normal;">Error Checking and Retries</font></font></font></h3>  <p class="MsoNormal" style="margin: 0cm 0cm 8pt; line-height: 12pt;"><font face="Calibri"><font style="font-size: 11pt;" color="#000000">The Stored Procedures outlined above do not take into consideration error checking and retry logic. The approach to either can be that it is the responsibility of the calling application or of the actual Stored Procedures.</font></font></p>  <p class="MsoNormal" style="margin: 0cm 0cm 8pt; line-height: 12pt;"><font face="Calibri"><font style="font-size: 11pt;" color="#000000">The associated scripts included with this post also include a version of the Stored Procedures that perform both error checking and retry logic. As an example here is the [UpsertBinaryItem] Stored Procedure:</font></font></p>  <p class="CodeBlockCxSpFirst" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span><font color="#0000ff"><font style="font-size: 9pt;">CREATE</font></font></span><font style="font-size: 9pt;"><font color="#000000"> </font><span><font color="#0000ff">PROCEDURE</font></span><font color="#000000"> [dbo]</font><span><font color="#808080">.</font></span><font color="#000000">[UpsertBinaryItem]</font></font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><span><font face="Consolas"><font style="font-size: 9pt;" color="#808080">(</font></font></span></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font color="#000000"><span style="mso-spacerun: yes;"><font style="font-size: 9pt;">&#160;&#160;&#160; </font></span><font style="font-size: 9pt;">@BinaryItemId<span style="mso-tab-count: 2;">&#160;&#160;&#160;&#160; </span></font></font><font style="font-size: 9pt;"><span><font color="#0000ff">uniqueidentifier</font></span></font><span><font style="font-size: 9pt;" color="#808080">,</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font color="#000000"><span style="mso-spacerun: yes;"><font style="font-size: 9pt;">&#160;&#160;&#160; </font></span><font style="font-size: 9pt;">@BinaryData<span style="mso-tab-count: 2;">&#160;&#160;&#160;&#160;&#160;&#160; </span></font></font><font style="font-size: 9pt;"><span><font color="#0000ff">varbinary</font></span><span><font color="#808080">(</font></span><font color="#000000">6000</font></font><span><font style="font-size: 9pt;" color="#808080">)</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><span><font face="Consolas"><font style="font-size: 9pt;" color="#808080">)</font></font></span></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span><font color="#0000ff"><font style="font-size: 9pt;">WITH</font></font></span><font style="font-size: 9pt;"><font color="#000000"> NATIVE_COMPILATION</font><span><font color="#808080">,</font></span><font color="#000000"> </font><span><font color="#0000ff">SCHEMABINDING</font></span><span><font color="#808080">,</font></span><font color="#000000"> </font><span><font color="#0000ff">EXECUTE</font></span><font color="#000000"> </font><span><font color="#0000ff">AS</font></span><font color="#000000"> </font></font><span><font style="font-size: 9pt;" color="#0000ff">OWNER</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><span><font face="Consolas"><font style="font-size: 9pt;" color="#0000ff">AS</font></font></span></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span><font color="#0000ff"><font style="font-size: 9pt;">BEGIN</font></font></span><font style="font-size: 9pt;"><font color="#000000"> ATOMIC </font><span><font color="#0000ff">WITH</font></span><font color="#000000"> </font></font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><span><font face="Consolas"><font style="font-size: 9pt;" color="#808080">(</font></font></span></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">TRANSACTION</font></span><font color="#000000"> </font><span><font color="#0000ff">ISOLATION</font></span><font color="#000000"> </font><span><font color="#0000ff">LEVEL</font></span><font color="#000000"> </font><span><font color="#808080">=</font></span><font color="#000000"> </font><span><font color="#0000ff">SNAPSHOT</font></span></font><span><font style="font-size: 9pt;" color="#808080">,</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">LANGUAGE</font></span><font color="#000000"> </font><span><font color="#808080">=</font></span><font color="#000000"> </font></font><span><font style="font-size: 9pt;" color="#ff0000">N'English'</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><span><font face="Consolas"><font style="font-size: 9pt;" color="#808080">)</font></font></span></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">DECLARE</font></span><font color="#000000"> @retry </font><span><font color="#0000ff">int</font></span><font color="#000000"> </font><span><font color="#808080">=</font></span><font color="#000000"> 3</font></font><span><font style="font-size: 9pt;" color="#808080">;</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font style="font-size: 9pt;" color="#000000">&#160;</font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-spacerun: yes;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span style="mso-tab-count: 1;"></span><span><font color="#0000ff">DECLARE</font></span><font color="#000000"> @now </font><span><font color="#0000ff">datetime</font></span><font color="#000000"> </font><span><font color="#808080">=</font></span><font color="#000000"> </font><span><font color="#ff00ff">GETUTCDATE</font></span></font><span><font style="font-size: 9pt;" color="#808080">();</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">DECLARE</font></span><font color="#000000"> @binaryId </font><span><font color="#0000ff">binary</font></span><span><font color="#808080">(</font></span><font color="#000000">16</font><span><font color="#808080">)</font></span><font color="#000000"> </font><span><font color="#808080">=</font></span><font color="#000000"> </font><span><font color="#ff00ff">CAST</font></span><span><font color="#808080">(</font></span><font color="#000000">@BinaryItemId </font><span><font color="#0000ff">AS</font></span><font color="#000000"> </font><span><font color="#0000ff">binary</font></span><span><font color="#808080">(</font></span><font color="#000000">16</font></font><span><font style="font-size: 9pt;" color="#808080">));</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">DECLARE</font></span><font color="#000000"> @itemIdFound </font><span><font color="#0000ff">binary</font></span><span><font color="#808080">(</font></span><font color="#000000">16</font><span><font color="#808080">)</font></span><font color="#000000"> </font><span><font color="#808080">=</font></span><font color="#000000"> </font></font><span><font style="font-size: 9pt;" color="#808080">NULL;</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><span style="mso-spacerun: yes;"><font face="Consolas"><font style="font-size: 9pt;" color="#000000">&#160;&#160;&#160; </font></font></span></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">WHILE </font></span><span><font color="#808080">(</font></span><font color="#000000">@retry </font><span><font color="#808080">&gt;</font></span><font color="#000000"> 0</font></font><span><font style="font-size: 9pt;" color="#808080">)</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><span><font style="font-size: 9pt;" color="#0000ff">BEGIN</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 2;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">BEGIN</font></span><font color="#000000"> </font></font><span><font style="font-size: 9pt;" color="#0000ff">TRY</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 3;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><span><font style="font-size: 9pt;" color="#008000">-- Delete any extended data</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 3;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">DELETE</font></span><font color="#000000"> </font><span><font color="#0000ff">FROM</font></span><font color="#000000"> [dbo]</font><span><font color="#808080">.</font></span><font color="#000000">[BinaryItemEx]</font></font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 3;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">WHERE</font></span><font color="#000000"> [BinaryItemId] </font><span><font color="#808080">=</font></span><font color="#000000"> @binaryId</font></font><span><font style="font-size: 9pt;" color="#808080">;</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font style="font-size: 9pt;" color="#000000">&#160;</font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 3;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><span><font style="font-size: 9pt;" color="#008000">-- First try an update as the primary may already exist</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 3;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#ff00ff">UPDATE</font></span><font color="#000000"> [dbo]</font><span><font color="#808080">.</font></span><font color="#000000">[BinaryItem]</font></font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 3;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">SET</font></span><font color="#000000"><span style="mso-tab-count: 1;">&#160; </span>@itemIdFound </font><span><font color="#808080">=</font></span><font color="#000000"> [BinaryItemId]</font></font><span><font style="font-size: 9pt;" color="#808080">,</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font color="#000000"><span style="mso-tab-count: 4;"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font style="font-size: 9pt;">[ItemAccessed] </font></font><font style="font-size: 9pt;"><span><font color="#808080">=</font></span><font color="#000000"> @now</font></font><span><font style="font-size: 9pt;" color="#808080">,</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font color="#000000"><span style="mso-tab-count: 4;"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font style="font-size: 9pt;">[BinaryData] </font></font><font style="font-size: 9pt;"><span><font color="#808080">=</font></span><font color="#000000"> @BinaryData</font></font><span><font style="font-size: 9pt;" color="#808080">,</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font color="#000000"><span style="mso-tab-count: 4;"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font style="font-size: 9pt;">[BinaryExParts] </font></font><font style="font-size: 9pt;"><span><font color="#808080">=</font></span><font color="#000000"> 0</font></font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 3;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">WHERE</font></span><font color="#000000"> [BinaryItemId] </font><span><font color="#808080">=</font></span><font color="#000000"> @binaryId</font></font><span><font style="font-size: 9pt;" color="#808080">;</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font style="font-size: 9pt;" color="#000000">&#160;</font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 3;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><span><font style="font-size: 9pt;" color="#008000">-- If no primary found the perform the insert</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 3;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">IF </font></span><span><font color="#808080">(</font></span><font color="#000000">@itemIdFound </font><span><font color="#808080">IS</font></span><font color="#000000"> </font></font><span><font style="font-size: 9pt;" color="#808080">NULL)</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 3;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><span><font style="font-size: 9pt;" color="#0000ff">BEGIN</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 4;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">INSERT</font></span><font color="#000000"> </font><span><font color="#0000ff">INTO</font></span><font color="#000000"> [dbo]</font><span><font color="#808080">.</font></span><font color="#000000">[BinaryItem]</font><span><font color="#0000ff"> </font></span><span><font color="#808080">(</font></span><font color="#000000">[BinaryItemId]</font><span><font color="#808080">,</font></span><font color="#000000"> [ItemCreated]</font><span><font color="#808080">,</font></span><font color="#000000"> [ItemAccessed]</font><span><font color="#808080">,</font></span><font color="#000000"> [BinaryData]</font><span><font color="#808080">,</font></span><font color="#000000"> [BinaryExParts]</font></font><span><font style="font-size: 9pt;" color="#808080">)</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 4;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">VALUES</font></span><span><font color="#808080">(</font></span><font color="#000000">@binaryId</font><span><font color="#808080">,</font></span><font color="#000000"> @now</font><span><font color="#808080">,</font></span><font color="#000000"> @now</font><span><font color="#808080">,</font></span><font color="#000000"> @BinaryData</font><span><font color="#808080">,</font></span><font color="#000000"> 0</font></font><span><font style="font-size: 9pt;" color="#808080">);</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 3;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><span><font style="font-size: 9pt;" color="#0000ff">END</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font style="font-size: 9pt;" color="#000000">&#160;</font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 3;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">SET</font></span><font color="#000000"> @retry </font><span><font color="#808080">=</font></span><font color="#000000"> 0</font></font><span><font style="font-size: 9pt;" color="#808080">;</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 2;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">END</font></span><font color="#000000"> </font></font><span><font style="font-size: 9pt;" color="#0000ff">TRY</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 2;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">BEGIN</font></span><font color="#000000"> </font></font><span><font style="font-size: 9pt;" color="#0000ff">CATCH</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 3;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">SET</font></span><font color="#000000"> @retry </font><span><font color="#808080">-=</font></span><font color="#000000"> 1</font></font><span><font style="font-size: 9pt;" color="#808080">;</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><span style="mso-spacerun: yes;"><font face="Consolas"><font style="font-size: 9pt;" color="#000000">&#160; </font></font></span></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 3;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">IF </font></span><span><font color="#808080">(</font></span><font color="#000000">@retry </font><span><font color="#808080">&lt;=</font></span><font color="#000000"> 0 </font><span><font color="#808080">OR</font></span><span><font color="#0000ff"> </font></span><span><font color="#808080">(</font></span><span><font color="#ff00ff">error_number</font></span><span><font color="#808080">()</font></span><font color="#000000"> </font><span><font color="#808080">&lt;&gt;</font></span><font color="#000000"> 41302 </font><span><font color="#808080">AND</font></span><font color="#000000"> </font><span><font color="#ff00ff">error_number</font></span><span><font color="#808080">()</font></span><font color="#000000"> </font><span><font color="#808080">&lt;&gt;</font></span><font color="#000000"> 41305 </font><span><font color="#808080">AND</font></span><font color="#000000"> </font><span><font color="#ff00ff">error_number</font></span><span><font color="#808080">()</font></span><font color="#000000"> </font><span><font color="#808080">&lt;&gt;</font></span><font color="#000000"> 41325 </font><span><font color="#808080">AND</font></span><font color="#000000"> </font><span><font color="#ff00ff">error_number</font></span><span><font color="#808080">()</font></span><font color="#000000"> </font><span><font color="#808080">&lt;&gt;</font></span><font color="#000000"> 41301</font></font><span><font style="font-size: 9pt;" color="#808080">))</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 3;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><span><font style="font-size: 9pt;" color="#0000ff">BEGIN</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 4;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><span><font style="font-size: 9pt;" color="#008000">-- insert custom error handling for other error conditions here</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 4;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#808080">;</font></span></font><span><font style="font-size: 9pt;" color="#0000ff">THROW</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 3;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><span><font style="font-size: 9pt;" color="#0000ff">END</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 2;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">END</font></span><font color="#000000"> </font></font><span><font style="font-size: 9pt;" color="#0000ff">CATCH</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><span><font style="font-size: 9pt;" color="#0000ff">END</font></span></font></p>  <p class="CodeBlockCxSpLast" style="margin: 0cm 0cm 6pt; line-height: normal;"><span><font face="Consolas"><font style="font-size: 9pt;" color="#0000ff">END</font></font></span></p>  <p class="MsoNormal" style="margin: 0cm 0cm 8pt; line-height: 12pt;"><font face="Calibri"><font style="font-size: 11pt;" color="#000000">This code covers the common errors associated with using an optimistic concurrency control mechanism, as used by the In Memory OLTP engine.</font></font></p>  <p class="MsoNormal" style="margin: 0cm 0cm 8pt; line-height: 12pt;"><font face="Calibri"><font style="font-size: 11pt;" color="#000000">In this case I have set the retry count to be 3, but this can easily be configured to suit your application and environment requirements.</font></font></p>  <h3 style="margin: 2pt 0cm 0pt; line-height: 13pt;"><font face="Calibri Light"><font style="font-size: 12pt;" color="#1f4d78"><font style="font-weight: normal;">Durability</font></font></font></h3>  <p class="MsoNormal" style="margin: 0cm 0cm 8pt; line-height: 12pt;"><font face="Calibri"><font style="font-size: 11pt;" color="#000000">In the samples above I have defined the tables to have a durability option of SCHEMA_AND_DATA. In the case of transient session data one could again improve performance using just the SCHEMA_ONLY option.</font></font></p>  <h3 style="margin: 2pt 0cm 0pt; line-height: 13pt;"><font face="Calibri Light"><font style="font-size: 12pt;" color="#1f4d78"><font style="font-weight: normal;">File Based Extension</font></font></font></h3>  <p class="MsoNormal" style="margin: 0cm 0cm 8pt; line-height: 12pt;"><font face="Calibri"><font style="font-size: 11pt;" color="#000000">In addition to taking the approach outlined above there is the object that if the data size is larger than the specified segment size a normal file based table can be used for saving the LOB data. In this case the table definitions would be more like the following:</font></font></p>  <p class="CodeBlockCxSpFirst" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span><font color="#0000ff"><font style="font-size: 9pt;">CREATE</font></font></span><font style="font-size: 9pt;"><font color="#000000"> </font><span><font color="#0000ff">TABLE</font></span><font color="#000000"> [dbo]</font><span><font color="#808080">.</font></span><font color="#000000">[BinaryItem] </font></font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><span><font face="Consolas"><font style="font-size: 9pt;" color="#808080">(</font></font></span></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font color="#000000"><span style="mso-spacerun: yes;"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></span><font style="font-size: 9pt;"><span style="mso-tab-count: 1;"></span>[BinaryItemId]<span style="mso-tab-count: 5;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></font></font><font style="font-size: 9pt;"><span><font color="#0000ff">binary</font></span><span><font color="#808080">(</font></span><font color="#000000">16</font><span><font color="#808080">)&#160;&#160;&#160;&#160; N</font></span><span><font color="#808080">OT</font></span><font color="#000000"> </font><span><font color="#808080">NULL</font></span></font><span style="mso-tab-count: 2;"><font style="font-size: 9pt;" color="#000000">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 2;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">CONSTRAINT</font></span><font color="#000000"> [PK_BinaryItem] </font><span><font color="#0000ff">PRIMARY</font></span><font color="#000000"> </font><span><font color="#0000ff">KEY</font></span><font color="#000000"> </font><span><font color="#0000ff">NONCLUSTERED</font></span><font color="#000000"> </font></font><span><font style="font-size: 9pt;" color="#0000ff">HASH</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 2;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">WITH </font></span><span><font color="#808080">(</font></span><font color="#000000">BUCKET_COUNT </font><span><font color="#808080">=</font></span><font color="#000000"> 1000000</font></font><span><font style="font-size: 9pt;" color="#808080">),</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font color="#000000"><span style="mso-tab-count: 1;"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></span><font style="font-size: 9pt;">[ItemCreated]<span style="mso-tab-count: 3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></font></font><font style="font-size: 9pt;"><span><font color="#0000ff">datetime</font></span><span style="mso-tab-count: 2;"><font color="#000000">&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><span><font color="#808080">NOT</font></span><font color="#000000"> </font></font><span><font style="font-size: 9pt;" color="#808080">NULL,</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font color="#000000"><span style="mso-tab-count: 1;"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></span><font style="font-size: 9pt;">[BinaryData]<span style="mso-tab-count: 3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></font></font><font style="font-size: 9pt;"><span><font color="#0000ff">varbinary</font></span><span><font color="#808080">(</font></span><font color="#000000">6000</font><span><font color="#808080">)</font></span><span style="mso-tab-count: 1;"><font color="#000000"> </font></span><span><font color="#808080">NULL,</font></span><font color="#000000"> </font></font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font color="#000000"><span style="mso-tab-count: 1;"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></span><font style="font-size: 9pt;">[IsExtended]<span style="mso-tab-count: 3;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></font></font><font style="font-size: 9pt;"><span><font color="#0000ff">int</font></span><span style="mso-tab-count: 3;"><font color="#000000">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><span><font color="#808080">NOT</font></span><font color="#000000"> </font></font><span><font style="font-size: 9pt;" color="#808080">NULL</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><span><font face="Consolas"><font style="font-size: 9pt;" color="#808080">)</font></font></span></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span><font color="#0000ff"><font style="font-size: 9pt;">WITH </font></font></span><font style="font-size: 9pt;"><span><font color="#808080">(</font></span><font color="#000000">MEMORY_OPTIMIZED </font><span><font color="#808080">=</font></span><font color="#000000"> </font><span><font color="#0000ff">ON</font></span><span><font color="#808080">,</font></span><font color="#000000"> DURABILITY </font><span><font color="#808080">=</font></span><font color="#000000"> SCHEMA_AND_DATA</font></font><span><font style="font-size: 9pt;" color="#808080">);</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><span><font face="Consolas"><font style="font-size: 9pt;" color="#0000ff">GO</font></font></span></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font style="font-size: 9pt;" color="#000000">&#160;</font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span><font color="#0000ff"><font style="font-size: 9pt;">CREATE</font></font></span><font style="font-size: 9pt;"><font color="#000000"> </font><span><font color="#0000ff">TABLE</font></span><font color="#000000"> [dbo]</font><span><font color="#808080">.</font></span><font color="#000000">[BinaryItemEx] </font></font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><span><font face="Consolas"><font style="font-size: 9pt;" color="#808080">(</font></font></span></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font color="#000000"><span style="mso-spacerun: yes;"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></span><font style="font-size: 9pt;"><span style="mso-tab-count: 1;"></span>[BinaryItemId]<span style="mso-tab-count: 2;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></font></font><font style="font-size: 9pt;"><span><font color="#0000ff">binary</font></span><span><font color="#808080">(</font></span><font color="#000000">16</font><span><font color="#808080">)</font></span><span style="mso-tab-count: 2;"><font color="#000000">&#160;&#160;&#160;&#160;&#160; </font></span><span><font color="#808080">NOT</font></span><font color="#000000"> </font></font><span><font style="font-size: 9pt;" color="#808080">NULL,</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font color="#000000"><span style="mso-spacerun: yes;"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></span><font style="font-size: 9pt;"><span style="mso-tab-count: 1;"></span>[TotalLength]<span style="mso-tab-count: 2;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></font></font><font style="font-size: 9pt;"><span><font color="#0000ff">int</font></span><span style="mso-tab-count: 3;"><font color="#000000">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><span><font color="#808080">NOT</font></span><font color="#000000"> </font></font><span><font style="font-size: 9pt;" color="#808080">NULL,</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font color="#000000"><span style="mso-tab-count: 1;"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></span><font style="font-size: 9pt;">[BinaryDataEx]<span style="mso-tab-count: 2;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></font></font><font style="font-size: 9pt;"><span><font color="#0000ff">varbinary</font></span><span><font color="#808080">(</font></span><span><font color="#ff00ff">max</font></span><span><font color="#808080">)</font></span><span style="mso-tab-count: 1;"><font color="#000000">&#160; </font></span><span><font color="#808080">NOT</font></span><font color="#000000"> </font></font><span><font style="font-size: 9pt;" color="#808080">NULL,</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">CONSTRAINT</font></span><font color="#000000"> [PK_BinaryItemEx] </font><span><font color="#0000ff">PRIMARY</font></span><font color="#000000"> </font><span><font color="#0000ff">KEY</font></span><font color="#000000"> </font></font><span><font style="font-size: 9pt;" color="#0000ff">CLUSTERED</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span><span style="mso-tab-count: 2;"><font color="#0000ff"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span></span><font style="font-size: 9pt;"><span><font color="#808080">(</font></span><font color="#000000">[BinaryItemId]</font></font><span><font style="font-size: 9pt;" color="#808080">)</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><span><font face="Consolas"><font style="font-size: 9pt;" color="#808080">)</font></font></span></p>  <p class="CodeBlockCxSpLast" style="margin: 0cm 0cm 6pt; line-height: normal;"><font face="Consolas"><span><font color="#0000ff"><font style="font-size: 9pt;">ON</font></font></span><font style="font-size: 9pt;"><font color="#000000"> [DEFAULT] </font><span><font color="#0000ff">TEXTIMAGE_ON</font></span><font color="#000000"> [MemBinaryStore_Binary]</font></font><span><font style="font-size: 9pt;" color="#808080">;</font></span></font></p>  <p class="MsoNormal" style="margin: 0cm 0cm 8pt; line-height: 12pt;"><font face="Calibri"><font style="font-size: 11pt;" color="#000000">In this case the number of segments is not required, but rather just an indicator of whether extended data exists.</font></font></p>  <p class="MsoNormal" style="margin: 0cm 0cm 8pt; line-height: 12pt;"><font face="Calibri"><font style="font-size: 11pt;" color="#000000">One could even take the approach of defining the database such that the LOB data is saved into a dedicated performant filegroup:</font></font></p>  <p class="CodeBlockCxSpFirst" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span><font color="#0000ff"><font style="font-size: 9pt;">CREATE</font></font></span><font style="font-size: 9pt;"><font color="#000000"> </font><span><font color="#0000ff">DATABASE</font></span><font color="#000000"> [MemBinaryStore] </font></font></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><font color="#000000"><span style="mso-tab-count: 1;"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></span><font style="font-size: 9pt;">CONTAINMENT </font></font><font style="font-size: 9pt;"><span><font color="#808080">=</font></span><font color="#000000"> </font><span><font color="#0000ff">NONE</font></span></font><span style="mso-tab-count: 2;"><font style="font-size: 9pt;" color="#000000">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">ON</font></span><font color="#000000"> </font><span><font color="#0000ff">PRIMARY </font></span><span><font color="#808080">(</font></span><font color="#000000">NAME </font><span><font color="#808080">=</font></span><font color="#000000"> [MemBinaryStore_Primary]</font><span><font color="#808080">,</font></span><font color="#000000"> </font><span><font color="#0000ff">FILENAME</font></span><font color="#000000"> </font><span><font color="#808080">=</font></span><font color="#000000"> </font><span><font color="#ff0000">'C:\MSSQL\DATA\MemBinaryStore_Prim.mdf'</font></span><span><font color="#808080">,</font></span><font color="#000000"> SIZE </font><span><font color="#808080">=</font></span><font color="#000000"> 10 MB</font><span><font color="#808080">,</font></span><font color="#000000"> FILEGROWTH </font><span><font color="#808080">=</font></span><font color="#000000"> 10 MB</font></font><span><font style="font-size: 9pt;" color="#808080">),</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">FILEGROUP</font></span><font color="#000000"> [MemBinaryStore_Mod] </font><span><font color="#ff00ff">CONTAINS</font></span><font color="#000000"> MEMORY_OPTIMIZED_DATA</font><span><font color="#0000ff"> </font></span><span><font color="#808080">(</font></span><font color="#000000">NAME </font><span><font color="#808080">=</font></span><font color="#000000"> [MemBinaryStore_Mod]</font><span><font color="#808080">,</font></span><font color="#000000"> </font><span><font color="#0000ff">FILENAME</font></span><font color="#000000"> </font><span><font color="#808080">=</font></span><font color="#000000"> </font><span><font color="#ff0000">'C:\MSSQL\DATA\MemBinaryStore.dir'</font></span></font><span><font style="font-size: 9pt;" color="#808080">),</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">FILEGROUP</font></span><font color="#000000"> [MemBinaryStore_Data]</font><span><font color="#0000ff"> </font></span><span><font color="#808080">(</font></span><font color="#000000">NAME </font><span><font color="#808080">=</font></span><font color="#000000"> [MemBinaryStore_Data1]</font><span><font color="#808080">,</font></span><font color="#000000"> </font><span><font color="#0000ff">FILENAME</font></span><font color="#000000"> </font><span><font color="#808080">=</font></span><font color="#000000"> </font><span><font color="#ff0000">'C:\MSSQL\DATA\MemBinaryStore_Data1.ndf'</font></span><span><font color="#808080">,</font></span><font color="#000000"> SIZE </font><span><font color="#808080">=</font></span><font color="#000000"> 10 MB</font><span><font color="#808080">,</font></span><font color="#000000"> FILEGROWTH </font><span><font color="#808080">=</font></span><font color="#000000"> 10 MB</font></font><span><font style="font-size: 9pt;" color="#808080">),</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span><span style="mso-tab-count: 2;"><font color="#0000ff"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span></span><font style="font-size: 9pt;"><span><font color="#808080">(</font></span><font color="#000000">NAME </font><span><font color="#808080">=</font></span><font color="#000000"> [MemBinaryStore_Data2]</font><span><font color="#808080">,</font></span><font color="#000000"> </font><span><font color="#0000ff">FILENAME</font></span><font color="#000000"> </font><span><font color="#808080">=</font></span><font color="#000000"> </font><span><font color="#ff0000">'C:\MSSQL\DATA\MemBinaryStore_Data2.ndf'</font></span><span><font color="#808080">,</font></span><font color="#000000"> SIZE </font><span><font color="#808080">=</font></span><font color="#000000"> 10 MB</font><span><font color="#808080">,</font></span><font color="#000000"> FILEGROWTH </font><span><font color="#808080">=</font></span><font color="#000000"> 10 MB</font></font><span><font style="font-size: 9pt;" color="#808080">),</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#0000ff">FILEGROUP</font></span><font color="#000000"> [MemBinaryStore_Binary]</font><span><font color="#0000ff"> </font></span><span><font color="#808080">(</font></span><font color="#000000">NAME </font><span><font color="#808080">=</font></span><font color="#000000"> [MemBinaryStore_Binary1]</font><span><font color="#808080">,</font></span><font color="#000000"> </font><span><font color="#0000ff">FILENAME</font></span><font color="#000000"> </font><span><font color="#808080">=</font></span><font color="#000000"> </font><span><font color="#ff0000">'C:\MSSQL\DATA\MemBinaryStore_Binary1.ndf'</font></span><span><font color="#808080">,</font></span><font color="#000000"> SIZE </font><span><font color="#808080">=</font></span><font color="#000000"> 10 MB</font><span><font color="#808080">,</font></span><font color="#000000"> FILEGROWTH </font><span><font color="#808080">=</font></span><font color="#000000"> 10 MB</font></font><span><font style="font-size: 9pt;" color="#808080">),</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span><span style="mso-tab-count: 2;"><font color="#0000ff"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span></span><font style="font-size: 9pt;"><span><font color="#808080">(</font></span><font color="#000000">NAME </font><span><font color="#808080">=</font></span><font color="#000000"> [MemBinaryStore_Binary2]</font><span><font color="#808080">,</font></span><font color="#000000"> </font><span><font color="#0000ff">FILENAME</font></span><font color="#000000"> </font><span><font color="#808080">=</font></span><font color="#000000"> </font><span><font color="#ff0000">'C:\MSSQL\DATA\MemBinaryStore_Binary2.ndf'</font></span><span><font color="#808080">,</font></span><font color="#000000"> SIZE </font><span><font color="#808080">=</font></span><font color="#000000"> 10 MB</font><span><font color="#808080">,</font></span><font color="#000000"> FILEGROWTH </font><span><font color="#808080">=</font></span><font color="#000000"> 10 MB</font></font><span><font style="font-size: 9pt;" color="#808080">)</font></span></font></p>  <p class="CodeBlockCxSpMiddle" style="margin: 0cm 0cm 0pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#ff00ff">LOG</font></span><font color="#000000"> </font><span><font color="#0000ff">ON </font></span><span><font color="#808080">(</font></span><font color="#000000">NAME </font><span><font color="#808080">=</font></span><font color="#000000"> [MemBinaryStore_Log1]</font><span><font color="#808080">,</font></span><font color="#000000"> </font><span><font color="#0000ff">FILENAME</font></span><font color="#000000"> </font><span><font color="#808080">=</font></span><font color="#000000"> </font><span><font color="#ff0000">'C:\MSSQL\DATA\MemBinaryStore_Log1.ldf'</font></span><span><font color="#808080">,</font></span><font color="#000000"> SIZE </font><span><font color="#808080">=</font></span><font color="#000000"> 5 MB</font><span><font color="#808080">,</font></span><font color="#000000"> FILEGROWTH </font><span><font color="#808080">=</font></span><font color="#000000"> 5 MB</font></font><span><font style="font-size: 9pt;" color="#808080">)</font></span></font></p>  <p class="CodeBlockCxSpLast" style="margin: 0cm 0cm 6pt; line-height: normal;"><font face="Consolas"><span style="mso-tab-count: 1;"><font color="#000000"><font style="font-size: 9pt;">&#160;&#160;&#160;&#160; </font></font></span><font style="font-size: 9pt;"><span><font color="#ff00ff">COLLATE</font></span><font color="#000000"> Latin1_General_100_CI_AS</font></font><span><font style="font-size: 9pt;" color="#808080">;</font></span></font></p>  <p class="MsoNormal" style="margin: 0cm 0cm 8pt; line-height: 12pt;"><font face="Calibri"><font style="font-size: 11pt;" color="#000000">In this case the extended processing Stored Procedures would just have to be modified to insert a single row of LOB data and return this single row.</font></font></p>]]></content:encoded>
    </item>
    <item>
      <title>Managing Hive Job Submissions With PowerShell</title>
      <link>https://docs.microsoft.com/archive/blogs/carlnol/managing-hive-job-submissions-with-powershell</link>
      <pubDate>Tue, 18 Jun 2013 10:26:56 GMT</pubDate>
      <dc:creator><![CDATA[Carl Nolan]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.cloudplatform.qa.msdn.wdslab.com/carlnol/2013/06/18/managing-hive-job-submissions-with-powershell/</guid>
      <description><![CDATA[In my previous post, I talked about “Managing Your HDInsight Cluster with PowerShell”. In this post...]]></description>
      <content:encoded><![CDATA[<p>In my previous post, I talked about “<a href="http://blogs.msdn.com/b/carlnol/archive/2013/06/07/managing-your-hdinsight-cluster-with-powershell.aspx" target="_blank">Managing Your HDInsight Cluster with PowerShell</a>”. In this post I made no mention of using Hive. I hope to re-address this balance by specifically talking about how you can submit Hive jobs from the same local management console.</p>  <p>As before all the scripts mentioned in this and the previous post can be downloaded from <a href="https://hadoopsdk.codeplex.com/downloads/get/690770">here</a>.</p>  <p>To run the samples mentioned in this post I am using the following Hive table definition that points to the sample data provided with the service:</p>  <p><font face="Consolas">CREATE EXTERNAL TABLE IF NOT EXISTS HiveSampleTable (      <br />&#160;&#160;&#160; ClientId string,       <br />&#160;&#160;&#160; QueryTime string,       <br />&#160;&#160;&#160; Market string,       <br />&#160;&#160;&#160; DevicePlatform string,       <br />&#160;&#160;&#160; DeviceMake string,       <br />&#160;&#160;&#160; DeviceModel string,       <br />&#160;&#160;&#160; State string,       <br />&#160;&#160;&#160; Country string,       <br />&#160;&#160;&#160; QueryDwellTime double,       <br />&#160;&#160;&#160; SessionId bigint,       <br />&#160;&#160;&#160; SessionPageViewOrder bigint       <br />)       <br />ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'       <br />LOCATION </font><a href="mailto:'@dataplatformrdpcarl.blob.core.windows.net/hive/sampledata'"><font face="Consolas">'/hive/warehouse/hivesampletable'</font></a><font face="Consolas">;</font></p>  <p>The process for Hive submission is basically the same as for MapReduce jobs. However there is an additional requirement to preserve the Hive meta-data. Basically if you define Hive tables/views/etc., you want these to persist between cluster creations.</p>  <p>To achieve this you will need to create a customized cluster. Once you do this you can use Hive for your Elastic job submissions.</p>  <h2><a name="_Ref359312012">Provision Customized Cluster</a></h2>  <p>You can also provision cluster and configure it to connect to more than one Azure Blob storage or custom Hive and Oozie meta-stores.</p>  <p><em>This advanced feature allows you to separate the lifetime of your data and metadata from the lifetime of the cluster. This not only includes job submission details but also allows one to retain Hive metadata, and hence table definitions, as long as Hive External tables are used.</em></p>  <p>Using the same principal of defining the italicized variables first, the first step is to get the subscription details and the storage key of the storage accounts you want to connect your cluster to, in this case 2 storage accounts:</p>  <p><font face="Consolas">$subid = Get-AzureSubscription -Current | %{ $_.SubscriptionId }      <br />$cert = Get-AzureSubscription -Current | %{ $_.Certificate }</font></p> <font face="Consolas"></font>  <p><font face="Consolas">$key1 = Get-AzureStorageKey $myStoreAcct1 | %{ $_.Primary }      <br />$key2 = Get-AzureStorageKey $myStoreAcct2 | %{ $_.Primary }</font></p>  <p>The next step is to create your custom SQL Azure Database and Server following this <a href="http://blogs.msdn.com/b/windowsazure/archive/2013/02/07/windows-azure-sql-database-management-with-powershell.aspx">blog post</a> or get credentials to the existing one.</p>  <p>To create a SQL Database one can easily use the management portal:</p>  <p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/76/metablogapi/1778.image_761EF66C.png" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-76-metablogapi/1778.image_5F00_761EF66C.png"><img title="image" style="display: inline; background-image: none;" border="0" alt="image" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/76/metablogapi/0005.image_thumb_3DD05987.png" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-76-metablogapi/0005.image_5F00_thumb_5F00_3DD05987.png" width="640" height="472" /></a></p>  <p>Using this connectivity information you can create cluster config object, pipe in additional storage and meta-stores configuration, and then finally pipe this config object into the New-AzureHDInsightCluster cmdlet to create a cluster based on this custom configuration.</p>  <p><font face="Consolas">Write-Host &quot;Creating '$numbernodes' Node Cluster named: $clustername&quot; -f yellow</font></p> <font face="Consolas"></font>  <p><font face="Consolas">New-AzureHDInsightConfig -ClusterSizeInNodes $numbernodes | Set-AzureHDInsightDefaultStorage -StorageAccountName $blobstorage1 -StorageAccountKey $key1 -StorageContainerName $containerdefault |Add-AzureHDInsightStorage -StorageAccountName $blobstorage2 -StorageAccountKey $key2 | Add-AzureHDInsightMetastore -SqlAzureServerName $sqlserver -DatabaseName $databaseoozie -UserName $sqlusername -Password $sqlpassword -MetastoreType OozieMetastore | Add-AzureHDInsightMetastore -SqlAzureServerName $sqlserver -DatabaseName $databasehive -UserName $sqlusername -Password $sqlpassword -MetastoreType HiveMetastore | New-AzureHDInsightCluster -SubscriptionId $subid -Certificate $cert -UserName $username -Password $password -Name $clustername -Location $location</font></p>  <p>As before a sample set of variable setting could be:</p>  <p><font face="Consolas">$mySubName = &quot;Windows Azure Subscription Name&quot;      <br />$myStoreAcct1 = &quot;storageaccount&quot;       <br />$myStoreAcct2 = &quot;storageaccountbck&quot;</font></p> <font face="Consolas"></font>  <p><font face="Consolas">$blobstorage1 = &quot;storageaccount.blob.core.windows.net&quot;      <br />$blobstorage2 = &quot;storageaccountbck.blob.core.windows.net&quot;</font></p> <font face="Consolas"></font>  <p><font face="Consolas">$containerdefault = &quot;hadooproot&quot;</font></p> <font face="Consolas"></font>  <p><font face="Consolas">$clustername = $Cluster      <br />$location = &quot;East US&quot;</font></p> <font face="Consolas"></font>  <p><font face="Consolas">$numbernodes = $Hosts</font></p> <font face="Consolas"></font>  <p><font face="Consolas">$username = &quot;Admin&quot;      <br />$password = &quot;Password&quot;</font></p> <font face="Consolas"></font>  <p><font face="Consolas">$sqlserver = &quot;sqlservername.database.windows.net&quot;      <br />$sqlusername = &quot;SqlAdmin&quot;       <br />$sqlpassword = &quot;Password&quot;</font></p> <font face="Consolas"></font>  <p><font face="Consolas">$databasehive = &quot;HiveDatabaseName&quot;      <br />$databaseoozie = &quot;OozieDatabaseName&quot;</font></p>  <p>As before one can use the PowerShell Param option to create a script that enables passing in parameters; such as number hosts, cluster name, etc. In doing this one could create a cluster using the following command:</p>  <p><font face="Consolas">. &quot;C:\Users\Me\Scripts\ClusterCreateComplex.ps1&quot; -Hosts 4 -Cluster &quot;clustername&quot;</font></p>  <p>In this case I have used a secondary storage account with a “bck” prefix. Other than this, along with the SQL parameters, the Azure storage configuration is the same as in the previous case.</p>  <h2><a name="_Toc359333222">Hive Job Submission</a></h2>  <p>The process for running Hive jobs is a little different to that of MapReduce. In this instance one has to use the Web client to Create and Submit the Hive job. As an aside the process for running a Pig script is very similar.</p>  <p>For Hive, you have two options available. Firstly you can provide the actual Hive command or you can provide a file location accessible from the cluster.</p>  <p>Thus, assuming you have created the cluster as specified in section “Provision Customized Cluster”, the script to submit a Hive command or file would be:</p>  <p><font face="Consolas">Param($Cluster = &quot;myclustername&quot;, [string] $OutputPath = $(throw &quot;Output Path Required.&quot;), [string] $Hql = $(throw &quot;Hive Query File Required.&quot;), $HqlType = &quot;Command&quot;)</font></p> <font face="Consolas"></font>  <p><font face="Consolas"># Import the management module and DLLS      <br />$basePath = &quot;C:\Users\Me&quot;       <br />$dllPath = $basePath + &quot;\Dlls&quot;       <br />Add-Type -Path ($dllPath + &quot;\microsoft.hadoop.client.dll&quot;)       <br />Add-Type -Path ($dllPath + &quot;\microsoft.hadoop.webclient.dll&quot;)       <br />Add-Type -Path ($dllPath + &quot;\System.Net.Http.dll&quot;)       <br />Add-Type -Path ($dllPath + &quot;\System.Net.Http.Formatting.dll&quot;)       <br />Add-Type -Path ($dllPath + &quot;\System.Net.Http.WebRequest.dll&quot;)</font></p> <font face="Consolas"></font>  <p><font face="Consolas"># Define the JObject type      <br />$newtonDll = $dllPath + &quot;\Newtonsoft.Json.dll&quot;       <br />$submitterAssembly = [System.Reflection.Assembly]::LoadFile($newtonDll)       <br />[Type] $jobjectType = $submitterAssembly.GetType(&quot;Newtonsoft.Json.Linq.JObject&quot;, 1)</font></p> <font face="Consolas"></font>  <p><font face="Consolas"># Define the connection properties      <br />$clusterName = &quot;</font><a href="https://$Cluster.azurehdinsight.net:563&quot;"><font face="Consolas">https://$Cluster.azurehdinsight.net:563&quot;</font></a>     <br /><font face="Consolas">[Uri] $clusterUri = New-Object System.Uri $clusterName</font></p> <font face="Consolas"></font>  <p><font face="Consolas">$mySubName = &quot;Windows Azure Subscription Name&quot;      <br />$clusterUsername = &quot;Admin&quot;       <br />$clusterPassword = &quot;Password&quot;</font></p> <font face="Consolas"></font>  <p><font face="Consolas"># Get the Hive Query from the specified File or Command      <br />[string] $hiveCommand = &quot;&quot;       <br />[string[]]$hiveFiles = @()</font></p> <font face="Consolas"></font>  <p><font face="Consolas">if ($HqlType -eq &quot;File&quot;) {      <br />&#160;&#160;&#160; $hiveFiles = $hiveFiles + $Hql       <br />&#160;&#160;&#160; $hiveCommand = $Null       <br />} elseif ($HqlType -eq &quot;Command&quot;) {       <br />&#160;&#160;&#160; $hiveFiles = $Null       <br />&#160;&#160;&#160; $hiveCommand = $Hql       <br />} else {       <br />&#160;&#160;&#160; throw &quot;Hql Type must be 'File' or 'Command' and '$HqlType' was entered.&quot;       <br />}</font></p> <font face="Consolas"></font>  <p><font face="Consolas"># Connect and Run the Hive Query      <br />$httpClient = New-Object Microsoft.Hadoop.WebHCat.Protocol.WebHCatHttpClient ($clusterUri, $clusterUsername, $clusterPassword, $clusterUsername)</font></p> <font face="Consolas"></font>  <p><font face="Consolas">Write-Host &quot;Submitting Hive $HqlType : $Hql&quot; -f yellow      <br />Write-Host &quot;Working Directory '$OutputPath'&quot; -f yellow       <br />Write-Host &quot;On Cluster '$clusterName'&quot; -f yellow       <br />&#160;&#160;&#160; <br />$hiveJob = $httpClient.CreateHiveJob($hiveCommand, $hiveFiles, $Null, $OutputPath, $Null)       <br />if (-Not $hiveJob.IsCompleted) { $hiveJob.Wait() }</font></p> <font face="Consolas"></font>  <p><font face="Consolas">if ($hiveJob.Exception) {      <br />&#160;&#160;&#160; Write-Host &quot;Job Submission Error&quot; -f yellow -b red       <br />&#160;&#160;&#160; Write-Host $hiveJob.Exception -f red       <br />}       <br />else       <br />{       <br />&#160;&#160;&#160; $response = $hiveJob.Result&#160;&#160;&#160; <br />&#160;&#160;&#160; $output = [System.Net.Http.HttpContentExtensions]::ReadAsAsync($response.Content, $jobjectType)       <br />&#160;&#160;&#160; $output.Wait()</font></p> <font face="Consolas"></font>  <p><font face="Consolas">&#160;&#160;&#160; $response.EnsureSuccessStatusCode()</font></p> <font face="Consolas"></font>  <p><font face="Consolas">&#160;&#160;&#160; $jobId = $output.Result.GetValue(&quot;id&quot;).ToString()      <br />&#160;&#160;&#160; $httpClient.WaitForJobToCompleteAsync($jobId).Wait()</font></p> <font face="Consolas"></font>  <p><font face="Consolas">&#160;&#160;&#160; Write-Host &quot;Job Run Information&quot; -f yellow      <br />&#160;&#160;&#160; Write-Host &quot;Job Id: &quot; $jobId -f yellow       <br />&#160;&#160;&#160; Write-Host $response       <br />}</font></p>  <p>&#160;</p>  <p>One has to remember that this script assumes the location of the file to be executed is local to the cluster. Also, when providing a string command there is a limit of about 2K of text.</p>  <p>Examples of running this script would be:</p>  <p><font face="Consolas">. &quot;C:\Users\Me\Scripts\ClusterHiveScriptSubmission.ps1&quot; -Cluster &quot;myclustername&quot; -OutputPath &quot;/hive/output&quot; -Hql &quot;/hive/sample/queries/HiveSampleExtractFile.hql&quot; -HqlType &quot;File&quot;</font></p>  <p><font face="Consolas">. &quot;C:\Users\Me\Scripts\ClusterHiveScriptSubmission.ps1&quot; -Cluster &quot;myclustername&quot; -OutputPath &quot;/hive/output&quot; -Hql &quot;INSERT OVERWRITE DIRECTORY '/hive/sample/output' SELECT DevicePlatform, DeviceMake, SUM(QueryDwellTime) FROM HiveSampleTable GROUP BY DevicePlatform, DeviceMake HAVING DevicePlatform &lt;&gt; 'Unknown'&quot;</font></p>  <p>In this instance the paths specified are actually ASV relative paths based on the default storage container.</p>  <p>The output path will contain the “exit”, “stderr”, and “stdout” data from the job execution.</p>  <p>In running this script you will need to point it to the necessary DLLs required for Web Client assemblies. If you create .Net MapReduce job the necessary DLLs will be fetched from <a href="http://www.nuget.org/packages/Microsoft.Hadoop.MapReduce/" target="_blank">Nuget</a>.</p>  <h2><a name="_Toc359333226">Elastic Hive Submissions</a></h2>  <p>As long as one has created a Customized Cluster, as defined above, one can recreate a cluster and still have access to the Hive definitions previously created. This option allows you to:</p>  <ul>   <li>Create a cluster </li>    <li>Define Hive metadata (tables, views, etc.) </li>    <li>Delete the cluster </li> </ul>  <p>When required you can then</p>  <ul>   <li>Create the cluster </li>    <li>Run Hive scripts against previously defined objects </li>    <li>Delete the cluster </li> </ul>  <p>Such a scenario allows for the usage of an Elastic service even when utilizing Hive.</p>  <p>Note. The following script assumes the default storage account is used as the basis for the Hadoop root. This is defined when performing the workstation configuration, as follows:</p>  <p><font face="Consolas">$mySubID = &quot;xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx&quot;      <br />$certThumbprint = &quot;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&quot;       <br />$myCert = Get-Item cert:\CurrentUser\My\$certThumbprint       <br />$mySubName = &quot;Windows Azure Subscription Name&quot;       <br />$myStoreAcct = &quot;defaultstorageaccount&quot;</font></p> <font face="Consolas"></font>  <p><font face="Consolas">Set-AzureSubscription -SubscriptionName $mySubName -Certificate $myCert -SubscriptionID $mySubID</font></p> <font face="Consolas"></font>  <p><font face="Consolas"># Set the current default subscription to be the one being managed along with the default storage account      <br />Set-AzureSubscription -DefaultSubscription $mySubName       <br />Set-AzureSubscription –SubscriptionName $mySubName -CurrentStorageAccount $myStoreAcct</font> </p>  <p>In defining a script to perform these operations one not only needs to define the job and cluster properties, but also the SQL Server properties such that the cluster can be pointed back to the original metadata. As such database credentials are needed along with the Hive and Oozie database names.</p>  <p>The following script will thus allow you to specify the HDInsight, Database, and Hive job properties:</p>  <p><font face="Consolas">Param($Hosts = 4, $Cluster = &quot;myclustername&quot;, [string] $SqlServerName = $(throw &quot;Sql Server Name Required.&quot;), [string] $HiveDatabase = $(throw &quot;Hive Database Name Required.&quot;), [string] $OozieDatabase = $(throw &quot;Oozie Database Name Required.&quot;), [string] $SqlUsername = $(throw &quot;Sql Username Required.&quot;), [string] $SqlPassword = $(throw &quot;Sql Password Required.&quot;), [string] $OutputPath = $(throw &quot;Output Path Required.&quot;), [string] $Hql = $(throw &quot;Hive Query File Required.&quot;), $HqlType = &quot;Command&quot;)</font></p>  <p><font face="Consolas">$basePath = &quot;C:\Users\Me&quot;</font><font face="Consolas"> </font></p>  <p><font face="Consolas"># Import the management module and DLLS      <br />$dllPath = $basePath + &quot;\Dlls&quot;       <br />$cmdletsPath = $basePath + &quot;\Cmdlets&quot;</font></p> <font face="Consolas"></font>  <p><font face="Consolas">Import-Module ($cmdletsPath + &quot;\Microsoft.WindowsAzure.Management.HDInsight.Cmdlet.dll&quot;)      <br />Add-Type -Path ($dllPath + &quot;\microsoft.hadoop.client.dll&quot;)       <br />Add-Type -Path ($dllPath + &quot;\microsoft.hadoop.webclient.dll&quot;)       <br />Add-Type -Path ($dllPath + &quot;\System.Net.Http.dll&quot;)       <br />Add-Type -Path ($dllPath + &quot;\System.Net.Http.Formatting.dll&quot;)       <br />Add-Type -Path ($dllPath + &quot;\System.Net.Http.WebRequest.dll&quot;)</font></p> <font face="Consolas"></font>  <p><font face="Consolas"># Define the JObject type      <br />$newtonDll = $dllPath + &quot;\Newtonsoft.Json.dll&quot;       <br />$submitterAssembly = [System.Reflection.Assembly]::LoadFile($newtonDll)       <br />[Type] $jobjectType = $submitterAssembly.GetType(&quot;Newtonsoft.Json.Linq.JObject&quot;, 1)</font></p> <font face="Consolas"></font>  <p><font face="Consolas"># Get the subscription information and set variables      <br />$subscriptionInfo = Get-AzureSubscription -Current</font></p> <font face="Consolas"></font>  <p><font face="Consolas">$subName = $subscriptionInfo | %{ $_.SubscriptionName }      <br />$subId = $subscriptionInfo | %{ $_.SubscriptionId }       <br />$cert = $subscriptionInfo | %{ $_.Certificate }</font></p> <font face="Consolas"></font>  <p><font face="Consolas">$storeAccount = $subscriptionInfo | %{ $_.CurrentStorageAccount }      <br />$storeAccountBck = $storeAccount + &quot;bck&quot;</font></p> <font face="Consolas"></font>  <p><font face="Consolas">$key = Get-AzureStorageKey $storeAccount | %{ $_.Primary }      <br />$keyBck = Get-AzureStorageKey $storeAccountBck | %{ $_.Primary }</font></p> <font face="Consolas"></font>  <p><font face="Consolas">$storageAccountInfo = Get-AzureStorageAccount $storeAccount      <br />$location = $storageAccountInfo | %{ $_.Location }</font></p> <font face="Consolas"></font>  <p><font face="Consolas">$clusterUsername = &quot;Admin&quot;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br />$clusterPassword = [System.Web.Security.Membership]::GeneratePassword(20, 5)       <br />$clusterName = $Cluster       <br />$numberNodes = $Hosts       <br />$containerDefault = &quot;hadooproot&quot;</font></p> <font face="Consolas"></font>  <p><font face="Consolas">$clusterHttp = &quot;</font><a href="https://$clusterName.azurehdinsight.net:563&quot;"><font face="Consolas">https://$clusterName.azurehdinsight.net:563&quot;</font></a>     <br /><font face="Consolas">$blobStorage = &quot;$storeAccount.blob.core.windows.net&quot;      <br />$blobStorageBck = &quot;$storeAccountBck.blob.core.windows.net&quot;</font></p> <font face="Consolas"></font>  <p><font face="Consolas">$sqlServer = &quot;$SqlServerName.database.windows.net&quot;</font></p> <font face="Consolas"></font>  <p><font face="Consolas"># Create the cluster      <br />Write-Host &quot;Creating '$numberNodes' Node Cluster named: $clusterName&quot; -f yellow       <br />Write-Host &quot;Storage Account '$storeAccount' and Container '$containerDefault'&quot; -f yellow       <br />Write-Host &quot;User '$clusterUsername' Password '$clusterPassword'&quot; -f green       <br />New-AzureHDInsightConfig -ClusterSizeInNodes $numberNodes | Set-AzureHDInsightDefaultStorage -StorageAccountName $blobStorage -StorageAccountKey $key -StorageContainerName $containerdefault |Add-AzureHDInsightStorage -StorageAccountName $blobstorageBck -StorageAccountKey $keyBck | Add-AzureHDInsightMetastore -SqlAzureServerName $sqlServer -DatabaseName $OozieDatabase -UserName $SqlUsername -Password $SqlPassword -MetastoreType OozieMetastore | Add-AzureHDInsightMetastore -SqlAzureServerName $sqlServer -DatabaseName $HiveDatabase -UserName $SqlUsername -Password $SqlPassword -MetastoreType HiveMetastore | New-AzureHDInsightCluster -SubscriptionId $subId -Certificate $cert -UserName $clusterUsername -Password $clusterPassword -Name $clusterName -Location $location       <br />Write-Host &quot;Created '$numbernodes' Node Cluster: $clusterName&quot; -f yellow</font></p> <font face="Consolas"></font>  <p><font face="Consolas"># Get the Hive Query from the specified File or Command      <br />[string] $hiveCommand = &quot;&quot;       <br />#$hiveFiles = New-Object System.Collections.Generic.List[System.String]       <br />[string[]]$hiveFiles = @()</font></p> <font face="Consolas"></font>  <p><font face="Consolas">if ($HqlType -eq &quot;File&quot;) {      <br />&#160;&#160;&#160; #$hiveFiles.Add($Hql)       <br />&#160;&#160;&#160; $hiveFiles = $hiveFiles + $Hql       <br />&#160;&#160;&#160; $hiveCommand = $Null       <br />} elseif ($HqlType -eq &quot;Command&quot;) {       <br />&#160;&#160;&#160; $hiveFiles = $Null       <br />&#160;&#160;&#160; $hiveCommand = $Hql       <br />} else {       <br />&#160;&#160;&#160; throw &quot;Hql Type must be 'File' or 'Command' and '$HqlType' was entered.&quot;       <br />}</font></p> <font face="Consolas"></font>  <p><font face="Consolas"># Connect and Run the Hive Query      <br />[Uri] $clusterUri = New-Object System.Uri $clusterHttp       <br />$httpClient = New-Object Microsoft.Hadoop.WebHCat.Protocol.WebHCatHttpClient ($clusterUri, $clusterUsername, $clusterPassword, $clusterUsername)</font></p> <font face="Consolas"></font>  <p><font face="Consolas">Write-Host &quot;Submitting Hive $HqlType : $Hql&quot; -f yellow      <br />Write-Host &quot;Working Directory '$OutputPath'&quot; -f yellow       <br />Write-Host &quot;On Cluster '$clusterName'&quot; -f yellow       <br />&#160;&#160;&#160; <br />$hiveJob = $httpClient.CreateHiveJob($hiveCommand, $hiveFiles, $Null, $OutputPath, $Null)       <br />if (-Not $hiveJob.IsCompleted) { $hiveJob.Wait() }</font></p> <font face="Consolas"></font>  <p><font face="Consolas">if ($hiveJob.Exception) {      <br />&#160;&#160;&#160; Write-Host &quot;Job Submission Error&quot; -f yellow -b red       <br />&#160;&#160;&#160; Write-Host $hiveJob.Exception -f red       <br />}       <br />else {       <br />&#160;&#160;&#160; $response = $hiveJob.Result&#160;&#160;&#160; <br />&#160;&#160;&#160; $output = [System.Net.Http.HttpContentExtensions]::ReadAsAsync($response.Content, $jobjectType)       <br />&#160;&#160;&#160; $output.Wait()</font></p> <font face="Consolas"></font>  <p><font face="Consolas">&#160;&#160;&#160; $response.EnsureSuccessStatusCode()</font></p> <font face="Consolas"></font>  <p><font face="Consolas">&#160;&#160;&#160; $jobId = $output.Result.GetValue(&quot;id&quot;).ToString()      <br />&#160;&#160;&#160; $httpClient.WaitForJobToCompleteAsync($jobId).Wait()</font></p> <font face="Consolas"></font>  <p><font face="Consolas">&#160;&#160;&#160; Write-Host &quot;Job Run Information&quot; -f yellow      <br />&#160;&#160;&#160; Write-Host &quot;Job Id: &quot; $jobId -f yellow       <br />&#160;&#160;&#160; Write-Host $response       <br />}</font></p> <font face="Consolas"></font>  <p><font face="Consolas"># Finally delete the cluster      <br />Write-Host &quot;Deleting Cluster named: $clusterName&quot; -f yellow       <br />Remove-AzureHDInsightCluster $clusterName -SubscriptionId $subId -Certificate $cert       <br />Write-Host &quot;Cluster $clusterName Deleted&quot; -f yellow</font></p>  <p>&#160;</p>  <p>This script however does make a few assumptions:</p>  <ol>   <li>The Default container is set to &quot;hadooproot&quot; </li>    <li>The subscription default storage container is the base one used for the cluster </li>    <li>Backup storage container is the default with a &quot;bck&quot; suffix </li> </ol>  <p>Of course one could optionally add these as additional parameters</p>  <p>To run this script to execute a Hive file saved on the cluster one can execute the following command:</p>  <p><font face="Consolas">. &quot;C:\Users\Me\Scripts\ClusterHiveElasticScriptSubmissionAuto.ps1&quot; -Hosts 4 -Cluster &quot;myclustername&quot; -SqlServerName &quot;myservername&quot; -SqlUsername &quot;sqlusername&quot; -SqlPassword &quot;sqlpassword&quot; -HiveDatabase &quot;HiveDatabaseName&quot; -OozieDatabase &quot;OozieDatabaseName&quot; -OutputPath &quot;/hive/output&quot; -Hql &quot;/hive/sample/queries/HiveSampleExtractFile.hql&quot; -HqlType &quot;File&quot;</font></p>  <p>Or when running a Hive command directly:</p>  <p><font face="Consolas">. &quot;C:\Users\Me\Scripts\ClusterHiveElasticScriptSubmissionAuto.ps1&quot; -Hosts 4 -Cluster &quot;myclustername&quot; -SqlServerName &quot;sqlservername&quot; -SqlUsername &quot;sqlusername&quot; -SqlPassword &quot;sqlpassword&quot; -HiveDatabase &quot;HiveDatabaseName&quot; -OozieDatabase &quot;OozieDatabaseName&quot; -OutputPath &quot;/hive/output&quot; -Hql &quot;INSERT OVERWRITE DIRECTORY '/hive/sample/output' SELECT DevicePlatform, DeviceMake, SUM(QueryDwellTime) FROM HiveSampleTable GROUP BY DevicePlatform, DeviceMake HAVING DevicePlatform &lt;&gt; 'Unknown'&quot;</font></p>  <p>Of course one can also specify a Hive Command as in the previous Hive job submission example.</p>  <p>As before, the script also generates a password, which is displayed in case the cluster is not successfully deleted after the job execution.</p>  <p>Of course other parameters could be included in this model to make the PowerShell script more applicable to more job types, and possibly remove or change any assumptions that I have made in the script creation.</p>]]></content:encoded>
    </item>
    <item>
      <title>Managing Your HDInsight Cluster with PowerShell</title>
      <link>https://docs.microsoft.com/archive/blogs/carlnol/managing-your-hdinsight-cluster-with-powershell</link>
      <pubDate>Fri, 07 Jun 2013 11:38:00 GMT</pubDate>
      <dc:creator><![CDATA[Carl Nolan]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.cloudplatform.qa.msdn.wdslab.com/carlnol/2013/06/07/managing-your-hdinsight-cluster-with-powershell/</guid>
      <description><![CDATA[An updated version of this post can be found here.
This blog post provides a mechanism for managing...]]></description>
      <content:encoded><![CDATA[<p><strong><em>An updated version of this post can be found <a href="http://blogs.msdn.com/b/carlnol/archive/2013/12/02/managing-your-hdinsight-cluster-and-net-job-submissions.aspx">here</a>.</em></strong></p>
<p>This blog post provides a mechanism for managing an HDInsight cluster using a local management console through the use of Windows PowerShell. The goal is to outline how to configure the local management console, create a simple cluster, submit jobs using MRRunner, and finally provide a mechanism for managing an elastic service.</p>
<p>The ultimate goal will be to present a script that demonstrates how you can provide a DLL and have the script bring a cluster online, run your Job, and then remove the cluster, whilst allowing you to specify the cluster name and the number of hosts needed to run the Job.</p>
<p>All the scripts mentioned in this post can be downloaded from <a href="https://hadoopsdk.codeplex.com/downloads/get/690770">here</a>.</p>
<p>Before provisioning a cluster one needs to ensure the Azure subscription has been correctly configured and a local management console has been setup. So lets start there.</p>
<h2><a name="_Toc358025995"></a>Prepare Azure Subscription Storage</h2>
<p>Under the subscription in which the HDInsight is going to be running one needs a storage account defined, and a management certificate uploaded.</p>
<h3><a name="_Toc358025996"></a>Create a Storage Account</h3>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/76/metablogapi/5148.image_05C34539.png" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-76-metablogapi/5148.image_5F00_05C34539.png"><img style="border: 0px currentcolor; display: inline; background-image: none;" title="image" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/76/metablogapi/2671.image_thumb_67B51177.png" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-76-metablogapi/2671.image_5F00_thumb_5F00_67B51177.png" alt="image" width="619" height="480" border="0" /></a></p>
<p>Next, under the storage account management create a new default container:</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/76/metablogapi/2438.image_0D0EDF19.png" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-76-metablogapi/2438.image_5F00_0D0EDF19.png"><img style="margin: 0px; border: 0px currentcolor; display: inline; background-image: none;" title="image" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/76/metablogapi/5076.image_thumb_6FD91141.png" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-76-metablogapi/5076.image_5F00_thumb_5F00_6FD91141.png" alt="image" width="592" height="480" border="0" /></a></p>
<p>At this point the Azure subscription is basically ready. It is assumed that the subscription has the HDInsight preview enabled, see &ldquo;Cluster Provisioning&rdquo; below.</p>
<h2><a name="_Toc358025997"></a><a name="_Ref356565353"></a>Setting Up Your Local Environment</h2>
<p>Once the Azure subscription has been configured, the next step is to configure your local management environment.</p>
<p>The first step is to install and configure Windows Azure PowerShell. Windows Azure PowerShell is the scripting environment that allows you to control and automate the deployment and management of your workloads in Windows Azure. This includes provisioning virtual machines, setting up virtual networks and cross-premises networks, and managing cloud services in Windows Azure.</p>
<p>To use the cmdlets in Windows Azure PowerShell you will need to download and import the modules, as well as import and configure information that provides connectivity to Windows Azure through your subscription. For instructions, see <a href="http://msdn.microsoft.com/en-us/library/windowsazure/jj554332.aspx">Get Started with Windows Azure Cmdlets</a>.</p>
<h3><a name="_Toc358025998"></a>Prepare the Work Environment</h3>
<p>The basic process for running the cmdlets by using the Windows Azure PowerShell command shell is as follows:</p>
<ul>
<li>Upload a Management Certificate. For more information about how to create and upload a management certificate, see <a href="http://msdn.microsoft.com/en-us/library/windowsazure/gg551721.aspx">How to: Manage Management Certificates in Windows Azure</a> and the section entitled &ldquo;Create a Certificate&rdquo;.</li>
<li>Install the Windows Azure module for Windows PowerShell. The install can be found on the <a href="http://www.windowsazure.com/en-us/downloads/?fb=en-us">Windows Azure Downloads</a> page, under Windows.</li>
<li>Finally set the Windows PowerShell execution policy. To achieve this run the <strong>Windows Azure PowerShell </strong>as an Administrator and execute the following command: <br /><span style="font-family: Consolas;">Set-ExecutionPolicy RemoteSigned</span></li>
</ul>
<p>The Windows PowerShell execution policy determines the conditions under which configuration files and scripts are run. The Windows Azure PowerShell cmdlets need the execution policy set to a value of RemoteSigned, Unrestricted, or Bypass.</p>
<h3><a name="_Toc358025999"></a><a name="_Ref356564342"></a>Create a Certificate</h3>
<p>From a Visual Studio Command Prompt run the following:</p>
<p><span style="font-family: Consolas;">makecert -sky exchange -r -n "CN=&lt;CertificateName&gt;" -pe -a sha1 -len 2048 -ss My "&lt;CertificateName&gt;.cer"</span></p>
<p>Where &lt;CertificateName&gt; is the name that you want to use for the certificate. It must have a .cer extension. The command loads the private key into your user store, indicated by the &ldquo;-ss My&rdquo; switch. In the certmgr.msc, it appears in the path of Certificates Current User\Personal\Certificates.</p>
<p>Upload the new certification to Azure:</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/76/metablogapi/1602.image_2B68FA35.png" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-76-metablogapi/1602.image_5F00_2B68FA35.png"><img style="margin: 0px; border: 0px currentcolor; display: inline; background-image: none;" title="image" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/76/metablogapi/1070.image_thumb_5FD9A6B0.png" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-76-metablogapi/1070.image_5F00_thumb_5F00_5FD9A6B0.png" alt="image" width="602" height="467" border="0" /></a></p>
<p>If one needs to connect from several machines the same certificate should be installed into the user store on that machine also.</p>
<h3><a name="_Toc358026000"></a><a name="_Ref358016929"></a>Configure Connectivity</h3>
<p>There are two ways one can configure connectivity between your local workstation and Windows Azure. Either manually by configuring the management certificate and subscription details with the <strong>Set-AzureSubscription</strong> and <strong>Select-AzureSubscription</strong> cmdlets, or automatically by downloading and importing the PublishSettings file from Windows Azure. The settings for Windows Azure PowerShell are stored in: <strong>&lt;user&gt;\AppData\Roaming\Windows Azure PowerShell</strong>.</p>
<p>The PublishSettings file method works well when you are responsible for a limited number of subscriptions, but it adds a certificate to any subscription that you are an administrator or co-administrator for. This can be achieved by:</p>
<ul>
<li>Execute the following command: <br /><span style="font-family: Consolas;">Get-AzurePublishSettingsFile</span></li>
<li>Save the .publishSettings file locally.</li>
<li>Import the downloaded .publishSettings file by running the PowerShell command: <br /><span style="font-family: Consolas;">Import-AzurePublishSettingsFile &lt;publishSettings-file&gt;</span></li>
</ul>
<p>Security note: This file contains an encoded management certificate that will serve as your credentials to administer all aspects of your subscriptions and related services. Store this file in a secure location, or delete it after you follow these steps.</p>
<p>In complex or shared development environments, it might be desirable to manually configure the publish settings and subscription information, including any management certificates, for your Windows Azure subscriptions. This is achieved by running the following script (for the management certificate):</p>
<p><span style="font-family: Consolas;">$mySubID = "&lt;subscritionID&gt;" $certThumbprint = "&lt;Thumbprint&gt;" $myCert = Get-Item cert:\CurrentUser\My\$certThumbprint $mySubName = "&lt;SubscriptionName&gt;" $myStoreAcct = "mydefaultstorageaccount"<br /><br />Set-AzureSubscription -SubscriptionName $mySubName -Certificate $myCert -SubscriptionID $mySubID </span></p>
<p><span style="font-family: Consolas;">Set-AzureSubscription -DefaultSubscription $mySubName <br /> Set-AzureSubscription &ndash;SubscriptionName $mySubName -CurrentStorageAccount $myStoreAcct <br />Select-AzureSubscription -SubscriptionName $mySubName</span></p>
<p>To view the details of the Azure Subscription run the command:</p>
<p><span style="font-family: Consolas;">Get-AzureSubscription</span></p>
<p>This script uses the <strong>Set-AzureSubscription</strong> cmdlet. This cmdlet can be used to configure a default Windows Azure subscription, set a default storage account, or a custom Windows Azure service endpoint.</p>
<p>When managing your cluster and you have multiple subscription it is advisable to set the default subscription.</p>
<h3><a name="_Toc358026001"></a><a name="_Ref356565468"></a>Install HDInsight cmdlets</h3>
<p>Ensure the HDInsight binaries are accessible and loaded. They can be downloaded from:</p>
<p><a href="https://hadoopsdk.codeplex.com/downloads/get/671067">https://hadoopsdk.codeplex.com/downloads/get/671067</a></p>
<p>Once downloaded and unzipped, into an accessible directory, and unblocked (open properties of the archive file and click Unblock), they are ready to be loaded into Azure PowerShell environment:</p>
<p><span style="font-family: Consolas;">Import-Module "C:\ local_folder\Microsoft.WindowsAzure.Management.HDInsight.Cmdlet.dll"</span></p>
<h2><a name="_Toc358026002"></a><a name="_Ref356565162"></a>Cluster Provisioning</h2>
<p>There are various ways one can provision a new cluster. If using the Management Portal there is a simple UX process one can follow:</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/76/metablogapi/1348.image_01955975.png" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-76-metablogapi/1348.image_5F00_01955975.png"><img style="margin: 0px; border: 0px currentcolor; display: inline; background-image: none;" title="image" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/76/metablogapi/8686.image_thumb_3F424131.png" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-76-metablogapi/8686.image_5F00_thumb_5F00_3F424131.png" alt="image" width="602" height="260" border="0" /></a></p>
<p>However for an automated process it is recommended that PowerShell is utilized. In running the script to be presented one has to remember to load the HDInsight management binaries, as outlined in the &ldquo;Install HDInsight cmdlets&rdquo; step.</p>
<p>The following sections present a series of scripts for automating cluster management. Further examples can also be found at <a href="http://hadoopsdk.codeplex.com/wikipage?title=PowerShell%20Cmdlets%20for%20Cluster%20Management&amp;referringTitle=Cluster%20Management">Microsoft .Net SDK for Hadoop - PowerShell Cmdlets for Cluster Management</a>.</p>
<h3><a name="_Toc358026003"></a>Provision Base Cluster</h3>
<p>Firstly one needs access to the subscription Id and Certificate, for the current Azure Subscription, that was registered in the &ldquo;Setting Up Your Environment&rdquo; step.</p>
<p>When running these scripts the italicized variables will first need to be defined.</p>
<ol>
<li>In this version you need to explicitly specify subscription information for cmdlets. The plan is after the cmdlets are integrated with Azure PowerShell tools this step won&rsquo;t be necessary. <br /><span style="font-family: Consolas;">$subid = Get-AzureSubscription -Current | %{ $_.SubscriptionId } <br />$cert = Get-AzureSubscription -Current | %{ $_.Certificate }</span></li>
<li>Get the storage account key so you can use it later. <br /><span style="font-family: Consolas;">$key1 = Get-AzureStorageKey <em>$myStoreAcct </em>| %{ $_.Primary }</span></li>
<li>Finally create the new cluster; the cmdlet will return Cluster object in about 10-15 minutes when cluster is finished provisioning: <br /><span style="font-family: Consolas;">Write-Host "Creating '$numbernodes' Node Cluster named: $clustername" -f yellow <br />New-AzureHDInsightCluster -SubscriptionId $subid -Certificate $cert -Name <em>$clustername</em> -Location "East US" -DefaultStorageAccountName $blobstorage -DefaultStorageAccountKey $key1 -DefaultStorageContainerName <em>$containerdefault</em> -UserName <em>$username</em> -Password <em>$password</em> -ClusterSizeInNodes <em>$numbernodes</em></span></li>
</ol>
<p>As an example a set of variable definitions would be:</p>
<p><span style="font-family: Consolas;">$mySubName = "Windows Azure Subscription Name" <br /> $myStoreAcct = "mystorageaccount" <br /> $blobstorage = "mystorageaccount.blob.core.windows.net" <br /> $containerdefault = "hadooproot" <br /> $clustername = "myhdinsighttest" <br /> $location = "East US" <br /> $numbernodes = 4 <br /> $username = "Admin" <br /> $password = "mypassword"</span></p>
<p>Once the cluster has been created you will see the results displayed on the screen:</p>
<p><span style="font-family: Consolas;">Name : myhdinsighttest <br />ConnectionUrl : </span><a href="https://myhdinsighttest.azurehdinsight.net"><span style="font-family: Consolas;">https://myhdinsighttest.azurehdinsight.net</span></a> <br /><span style="font-family: Consolas;"> State : Running <br />CreateDate : 07/05/2013 14:16:37 <br />UserName : Admin <br /> Location : Admin <br />ClusterSizeInNodes : 4</span></p>
<p>You will also be able to see the cluster in the management portal:</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/76/metablogapi/8004.image_3C4CDC7E.png" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-76-metablogapi/8004.image_5F00_3C4CDC7E.png"><img style="margin: 0px; border: 0px currentcolor; display: inline; background-image: none;" title="image" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/76/metablogapi/8836.image_thumb_5F4D2821.png" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-76-metablogapi/8836.image_5F00_thumb_5F00_5F4D2821.png" alt="image" width="602" height="386" border="0" /></a></p>
<p>If the subscription one is managing is not the configured default, before creating the cluster, one should execute the following command:</p>
<p><span style="font-family: Consolas;">Set-AzureSubscription -DefaultSubscription $mySubName</span></p>
<p>To create a script that enables one to pass in parameters, such as the number of required hosts, you can have as your first few lines in the script the following:</p>
<p><span style="font-family: Consolas;">Param($Hosts = 4, $Cluster = "myhdinsighttest") <br /> $numbernodes = $Hosts <br /> $clustername = $Cluster</span></p>
<p>The script can then be executed as follows (assuming saved as ClusterCreateSimple.ps1):</p>
<p><span style="font-family: Consolas;">. "C:\\Scripts\ClusterCreateSimple.ps1" -Hosts 4 &ndash;Cluster "myhdinsighttest"</span></p>
<p>Of course other variables can also be passed in.</p>
<p>Currently the nodes supporting the creation of an HDInsight cluster are:</p>
<ul>
<li>East US</li>
<li>North Europe</li>
</ul>
<p>More will be added over time.</p>
<h3><a name="_Toc358026004"></a>Manage Your Cluster</h3>
<p>One can view your current clusters within the current subscription using the following command:</p>
<p><span style="font-family: Consolas;">Get-AzureHDInsightCluster -SubscriptionId $subid -Certificate $cert</span></p>
<p>To view the details of a specific cluster use the following command:</p>
<p><span style="font-family: Consolas;">Get-AzureHDInsightCluster $clustername -SubscriptionId $subid -Certificate $cert</span></p>
<p>To delete the cluster, needed for elastic services, run the command:</p>
<p><span style="font-family: Consolas;">Remove-AzureHDInsightCluster $clustername -SubscriptionId $subid -Certificate $cert</span></p>
<p>There are a few words that need to be mentioned around versioning and cluster name.</p>
<p>When a cluster is currently provisioned you will always get the latest build. However if there are breaking changes between your current build and latest build currently executing code could break. Also, when you delete a cluster there is no guarantee that when you recreate the cluster the name will be available. To ensure that your name is not going to be used, abstract cluster names should be utilized.</p>
<h2><a name="_Toc358026006"></a>Job Submission</h2>
<p>In addition to managing the HDInsight cluster the Microsoft .Net SDK for Hadoop also allows one to write MapReduce jobs utilizing the .Net Framework. Under the covers this is employing the Hadoop Streaming interface. The documentation for creating such jobs can be found at:</p>
<p><a href="http://hadoopsdk.codeplex.com/wikipage?title=Map%2fReduce">http://hadoopsdk.codeplex.com/wikipage?title=Map%2fReduce</a></p>
<h3><a name="_Toc358026007"></a>MRRunner Support</h3>
<p>To submit such jobs there is a command-line utility called MRRunner that should be utilized. To support the MRRunner utility you should have an assembly (a .net DLL or EXE) that defines at least one implementation of HadoopJob&lt;&gt;.</p>
<p>If the Dll contains only one implementation of Hadoop&lt;&gt;, you can run the job with:</p>
<p><span style="font-family: Consolas;">MRRunner -dll MyDll</span></p>
<p>If the Dll contains multiple implementations of HadoopJob&lt;&gt;, then you need to indicate the one you wish to run:</p>
<p><span style="font-family: Consolas;">MRRunner -dll MyDll -class MyClass</span></p>
<p>To supply options to your job, pass them as trailing arguments on the command-line, after a double-hyphen:</p>
<p><span style="font-family: Consolas;">MRRunner -dll MyDll -class MyClass -- extraArg1 extraArg2</span></p>
<p>These additional arguments are provided to your job via a context object that is available to all methods on HadoopJob&lt;&gt;.</p>
<h3><a name="_Toc358026008"></a>Local PowerShell Submission</h3>
<p>Of course one can submit jobs directly from a .Net executable and thus PowerShell scripts.</p>
<p>MapReduceJob is a property on Hadoop connection object. It provides access to an implementation of the&nbsp; IStreamingJobExecutor interface which handles the creation and execution of Hadoop Streaming jobs. Depending on how the connection object was created it can execute jobs locally, using Hadoop command-line, or against remote cluster using WebHCat client library.</p>
<p>Under the covers the MRRunner utility will invoke LocalStreamingJobExecutor implementation on your behalf; as discussed.</p>
<p>Alternatively one can invoke the executor directly and request it execute a Hadoop Job:</p>
<p><span style="font-family: Consolas;">var hadoop = Hadoop.Connect(); <br /></span><span style="font-family: Consolas;">hadoop.MapReduceJob.ExecuteJob&lt;JobType&gt;(arguments);</span></p>
<p>Utilizing this approach one can thus integrate Job Submission into cluster management scripts:</p>
<p><span style="font-family: Consolas;"># Define Base Paths <br /> $BasePath = "C:\Users\Me\Projects" <br /> $dllPath = $BasePath + "\WordCountSampleApplication\bin" <br /> $submitterDll = $dllPath + "\WordCount.dll" <br /> $hadoopcmd = $env:HADOOP_HOME + "/bin/hadoop.cmd"</span></p>
<p><span style="font-family: Consolas;"># Add submission file references <br /> Add-Type -Path ($dllPath + "\microsoft.hadoop.client.dll") <br /> Add-Type -Path ($dllPath + "\microsoft.hadoop.mapreduce.dll") <br /> Add-Type -Path ($dllPath + "\microsoft.hadoop.webclient.dll") <br />&nbsp; <br /> # Define the Type for the Job <br /> $submitterAssembly = [System.Reflection.Assembly]::LoadFile($submitterDll) <br /> [Type] $jobType = $submitterAssembly.GetType("WordCountSampleApplication.WordCount", 1) <br />&nbsp; <br /> # Connect and Run the Job <br /> $hadoop = [Microsoft.Hadoop.MapReduce.Hadoop]::Connect() <br /> $job = $hadoop.MapReduceJob <br /> $result = $job.ExecuteJob($jobType)</span></p>
<p><span style="font-family: Consolas;">Write-Host "Job Run Information" <br /> Write-Host "Job Id: " $result.Id <br /> Write-Host "Exit Code: " $result.Info.ExitCode</span></p>
<p>The only challenge becomes managing the generic objects within PowerShell.</p>
<p>In this case I have used a version of the WordCountSample application that comes with the SDK. I have removed the Driver entry point and compiled the assembly into a DLL rather than an EXE.</p>
<h3><a name="_Toc358026009"></a>Remote PowerShell Submission</h3>
<p>Of course the ultimate goal is to submit the job from your management console. The process for this is very similar to the Local submission, with the inclusion of the connection information for the Azure cluster.</p>
<h4><a name="_Toc358026010"></a>Hadoop Job Defined</h4>
<p>In this sample the management objects are used, as in the provisioning case, to obtain the subscription information. It is assumed that the Job definition is contained within the code:</p>
<p><span style="font-family: Consolas;"># Import the management module <br /> Import-Module "C:\Users\Me\Cmdlets\Microsoft.WindowsAzure.Management.HDInsight.Cmdlet.dll" <br />&nbsp; <br /> # Define Local Base Paths <br /> $BasePath = "C:\Users\Me\Projects" <br /> $dllPath = $BasePath + "\WordCountSampleApplication\bin" <br /> $submitterDll = $dllPath + "\WordCount.dll" <br />&nbsp; <br /> # Add submission file references <br /> Add-Type -Path ($dllPath + "\microsoft.hadoop.client.dll") <br /> Add-Type -Path ($dllPath + "\microsoft.hadoop.mapreduce.dll") <br /> Add-Type -Path ($dllPath + "\microsoft.hadoop.webclient.dll") <br />&nbsp; <br /> # Define the Type for the Job <br /> $submitterAssembly = [System.Reflection.Assembly]::LoadFile($submitterDll) <br /> [Type] $jobType = $submitterAssembly.GetType("WordCountSampleApplication.WordCount", 1) <br />&nbsp; <br /> # Define the connection properties <br /> [Uri] $cluster = New-Object System.Uri "</span><a href="https://myclustername.azurehdinsight.net:563&quot;"><span style="font-family: Consolas;">https://myclustername.azurehdinsight.net:563"</span></a> <br /><span style="font-family: Consolas;"> $mySubName = "Windows Azure Subscription Name" <br /> $clusterUsername = "Admin" <br /> $clusterPassword = "mypassword" <br /> $hadoopUsername = "Hadoop" <br /> $storage = "mystorageaccount.blob.core.windows.net" <br /> $containerdefault = "hadooproot" <br /> [Boolean] $createContinerIfNotExist = $True <br />&nbsp; <br /> # Get the storage key <br /> Set-AzureSubscription -DefaultSubscription $mySubName <br /> $key = Get-AzureStorageKey $myStoreAcct | %{ $_.Primary } <br />&nbsp; <br /> # Connect and Run the Job <br /> $hadoop = [Microsoft.Hadoop.MapReduce.Hadoop]::Connect($cluster, $clusterUsername, $hadoopUsername, $clusterPassword, $storage, $key, $containerdefault, $createContinerIfNotExist) <br /> $job = $hadoop.MapReduceJob <br /> $result = $job.ExecuteJob($jobType)</span></p>
<p>To execute the job one needs to define the type for the HadoopJob, which provides the necessary configuration details.</p>
<p>After running this job, from within the management portal, you should see the metrics for the number of mapper and reducers executed be updated:</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/76/metablogapi/6646.image_01E140D0.png" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-76-metablogapi/6646.image_5F00_01E140D0.png"><img style="margin: 0px; border: 0px currentcolor; display: inline; background-image: none;" title="image" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/76/metablogapi/0272.image_thumb_2A9CA619.png" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-76-metablogapi/0272.image_5F00_thumb_5F00_2A9CA619.png" alt="image" width="602" height="299" border="0" /></a></p>
<p>Unfortunately at this point in time you will not see your job within the job history when managing the cluster. For this you will have to use the standard Hadoop UI interfaces; accessible by connecting to the head node of the cluster.</p>
<h4><a name="_Toc358026011"></a>PowerShell Configuration</h4>
<p>If one wants to provide the configuration details for the job within the PowerShell environment then the code is slightly different:</p>
<p><span style="font-family: Consolas;">Param($Cluster = "myhdinsighttest", [string] $InputPath = $(throw "Input Path Required."), $OutputFolder = $(throw "Output Path Required."))</span></p>
<p><span style="font-family: Consolas;"># Import the management module <br /> Import-Module "C:\Users\Me\Cmdlets\Microsoft.WindowsAzure.Management.HDInsight.Cmdlet.dll"</span></p>
<p><span style="font-family: Consolas;"># Define Local Base Paths <br /> $dllPath = "C:\Users\Me\Projects\WordCountSampleApplication\bin" <br /> $submitterDll = $dllPath + "\WordCount.dll"</span></p>
<p><span style="font-family: Consolas;"># Add submission file references <br /> Add-Type -Path ($dllPath + "\microsoft.hadoop.client.dll") <br /> Add-Type -Path ($dllPath + "\microsoft.hadoop.mapreduce.dll") <br /> Add-Type -Path ($dllPath + "\microsoft.hadoop.webclient.dll")</span></p>
<p><span style="font-family: Consolas;"># Define the Types for the Job (Mapper. Reducer, Combiner) <br /> $submitterAssembly = [System.Reflection.Assembly]::LoadFile($submitterDll) <br /> [Type] $mapperType = $submitterAssembly.GetType("WordCountSampleApplication.WordCountMapper", 1) <br /> [Type] $reducerType = $submitterAssembly.GetType("WordCountSampleApplication.WordCountReducer", 1) <br /> [Type] $combinerType = $submitterAssembly.GetType("WordCountSampleApplication.WordCountReducer", 1)</span></p>
<p><span style="font-family: Consolas;"># Define the configuration properties <br /> $config = New-Object Microsoft.Hadoop.MapReduce.HadoopJobConfiguration <br /> [Boolean] $config.Verbose = $True <br /> $config.InputPath = $InputPath <br /> $config.OutputFolder = $OutputFolder <br /> $config.AdditionalGenericArguments.Add("-D""mapred.map.tasks=3""")</span></p>
<p><span style="font-family: Consolas;"># Define the connection properties <br /> $clustername = "</span><a href="https://$Cluster.azurehdinsight.net:563&quot;"><span style="font-family: Consolas;">https://$Cluster.azurehdinsight.net:563"</span></a> <br /><span style="font-family: Consolas;"> [Uri] $clusterUri = New-Object System.Uri $clustername <br /> $mySubName = "Windows Azure Subscription Name" <br /> $clusterUsername = "Admin" <br /> $clusterPassword = "mypassword" <br /> $hadoopUsername = "Hadoop" <br /> $storage = "mystorageaccount.blob.core.windows.net" <br /> $containerdefault = "hadooproot" <br /> [Boolean] $createContinerIfNotExist = $True</span></p>
<p><span style="font-family: Consolas;"># Get the storage key <br /> Set-AzureSubscription -DefaultSubscription $mySubName <br /> $key = Get-AzureStorageKey $myStoreAcct | %{ $_.Primary }</span></p>
<p><span style="font-family: Consolas;"># Connect and Run the Job <br /> $hadoop = [Microsoft.Hadoop.MapReduce.Hadoop]::Connect($cluster, $clusterUsername, $hadoopUsername, $clusterPassword, $storage, $key, $containerdefault, $createContinerIfNotExist) <br /> $job = $hadoop.MapReduceJob <br /> $result = $job.Execute($mapperType, $reducerType, $combinerType, $config)</span></p>
<p>In this instance a HadoopJobConfiguration is created which allows for the definition of input and output files, along with other Job parameters. To execute the Job one has to provide the types for the Mapper, Reducer, and Combiner. If a Reducer or Combiner are not needed then the parameter value of $Null can be used.</p>
<p>One has to remember as we are submitting the job to an Azure cluster the default file system will be ASV. As such, to run this sample I uploaded some text files to the appropriate Azure storage location:</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/76/metablogapi/4721.image_14F247AF.png" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-76-metablogapi/4721.image_5F00_14F247AF.png"><img style="margin: 0px; border: 0px currentcolor; display: inline;" title="image" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/76/metablogapi/7635.image_thumb_4243B7B2.png" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-76-metablogapi/7635.image_5F00_thumb_5F00_4243B7B2.png" alt="image" width="602" height="160" border="0" /></a></p>
<p>To run this script one just needs to execute the following command (assuming saved as ClusterJobConfigSubmission.ps1):</p>
<p><span style="font-family: Consolas;">. "C: \Scripts\ClusterJobConfigSubmission.ps1" &ndash;Cluster &ldquo;clustername&rdquo; -InputPath "/wordcount/input" -OutputFolder "/wordcount/output"</span></p>
<p>The output then will show up in the wordcount/output container.</p>
<h4><a name="_Toc358026012"></a>Providing an Elastic Service</h4>
<p>The idea behind an Elastic Service is that the cluster can be brought up, with the necessary hosts, when Job execution is necessary. To achieve this, with the provided script, one can use the following script:</p>
<p><span style="font-family: Consolas;">Param($Hosts = 4, $Cluster = "clustername", [string] $InputPath = $(throw "Input Path Required."), [string] $OutputFolder = $(throw "Output Path Required."))</span></p>
<p><span style="font-family: Consolas;"># Create the Cluster <br /> . ".\ClusterCreateSimple.ps1" -Hosts $Hosts -Cluster $Cluster</span></p>
<p><span style="font-family: Consolas;"># Execute the Job <br /> . ".\ClusterRemoteJobConfigSubmission.ps1" -Cluster $Cluster -InputPath $InputPath -OutputFolder $OutputFolder</span></p>
<p><span style="font-family: Consolas;"># Delete the Cluster <br /> . ".\ClusterDelete.ps1" -Cluster $Cluster</span></p>
<p>To execute the script one just has to execute (assuming saved as ClusterElasticJobSubmission.ps1):</p>
<p><span style="font-family: Consolas;">. "C:\Scripts\ClusterElasticJobSubmission.ps1" -Hosts 4 -Cluster "clustername" -InputPath "/wordcount/input" -OutputFolder "/wordcount/output"</span></p>
<p>This will create the cluster, run the required job, and then delete the cluster. During the creation the cluster name and number of hosts is specified, and during the job submission the input and output paths are specified. One could of course customize these scripts to include additional parameters such as number of mappers, additional job arguments, etc.</p>
<p>This process is possible because the storage used for input and output is Azure Blob Storage. As such the cluster is only needed for compute operations and not storage.</p>
<p>Further customization of the script allows for the actual Mapper and Reducers to be specified during the submission. If you have previously configured your connectivity, as outlined in the Configure Connectivity section, with a default subscription:</p>
<p><span style="font-family: Consolas;">Set-AzureSubscription -DefaultSubscription $mySubName</span></p>
<p>Then when submitting Jobs you can derive most of the necessary subscription information and have a very general Job submission utility that uses parameters for all job specific configurations:</p>
<p><span style="font-family: Consolas;">Param($Hosts = 4, $Cluster = "myclustername", [string] $StorageContainer = $(throw "Storage Container Required."), [string] $InputPath = $(throw "Input Path Required."), [string] $OutputFolder = $(throw "Output Path Required."), [string] $Dll = $(throw "MapReduce Dll Path Required."), [string] $Mapper = $(throw "Mapper Type Required."), [string] $Reducer = $Null, [string] $Combiner = $Null)</span></p>
<p><span style="font-family: Consolas;"># TODO Set to location of cmdlets <br /> $modulePath = "C:\Users\Me\Cmdlets"</span></p>
<p><span style="font-family: Consolas;"># Define all local variable names for script execution <br /> $submitterDll = $Dll <br /> $dllPath = Split-Path $submitterDll -parent <br /> $dllFile = Split-Path $submitterDll -leaf</span></p>
<p><span style="font-family: Consolas;"># Import the management module and set file references <br /> Import-Module "$modulePath\Microsoft.WindowsAzure.Management.HDInsight.Cmdlet.dll" <br /> Add-Type -Path ($dllPath + "\microsoft.hadoop.client.dll") <br /> Add-Type -Path ($dllPath + "\microsoft.hadoop.mapreduce.dll") <br /> Add-Type -Path ($dllPath + "\microsoft.hadoop.webclient.dll")</span></p>
<p><span style="font-family: Consolas;"># Get the subscription information and set variables <br /> $subscriptionInfo = Get-AzureSubscription -Current <br /> $subName = $subscriptionInfo | %{ $_.SubscriptionName } <br /> $subId = $subscriptionInfo | %{ $_.SubscriptionId } <br /> $cert = $subscriptionInfo | %{ $_.Certificate } <br /> $storeAccount = $subscriptionInfo | %{ $_.CurrentStorageAccount } <br /> $key = Get-AzureStorageKey $storeAccount | %{ $_.Primary } <br /> $storageAccountInfo = Get-AzureStorageAccount $storeAccount <br /> $location = $storageAccountInfo | %{ $_.Location }</span></p>
<p><span style="font-family: Consolas;">$clusterUsername = "Admin" <br /> $clusterPassword = [System.Web.Security.Membership]::GeneratePassword(20, 5) <br /> $hadoopUsername = "Hadoop" <br /> $clusterName = $Cluster <br /> $containerDefault = $StorageContainer <br /> $numberNodes = $Hosts <br /> $clusterHttp = "</span><a href="https://$clusterName.azurehdinsight.net:563&quot;"><span style="font-family: Consolas;">https://$clusterName.azurehdinsight.net:563"</span></a> <br /><span style="font-family: Consolas;"> $blobStorage = "$storeAccount.blob.core.windows.net"</span></p>
<p><span style="font-family: Consolas;"># Create the cluster <br /> Write-Host "Creating '$numbernodes' Node Cluster named: $clusterName" -f yellow <br /> Write-Host "Storage Account '$storeAccount' and Container '$containerDefault'" -f yellow <br /> Write-Host "User '$clusterUsername' Password '$clusterPassword'" -f green <br /> New-AzureHDInsightCluster -SubscriptionId $subId -Certificate $cert -Name $clusterName -Location $location -DefaultStorageAccountName $blobStorage -DefaultStorageAccountKey $key -DefaultStorageContainerName $containerDefault -UserName $clusterUsername -Password $clusterPassword -ClusterSizeInNodes $numberNodes <br /> Write-Host "Created '$numbernodes' Node Cluster: $clusterName" -f yellow</span></p>
<p><span style="font-family: Consolas;"># Define the Types for the Job (Mapper. Reducer, Combiner) <br /> $submitterAssembly = [System.Reflection.Assembly]::LoadFile($submitterDll) <br /> [Type] $mapperType = $submitterAssembly.GetType($Mapper, 1) <br /> [Type] $reducerType = $Null <br /> if ($Reducer) { $reducerType = $submitterAssembly.GetType($Reducer, 1) } <br /> [Type] $combinerType = $Null <br /> if ($Combiner) { $combinerType = $submitterAssembly.GetType($Combiner, 1) }</span></p>
<p><span style="font-family: Consolas;"># Define the configuration properties <br /> $config = New-Object Microsoft.Hadoop.MapReduce.HadoopJobConfiguration <br /> [Boolean] $config.Verbose = $True <br /> $config.InputPath = $InputPath <br /> $config.OutputFolder = $OutputFolder <br /> $config.AdditionalGenericArguments.Add("-D""mapred.map.tasks=3""")</span></p>
<p><span style="font-family: Consolas;"># Define the connection properties <br /> [Boolean] $createContinerIfNotExist = $True <br /> [Uri] $clusterUri = New-Object System.Uri $clusterHttp</span></p>
<p><span style="font-family: Consolas;"># Connect and Run the Job <br /> Write-Host "Executing Job Dll '$dllFile' on Cluster $clusterName" -f yellow</span></p>
<p><span style="font-family: Consolas;">$hadoop = [Microsoft.Hadoop.MapReduce.Hadoop]::Connect($clusterUri, $clusterUsername, $hadoopUsername, $clusterPassword, $blobStorage, $key, $containerDefault, $createContinerIfNotExist) <br /> $job = $hadoop.MapReduceJob <br /> $result = $job.Execute($mapperType, $reducerType, $combinerType, $config)</span></p>
<p><span style="font-family: Consolas;">Write-Host "Job Run Information" -f yellow <br /> Write-Host "Job Id: " $result.Id <br /> Write-Host "Exit Code: " $result.Info.ExitCode</span></p>
<p><span style="font-family: Consolas;"># Finally delete the cluster <br /> Write-Host "Deleting Cluster named: $clusterName" -f yellow <br /> Remove-AzureHDInsightCluster $clusterName -SubscriptionId $subId -Certificate $cert <br /> Write-Host "Cluster $clusterName Deleted" -f yellow</span></p>
<p>In this instance to run the job one would use the following command (assuming saved as ClusterElasticJobConfigSubmissionAuto.ps1):</p>
<p><span style="font-family: Consolas;">. "C:\Users\Me\Scripts\ClusterElasticJobConfigSubmissionAuto.ps1" -Hosts 4 -Cluster "myclustername" -StorageContainer "hadooproot" -InputPath "/wordcount/input" -OutputFolder "/wordcount/output" -Dll "C:\Users\Me\bin\WordCount.dll" -Mapper "WordCountSampleApplication.WordCountMapper" -Reducer "WordCountSampleApplication.WordCountReducer" -Combiner "WordCountSampleApplication.WordCountReducer"</span></p>
<p>In this instance the only additional piece of information needed, giving you a completely reusable script, is the default storage container for the storage account. The script also generates a password, which is displayed in case the cluster is not successfully deleted after the job execution.</p>
<p>Of course other parameters could be included in this model to make the PowerShell script more applicable to more Job types.</p>
<h3><a name="_Toc358026013"></a>Specifying ASV Paths</h3>
<p>As mentioned the default schema when submitting MapReduce Jobs is Azure Blob Storage. This means the default container specified when creating the cluster is used as the root.</p>
<p>In the example above the path &ldquo;/wordcount/input&rdquo; actually equates to (assuming a storage account of &ldquo;dataplatformrdpcarl&rdquo;):</p>
<p><span style="font-family: Consolas;">asv://hadooproot@dataplatformrdpcarl.blob.core.windows.net/wordcount/input</span></p>
<p>Thus if one wanted to operate out of a different default container such as &ldquo;data&rdquo; fully-qualified names could be specified such as:</p>
<p><span style="font-family: Consolas;">-InputPath "asv://data@dataplatformrdpcarl.blob.core.windows.net/books" <br /></span><span style="font-family: Consolas;">-OutputFolder "asv://data@dataplatformrdpcarl.blob.core.windows.net/wordcount/output"</span></p>
<p>This would equate to the &ldquo;data&rdquo; container:</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/76/metablogapi/8078.image_51B6A3B4.png" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-76-metablogapi/8078.image_5F00_51B6A3B4.png"><img style="margin: 0px; border: 0px currentcolor; display: inline; background-image: none;" title="image" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/76/metablogapi/0804.image_thumb_333C3CFE.png" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-76-metablogapi/0804.image_5F00_thumb_5F00_333C3CFE.png" alt="image" width="602" height="165" border="0" /></a></p>
<p>The containers being referenced just need to be under the same storage account.</p>
<h2><a name="_Toc358026014"></a>References</h2>
<p><a href="http://msdn.microsoft.com/en-us/library/windowsazure/jj156055.aspx">Windows Azure PowerShell</a>.</p>
<p><a href="http://msdn.microsoft.com/en-us/library/windowsazure/jj554332.aspx">Get Started with Windows Azure Cmdlets</a>.</p>
<p><a href="http://hadoopsdk.codeplex.com/wikipage?title=PowerShell%20Cmdlets%20for%20Cluster%20Management&amp;referringTitle=Cluster%20Management">Microsoft .Net SDK for Hadoop - PowerShell Cmdlets for Cluster Management</a>.</p>
<p><a href="http://msdn.microsoft.com/en-us/library/windowsazure/gg551721.aspx">How to: Manage Management Certificates in Windows Azure</a>.</p>
<p><a href="http://www.windowsazure.com/en-us/downloads/?fb=en-us">Windows Azure Downloads</a>.</p>
<p><a href="https://hadoopsdk.codeplex.com/downloads/get/671067">HDInsight cmdlets</a>.</p>
<p><a href="http://blogs.msdn.com/b/windowsazure/archive/2013/02/07/windows-azure-sql-database-management-with-powershell.aspx">Windows Azure SQL Database Management with PowerShell</a>.</p>
<p><a href="http://hadoopsdk.codeplex.com/wikipage?title=Map%2fReduce">Microsoft .Net SDK for Hadoop - Map/Reduce</a>.</p>
<p><a href="https://hadoopsdk.codeplex.com/downloads/get/690770">Managing HDInsight with PowerShell Download</a></p>]]></content:encoded>
    </item>
    <item>
      <title>Hadoop .Net HDFS File Access</title>
      <link>https://docs.microsoft.com/archive/blogs/carlnol/hadoop-net-hdfs-file-access</link>
      <pubDate>Fri, 08 Feb 2013 10:22:45 GMT</pubDate>
      <dc:creator><![CDATA[Carl Nolan]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.cloudplatform.qa.msdn.wdslab.com/carlnol/2013/02/08/hadoop-net-hdfs-file-access/</guid>
      <description><![CDATA[Provided with the Microsoft Distribution of Hadoop, HDInsight, is a C library for HDFS file access....]]></description>
      <content:encoded><![CDATA[<p>Provided with the Microsoft Distribution of Hadoop, HDInsight, is a C library for HDFS file access. This code extends this library through a Managed C++ solution. This solution enables one to consume HDFS files from within a .Net environment. The purpose of this post is first to ensure folks know about the new Windows HDFS Managed library (WinHdfsManaged) provided alongside the native C library, and secondly to give a few samples of its usage from C#.</p>  <p>The complete code libraries can be downloaded from here: <a title="http://code.msdn.microsoft.com/Hadoop-Net-HDFS-File-Access-18e9bbee" href="http://code.msdn.microsoft.com/Hadoop-Net-HDFS-File-Access-18e9bbee">http://code.msdn.microsoft.com/Hadoop-Net-HDFS-File-Access-18e9bbee</a>&#160;</p>  <h3>Class Structure</h3>  <p>Let’s start with a simple class diagram of the Win HDFS Managed library:</p>  <p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/76/metablogapi/7288.WinHdfsManagedModel_3F8147B3.png" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-76-metablogapi/7288.WinHdfsManagedModel_5F00_3F8147B3.png"><img style="border: 0px currentcolor; display: inline; background-image: none;" title="WinHdfsManagedModel" border="0" alt="WinHdfsManagedModel" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/76/metablogapi/2388.WinHdfsManagedModel_thumb_0D49D19C.png" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-76-metablogapi/2388.WinHdfsManagedModel_5F00_thumb_5F00_0D49D19C.png" width="650" height="656" /></a></p>  <p>The main premise is that the HdfsFileSystem is your starting point, from which one can acquire a HdfsFileStream or a HdfsFileHandle. From the HdfsFileStream you can perform operations one would normally expect when working with .Net Streams. From the HdfsFileHandle you can perform operations analogous to normal HDFS file operations.</p>  <p>For brevity I have excluded samples using the HdfsFileHandle. So let’s run through some sample file operations.</p>  <h3>Directory Operations</h3>  <p>As in all operations one firstly needs to get a connection to the HDFS cluster. This is achieved by calling a Connect() method and specifying the host, name or IP address, and access port:</p>  <div style="margin: 0px; padding: 0px; float: none; display: inline;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:dd355403-2e42-4f0c-a9e4-6cb323f7bd94" class="wlWriterSmartContent">   <div class="code-container">     <div class="code-titleblock">Create File System Access</div>      <div style="background: rgb(255, 255, 255); overflow: auto;">       <ol style="background: rgb(222, 222, 222); margin: 0px; padding: 0px 0px 0px 5px; white-space: nowrap;">         <li><span style="color: rgb(0, 0, 255);">using</span> (<span style="color: rgb(43, 145, 175);">HdfsFileSystem</span> hdfsSystem = <span style="color: rgb(43, 145, 175);">HdfsFileSystem</span>.Connect(<span style="color: rgb(163, 21, 21);">&quot;127.0.0.1&quot;</span>, 9000))</li>          <li class="code-even">{</li>          <li>&#160;&#160;&#160; ...</li>          <li class="code-even">}</li>       </ol>     </div>   </div> </div>  <p>Once one has the connection one can then easily perform a directory traversal to enquire into the files and directories:</p>  <div style="margin: 0px; padding: 0px; float: none; display: inline;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:e0bc866c-c9ad-460d-8049-d78534ccf0ab" class="wlWriterSmartContent">   <div class="code-container">     <div class="code-titleblock">List Directory Structure</div>      <div style="background: rgb(255, 255, 255); overflow: auto;">       <ol style="background: rgb(222, 222, 222); margin: 0px; padding: 0px 0px 0px 5px; white-space: nowrap;">         <li><span style="color: rgb(43, 145, 175);">Action</span>&lt;<span style="color: rgb(0, 0, 255);">string</span>&gt; processDirectory = <span style="color: rgb(0, 0, 255);">null</span>;</li>          <li class="code-even">processDirectory = (looppath) =&gt;</li>          <li>{</li>          <li class="code-even">&#160;&#160;&#160; <span style="color: rgb(0, 0, 255);">using</span> (<span style="color: rgb(43, 145, 175);">HdfsFileInfoEntries</span> entries = hdfsSystem.ListDirectory(looppath))</li>          <li>&#160;&#160;&#160; {</li>          <li class="code-even">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0, 0, 255);">foreach</span> (<span style="color: rgb(43, 145, 175);">HdfsFileInfoEntry</span> entry <span style="color: rgb(0, 0, 255);">in</span> entries.Entries)</li>          <li>&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</li>          <li class="code-even">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0, 0, 255);">string</span> kind = entry.Kind == <span style="color: rgb(43, 145, 175);">HdfsFileInfoEntryKind</span>.Directory ? <span style="color: rgb(163, 21, 21);">&quot;Directory&quot;</span> : <span style="color: rgb(163, 21, 21);">&quot;\tFile&quot;</span>;</li>          <li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(43, 145, 175);">Console</span>.WriteLine(<span style="color: rgb(0, 0, 255);">string</span>.Format(<span style="color: rgb(163, 21, 21);">@&quot;{0}:&quot;&quot;{1}&quot;&quot;, Modified/Accessed:&quot;&quot;{2:G}, {3:G}&quot;&quot;, Owner:&quot;&quot;{4}&quot;&quot;&quot;</span>, kind, entry.Name, entry.LastModified, entry.LastAccessed, entry.Owner));</li>          <li class="code-even">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0, 0, 255);">if</span> (entry.Kind == <span style="color: rgb(43, 145, 175);">HdfsFileInfoEntryKind</span>.Directory)</li>          <li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</li>          <li class="code-even">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; processDirectory(entry.Name);</li>          <li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</li>          <li class="code-even">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</li>          <li>&#160;&#160;&#160; }</li>          <li class="code-even">};</li>          <li>processDirectory(hdfspath)</li>       </ol>     </div>   </div> </div>  <p>Here is a sample output created from the test application:</p>  <div style="margin: 0px; padding: 0px; float: none; display: inline;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:864e0f0e-7989-4194-9c35-15f1eaa7a2c6" class="wlWriterSmartContent">   <div class="code-container">     <div style="background: rgb(255, 255, 255); overflow: auto;">       <ol style="background: rgb(222, 222, 222); margin: 0px; padding: 0px 0px 0px 5px; white-space: nowrap;">         <li>Directory:&quot;hdfs://127.0.0.1:9000/user/isotope/qwanchi&quot;, Modified/Accessed:&quot;30/01/2012 20:46:38, 01/01/1970 00:00:00&quot;, Owner:&quot;isotope&quot;</li>          <li class="code-even">&#160;&#160;&#160;&#160;&#160;&#160;&#160; File:&quot;hdfs://127.0.0.1:9000/user/isotope/qwanchi/MobileSampleData.txt&quot;, Modified/Accessed:&quot;30/01/2012 20:46:38, 30/01/2012 20:46:38&quot;, Owner:&quot;isotope&quot;</li>          <li>Directory:&quot;hdfs://127.0.0.1:9000/user/isotope/qwanchi/duplicate&quot;, Modified/Accessed:&quot;30/01/2012 20:46:38, 01/01/1970 00:00:00&quot;, Owner:&quot;isotope&quot;</li>          <li class="code-even">&#160;&#160;&#160;&#160;&#160;&#160;&#160; File:&quot;hdfs://127.0.0.1:9000/user/isotope/qwanchi/duplicate/testdata.txt&quot;, Modified/Accessed:&quot;30/01/2012 20:46:38, 30/01/2012 20:46:38&quot;, Owner:&quot;isotope&quot;</li>          <li>&#160;&#160;&#160;&#160;&#160;&#160;&#160; File:&quot;hdfs://127.0.0.1:9000/user/isotope/qwanchi/testdata.txt&quot;, Modified/Accessed:&quot;28/01/2012 20:46:38, 29/01/2012 20:46:38&quot;, Owner:&quot;isotope&quot;</li>       </ol>     </div>   </div> </div>  <p>In addition to getting directory information one can also query on a file or directory directly: </p>  <div style="margin: 0px; padding: 0px; float: none; display: inline;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:8eedb2cf-db9d-4b83-8d4e-56546d427a60" class="wlWriterSmartContent">   <div class="code-container">     <div class="code-titleblock">Get Path Information</div>      <div style="background: rgb(255, 255, 255); overflow: auto;">       <ol style="background: rgb(222, 222, 222); margin: 0px; padding: 0px 0px 0px 5px; white-space: nowrap;">         <li>hdfsSystem.SetWorkingDirectory(hdfspath);</li>          <li class="code-even"><span style="color: rgb(0, 0, 255);">using</span> (<span style="color: rgb(43, 145, 175);">HdfsFileInfoEntry</span> pathinfo = hdfsSystem.GetPathInfo(hdfspath))</li>          <li>{</li>          <li class="code-even">&#160;&#160;&#160; <span style="color: rgb(0, 0, 255);">if</span> (pathinfo != <span style="color: rgb(0, 0, 255);">null</span>)</li>          <li>&#160;&#160;&#160; {</li>          <li class="code-even">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0, 0, 255);">string</span> kind = pathinfo.Kind == <span style="color: rgb(43, 145, 175);">HdfsFileInfoEntryKind</span>.Directory ? <span style="color: rgb(163, 21, 21);">&quot;Directory&quot;</span> : <span style="color: rgb(163, 21, 21);">&quot;\tFile&quot;</span>;</li>          <li>&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(43, 145, 175);">Console</span>.WriteLine(<span style="color: rgb(0, 0, 255);">string</span>.Format(<span style="color: rgb(163, 21, 21);">@&quot;{0}:&quot;&quot;{1}&quot;&quot;, Modified/Accessed:&quot;&quot;{2:G}, {3:G}&quot;&quot;, Owner:&quot;&quot;{4}&quot;&quot;&quot;</span>, kind, pathinfo.Name, pathinfo.LastModified, pathinfo.LastAccessed, pathinfo.Owner));</li>          <li class="code-even">&#160;&#160;&#160; }</li>          <li>}</li>       </ol>     </div>   </div> </div>  <p>The HdfsFileSystem class also supports other operations such as copying and moving files, file renaming, deleting files, modifying security, checking a file exists, etc. The copy and move operations support copying and moving these files between systems.</p>  <p>So now onto creating and reading files.</p>  <h3>Reading Files</h3>  <p>Processing HDFS files is not that dissimilar from normal .Net file operations. Once one has opened a file for reading, operations are available for operations such as reading a byte, line, or block of bytes:</p>  <div style="margin: 0px; padding: 0px; float: none; display: inline;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:5ff77374-9fc8-4daa-ae08-dae558399feb" class="wlWriterSmartContent">   <div class="code-container">     <div class="code-titleblock">Reading Stream File Data</div>      <div style="background: rgb(255, 255, 255); overflow: auto;">       <ol style="background: rgb(222, 222, 222); margin: 0px; padding: 0px 0px 0px 5px; white-space: nowrap;">         <li><span style="color: rgb(0, 0, 255);">using</span> (<span style="color: rgb(43, 145, 175);">HdfsFileStream</span> file = hdfsSystem.OpenFileStream(filename, <span style="color: rgb(43, 145, 175);">HdfsFileAccess</span>.Write, chunksize))</li>          <li class="code-even">{</li>          <li>&#160;&#160;&#160; file.Write(dataBytes, 0, data.Length);</li>          <li class="code-even">&#160;&#160;&#160; file.WriteByte((<span style="color: rgb(0, 0, 255);">byte</span>)47);</li>          <li>&#160;&#160;&#160; file.Flush();</li>          <li class="code-even">}</li>       </ol>     </div>   </div> </div>  <p>The OpenFile operations support parameter overrides for the file block size and replication factors, whereas a value of zero implies the default values will be used.</p>  <p>If one wants to read the full contents of a file into a second Stream, the HdfsFileStream makes this a simple process:</p>  <div style="margin: 0px; padding: 0px; float: none; display: inline;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:d86c5c70-9150-46da-87bf-8cfbee2e428d" class="wlWriterSmartContent">   <div class="code-container">     <div class="code-titleblock">Reading a File by Stream</div>      <div style="background: rgb(255, 255, 255); overflow: auto;">       <ol style="background: rgb(222, 222, 222); margin: 0px; padding: 0px 0px 0px 5px; white-space: nowrap;">         <li><span style="color: rgb(0, 0, 255);">using</span> (<span style="color: rgb(43, 145, 175);">HdfsFileStream</span> hdfsStream = hdfsSystem.OpenFileStream(localhdfsfilename, <span style="color: rgb(43, 145, 175);">HdfsFileAccess</span>.Read))</li>          <li class="code-even">{</li>          <li>&#160;&#160;&#160; <span style="color: rgb(0, 0, 255);">using</span> (<span style="color: rgb(43, 145, 175);">FileStream</span> fileStream = <span style="color: rgb(0, 0, 255);">new</span> <span style="color: rgb(43, 145, 175);">FileStream</span>(localfilestream, <span style="color: rgb(43, 145, 175);">FileMode</span>.Create, <span style="color: rgb(43, 145, 175);">FileAccess</span>.Write))</li>          <li class="code-even">&#160;&#160;&#160; {</li>          <li>&#160;&#160;&#160;&#160;&#160;&#160;&#160; hdfsStream.CopyTo(fileStream);</li>          <li class="code-even">&#160;&#160;&#160; }</li>          <li>}</li>       </ol>     </div>   </div> </div>  <p>There are other options available for reading the full contents of a file. The first option is to perform a ReadLine() until a null is returned, processed using a StreamReader:</p>  <div style="margin: 0px; padding: 0px; float: none; display: inline;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:c4c63b1e-c1a1-4fc8-babd-ce9fb8e49e92" class="wlWriterSmartContent">   <div class="code-container">     <div class="code-titleblock">Writing a HDFS to Local Stream</div>      <div style="background: rgb(255, 255, 255); overflow: auto;">       <ol style="background: rgb(222, 222, 222); margin: 0px; padding: 0px 0px 0px 5px; white-space: nowrap;">         <li><span style="color: rgb(0, 0, 255);">using</span> (<span style="color: rgb(43, 145, 175);">StreamReader</span> reader = <span style="color: rgb(0, 0, 255);">new</span> <span style="color: rgb(43, 145, 175);">StreamReader</span>(hdfsSystem.OpenFileStream(localhdfsfilename, <span style="color: rgb(43, 145, 175);">HdfsFileAccess</span>.Read, chunksize)))</li>          <li class="code-even">{</li>          <li>&#160;&#160;&#160; <span style="color: rgb(0, 0, 255);">using</span> (<span style="color: rgb(43, 145, 175);">StreamWriter</span> writer = <span style="color: rgb(0, 0, 255);">new</span> <span style="color: rgb(43, 145, 175);">StreamWriter</span>(localfileline, <span style="color: rgb(0, 0, 255);">false</span>, <span style="color: rgb(43, 145, 175);">Encoding</span>.UTF8))</li>          <li class="code-even">&#160;&#160;&#160; {</li>          <li>&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0, 0, 255);">string</span> line;</li>          <li class="code-even">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0, 0, 255);">while</span> ((line = reader.ReadLine()) != <span style="color: rgb(0, 0, 255);">null</span>)</li>          <li>&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</li>          <li class="code-even">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; writer.WriteLine(line);</li>          <li>&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</li>          <li class="code-even">&#160;&#160;&#160; }</li>          <li>}</li>       </ol>     </div>   </div> </div>  <p>Alternatively, for more efficient reading of files, one can read the blocks of data into a byte array: </p>  <div style="margin: 0px; padding: 0px; float: none; display: inline;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:b02ffd15-4450-462e-982f-c506d7da4746" class="wlWriterSmartContent">   <div class="code-container">     <div class="code-titleblock">Reading a File in Bytes</div>      <div style="background: rgb(255, 255, 255); overflow: auto;">       <ol style="background: rgb(222, 222, 222); margin: 0px; padding: 0px 0px 0px 5px; white-space: nowrap;">         <li><span style="color: rgb(0, 0, 255);">using</span> (<span style="color: rgb(43, 145, 175);">HdfsFileStream</span> file = hdfsSystem.OpenFileStream(filename, <span style="color: rgb(43, 145, 175);">HdfsFileAccess</span>.Read))</li>          <li class="code-even">{</li>          <li>&#160;&#160;&#160; <span style="color: rgb(0, 0, 255);">while</span> ((chunk = file.Read(readBytes, 0, chunksize)) &gt; 0)</li>          <li class="code-even">&#160;&#160;&#160; {</li>          <li>&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(43, 145, 175);">Console</span>.Write(<span style="color: rgb(43, 145, 175);">Encoding</span>.UTF8.GetString(readBytes, 0, chunk));</li>          <li class="code-even">&#160;&#160;&#160; }</li>          <li>}</li>       </ol>     </div>   </div> </div>  <p>Other operations that are supported are PositionalReadByte(), PositionalReadBytes(), and Seek(). These operations allow reading the contents of a file from specific positions.</p>  <p>One final sample worth noting is copying a HDFS file to a local file using byte reads:</p>  <div style="margin: 0px; padding: 0px; float: none; display: inline;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:497129e8-70a2-4c91-a52c-a5d599f4002d" class="wlWriterSmartContent">   <div class="code-container">     <div class="code-titleblock">Writing a HDFS to Local File</div>      <div style="background: rgb(255, 255, 255); overflow: auto;">       <ol style="background: rgb(222, 222, 222); margin: 0px; padding: 0px 0px 0px 5px; white-space: nowrap;">         <li><span style="color: rgb(0, 0, 255);">using</span> (<span style="color: rgb(43, 145, 175);">HdfsFileStream</span> file = hdfsSystem.OpenFileStream(localhdfsfilename, <span style="color: rgb(43, 145, 175);">HdfsFileAccess</span>.Read, chunksize))</li>          <li class="code-even">{</li>          <li>&#160;&#160;&#160; <span style="color: rgb(0, 0, 255);">using</span> (<span style="color: rgb(43, 145, 175);">FileStream</span> stream = <span style="color: rgb(0, 0, 255);">new</span> <span style="color: rgb(43, 145, 175);">FileStream</span>(localfilewrite, <span style="color: rgb(43, 145, 175);">FileMode</span>.Create, <span style="color: rgb(43, 145, 175);">FileAccess</span>.Write))</li>          <li class="code-even">&#160;&#160;&#160; {</li>          <li>&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0, 0, 255);">while</span> ((chunk = file.Read(readBytes, 0, chunksize)) &gt; 0)</li>          <li class="code-even">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</li>          <li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; stream.Write(readBytes, 0, chunk);</li>          <li class="code-even">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</li>          <li>&#160;&#160;&#160; }</li>          <li class="code-even">}</li>       </ol>     </div>   </div> </div>  <p>The reason a chunk size is specified in this case is to sync the size being used for HDFS file access to the byte array used for writing the local file.</p>  <p>If one has a Stream reference one can also get the associated file information:</p>  <div style="margin: 0px; padding: 0px; float: none; display: inline;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:bcd7a0bf-9862-4b8b-b81d-d6a203ab717d" class="wlWriterSmartContent">   <div class="code-container">     <div class="code-titleblock">Get File Information</div>      <div style="background: rgb(255, 255, 255); overflow: auto;">       <ol style="background: rgb(222, 222, 222); margin: 0px; padding: 0px 0px 0px 5px; white-space: nowrap;">         <li><span style="color: rgb(43, 145, 175);">HdfsFileInfoEntry</span> fileinfo = file.GetInformation();</li>          <li class="code-even"><span style="color: rgb(0, 0, 255);">if</span> (fileinfo != <span style="color: rgb(0, 0, 255);">null</span>)</li>          <li>{</li>          <li class="code-even">&#160;&#160;&#160; <span style="color: rgb(43, 145, 175);">Console</span>.WriteLine(<span style="color: rgb(0, 0, 255);">string</span>.Format(<span style="color: rgb(163, 21, 21);">@&quot;'{0}', Modified/Accessed:&quot;&quot;{1:G}, {2:G}&quot;&quot;, Owner:&quot;&quot;{3}&quot;&quot;&quot;</span>, fileinfo.Name, fileinfo.LastModified, fileinfo.LastAccessed, fileinfo.Owner));</li>          <li>}</li>       </ol>     </div>   </div> </div>  <p>Also one can modify the file properties:</p>  <div style="margin: 0px; padding: 0px; float: none; display: inline;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:72319b8a-dee1-4743-ae3f-acd48fc0709f" class="wlWriterSmartContent">   <div class="code-container">     <div class="code-titleblock">Modifying File Properties</div>      <div style="background: rgb(255, 255, 255); overflow: auto;">       <ol style="background: rgb(222, 222, 222); margin: 0px; padding: 0px 0px 0px 5px; white-space: nowrap;">         <li>file.Chown(<span style="color: rgb(163, 21, 21);">&quot;isotope&quot;</span>, <span style="color: rgb(0, 0, 255);">null</span>);</li>          <li class="code-even">file.SetTimes(<span style="color: rgb(43, 145, 175);">DateTime</span>.Now.AddDays(-2), <span style="color: rgb(43, 145, 175);">DateTime</span>.Now.AddDays(-1));</li>       </ol>     </div>   </div> </div>  <p>So now onto writing files.</p>  <h3>Writing Files</h3>  <p>As in the case for reading, writing operations are supported for writing a byte, line, and block of bytes:</p>  <div style="margin: 0px; padding: 0px; float: none; display: inline;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:f16a0d78-4699-48bb-b12d-5ee8c312cff3" class="wlWriterSmartContent">   <div class="code-container">     <div class="code-titleblock">Writing File Stream Data</div>      <div style="background: rgb(255, 255, 255); overflow: auto;">       <ol style="background: rgb(222, 222, 222); margin: 0px; padding: 0px 0px 0px 5px; white-space: nowrap;">         <li><span style="color: rgb(0, 0, 255);">using</span> (<span style="color: rgb(43, 145, 175);">HdfsFileStream</span> file = hdfsSystem.OpenFileStream(filename, <span style="color: rgb(43, 145, 175);">HdfsFileAccess</span>.Write, chunksize))</li>          <li class="code-even">{</li>          <li>&#160;&#160;&#160; file.Write(dataBytes, 0, data.Length);</li>          <li class="code-even">&#160;&#160;&#160; file.WriteByte((<span style="color: rgb(0, 0, 255);">byte</span>)47);</li>          <li>&#160;&#160;&#160; file.Flush();</li>          <li class="code-even">}</li>       </ol>     </div>   </div> </div>  <p>The chunk size when opening a file is set to correspond to the size of the buffer used for writing the data.</p>  <p>As in the reading case, if one wants to copy a file from the local file system to an HDFS file one would write:</p>  <div style="margin: 0px; padding: 0px; float: none; display: inline;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:9efc58ad-50a1-448c-9ec2-c026662d926b" class="wlWriterSmartContent">   <div class="code-container">     <div class="code-titleblock">Writing a Local to HDFS Stream</div>      <div style="background: rgb(255, 255, 255); overflow: auto;">       <ol style="background: rgb(222, 222, 222); margin: 0px; padding: 0px 0px 0px 5px; white-space: nowrap;">         <li><span style="color: rgb(0, 0, 255);">using</span> (<span style="color: rgb(43, 145, 175);">HdfsFileStream</span> file = hdfsSystem.OpenFileStream(localhdfsfilename, <span style="color: rgb(43, 145, 175);">HdfsFileAccess</span>.Write, chunksize))</li>          <li class="code-even">{</li>          <li>&#160;&#160;&#160; <span style="color: rgb(0, 0, 255);">using</span> (<span style="color: rgb(43, 145, 175);">FileStream</span> stream = <span style="color: rgb(0, 0, 255);">new</span> <span style="color: rgb(43, 145, 175);">FileStream</span>(localfilepath, <span style="color: rgb(43, 145, 175);">FileMode</span>.Open, <span style="color: rgb(43, 145, 175);">FileAccess</span>.Read))</li>          <li class="code-even">&#160;&#160;&#160; {</li>          <li>&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: rgb(0, 0, 255);">while</span> ((chunk = stream.Read(localbytes, 0, chunksize)) &gt; 0)</li>          <li class="code-even">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</li>          <li>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; file.Write(localbytes, 0, chunk);</li>          <li class="code-even">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</li>          <li>&#160;&#160;&#160; }</li>          <li class="code-even">&#160;</li>          <li>&#160;&#160;&#160; file.Flush();</li>          <li class="code-even">}</li>       </ol>     </div>   </div> </div>  <p>All one has to do is read, in byte chunks, data from the local file and write the corresponding bytes to the HDFS file.</p>  <p>Of course one can also use the CopyTo operation:</p>  <div style="margin: 0px; padding: 0px; float: none; display: inline;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:a7f95e9b-f1c7-4f02-aaf8-7a9063d8dc7c" class="wlWriterSmartContent">   <div class="code-container">     <div class="code-titleblock">CopyTo Local to HDFS Stream</div>      <div style="background: rgb(255, 255, 255); overflow: auto;">       <ol style="background: rgb(222, 222, 222); margin: 0px; padding: 0px 0px 0px 5px; white-space: nowrap;">         <li><span style="color: rgb(0, 0, 255);">using</span> (<span style="color: rgb(43, 145, 175);">HdfsFileStream</span> file = hdfsSystem.OpenFileStream(localhdfsfilename, <span style="color: rgb(43, 145, 175);">HdfsFileAccess</span>.Write, chunksize))</li>          <li class="code-even">{</li>          <li>&#160;&#160;&#160; <span style="color: rgb(0, 0, 255);">using</span> (<span style="color: rgb(43, 145, 175);">FileStream</span> stream = <span style="color: rgb(0, 0, 255);">new</span> <span style="color: rgb(43, 145, 175);">FileStream</span>(localfilepath, <span style="color: rgb(43, 145, 175);">FileMode</span>.Open, <span style="color: rgb(43, 145, 175);">FileAccess</span>.Read))</li>          <li class="code-even">&#160;&#160;&#160; {</li>          <li>&#160;&#160;&#160;&#160;&#160;&#160;&#160; stream.CopyTo(file);</li>          <li class="code-even">&#160;&#160;&#160; }</li>          <li>}</li>       </ol>     </div>   </div> </div>  <p>A quick word is warranted on appending to a file. Although the API currently supports open files for Append, this is only supported in Hadoop version 1.0.0 and above.</p>  <h3>Building the Library</h3>  <p>The download not only consists of the compiled libraries but also the full source code and sample C# application that this post is based upon. The source supports both x86 and x64 compilations. However one has to remember that if one does a 32-bit compilation then a 32-bit version of the JRE will also be required.</p>  <p>In the Native library the include path is defined as:</p>  <p>$(JAVA_HOME)\include;$(JAVA_HOME)\include\win32;$(IncludePath)</p>  <p>If your environment variable is not correctly defined for the Java include directory this will have to be modified.</p>  <p>Happy HDFS file access!</p>]]></content:encoded>
    </item>
    <item>
      <title>Submitting Hadoop MapReduce Jobs using PowerShell</title>
      <link>https://docs.microsoft.com/archive/blogs/carlnol/submitting-hadoop-mapreduce-jobs-using-powershell</link>
      <pubDate>Tue, 05 Feb 2013 05:04:17 GMT</pubDate>
      <dc:creator><![CDATA[Carl Nolan]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.cloudplatform.qa.msdn.wdslab.com/carlnol/2013/02/05/submitting-hadoop-mapreduce-jobs-using-powershell/</guid>
      <description><![CDATA[As always here is a link to the “Generics based Framework for .Net Hadoop MapReduce Job Submission”...]]></description>
      <content:encoded><![CDATA[<p>As always here is a link to the “<a href="http://code.msdn.microsoft.com/Framework-for-Composing-af656ef7">Generics based Framework for .Net Hadoop MapReduce Job Submission</a>” code. </p>  <p>In all the samples I have shown so far I have always used the command-line consoles. However this does not need to be the case, PowerShell can be used. The Console application which is used to submit the MapReduce jobs call a .Net Submissions API. As such one can call the .Net API directly from within PowerShell; as I will now demonstrate.</p>  <p>The key types one needs to be concerned with are:</p>  <ul>   <li>MSDN.Hadoop.Submission.Api.SubmissionContext – The type containing the job submission options</li>    <li>MSDN.Hadoop.Submission.Api.SubmissionApi – The type used for submitting the job</li> </ul>  <p>To use the .Net API one firstly has to create the two required objects:</p>  <p><font face="Consolas">$SubmitterApi = $BasePath + &quot;\Release\MSDN.Hadoop.Submission.Api.dll&quot;     <br />Add-Type -Path $SubmitterApi      <br />$context = New-Object -TypeName MSDN.Hadoop.Submission.Api.SubmissionContext      <br />$submitter = New-Object -TypeName MSDN.Hadoop.Submission.Api.SubmissionApi</font></p>  <p>After this one just has to define the context with the necessary job submission properties:</p>  <p><font face="Consolas">[string[]]$inputs = @(&quot;mobile/data&quot;)     <br />[string[]]$files = @($BasePath + &quot;\Sample\MSDN.Hadoop.MapReduceCSharp.dll&quot;) </font></p> <font face="Consolas"></font>  <p><font face="Consolas">$config = New-Object 'Tuple[string,string]'(&quot;DictionaryCapacity&quot;, &quot;1000&quot;)     <br />$configs = @($config)</font></p> <font face="Consolas"></font>  <p><font face="Consolas">$context.InputPaths = $inputs     <br />$context.OutputPath = &quot;mobile/querytimes&quot;      <br />$context.MapperType = &quot;MSDN.Hadoop.MapReduceCSharp.MobilePhoneRangeMapper, MSDN.Hadoop.MapReduceCSharp&quot;      <br />$context.ReducerType = &quot;MSDN.Hadoop.MapReduceCSharp.MobilePhoneRangeReducer, MSDN.Hadoop.MapReduceCSharp&quot;      <br />$context.Files = $files      <br />$context.ExeConfigurations = $configs</font></p>  <p>One just has to remember that the input and files specifications are defined as string arrays. </p>  <p>In a recent build I added support for adding user-defined key-value pairs to the application configuration file. This ExeConfigurations property expects an array of Tuple&lt;string, String&gt; types, hence the object definition for the $config value.</p>  <p>Optionally one can also set the Data and Output format types:</p>  <p><font face="Consolas">$context.DataFormat = [MSDN.Hadoop.Submission.Api.DataFormat]::Text     <br />$context.OutputFormat = [MSDN.Hadoop.Submission.Api.OutputFormat]::Text</font></p>  <p>However, this is not necessary if one is using the default Text values.</p>  <p>Once the context has been defined one just has to run the job:</p>  <p><font face="Consolas">$submitter.RunContext($context)</font></p>  <p>To call the PowerShell script from the Hadoop command-line once can use:</p>  <p><font face="Consolas">powershell -ExecutionPolicy unrestricted /File %BASEPATH%\SampleScripts\hadoopcstextrangesubmit.ps1</font> </p>  <p>All in all a simple process.</p>]]></content:encoded>
    </item>
  </channel>
</rss>