How to get properties in spark dataframe that starts with '#'? - scala

I call an api that I load in a dataframe and it's schema looks like that:
root
|-- #odata.count: integer (nullable = true)
|-- #odata.nextLink: string (nullable = true)
|-- value: array (nullable = true)
If I want to aces value property it that simple...
df.select(col("value"))
But if I want to aces #odata.count property with this piece of code:
df.select(col("#odata.count"))
I get this error: AnalysisException: cannot resolve '#odata.count' given input columns: [#odata.count, #odata.nextLink, value];
'Project ['#odata.count]
I believe that the issue is the '#' char at the begining of property, but because I didn't have any control to that, I should find a way to read it.
Do you have any idea how to overcome this issue? There is a way to take that value by position?
Thanks

Thanks to #anky, the solution is:
df.select(col("`#odata.count`"))

Related

getting description of bigquery table

I am trying to get the description of a bq table using the following:
val bigquery = BigQueryOptions.getDefaultInstance().getService()
val table = bigquery.getTable(tableId)
tableDescription = table.getDescription()
what I am thinking of what if not description is provided to the table I am trying to get the description from? It's written in the documenation that getDescription returns string, but what if there is no descprition provided? will it return null? and if yes, how do I avoid this using an option for example or any other way?
The documentation indicates that the method has the signature:
public String getDescription()
It's reasonable to assume that it might return null since we're dealing with a Java library. You can handle this safely by wrapping the expression as an Option:
tableDescription: Option[String] = Option(table.getDescription())

How can I use a string to load a variable stored in a .mat-file? [duplicate]

If for instance I have a variable xa=2, and then I construct a string by joining 'x' and 'a', how can I make this new string have the value 2?
xa=2;
var=strcat('x','a');
The result of this is var=xa, but what I want is var=2.
Thank you
Use eval():
var = eval(strcat('x','a'));
It will "evaluate" the string 'xa' and translate it to the value of the variable xa.
Source : MATLAB documentation

How exclude files from the module loader sails.js uses internally

I'm using sails.js with a module i created called def-inc to get some sort of inheritance into controllers and models via mixins. Now i want to store the mixins/traits in a traits folder inside models and controllers. I don't want to pollute the api root with another folder to hold my traits, so the ponint is that if it is possible to exclude a folder or a file, with out having to modify the core files?, or at least a way to override the module-loader and configure it to do this.
This is an example of the path structure i want to use, but without getting extra models/controllers.
.
|-- api
| |-- models
| | |-- traits
| | | |-- accountTraits.js
| | |-- User.coffee
| |-- controllers
| | |-- traits
| | | |-- restfullTraits.js
| | |-- UserController.js
Right now if i do that, i get an extra model called accountTraits (and a table if using mysql adapter).
I've checked the code and documentation, and so far this doesn't seem to be supported atm, but since probably it is a regular pattern (outside sails, rails, laravel, etc) to use other objects that are part of the model domain, but aren't specific db models, i assume that someone have done something similar.
Note: I know that for simplicity i can just move the traits folder to the api root path, and i don't consider traits to be part of services, so please avoid answering that, if it isn't possible, just comment my question.
EDIT:
Based on the code provided by #sgress454, i created this code, just to support loadModels too (Which works in the same way), and have a single fn to modify in case i want to apply the same behavior to other moduleLoader methods. Anyways, i'll leave it here just in case somebody needs it (But be sure to upvote #sgress454 :)
var liftOptions = rc('sails');
// Create the module loader override function
liftOptions.moduleLoaderOverride = function(sails, base) {
// Get a reference to the base loaders methods we want to extend
var baseLoadController = base.loadControllers;
var baseLoadModels = base.loadModels;
// Reusable fn to remove modules that match the defined pattern
var removeTraitsFromAutoLoadModules = function(cb, err, modules){
// Remove all modules whose identity ends with "traits"
modules = _.omit(modules, function(module, identity) {
return identity.match(/traits$/);
});
// Return the rest
return cb(err, modules);
};
return {
loadControllers: function (cb) {
baseLoadController(removeTraitsFromAutoLoadModules.bind(null, cb));
},
loadModels: function(cb) {
baseLoadModels(removeTraitsFromAutoLoadModules.bind(null, cb));
}
};
};
// Start server
sails.lift(liftOptions);
You can override the module loader by passing a moduleLoaderOverride function as an option to sails.lift. The function takes two arguments--a reference to the Sails instance, and an object containing the original module loader methods so that you can still call them. The function should return an object containing methods of the module loader that you'd like to override. For example:
// bottom of app.js
// Get the lift options from the .sailsrc file
var liftOptions = rc('sails');
// Include lodash (you may have to npm install it), or else rewrite
// below without the _.omit call
var _ = require('lodash');
// Create the module loader override function
liftOptions.moduleLoaderOverride = function(sails, base) {
// Get a reference to the base loadControllers method we want to extend
var baseLoadControllers = base.loadControllers;
return {
loadControllers: function (cb) {
// Load all of the controllers
baseLoadControllers(function(err, controllers) {
// Remove all controllers whose identity starts with "traits"
controllers = _.omit(controllers, function(controller, identity) {return identity.match(/^traits/);});
// Return the rest
return cb(err, controllers);
});
}
};
};
// Lift Sails
sails.lift(liftOptions);
You'll have to lift your app with node app.js for this to work--there's no way to put this in a regular configuration file and use sails lift, since those are loaded by the module loader!

Struct field name by referencing to a value in MATLAB

I am currently working on a struct in MATLAB and have a question regarding this.
Let us say i have declared a struct:
structVariable=struct('abc',[],'cde',[])
i.e.
structVariable =
abc: []
cde: []
Further I have a char variable,
charVariable='abc';
Now, I am trying to use structVariable.abc with something like
structVariable.charVariable =5;
but this does not work. Is it possible to reference to the value of charVariable with something like &charVariable as in c++ ?
This seems to be the easiest way:
structVariable.(charVariable) = 5;
To set the field value:
setfield(structVariable,charVariable,5)
To get the field value:
getfield(structVariable,charVariable)

Ext-gwt (gxt) TextField getFieldValue() problem

I have an TextField with datatype Integer, so I am trying to getFieldValue() and write it to Integer field. So in runtime I have an error here:
TextField<Integer> priceField = new TextField<Integer>();
Integer newPriceFieldValue = priceField.getValue(); //here is an error in runtime
So I cant understand whats the problem - proceField.getValue() should be Integer, why string? Maybe I should another type of Field?
java.lang.ClassCastException: java.lang.String cannot be cast to
java.lang.Integer
at
ru.braginini.client.ProductForm$2.componentSelected(ProductForm.java:64)
at
ru.braginini.client.ProductForm$2.componentSelected(ProductForm.java:1)
If you are expecting only numbers to be used in this field NumberField may be the better choice.
NumberField field = new NumberField();
field.setPropertyEditorType(Integer.class);
It will ensure only numbers are entered, and save you some casting & error handling on the getValue() call.
getValue returns a String!
You want to assign this String to an Integer which causes an CastException (like it would in any Type oriented Programming language.
Try
Integer newPriceFieldValue = Integer.parseInt(priceField.getValue());
Regards,
Stefan