Is it possible to cast Json fields when loading them into pig? - json

I was wondering if there is a way to cast a field that comes in through the JsonLoader(). This is basically what I want but it doesn't work.
Person = LOAD 'people' USING JsonLoader() AS (name:chararray)

I haven't tried with JsonLoader, but i have faced a similar situation with HCatLoader.
There i did the casting in the second line.
Person_tmp = LOAD 'people' USING JsonLoader();
Person = FOREACH Person_tmp GENERATE name:chararray;
Just try it out. It might work out.

Related

Inserting and retrieving information from MySQL field in Laravel

I have a JSON field which I am experimenting with but I am having a bit of trouble with it.
I have added the following to my Customer model:
protected $casts = [
'billingConfig' => 'array'
];
And I updated a test field using the following in my controller:
$customer->billingConfig = ['attachableType' => $request->attachmentsConfig];
$customer->save();
After this, the following appears in my database:
{"attachableType": "combined"}
Now, when I go to grab this specific value through my blade:
{{$customer->billingConfig->attachableType}}
I get "Trying to get property 'attachableType' of non-object"
But when I use the below:
{{$customer->billingConfig['attachableType']}}
I get the "combined" value I was looking for.
I was using this guide: https://www.qcode.in/use-mysql-json-field-in-laravel/, and I guess I wanted to make sure I was doing everything right and their method was wrong or I had goofed up somewhere.
JSON data fields are being read as an array, which you can not call a property for, that's what I believe the reason for the error you got when called
{{$customer->billingConfig->attachableType}}
because it's an array, not a data object (like the case in javascript).
Thanks for sharing.

Django save/update not change data in database

Django 1.11.7
MySQL
I was trying to change the value of an object like this:
# change the value of the filed and save
def patch(...):
instance.field_name = new_name
instance.save()
print(instance.filed_name)
When I run the code I got the print result as new_name. But when I check the database manually I got the result as old_name.
Then I tried ways like:
instance.save(update_fields=['field'])
and
ModelName.objects.filter(id=instance.id).update(field_name=new_name)
but get the above problem as well. And meanwhile, the project runs perfectly functional except for this segment of code.
Any idea what caused this problem or suggestion on how to solve it?
Is that piece of code inside a transaction? Maybe the transaction gets rolledback somewhere later.
When you read from the DB are you inside a transaction? Some transaction modes may not show you this change.
Are you sure that field_name is the correct field name? Maybe you have a typo and you just set a property of the object without changing model field. From what I see you sometimes type "field_name" and sometimes "filed_name"

MySqlDataReader example with 2 sets of brackets

on the internet i saw an MySqlDataReader example.
It was saying something like:
read("products")("amount")
I know you can get data from field "product" from the reader by read("product")
but I didn't understand the second () set.
Nowhere info to be found about it.
Is it invalid syntax or a not documented option?
The .read method is used to advance the cursor forward. You can look up the spec online here: https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.read%28v=vs.110%29.aspx. I've never seen anything like that syntax myself and certainly the doc doesn't indicate it exists.
If you want to retrieve multiple column values in one go you can use .GetValues(). This will populate an array of objects so depending on what you want to do with the data you may need to cast them after retrieval.

Finding a specific value out of an Array (RoR / MySQL)

I am trying to find a specific value inside an array. The array is composed from data in MySQL database and looks like:
info = [#<Info1: bla, Info2: blo>,#<Info1: bli, Info2, Ble>]
Now I want to get every Info1's value from it, but I do not know how.
The array was formed by calling
info = Info.find(:all)
Can anyone help me?
I am using Rails 2.2.2 (don't ask, can't do anything about it) and Ruby 1.8.
Edit: More details
Info is a database, where Info1 and info 2 are the columns. Calling it with info = Info.find(:all) returns the array above.
What I have tried so far involves trying to go through the array with each, but so far no luck.
Most of what I have tried like
a.grep(/^info1/)
and
info.select(|i| i.name == "info1")
all return empty arrays
Edit
Nevermind, I found the answer. I was thinking too weird. The answer is
info.each do |object|
puts object.info2
end
What's your selection criteria? You can do something like
info.select{|i| i.name == 'hello' }
and you will get all the Info objects with name = 'hello'.
But I would prefer to change the query, if you can, to filter them in the database query directly.

Make the JSON object empty again

When I get the data thru socket in node js, I use the data and before writing new data to that JSON, I need to delete some parts of it, but there is too many different things would be deleted. I want to make JSON object clear, erase everything before start adding anything to it.
So I have tried something like:
JSON_object = {}; // does nothing
JSON_object = null; // error
JSON_object.empty(); // error...
and so on..
Nothing works.
Does any1 know how to make it empty?