Are Guids unique when using a U-SQL Extractor? - guid

As these questions point out, Guid.NewGuid will return the same value for all rows due to the enforced deterministic nature of U-SQL i.e if it's scaled out if an element (vertex) needs retrying then it should return the same value....
Guid.NewGuid() always return same Guid for all rows
auto_increment in U-SQL
However.... the code example in the officials documentation for a User Defined Extractor purposefully uses Guid.NewGuid().
I'm not querying the validity of the answers for the questions above, as they are from an authoritative source (the programme manager for u-sql, so very authoritative!). However, what I'm wondering if the action of using an Extractor means NewGuid can be used as normal? Is it simply within c# expressions in u-sql and User Defined Functions in which NewGuid is unsafe?
[SqlUserDefinedExtractor(AtomicFileProcessing = true)]
public class FullDescriptionExtractor : IExtractor
{
private Encoding _encoding;
private byte[] _row_delim;
private char _col_delim;
public FullDescriptionExtractor(Encoding encoding, string row_delim = "\r\n", char col_delim = '\t')
{
this._encoding = ((encoding == null) ? Encoding.UTF8 : encoding);
this._row_delim = this._encoding.GetBytes(row_delim);
this._col_delim = col_delim;
}
public override IEnumerable<IRow> Extract(IUnstructuredReader input, IUpdatableRow output)
{
string line;
//Read the input line by line
foreach (Stream current in input.Split(_encoding.GetBytes("\r\n")))
{
using (System.IO.StreamReader streamReader = new StreamReader(current, this._encoding))
{
line = streamReader.ReadToEnd().Trim();
//Split the input by the column delimiter
string[] parts = line.Split(this._col_delim);
int count = 0; // start with first column
foreach (string part in parts)
{
if (count == 0)
{ // for column “guid”, re-generated guid
Guid new_guid = Guid.NewGuid();
output.Set<Guid>(count, new_guid);
}
else if (count == 2)
{
// for column “user”, convert to UPPER case
output.Set<string>(count, part.ToUpper());
}
else
{
// keep the rest of the columns as-is
output.Set<string>(count, part);
}
count += 1;
}
}
yield return output.AsReadOnly();
}
yield break;
}
}
https://learn.microsoft.com/en-us/azure/data-lake-analytics/data-lake-analytics-u-sql-programmability-guide#use-user-defined-extractors

Related

Asp.Net Mvc Uniq Routing

I am creating a site which is personel blog. I want to give a specific routing when I enter a new blog in admin panel. Normally when I save it matches the database id. I do not have access to static routing anyway.
I want the link parameter to be stored in the database when the blog is being entered via the routing
Default : localhost/ControlName/ActionName/id (localhost/Blog/GetBlogs/2)
bu I want that
Wanted : localhost/ControlName/ActionName/storedValue(localhost/Blog/GetBlog/bluesky)
or
localhost/storedValue(localhost/bluesky)
What you're talking about is a slug. You just have to add a property on your blog class to hold some unique string value that will compose part of the URL. For example:
[Index]
[StringLength(80)]
public string Slug { get; set; }
Then, when creating the blog, you either manually specify the value for Slug (make it a field in the form) or compose it by "slugifying" the title of the blog or something. I use the following string extensions:
public static string RemoveDiacritics(this string s)
{
s = s ?? string.Empty;
if (s.Length > 0)
{
char[] chars = new char[s.Length];
int charIndex = 0;
s = s.Normalize(NormalizationForm.FormD);
for (int i = 0; i < s.Length; i++)
{
char c = s[i];
if (CharUnicodeInfo.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark)
chars[charIndex++] = c;
}
return new string(chars, 0, charIndex).Normalize(NormalizationForm.FormC);
}
return s;
}
public static string Slugify(this string s, int maxLength = 80)
{
s = s ?? string.Empty;
//First to lower case
s = s.ToLowerInvariant().RemoveDiacritics();
//Replace spaces
s = Regex.Replace(s, #"\s", "-", RegexOptions.Compiled);
//Remove invalid chars
s = Regex.Replace(s, #"[^a-z0-9s\-_]", "", RegexOptions.Compiled);
//Trim dashes from end
s = s.Trim('-', '_');
//Replace double occurences of - or _
s = Regex.Replace(s, #"([\-_]){2,}", "$1", RegexOptions.Compiled);
while (s.Length > maxLength)
{
var pieces = s.Split('-');
pieces = pieces.Take(pieces.Count() - 1).ToArray();
s = string.Join("-", pieces);
}
return s;
}
Then, for example, you could do something like:
blog.Slug = blog.Title.Slugify();
However you create the slug, you'll then use the URL param to look up the blog by that:
public ActionResult GetBlog(string slug)
{
var blog = db.Blogs.SingleOrDefault(m => m.Slug == slug);
This is why the Slug property is decorated with [Index] above. That makes EF create an index for the column when it creates the table/adds the column. Any column you intend to query on should be indexed for performance reasons. Also, you have to define a set length for the column, as NVARCHAR(MAX) (the default column type for a string) cannot be indexed.

How to Repeat signature horizontally based on Characters in aspose.word

We have a word document with [Signature] Key as a paragraph, All we need to do is replace with signature with some Names, based on the names we need to repeat the [signature] key.
Ex: if names are containing 10 to 15 characters it should be repeat 2 times in a row like below
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
if names are containing 5 charecters should be repeat 3 times
XXXXXXXXXXXXXX XXXXXXXXXXXXXX XXXXXXXXXXXXXXX
based on the name node will repeat...?
please help how to solve this task ......
To find and replace text in a word document, Aspose.Words provides IReplacingCallback interface which can easily be used to achieve your goal. I used a static string to test the scenario as I don't have the details of your data source. You will require to add a check in your code based on the name length, you need to add the signature. Check the following sample:
//Open the file
Document doc = new Document("c:\\data\\Signature.docx");
//Specify the string / tag to be replace
doc.Range.Replace(new Regex(#"\[Signature\]", RegexOptions.IgnoreCase), new ReplaceEvaluatorSignature(), false);
//Save the updated document
doc.Save("c:\\data\\Output.docx");
/// <summary>
/// Class to change the signature
/// </summary>
public class ReplaceEvaluatorSignature : IReplacingCallback
{
/// <summary>
/// This method is called by the Aspose.Words find and replace engine for each match.
/// This method highlights the match string, even if it spans multiple runs.
/// </summary>
ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)
{
// This is a Run node that contains either the beginning or the complete match.
Node currentNode = e.MatchNode;
// The first (and may be the only) run can contain text before the match,
// in this case it is necessary to split the run.
if (e.MatchOffset > 0)
currentNode = SplitRun((Run)currentNode, e.MatchOffset);
// This array is used to store all nodes of the match for further removing.
ArrayList runs = new ArrayList();
// Find all runs that contain parts of the match string.
int remainingLength = e.Match.Value.Length;
while (
(remainingLength > 0) &&
(currentNode != null) &&
(currentNode.GetText().Length <= remainingLength))
{
runs.Add(currentNode);
remainingLength = remainingLength - currentNode.GetText().Length;
// Select the next Run node.
// Have to loop because there could be other nodes such as BookmarkStart etc.
do
{
currentNode = currentNode.NextSibling;
}
while ((currentNode != null) && (currentNode.NodeType != NodeType.Run));
}
// Split the last run that contains the match if there is any text left.
if ((currentNode != null) && (remainingLength > 0))
{
SplitRun((Run)currentNode, remainingLength);
runs.Add(currentNode);
}
//Name is defined for testing, replace it with your data source
// string TestName = "Nausherwan Aslam";
//Following is to test less or equal to 10 charators
string TestName = "Nausherwan";
// Create Document Buidler
DocumentBuilder builder = new DocumentBuilder(e.MatchNode.Document as Document);
builder.MoveTo((Run)runs[runs.Count - 1]);
if (TestName.Length > 10)
{
builder.Write(TestName+ " " + TestName);
}
else
{
builder.Write(TestName + " " + TestName + " " + TestName);
}
// Now remove all runs in the sequence.
foreach (Run run in runs)
run.Remove();
// Signal to the replace engine to do nothing because we have already done all what we wanted.
return ReplaceAction.Skip;
}
private static Run SplitRun(Run run, int position)
{
Run afterRun = (Run)run.Clone(true);
afterRun.Text = run.Text.Substring(position);
run.Text = run.Text.Substring(0, position);
run.ParentNode.InsertAfter(afterRun, run);
return afterRun;
}
}

How to check for a specific value in List<T> foreach loop

Need help checking for a specific value in List<T> foreach loop. If there is specific value then display a specific string value.
For example how do I…
If (value.something_2 == "Null")
{
value.something_2 == ".";
}
Elseif (value.something_2 == " ")
{
value.something_2 == "0";
}
How would I incorporate the above example within the “foreach” loop?
See code below.
protected void MyReport(string filename, IMyRepository repository)
{
using (FileStream fileStream = new FileStream(Server.MapPath(#"~/Includes/") + filename, FileMode.Create))
{
using (StreamWriter writer = new StreamWriter(fileStream))
{
List<Report> _report = repository.GetMyReport().ToList();
foreach (var value in _report)
{
String row01 = String.Format("{0, -10}{1, 23}{2, 120}{3, 8}",
value.somthing_1,
values.something_2,
value.something_3);
String row02 = String.Format("{0, -10}{1, 23}{2, 120}{3, 8}",
value.somthing_4,
values.something_5,
value.something_6);
Writer.WriteLine(row01);
Writer.WriteLine(row02);
}
}
writer.Close();
}
}
There is no clever built-in String.Format that you can do for this if that's what you have in mind. However, the compiler has some tricks that can reduce the amount of code you need to write e.g.
// if it's null, assign it to "."
var s2 = value.something_2 ?? ".";
// it can never be null here, so if there is whitespace default to "0"
value.something_2 = String.IsNullOrWhitespace(s2) ? "0" : s2;
If I'm understanding what you're saying, it might be easier just to have another function returning the (possibly) modified string and just pass each of your values into it, inline.
Sting row01 = String.Format("{0, -10}{1, 23}{2, 120}{3, 8}", myFunc(value.somthing_1), myFunc(values.something_2), myFunc(value.something_3));
and then have this in the same class
private string myFunc(string something){
if (something == “Null”){
return “.“;
} else if (something == “ “) {
return “0”;
} else {
return something;
}
}

How to generate a document ID or Report ID of 8 characters in .net

Can someone point me to the preferred method for generating a report or document ID? I have been looking at maybe using a guid that would be reduced down to a shorter length. We have an application that creates an ID for reports that is about 8 characters long. They appear to be using some type of hash code. Probably using a base 36 encoding scheme. But I cant seem to find a way to make the hash code come out to a length of 8 characters since people have to use them to refer to the documents. They would also be used in a disconnected environment, so you couldnt look up the next usable serialized number in the chain. Just wondering what some of you use in applications like this?
The .net Framwork provides RNGCryptoServiceProvider class which Implements a cryptographic Random Number Generator (RNG) using the implementation provided by the cryptographic service provider (CSP). This class is usually used to generate random numbers. Although I can use this class to generate unique number in some sense but it is also not collision less. Moreover while generating key we can make key more complicated by making it as alpha numeric rather than numeric only. So, I used this class along with some character masking to generate unique key of fixed length.
private string GetUniqueKey()
{
int maxSize = 8 ;
int minSize = 5 ;
char[] chars = new char[62];
string a;
a = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
chars = a.ToCharArray();
int size = maxSize ;
byte[] data = new byte[1];
RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
crypto.GetNonZeroBytes(data) ;
size = maxSize ;
data = new byte[size];
crypto.GetNonZeroBytes(data);
StringBuilder result = new StringBuilder(size) ;
foreach(byte b in data )
{ result.Append(chars[__b % (chars.Length - )>); }
<span class="code-keyword">return result.ToString();
}
http://www.codeproject.com/Articles/14403/Generating-Unique-Keys-in-Net
This is what I ended up using. It is a base36 encoding. I borrowed parts of the code from other people, so I cant claim that I wrote it all, but I hope this helps others. This will produce about a 12 digit record ID, or unique ID for databases etc. It uses only the last 2 digits of the year, so it should be good for 100 years.
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace Base36Converter
{
public partial class Form1 : Form
{
private const string CharList = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
public Form1()
{
InitializeComponent();
}
//Base 36 number consists of only numbers and uppercase letters only.
private void button1_Click(object sender, EventArgs e)
{
if (textBox2.Text.Length > 0)
{
label3.Text = "";
//Get Date and Time Stamp
string temp1 = GetTimestamp(DateTime.Now);
//Turn it into a long number
long l = Convert.ToInt64(temp1);
//Now encode it as a base36 number.
string s1 = Encode(l);
//Get userID as a number, i.e. 1055 (User's index number) and create as a long type.
long l1 = Convert.ToInt64(textBox2.Text);
//Encode it as a base36 number.
string s2 = Encode(l1);
//Now display it as the encoded user number + datetime encoded number (Concatenated)
textBox1.Text = s2 + s1;
}
else
{
label3.Text = "User Number must be greater than 0. ie 1055";
}
}
public static String Encode(long input)
{
if (input < 0) throw new ArgumentOutOfRangeException("input", input, "input cannot be negative");
char[] clistarr = CharList.ToCharArray();
var result = new Stack<char>();
while (input != 0)
{
result.Push(clistarr[input % 36]);
input /= 36;
}
return new string(result.ToArray());
}
public static String GetTimestamp(DateTime value)
{
return value.ToString("yyMMddHHmmssffff");
}
private void Form1_Load(object sender, EventArgs e)
{
label3.Text = "";
}
}
}

How to show HTTP response in Object Choice Field in Blackberry

I am reading a response from server using this code.
public static String getContentString(HttpConnection Connection) throws IOException
{
String Entity=null;
InputStream inputStream;
inputStream = Connection.openInputStream();//May give network io
StringBuffer buf = new StringBuffer();
int c;
while ((c = inputStream.read()) != -1)
{
buf.append((char) c);
}
//Response Formation
try
{
Entity = buf.toString();
return Entity;
}
catch(NullPointerException e)
{
Entity = null;
return Entity;
}
}
I need to show this Entity in object choice field.
For example:
suppose i get response Entity=ThisIsGoingToGood
then, I need to show below way in object choice drop down list.
This
Is
Going
To
Good
Please tell me how to achieve this.
This solution assumes:
The Camel Case format of your strings will always start with an upper case letter.
Only one upper case character in a row is used, even if the word is an acronym. For example, "HTTP response" would be written as "HttpResponse".
public static Vector getContentStrings(HttpConnection connection) throws IOException {
Vector words = new Vector();
InputStream inputStream = connection.openInputStream();
StringBuffer buf = new StringBuffer();
int c;
while ((c = inputStream.read()) != -1)
{
char character = (char)c;
if (CharacterUtilities.isUpperCase(character)) {
// upper case -> new word
if (buf.length() > 0) {
words.addElement(buf.toString());
buf = new StringBuffer();
}
}
buf.append(character);
}
// add the last word
words.addElement(buf.toString());
return words;
}
Then, you'll have a nice Vector full of the choices for your ObjectChoiceField. You can then insert() them, as shown in Signare's answer.
Note: always remember to close your streams, too. I've leave it to you to decide when you're really finished with it, though.
With ref from Nate's Answer- try this -
ObjectListField ol = new ObjectListField(ObjectListField.ELLIPSIS);
ol.setSize(words.size()); //Where words is the vector
for (int i = 0; i < size; i++)
{
ol.insert(i, words.elementAt(i));
}
add(ol);

Resources