Write and Read an object with binaryformatting, u-sql

Viewed 64

So I am currently attempting to create a couple of U-sql scripts one where a specific object is written to file, another script which reads the file and gets the object back.

Write U-sql script :

@bloomTest =
    EXTRACT date DateTime,
            region string,
            tenantName string,
            fileName string,
            modifyingUsers string,
            firstAccess string,
            lastAccess string
    FROM "SearchLog.tsv"
    USING Extractors.Tsv();

@rs0 =
    SELECT sqlProjsend.Send.send(region) AS Obj
    FROM @bloomTest;


OUTPUT @rs0
TO "/output/bloomtestSend.csv"
USING Outputters.Csv();

Write Code-Behind :

 public static class Send
    {

        public static string send(string s)
        {
            IBloomfilter bloom = new BloomFilter(52);

            bloom.Add("hello");

            byte[] bArr = Serialize(bloom);
            return "|" + BitConverter.ToString(bArr).Replace("-", "") + "|";
        }




        static byte[] Serialize(IBloomfilter bloom)
        {
            MemoryStream stream = new MemoryStream();
            IFormatter formatter = new BinaryFormatter();
            formatter.Serialize(stream, bloom);
            return stream.ToArray();
        }
    }

Receive script :

@bloomTest =
    EXTRACT bloom string

    FROM "/output/bloomtestSend.csv"
    USING Extractors.Csv();

@rs0 =
    SELECT sqlProjrecv.Recieve.recv(bloom) AS bloomRes
    FROM @bloomTest;


OUTPUT @rs0
TO "/output/bloomtestRecv.csv"
USING Outputters.Csv();

Receive Code-Behind :

public static class Recieve
    {   
        public static string recv(string b)
        {

            string[] ss = b.Split('|');

            IBloomfilter bloom = Deserialize(StringToByteArray(ss[1]));

            string[] strings = new string[3];
            strings[0] = "nice";
            strings[1] = "world";
            strings[2] = "hello";

            string retVal = "";
            foreach(string s in strings)
            {

            }
            return retVal;
        }

        public static byte[] StringToByteArray(String hex)
        {
            int NumberChars = hex.Length;
            byte[] bytes = new byte[NumberChars / 2];
            for (int i = 0; i < NumberChars; i += 2)
                bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
            return bytes;
        }
        private static IBloomfilter Deserialize(byte[] param)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                ms.Write(param, 0, param.Length);
                IFormatter br = new BinaryFormatter();
                ms.Position = 0;
                return br.Deserialize(ms) as BloomFilter;
            }
        }
    }

The Error I keep getting when trying to run recieve local is could not find collection __codeBehind_g4nuvpoe.vvy Which doesnt tell me much. The bloomfilter object I am trying to send does work. I have also tried to use the exact same code in another project without u-sql scripts where it works perfectly :

class Program
    {
        public static byte[] StringToByteArray(String hex)
        {
            int NumberChars = hex.Length;
            byte[] bytes = new byte[NumberChars / 2];
            for (int i = 0; i < NumberChars; i += 2)
                bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
            return bytes;
        }
        private static IBloomfilter Deserialize(byte[] param)
        {
            using (MemoryStream ms = new MemoryStream(param))
            {
                IFormatter br = new BinaryFormatter();
                return br.Deserialize(ms) as BloomFilter;
            }
        }

        static byte[] Serialize(IBloomfilter bloom)
        {
            MemoryStream stream = new MemoryStream();
            IFormatter formatter = new BinaryFormatter();
            formatter.Serialize(stream, bloom);
            return stream.ToArray();
        }


        static void Main(string[] args)
        {

            IBloomfilter bloom = new BloomFilter(51);
            bloom.Add("hello");


            byte[] ser = Serialize(bloom);
            string bloomString = BitConverter.ToString(ser).Replace("-", "");


            ser = StringToByteArray(bloomString);

            bloom = Deserialize(ser);


            string[] k = new string[4];

            k[0] = "hello";
            k[1] = "world";
            k[2] = "not";
            k[3] = "this";

            foreach(string s in k)
            {
                if (bloom.Contains(s))
                {
                    Console.WriteLine(s);
                }

            }

            Console.ReadLine();
        }
     }

This codes run as you would expect which is not functionally different you'd think from the u-sql implementation so is there something wrong with the actual scripts?

Any ideas would be much welcome!

1 Answers
Related