Tuesday, July 22, 2014

Import CSV file into Microsoft SQL Server via SQL command

I find this method way easier than messing with SQL Server Import/Export Wizard.  Although it can be useful to have the wizard create the target table definition first.

CREATE TABLE [dbo].[TargetTable] (
    [Id] [int] NULL,
    [StringCol] [varchar](MAX) NULL,
    [IntCol] [int] NULL
    -- additional columns here
) ON [PRIMARY]
GO


BULK INSERT [dbo].[TargetTable]
    FROM 'C:\pathto\SourceFile.csv'
    WITH
    (
        FIRSTROW = 2,             -- 2 if there are headers
        FIELDTERMINATOR = ',',    -- field delimiter
        ROWTERMINATOR = '\n',     -- next row
        ERRORFILE = 'C:\pathto\_ImportErrors.txt',
        TABLOCK
    )
GO



Monday, July 21, 2014

Background download of wikipedia dump on Linux

A handy way to download a wikipedia dump in the background in such a way that it continues after ending a terminal session:

$ nohup wget -bqc http://dumps.wikimedia.org/enwiki/20140707/enwiki-20140707-pages-articles-multistream.xml.bz2

Latest wiki dump url can be found here:
http://dumps.wikimedia.org/backup-index.html

References:
http://www.cyberciti.biz/tips/nohup-execute-commands-after-you-exit-from-a-shell-prompt.html
http://www.cyberciti.biz/tips/linux-wget-your-ultimate-command-line-downloader.html

Tuesday, July 15, 2014

Setting up an FTP server on CentOS 6.x

Need FTP access on a CentOS machine? Easy.

sudo yum install vsftpd
sudo service vsftpd start
sudo chkconfig vsftpd on
chkconfig --list

sudo vi /etc/vsftpd/vsftpd.conf # edit default conf file to have these lines:
    anonymous_enable=NO
    chroot_local_user=YES
    chroot_list_enable=NO
    chroot_list_file=/etc/vsftpd/chroot_list
    pasv_min_port=3000
    pasv_max_port=3050
   
sudo touch /etc/vsftpd/chroot_list
sudo /sbin/service vsftpd restart


Done.

Note that these steps have been grabbed from this Rackspace article that includes much more detail:
http://www.rackspace.com/knowledge_center/article/rackspace-cloud-essentials-centos-installing-vsftpd

Tuesday, June 10, 2014

New Eclipse workspace settings


Each new Eclipse workspace has the default settings, arg! I'm going to collect my customisations here.


Window > Preferences...

General > Editors > Text Editors > Spelling | Turn off

General > Editors > Text Editors | Show line numbers

General > Keys | Next Editor bound to Ctrl-Tab; Prev Editor bound to Ctrl-Shift-Tab


Need to check out:
https://code.google.com/a/eclipselabs.org/p/workspacemechanic/
http://mcuoneclipse.com/2012/04/04/copy-my-workspace-settings/





Wednesday, March 12, 2014

Setting up Hive with HBase external storage on CDH

If you have existing HBase tables it can be very handy to create Hive external tables wrapping these so that you can run HiveQL queries.

The following HiveQL will create the metastore table schema on top of MyTableName:

CREATE EXTERNAL TABLE MyTableName(key string, Column1 string, Column2 string)
STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
WITH SERDEPROPERTIES ("hbase.columns.mapping" = ":key,cf:Column1,cf:Column2")
TBLPROPERTIES ('hbase.table.name' = 'MyTableName');



The next step is to add the hbase auxlib jars to hive-site.xml to ensure certain HiveQL queries will run (e.g. select count(1) from MyTableName):
<property>
<name>hive.aux.jars.path</name>
<value>file:///opt/cloudera/parcels/CDH/lib/hive/lib/zookeeper.jar,file:///opt/cloudera/parcels/CDH/lib/hive/lib/hbase.jar,file:///opt/cloudera/parcels/CDH/lib/hive/lib/hive-hbase-handler-0.10.0-cdh4.6.0.jar,file:///opt/cloudera/parcels/CDH/lib/hive/lib/guava-11.0.2.jar</value>
</property>


This property must be added to the hive1 service > Config > Service-Wide > Advanced > Hive Service Configuration Safety Valve for hive-site.xml section to be able to execute certain HiveQL commands from the host command line.

To execute HiveQL from the Hue HiveUI add it to the hue1 service > Config > Beeswax Server (Default) > Advanced > Hive Configuration Safety Valve section.


Refs:
https://cwiki.apache.org/confluence/display/Hive/HBaseIntegration
http://www.confusedcoders.com/bigdata/hive/hbase-hive-integration-querying-hbase-via-hive
http://www.cloudera.com/content/cloudera-content/cloudera-docs/CDH4/latest/CDH4-Installation-Guide/cdh4ig_topic_18_10.html
https://groups.google.com/a/cloudera.org/forum/#!topic/cdh-user/E8GfiwMOIPw

Sunday, March 9, 2014

Free disk space email alert on Windows Server

Schedule the following PowerShell script to run periodically to receive email alerts on low disk space:


# Schedule with: %windir%\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -WindowStyle "Hidden" -File C:\path_to_scripts\freedisk_space_alert.ps1

$minGbThreshold = 10;

$computers = "localhost"
$smtpAddress = "localhost";
$toAddress = "someone@test.com";
$fromAddress = "someone@test.com";

foreach($computer in $computers)
{
    $disks = Get-WmiObject -ComputerName $computer -Class Win32_LogicalDisk -Filter "DriveType = 3";
    $computer = $computer.toupper();
    $deviceID = $disk.DeviceID;
    foreach($disk in $disks)
    {
        $freeSpaceGB = [Math]::Round([float]$disk.FreeSpace / 1073741824, 2);
        if($freeSpaceGB -lt $minGbThreshold)
        {
            $smtp = New-Object Net.Mail.SmtpClient($smtpAddress)
            $msg = New-Object Net.Mail.MailMessage
            $msg.To.Add($toAddress)
            $msg.From = $fromAddress
            $msg.Subject = "Diskspace below threshold: " + $computer + "\" + $disk.DeviceId
            $msg.Body = $computer + "\" + $disk.DeviceId + " " + $freeSpaceGB + "GB Remaining";
           
            $smtp.UseDefaultCredentials = $false;       
            $cred = New-Object System.Net.NetworkCredential("\smtpuser", "<enter password>");  # Ensure smtpuser is authorised to send emails
            $smtp.Credentials = $cred;

            $smtp.Send($msg)
        }
    }
}



This script is slightly modified from the one found here to include credentials:
http://gavindraper.com/2012/09/22/automatic-low-hard-disk-alerts-for-windows-server/
Thanks Gavin!

Also you may need to enable script execution in PowerShell by running the following:
%windir%\SysWOW64\WindowsPowerShell\v1.0\powershell.exe set-executionpolicy remotesigned
Which will still restrict internet downloaded scripts and is better than setting this value to unrestricted
See: http://superuser.com/questions/106360/how-to-enable-execution-of-powershell-scripts

Thursday, February 27, 2014

Efficient uploading of Jar files to your Hadoop cluster

Copying fat Jar files up to your Hadoop cluster to execute jobs on production-sized data sets in order to find bottlenecks can be painful when you want a quick turn around whilst debugging.

Sometimes local mode just doesn't cut it.

A good solution is to use rsync which supports incremental checking to only transfer file differences.

Command:
rsync -avz /your_source_directory/somejob-0.0.1.jar login@servername:/target_directory/somejob-0.0.1.jar

Options:
-a archive mode
-v verbose mode
-z compress file data during the transfer