Last active
August 29, 2015 14:00
-
-
Save dharkum/11381357 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#region edits | |
# This is the name of the Azure HDInsight Subscription | |
[string]$SubscriptionName = "Contoso2014" | |
# This is the HDInsight Cluster Name that you want to work with | |
[string]$HDInsightClusterName = "ServerLogs" | |
# The table specified here will be dropped, please ensure that this is a test table | |
[string]$TableName = "samplelog" | |
# This is the location where the external table's data will be located on WASB | |
[string]$Location = "wasb://data@serverlogs1.blob.core.windows.net/$TableName" | |
#endregionregion edits | |
#region code | |
try | |
{ | |
Set-AzureSubscription -SubscriptionName $SubscriptionName | |
Use-AzureHDInsightCluster -Name $HDInsightClusterName | |
$provider = New-Object System.Globalization.CultureInfo "en-US" | |
# The code snippet below prepares the demo managed Hive table samplelog determined by $TableName by dropping the table if it already exists | |
# and creating the new partitioned table, partitioned by column dt. | |
[string]$QueryString="" | |
$QueryString=" drop table if exists " + $TableName + ";" | |
$QueryString = $QueryString + " create table " + $TableName + "(id int, logentry string) | |
partitioned by (dt string) | |
row format delimited | |
fields terminated by ',' | |
stored as textfile | |
location '" + $Location + "';" | |
Invoke-Hive -Query $QueryString | Out-Null | |
# The code below adds partitions to the Hive managed table created above for the last three months and the current month. So, if the current month is April, this code | |
# adds partitions for Feb, Mar, Apr (last 3 months) and the current month May in the form of yyyy-mm | |
$QueryString = $QueryString + "ALTER TABLE $tableName ADD IF NOT EXISTS PARTITION (dt = '2014-02');" | |
$QueryString = $QueryString + "ALTER TABLE $tableName ADD IF NOT EXISTS PARTITION (dt = '2014-03');" | |
$QueryString = $QueryString + "ALTER TABLE $tableName ADD IF NOT EXISTS PARTITION (dt = '2014-04');" | |
$QueryString = $QueryString + "ALTER TABLE $tableName ADD IF NOT EXISTS PARTITION (dt = '2014-05');" | |
Invoke-Hive -Query $QueryString | |
$QueryString = "SHOW PARTITIONS " + $TableName + ";"; | |
Invoke-Hive -Query $QueryString | |
} | |
catch | |
{ | |
write-host "Caught an exception:" -ForegroundColor Red | |
write-host "Exception Type: $($_.Exception.GetType().FullName)" -ForegroundColor Red | |
write-host "Exception Message: $($_.Exception.Message)" -ForegroundColor Red | |
} | |
#endregion code |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment