SSIS Formatting input from flat file - sql-server-2008-r2

I'm a SSIS newbie. I wanna format the inputs of my flat file before saving the entries in a database table. Initially I created a flat file as follows:-
"1","Superman","Metropolis"
"2","Batman","Gotham"
"3","Spiderman","New York"
"4","James Bond","London"
"5","Green Lantern","Oa"
The solution for stripping this was simple as shown here http://www.mssqltips.com/sqlservertip/1316/strip-double-quotes-from-an-import-file-in-integration-services-ssis/
But now i have created a new similar package and given my input file like this:-
"6", "TMNT", "Sewers NY"
"7", "Iron Man", "New York"
Note here I've put a space after the delimiting comma. Now when I follow the above method the first number field stripped of the double quotes, but rest of the entries retain their quotes. Any idea how to work around this? One suggestion on a similar question on stackoverflow mentioned use of a "Transformation script". Since I'm a newbie can anyone please throw light on this method?

Yes, you can use Script component transformation. Select all columns, and change them to ReadWrite. The code:
public override void Input0_ProcessInputRow(Input0Buffer Row)
{
Row.ID = Row.ID.Replace("\"", string.Empty);
Row.Movie = Row.Movie.Replace("\"", string.Empty);
Row.City = Row.City.Replace("\"", string.Empty);
}
If you want to trim the spaces you can use
Row.ID.Replace("\"", string.Empty).Trim();
You would also need to take care if you want to preserve the values that are " ". Please post if the suggestion was helpful or if you have any questions.

In the 'General' tab you can set a text qualifier of ". Then those quotes will be ignored.
Then you don't need to write error prone script when there is a simple solution.

Related

SharePoint REST: Column does not exist error

I'm calling a REST API with a SharePoint Designer workflow on SharePoint online. I'm setting the column name with a variable, and when i put the variable into my URL to call it it says "Column [name] does not exist".
Annoying part is when I call just /items? I can see the column in the result, but if I try select it or filter by it I get 'does not exist'. I have alot of these columns similarly named, and I get the error for all of them.
I am using the internal name, I have tried adding "OData_" to the front. I've tried typing the url manually in the browser and entering values (incase the variable was causing issues) but I get the same error, column does not exist. but i can see it them I call all items. :(
so I have quite a few columns with naming convention "[Q#] Score [#]" eg "Q4 Score 2". The internal name that's clearly appearing in the full items results is "Q4_x0020_Score_x0020_2".
This works:
https://MYSITE.sharepoint.com/sites/portal/intranet/CorpServices/QSR/_api/web/lists/GetbyTitle('Audit')/Items?
and I get big full normal REST results that includes the line:
0
However if I try:
https://MYSITE.sharepoint.com/sites/portal/intranet/CorpServices/QSR/_api/web/lists/GetbyTitle('Audit')/Items?$Select=Q4_x0020_Score_x0020_2
Then I get:
-2146232832, Microsoft.SharePoint.SPExceptionColumn 'Q4_x0020_Score_x0020_2' does not exist. It may have been deleted by another user.
I expect to be able to select that column (and the dozens like it) but none work. I've searched all similar problems on the forum and they've usually got a spelling mistake or forgot the ODATA_ but i cant seem to get the problem. Please help.
It seemed to be working intermittently.... So it was in fact just a naming error...... Half of the columns were [Q#]_x0020_Score_x0020_[#] and half were [Q#]_x0020_score_x0020_[#]. The word "Score" was capitalized on some and not others. I Didn't realize the HTTP Calls were case sensitive. Now I have added a bunch of if statements to handle the different variations haha. Thanks for reading.
Judging by the naming convention you are on an older version of SharePoint. The x0020 is the value for a space. Ideally when you first create the column you would name it without the spacing. For example UserInformation. Then come in and edit the name after the fact and call it User Information. If you click on the title name and look at the URL, you will see what the actual name of the column is at the end of the URL. It is case sensitive because you can have a column named score and Score which is ultimately why it wasn't working.
Must load all fields like this
var lists = context.Web.Lists;
context.Load(lists);
context.ExecuteQuery();
CamlQuery query = new CamlQuery();
query.ViewXml = #"";
var list = lists.GetById("file.guid");
var listitems = list.GetItems(query);
context.Load(listitems);
context.ExecuteQuery();
var creationInformation = new ListItemCreationInformation();
var newItem=list.AddItem(creationInformation);
var fields = list.Fields;
context.Load(fields);
context.ExecuteQuery();

Mongo query with regex fails when backslash\newline is there in a field

Hi I have a field in a user collection called "Address".User saving their address from a textarea in my application. mongodb convert it to new line like following.
{
"_id": ObjectId("56a9ba0ffbe0856d4f8b456d"),
"address": "HOUSE NO. 3157,\r\nSECTOR 50-D",
"pincode": "",
},
{
"_id": ObjectId("56a9ba0ffbe0856d4f8b456d"),
"address": "HOUSE NO. 3257,\r\nSECTOR 50-C",
"pincode": "",
}
So now When I am running a search query on the basis of "address".Like following:
guardianAdd = $dm->getRepository('EduStudentBundle:GuardianAddress')->findBy(array(
'address' => new \MongoRegex('/.*' .$data['address'] . '.*/i'),
'isDelete' => false
));
echo count($guardianAdd);die;
it does not give any result. My Searchi key word is : "HOUSE NO.3157 SECTOR 50-D".
However if I am searching using like: HOUSE NO. 3157 its giving correct result.
Please advice how to fix this.Thanks in advance
First of all, trailing .* are redundant. regexps /.*aaa.*/ and /aaa/ are identical and match the same pattern.
Second, you probably need to use multiline modifier /pattern/im
Finally, it is not quite clear what you want to fix. The best think you can do is to provide some basic explanation of regex syntax in the search form, so users can search properly, e.g. HOUSE NO.*3157.*SECTOR 50-D to get best results.
You can make some bold assumptions and build the pattern with something like
$pattern = implode('\W+',preg_split('/\W+/', $data['address']))
which will give you a regexp HOUSE\W+NO\W+3157\W+SECTOR\W+50\W+D for different kind of HOUSE NO.3157 SECTOR 50-D requests, but it will cut all the regex flexibility available with bare input, and eventually will result with unexpected response anyway. You can follow this slippery slope and end up with your own query DSL to compile to regex, but I doubt it can be any better or more convenient than pure regex. It will be more error prone for sure.
Asking right question to get right answers is true not only on SO, but also in your application. Unfortunately there is no general solution to search for something that people have in mind, but fail to ask. I believe that in your particular case best code is no code.

Elasticsearch mongodb river script in index doesn't work

I'm trying to change few fields strings using javascript.
For example take only the last part of the URL taken from mongo through the river so in elasticsearch I'll have only the end of it.
When creating the index (using curl) I added under "options" the following script:
"script": "ctx.document.shorturl = ctx.document.url.substr(-4);delete ctx.document.url;
I tried some manipulations such as adding \"...\" or use ctx['doc']['url'] and others but nothing seems to work.
I always get only url field with the full url (shorturl is not created at all).
Can anyone suggest what is the right syntax to make it work?
Another thing I need to do is combine to fields - lat & long, to one "location" field in order to use it in Kibana, can anyone suggest the right script for that? (create new field called "location" which contain both field "lat" & "long" with comma between them).
Thanks.
You did substring(-4), hence it will return the whole string. You should use substring(4) instead:
ctx.document.shorturl = ctx.document.url.substr(4);delete ctx.document.url;

How do I write a comment to a PropertiesConfiguration file?

Given one of these instances: org.apache.commons.configuration.PropertiesConfiguration I want to write a comment. How?
pc = new PropertiesConfiguration();
writeComment("this is a comment about the stuff below"); // HOW DO I WRITE THIS?
pc.addProperty("label0", myString);
writeComment("end of the stuff that needed a comment.");
Edit: I have a crude solution. Hopefully it can be improved upon.
Here's the best I could do. It leaves an extraneous line in the file.
pc = new PropertiesConfiguration();
writeComment(pc, "The following needed a comment so this is a comment.");
pc.addProperty(label0, stuff0);
writeComment(pc, "End of the stuff that needed a comment.");
...
private void writeComment(PropertiesConfiguration pc, String s)
{
String propertyName = String.format("%s%d", "comment", this.commentNumber++);
pc.getLayout().setComment(propertyName, s + " (" + propertyName + ")");
// make a dummy property
pc.addProperty(propertyName, ".");
// put in a dummy right-hand-side value so the = sign is not lonely
}
One of the problems with this approach is that the PropertiesConfiguration doc is a little vague about the layout. It does not explicitly say that the comment will appear above the dummy line so there seems to be the risk that PropertiesConfiguration is free to re-arrange the file on subsequent invocations. I have not even seen an guarantee that property line order is preserved so I cannot guarantee that the comment (and dummy line) will always be above the property that the comment applies to: property label0. Of course, I'm being a little paranoid here. However, the doc does say that layouts are not guaranteed to remain unmodified. Hopefully somebody can come up with something without the dummy line and a Java doc or website guarantee on the position of the comment relative to the property it is meant to comment on. Edit: You might wonder why I would create a dummy property instead of just attaching the comment to one of the properties that would already be in the file. The reason is because I want a comment to introduce a block of properties and changes (new ones, or a switch in the order) are possible. I don't want to create a maintenance problem. My comment should say "this is the section for data mining results" or "this is the section for the schedule" and I should never have to revisit this.
Comment like this?
# This is comment
The PropertiesConfiguration JavaDoc documents
Blank lines and lines starting with character '#' or '!' are skipped.
EDIT: Ok, you want to write the comment from code. Maybe - if you just need to write a property file - you can use the PropertiesConfiguration.PropertiesWriter and its writeComment method like this:
FileWriter writer = new FileWriter("test.properties");
PropertiesWriter propWriter = new PropertiesWriter(writer, ';');
propWriter.writeComment("Example properties");
propWriter.writeProperty("prop1","foo");
propWriter.writeProperty("prop2", "bar");
propWriter.close();
The property file will look like this:
# Example properties
prop1 = foo
prop2 = bar
Update
Summarized: The PropertiesConfiguration does not provide the functionality you are looking for.

How would I manipulate a string to display different parts?

All,
I have a string in a dictionary that's in an array that I need to manipulate a bit. The string returns: TEST-TEST-ABC_Dry_Cleaning-R12-01.
Here's what I need to do with it:
I need to pull out "ABC_Dry_Cleaning" and change it to "ABC Dry Cleaning" (no underscores)
I need to delete "TEST-TEST-"
I need to pull out "R12" and put that in a different string
I need to add "01" on to the end of "ABC Dry Cleaning" (looks like "ABC Dry Cleaning (01)")
How would I do these general things? There is much more that needs to be done, but once I know the way to do these tasks I can change it around for the others as needed. NOTE: "ABC_Dry_Cleaning" could be just "Red_Cups" or "McDonalds_Bag" - basically, a count of characters won't work.
Thanks!
If you're on iOS4+, consider using a regular expression to match the segments that you're interested in retaining. Take a look at the NSRegularExpression class.
You can then use the matches to build up the final string.