Wednesday, November 28, 2012

Decode a mongo BinData field into a Guid with C#

This C# snippet will decode a mongo BinData field into a Guid

got this from Mongo:
{
  "_id" : ObjectId("50b40a5db14ea8902cd5d5ed"),
   "MyGuid : new BinData(3, "MC/0P9nkEeGgjYQrK2V3pQ=="),

   ...
}

but want to know the value of MyGid?  Try this little snippet:

using System;
namespace MongoGuidDecoder
{
    class Program
    {
        static void Main(string[] args)
        {
            var input = args[0];
            var bytes = Convert.FromBase64String(input.Trim());
            var guid = new Guid(bytes);
            Console.WriteLine(input);
            Console.WriteLine(guid);
        }
    }
}


Now run to get the Guid representation:
>MongoGuidDecoder MC/0P9nkEeGgjYQrK2V3pQ==
MC/0P9nkEeGgjYQrK2V3pQ==
3ff42f30-e4d9-e111-a08d-842b2b6577a5

Tuesday, September 18, 2012

My CentOS Gottchas

Lessons for CentOS 6.3 in Virtualbox

Configure Bridged Adapter to see the internet:


# vi /etc/sysconfig/network-scripts/ifcfg-eth0
edit to have NM_CONTROLLED="no" and ONBOOT="yes"
Ref:  http://serverfault.com/questions/349993/virtualbox-centos-no-eth0

Then run:
# service network restart


Friday, June 8, 2012

R is fantastic (esp with ggplot2)

I keep being amazed at the R language, especially when using the free RStudio IDE.  I've mainly focused on using various analytical packages (like randomForest), but wow ggplot2 is awesome in it's simplicity for doing visualisations.  The following isn't the best data for an example, but you get the idea:


scores = rbind(0.25199,0.26516,0.29329,0.30794,0.30630,0.33529,0.33910,0.34185,0.33703,0.34423,0.34877,0.32457,0.33826,0.33670,0.34237,0.33503,0.33186,0.33125,0.32908,0.34548,0.34357,0.34371,0.35440,0.35082,0.34633,0.35529)
var = rbind(7.62,5.32,9.60,11.38,13.31,14.38,13.60,15.60,12.18,11.98,10.88,25.00,13.59,13.67,14.17,25.27,26.73,25.90,22.97,38.98,21.21,23.86,21.48,23.07,24.38,22.46)


summary(scores)
summary(var)


library(ggplot2)
qplot(scores,var) + geom_smooth()


Monday, May 21, 2012

Using WinMerge as merge/diff tool in TFS


Original source: http://jonfuller.codingtomusic.com/2008/08/26/tfs-using-winmerge-as-your-mergediff-tool/
Thanks Jon!

In VS, go To: Tools/Options/SourceControl/Visual Studio Team Foundation Server/Configure User Tools

Click "Add..."
Extension:  .*
Operation: Compare
Command: C:\Program Files\WinMerge\WinMergeU.exe
Arguments: /e /wl /dl %6 /dr %7 %1 %2
Click "Add..."

Extension:  .*
Operation: Merge
Command: C:\Program Files\WinMerge\WinMergeU.exe
Arguments: /ub /dl %6 /dr %7 %1 %2 %4

The winmerge command line reference can be found here.

Thursday, May 17, 2012

Javascript console.log() and IE

Arg how I loath IE, why doesn't it support console.log(obj)?

To prevent IE from throwing an exception when it encounters console.log, add the following to a site-wide js file:

if (!window.console) console = { log: function () { } };


This makes cross browser testing much easier while in development.

Wednesday, May 16, 2012

Configure Elmah on IIS7.x vs IIS6

Elmah in IIS 7.x:

<system.webServer>
    <validation validateIntegratedModeConfiguration="false" /> <!-- false if you also have httpModules in config -->
    <modules runAllManagedModulesForAllRequests="true">
      <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" preCondition="managedHandler" />
      <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" preCondition="managedHandler" />
      <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" preCondition="managedHandler" />
    </modules>
    <handlers>
      <add name="Elmah" path="elmah.axd" verb="POST,GET,HEAD" type="Elmah.ErrorLogPageFactory, Elmah" preCondition="integratedMode" />
    </handlers>
</system.webServer>


Elmah in IIS 6, if you must:

<elmah>
    <!-- See http://code.google.com/p/elmah/wiki/SecuringErrorLogPages for more information on remote access and securing ELMAH. -->
    <security allowRemoteAccess="false" />
</elmah>


<system.web>
   <httpModules>
      <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" />
      <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" />
      <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" />
    </httpModules>
    <httpHandlers>
      <add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
    </httpHandlers>
</system.web>

Thanks Elmah - you're awesome!

Sunday, April 29, 2012

Contained Javascript Library template

This Javascript template enables a library to be written in its own namespace and handle being included multiple times on a page.
It's based on Douglas Crockford's json2.js library.


var MyLibrary;
if (!MyLibrary) {
    MyLibrary = {};
}


(function () {
    //'use strict';
    if (typeof MyLibrary.myFunction !== 'function') {
        MyLibrary.myFunction = function (arg1, arg2) {
          //...method body here...
          return true;
        };
    }
}());

Thursday, April 26, 2012

Enable IE F12 Dev Tools

New contract, new machine.....and the Internet Explorer F12 debug tools don't work for me.

This fixed it:

Run gpedit.msc
In the left pane, click on to expand Computer Configuration, Administrative Templates, Windows Components, Internet Explorer, and Toolbars.
In the right pane, double click on Turn off Developer Tools
Select Disabled

Arg, how I hate IE.

Wednesday, April 25, 2012

Help Internet Explorer suck less by removing compat mode

The compatibility mode in IE has it's uses, but can cause nightmares when clients are testing.

To remove it as an option add the following as teh first line after the <head> tag in your mark up:
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />

More info:
http://blogs.msdn.com/b/askie/archive/2009/03/23/understanding-compatibility-modes-in-internet-explorer-8.aspx
http://stackoverflow.com/questions/3960120/turn-off-ie-8-compatibility-mode-for-site

Saturday, April 21, 2012

SQL 2005/2008 Median function

This is this simplest SQL 2005/2008 Median function I've come across:



SELECT
(
 (SELECT MAX(MyColumn) FROM
   (SELECT TOP 50 PERCENT MyColumn FROM MyTable ORDER BY MyColumn) AS BottomHalf)
 +
 (SELECT MIN(MyColumn) FROM
   (SELECT TOP 50 PERCENT MyColumn FROM MyTable ORDER BY MyColumn DESC) AS TopHalf)
) / 2 AS Median


Thanks Jeff Atwood!
http://stackoverflow.com/questions/1342898/function-to-calculate-median-in-sql-server

Friday, April 6, 2012

Installing Cloudera Hadoop 0.20 and Mahout on Ubuntu 11

Notes for installing Cloudera Hadoop 0.20 and Mahout on Ubuntu 11.10
April 2012

UPDATE SEPT 2014 - ok this is very old now.  A better way is to just use the Cloudera CDH installer!
# wget http://archive.cloudera.com/cm5/installer/latest/cloudera-manager-installer.bin
# chmod u+x cloudera-manager-installer.bin
# sudo ./cloudera-manager-installer.bin
Then follow the local installer steps before navigating to the web installer when prompted.

Original April 2012 Instructions:

0. Ubuntu
Fresh install of Ubuntu ubuntu-11.10-desktop-amd64.iso on VirualBox with all updates and Guest Addons
Also mount a shared folder created in VirtualBox:
$ sudo mount -t vboxsf _Shared ~/_Shared
or add this line (without sudo) to /etc/rc.local

1. Java and ssh prerequisites:
http://cloudblog.8kmiles.com/2011/12/02/hadoop-prerequisite-for-hadoop-setup-in-ubuntu/
http://softwareinabottle.wordpress.com/2011/11/17/install-sun-jdk-6-on-ubuntu-11-10/

2. Update Java env by adding the following to: /etc/bash.bashrc
http://blog.sanaulla.info/2009/04/02/installing-jdk-setting-java_home-in-ubuntu/
JAVA_HOME=/usr/lib/jvm/java-6-sun
export JAVA_HOME
PATH=$PATH:$JAVA_HOME/bin:
export PATH
CLASSPATH=$JAVA_HOME/lib/:.
export CLASSPATH

3. CDH3 install hadoop core:
http://cloudblog.8kmiles.com/2011/12/06/hadoop-cdh3-setup-in-ubuntu/
3.1 create cloudera.list:
$ sudo pico /etc/apt/sources.list.d/cloudera.list
with content:
deb http://archive.cloudera.com/debian maverick-cdh3 contrib
deb-src http://archive.cloudera.com/debian maverick-cdh3 contrib
$ curl -s http://archive.cloudera.com/debian/archive.key | sudo apt-key add -
$ sudo apt-get update
$ sudo apt-get install hadoop-0.20
$ hadoop version

4. CDH3 install daemons for Pseudo Distributed Mode:
http://cloudblog.8kmiles.com/2011/12/07/hadoop-cdh3-pseudo-distributed-mode-setup/
to set up Standalone mode see: http://cloudblog.8kmiles.com/2011/12/07/hadoop-cdh3-standalone-mode-setup/

$ sudo apt-get install hadoop-0.20-namenode
$ sudo apt-get install hadoop-0.20-secondarynamenode
$ sudo apt-get install hadoop-0.20-jobtracker
$ sudo apt-get install hadoop-0.20-datanode
$ sudo apt-get install hadoop-0.20-tasktracker

$ sudo apt-get install hadoop-0.20-conf-pseudo
$ sudo alternatives --display hadoop-0.20-conf

5. Test the daemons:
for service in /etc/init.d/hadoop-0.20-*; do sudo $service start; done
sudo jps
hadoop jar /usr/lib/hadoop/hadoop-*-examples.jar pi 2 100000

Navigate to: http://localhost:50070/ for Namenode details
Navigate to: http://localhost:50030/ for Job details

for service in /etc/init.d/hadoop-0.20-*; do sudo $service stop; done

6. Install mahout:
http://cloudblog.8kmiles.com/2012/02/16/cdh3-mahout-setup/
$ sudo apt-get install mahout


7. Install Hue interface:
https://ccp.cloudera.com/display/CDHDOC/Hue+Installation#HueInstallation-Installing%2CConfiguring%2CandStartingHueonOneMachine
$ sudo apt-get install hue

Open the /etc/hue/hue.ini configuration file and add:
[desktop]
secret_key=jFE93j;2[280-eiw.tyXrN2s3['d:/.q[eI^^y#e=+Iei*@Mn<qW5o

$ for service in /etc/init.d/hadoop-0.20-*; do sudo $service start; done
sudo /etc/init.d/hue start
Navigate to: http://localhost:8088/ for Hue

I hope these notes and links help someone out.  Please leave a comment if you have have any problems or additions.

Saturday, March 24, 2012

Adding external libs to an Eclipse Galileo project


In eclipse Galileo :
Open the project's properties
Select Java Build Path
Select Libraries tab
From there you can Add External Jars
http://stackoverflow.com/questions/179024/adding-a-jar-to-an-eclipse-java-library

Also, when importing java source files into the project, the root import folder chosen must match the package structure.

Thursday, March 22, 2012

Get object dependencies in SQL Server


Get object dependencies of a sproc via script instead of Management Studio:

DECLARE @my_object nvarchar(1024) = N'dbo.my_sproc'
CREATE TABLE #tempTable (name nvarchar(1024), [type] nvarchar(1024), updated nvarchar(1024), selected nvarchar(1024), [column] nvarchar(1024))
INSERT INTO #tempTable EXEC sp_depends @objname = @my_object
SELECT name FROM #tempTable group by name
DROP TABLE #tempTable
GO

Also useful:

SELECT * FROM sys.Tables

http://blog.sqlauthority.com/2009/09/23/sql-server-insert-values-of-stored-procedure-in-table-use-table-valued-function/


Tuesday, March 6, 2012

Increasing heap for Weka

Any OS
From command/terminal:
java -Xmx1024m -jar weka.jar

OSX gui
From terminal:
cd /Applications/weka-3-5-7.app/Contents
open Info.plist
Under  "Java" you will find an entry called "VMOptions" with the value "-Xmx256M".

Windows gui
Edit the line maxheap=1536m from C:\Program Files\Weka-3-X\RunWeka.ini

Sunday, February 26, 2012

SQL to concatenate many rows into a single string

I used this SQL script to convert multiple returned rows into a single text string in SQL Server 2008.
SELECT REPLACE(
(SELECT CASE TBL.DayNumber
WHEN 1 THEN 'Sun'
WHEN 2 THEN 'Mon'
WHEN 3 THEN 'Tue'
WHEN 4 THEN 'Wed'
WHEN 5 THEN 'Thu'
WHEN 6 THEN 'Fri'
WHEN 7 THEN 'Sat'
ELSE NULL
END AS 'data()' 
FROM [dbo].[MyTableName] TBL
FOR XML PATH (''))
, ' ', ',') AS CommaDelimDayNames

This will convert:
Id          DayNumber
----------- --------------------
77          4
77          5
77          6

into:
Id          CommaDelimDayNames
----------- --------------------
77          Wed,Thu,Fri


As usual I had inspiration from the wonderful stackoverflow, in particular Diwakar's answer:
http://stackoverflow.com/questions/194852/concatenate-many-rows-into-a-single-text-string/5580166#5580166
This has the answer:

REPLACE((select FName AS 'data()'  from NameList  for xml path('')), ' ', ', ')




Friday, February 24, 2012

My hg/mercurial settings

My .hgignore for Visual Studio projects:


# use glob syntax
syntax: glob


# Ignore Visual Studio files
*.obj
*.exe
*.pdb
*.user
*.vspscc
*.bak
*.cache
*.ilk
*.log
*.sbr
*.scc
[Bb]in
[Dd]ebug*/
obj/
[Rr]elease*/
_ReSharper*/
[Tt]est[Rr]esult*
[Bb]uild[Ll]og.*
*.[Pp]ublish.xml


#*.suo # depending on public/private repo



Monday, February 13, 2012

SQL Server PIVOT and UNPIVOT to transpose a table


I had to rotate/transpose a SQL table variable for reporting purposes and eventually got SQL Server 2008 PIVOT and UNPIVOT operators to achieve this (rather that using a longer UNION ALL approach.)

Source table definition:
DECLARE @ResultTable TABLE
(
Ordinal INT,
Calc1 FLOAT,
Calc2 FLOAT,
Calc3 FLOAT
)

SELECT * FROM @ResultTable:
1 | 1000000 | 0.438556290234799 | 3.53674427608709
2 | 0 | 0.877112580469597 | 7.07348855217417
3 | 1000000 | 0.877112580469597 | 7.07348855217417
4 | 0 | 1.3156688707044 | 10.6102328282613
5 | 1000000 | 1.3156688707044 | 10.6102328282613
6 | 0 | 0 | 0

SQL to rotate/transpose rows to columns:
;WITH CTE AS 
  (
    SELECT Ordinal, Col1 as SeriesName, Col as Value FROM (SELECT * FROM @ResultTable) P
      UNPIVOT (Col FOR Col1 IN (
        Calc1, Calc2, Calc3 -- Series to return as rows
      )) UN
  )
  SELECT * FROM CTE
    PIVOT 
    (MAX(value) FOR Ordinal IN (
      [1],[2],[3],[4],[5],[6]  -- Ordinals to return as columns
    )) PVT

Transposed table results:
Calc1 | 1000000 | 0 | 1000000 | 0 | 1000000 | 0
Calc3 | 3.53674427608709 | 7.07348855217417 | 7.07348855217417 | 10.6102328282613 | 10.6102328282613 | 0
Calc2 | 0.438556290234799 | 0.877112580469597 | 0.877112580469597 | 1.3156688707044 | 1.3156688707044 | 0

Note that the limitation with this solution is the number rows in source table/columns in transposed table is fixed and requires SQL Server 2005+


Monday, January 30, 2012

ASP.NET url changes with 301 permanent redirect

I deployed a new ASP.NET MVC web site and needed to indicate to google and links from other sites that the url for most pages had permanently changed (rather than give them a 404 error).

Adding simple dummy .aspx files for these old pages with the following (non-code behind) script with a 301 HTTP response did the trick with little fuss:


<%@ Page Language="C#" AutoEventWireup="false" Inherits="System.Web.UI.Page" %>
<script runat="server">
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        // Tell old links to go to the new url:
        Response.Status = "301 Moved Permanently";
Response.AddHeader("Location","http://www.mydomain.com/mynewurl");
    }
</script>

At some point I'll simply delete them once I think the important external links have been updated.

Friday, January 27, 2012

Install Elmah logging with ASP.NET MVC 3

Elmah is a great logging tool for ASP.NET and works fine with ASP.NAT MVC 3 with these quick steps:

1. Ensure NuGet is installed in Visual Studio 2010 (shows as Library Package Manager under Tools menu)

2. Open the Package Manager console and install Elmah by typing:
PM> Install-Package Elmah.MVC
This updates the web.config with sectionGroup, httpModules and httpHandlers and adds dll references.

3. Add to web.config:
  <elmah>
    <security allowRemoteAccess="no" />
    <errorLog type="Elmah.XmlFileErrorLog, Elmah" logPath="~/App_Data/Elmah" />
  </elmah>

4. To enable error logging when customErrors is "on" add the following to Global.asax: [see: http://stackoverflow.com/questions/766610/how-to-get-elmah-to-work-with-asp-net-mvc-handleerror-attribute/5936867#5936867]

public class ElmahHandledErrorLoggerFilter : IExceptionFilter
{
    public void OnException (ExceptionContext context)
    {
        // Log only handled exceptions, because all other will be caught by ELMAH anyway.
        if (context.ExceptionHandled)
            Elmah.ErrorSignal.FromCurrentContext().Raise(context.Exception);
    }
}


And:

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new ElmahHandledErrorLoggerFilter());  // Required so that Elmah still loggs errors when customErrors mode="On"
            filters.Add(new HandleErrorAttribute());
        }

Done.