How to convert BeamRecord into String in BeamSql - apache-beam

I'm converting a beamRecord into String . Below is the code snippet :
PCollection<BeamRecord> output_A = apps.apply(BeamSql.query("select Outlet from PCOLLECTION"));
output_A.apply(TextIO.write().to("gs://google_bucket/output/sbc.txt"));
output_A is in BeamRecord format and needs to get converted into String so that it can be written to output file .
Below is the error on output_A.apply:
The method apply(PTransform,OutputT>) in the type PCollection is not applicable for the arguments (TextIO.Write)
So my question is how to convert BeamRecord into String format.

You need to apply a transform to the pipeline which converts a BeamRecord to a String before applying the TextIO.Write transform. There are a number of ways to do this. You can convert the BeamRecord to a String in any way you want.
Simple toString().
public void processElement(ProcessContext c) {
// c.element() returns a BeamRecord
c.output(c.element().toString());
}
Or get specific field value(s) from the BeamRecord.
public void processElement(ProcessContext c) {
// c.element() returns a BeamRecord
c.output(c.element().getString("fieldName"));
}
Reference: BeamRecord

Related

how to create a Instant class variable in kotlin with my own timestamp

Till now i was using
val date = Instant.now(Clock.system(ZoneId.of("UTC")))
to generate the instant timestamp.
Now I need to substitute it with the date that I want to specify for example "2021-05-03T00:00:00.000Z". When i insert it as a string into the function, the idea gives me the error "Type mismatch. Required: Instant! Found: String". I can't change the function as I have no such access to it. So i need to somehow turn this date into "Instant!" class.
this is how the function that i can't change looks like
public TimeTZ(Instant timestamp, Boolean isLocal) {
this.timestamp = timestamp;
this.isLocal = isLocal;
}
val date = Instant.parse("2021-05-03T00:00:00.000Z")
Converting a string to an Instant (or other typed value) is called parsing. So use the parse method of Instant.

Object of type 'MyClass' cannot be converted to type 'System.Object[]' C#

I've been looking into this for a couple of hours but so far haven't gotten any luck.
Here's my C# code:
myClassInstance = new MyClass("MyParam", 1);
object[] args = new object[1] { myClassInstance };
MethodInfo methodInfo = GetType().GetMethod(myMethod, BindingFlags.NonPublic | BindingFlags.Instance);
string method = (string)methodInfo.Invoke(this, args);
I have MethodInfo and System.Reflection imported. The Unity error is this:
ArgumentException: Object of type 'SystemController' cannot be converted to type 'System.Object[]'
It doesn't point to a specific line in the code, but from what I can tell it seems to be an issue with converting the myClassInstance variable to an object, which doesn't make sense to me, as I believed everything in C# inherited from System.Object.
Here is MyClass:
public class MyClass
{
public string var1;
public int var2;
public MyClass(string param1, int param2)
{
var1 = param1;
var2 = param2;
}
}
Clearly, I'm not showing the entire class, but the only difference is that there are more variables and parameters to store. Those shouldn't change anything, so I won't bore you with them. It's just a class with a constructor, not inheriting from anything.
Any help I could get with this would be greatly appreciated. Let me know if you need more info.
The error here was me trying to pass the entire object[] array into my method as a parameter when I should have only passed the contents of the array. See here:
I was doing this:
void MyMethod(object[] args) {
MyClass instance = (MyClass)args[0];
...
}
But should've done this:
void MyMethod(MyClass myClassInstance) {
...
}
After reading some more documentation and reviewing the comments above I discovered that the .Invoke() method passes what's inside the args array instead of the entire array. At least, that's my current understanding, and it's what made my code work.
Thanks for the help.

Talend String handling convert "0.12900-" string to -0.12900 in float

Need help with the below conversion in Talend:
"0.12900-" string to -0.12900 in float via Tmap expression.
I am not well versed with Java hence the difficulty.
You could try something like this :
row1.column.contains("-")?Float.parseFloat( "-"+ StringHandling.LEFT(row1.column,row1.column.length()-1)):Float.parseFloat(row1.column)
Float.parseFloat allows you to convert a string to a float type.
StringHandling.LEFT gets the first characters of a string, here the total length-1.
Ternary operator controls if your string contains "-", otherwise you just have to parse the "-" symbol

Even though decimal digit are correct, assert are getting failed

I have those 2 values :
Expected Value (fResult)= 103393.431493782901937514
Actual Value (output) = 103393.431493782901937514
When I assert on the value, my expected result was consider as 103393.431493783m
Because of that my Assert was fails. Can anyone help in this regard.
Assert.That(output, Is.EqualTo(fResult));
More Information: both Actual and Expected values are Decimal data type,
[TestCase("value1", "value2", 5, 103393.431493782901937514)]
public void converFormulaforPressure(String Fromunit, String toUnit, decimal Avalue, decimal fResult) {
var output = ut1.Convert(ut1.GetUnit("Pressure", Fromunit), ut1.GetUnit("Pressure", toUnit), Avalue).Val;
Assert.That(output, Is.EqualTo(fResult));
}
ut1.Convert is a method which converts the value and give the Actual result .
You need to provide a tolerance level and you cannot pass a decimal as a TestCase parameter. The code you provided is passing the last value as a double, hence the rounding when you run the assert. You can solve this by using TestCaseSource instead.
The test below passes:
private static readonly object[] TestCases = {
new object[] {"value1", "value2", 5m, 103393.431493782901937514m}
};
[Test, TestCaseSource("TestCases")]
public void TestExample(string fromUnit, string toUnit, decimal value, decimal fResult) {
//Replace the line below with your convert method using values from testCase
var output = 103393.431493782901937514m;
Assert.That(output, Is.EqualTo(fResult).Within(0.00000000000001));
}

NTriplesParser extract textual value from string

I am using dotnetrdf and trying to parse some triples with NTriplesParser. I have my own handler RobHandler in which I process each triple in turn.
public class RobHandler : BaseRdfHandler
{
protected override bool HandleTripleInternal(Triple t)
{
string predicateUrl = ((BaseUriNode)(t.Predicate)).Uri.AbsoluteUri;
string value = t.Object.ToString();
}
}
This works fine but I want to get the object minus the language. My objects look like "Lincoln"#en. I could obviously write some code to remove the #en bit, but I'd rather use some library code rather than my own that hard-coded strings like #en. To do this I think I need to create a LiteralNode but there doesn't seem to be a way to get from a string which is what I have (my variable value) to a LiteralNode.
How can I extract just the textual value from an object string?
Actually I think I have the answer myself:
if (t.Object.NodeType == NodeType.Literal)
{
var node = (ILiteralNode)t.Object;
}