I am having real difficulty referencing messages within this hash.
[
{
"MessageThreadID": 1,
"CustomerID": 171,
"MessageType": 1,
"Subject": "Test Message",
"OpenDate": "2015-09-17T00:00:00",
"Closed": false,
"ClosedDate": null,
"Messages": [
{
"IBMessageID": 1,
"MessageThreadID": 1,
"MessageText": "Test Message",
"FromCustomer": true,
"UserID": null,
"Date": "2015-09-17T17:23:00"
},
{
"IBMessageID": 2,
"MessageThreadID": 1,
"MessageText": "Test this Update",
"FromCustomer": false,
"UserID": 1,
"Date": "2015-09-17T17:23:00"
},
{
"IBMessageID": 3,
"MessageThreadID": 1,
"MessageText": "My New Message",
"FromCustomer": false,
"UserID": 1,
"Date": "2015-09-17T17:23:00"
},
{
"IBMessageID": 4,
"MessageThreadID": 1,
"MessageText": "Reply",
"FromCustomer": false,
"UserID": 1,
"Date": "2015-09-17T17:05:00"
},
{
"IBMessageID": 5,
"MessageThreadID": 1,
"MessageText": "Some sensible shit",
"FromCustomer": false,
"UserID": 1,
"Date": "2015-09-17T17:23:00"
},
{
"IBMessageID": 14,
"MessageThreadID": 1,
"MessageText": "Message 2",
"FromCustomer": true,
"UserID": null,
"Date": "2015-09-21T14:10:00"
},
{
"IBMessageID": 16,
"MessageThreadID": 1,
"MessageText": "Message 2",
"FromCustomer": true,
"UserID": null,
"Date": "2015-09-22T16:22:00"
},
{
"IBMessageID": 25,
"MessageThreadID": 1,
"MessageText": "Added via abacus\r\n",
"FromCustomer": false,
"UserID": 1,
"Date": "2015-09-22T16:22:00"
},
{
"IBMessageID": 26,
"MessageThreadID": 1,
"MessageText": "sdsdsdsd",
"FromCustomer": true,
"UserID": null,
"Date": "2015-09-22T16:40:00"
},
{
"IBMessageID": 27,
"MessageThreadID": 1,
"MessageText": "test",
"FromCustomer": true,
"UserID": null,
"Date": "2015-09-22T17:02:00"
},
{
"IBMessageID": 28,
"MessageThreadID": 1,
"MessageText": "test",
"FromCustomer": true,
"UserID": null,
"Date": "2015-09-22T17:06:00"
}
]
},
{
"MessageThreadID": 5,
"CustomerID": 171,
"MessageType": 1,
"Subject": "Unit Test Thread",
"OpenDate": "2015-09-21T13:11:00",
"Closed": false,
"ClosedDate": null,
"Messages": [
{
"IBMessageID": 12,
"MessageThreadID": 5,
"MessageText": "Test Message",
"FromCustomer": true,
"UserID": null,
"Date": "2015-09-21T13:11:00"
}
]
},
{
"MessageThreadID": 11,
"CustomerID": 171,
"MessageType": 0,
"Subject": "Test5",
"OpenDate": "2015-09-22T14:39:00",
"Closed": false,
"ClosedDate": null,
"Messages": [
{
"IBMessageID": 22,
"MessageThreadID": 11,
"MessageText": "Test5",
"FromCustomer": true,
"UserID": null,
"Date": "2015-09-22T14:39:00"
}
]
},
{
"MessageThreadID": 13,
"CustomerID": 171,
"MessageType": 0,
"Subject": "Test6",
"OpenDate": "2015-09-22T14:41:00",
"Closed": false,
"ClosedDate": null,
"Messages": [
{
"IBMessageID": 24,
"MessageThreadID": 13,
"MessageText": "Test6",
"FromCustomer": true,
"UserID": null,
"Date": "2015-09-22T14:41:00"
}
]
},
{
"MessageThreadID": 12,
"CustomerID": 171,
"MessageType": 0,
"Subject": "Test5",
"OpenDate": "2015-09-22T14:41:00",
"Closed": false,
"ClosedDate": null,
"Messages": [
{
"IBMessageID": 23,
"MessageThreadID": 12,
"MessageText": "Test5",
"FromCustomer": true,
"UserID": null,
"Date": "2015-09-22T14:41:00"
}
]
},
{
"MessageThreadID": 14,
"CustomerID": 171,
"MessageType": 1,
"Subject": "Test",
"OpenDate": "2015-09-23T12:24:00",
"Closed": false,
"ClosedDate": null,
"Messages": [
{
"IBMessageID": 29,
"MessageThreadID": 14,
"MessageText": "Test",
"FromCustomer": true,
"UserID": null,
"Date": "2015-09-23T12:24:00"
}
]
},
{
"MessageThreadID": 15,
"CustomerID": 171,
"MessageType": 1,
"Subject": "Accounts Test",
"OpenDate": "2015-09-23T15:35:00",
"Closed": false,
"ClosedDate": null,
"Messages": [
{
"IBMessageID": 30,
"MessageThreadID": 15,
"MessageText": "Accounts Test",
"FromCustomer": true,
"UserID": null,
"Date": "2015-09-23T15:35:00"
}
]
},
{
"MessageThreadID": 16,
"CustomerID": 171,
"MessageType": 2,
"Subject": "Savings ",
"OpenDate": "2015-09-23T15:36:00",
"Closed": false,
"ClosedDate": null,
"Messages": [
{
"IBMessageID": 31,
"MessageThreadID": 16,
"MessageText": "Savings",
"FromCustomer": true,
"UserID": null,
"Date": "2015-09-23T15:36:00"
}
]
},
{
"MessageThreadID": 17,
"CustomerID": 171,
"MessageType": 2,
"Subject": "Savings ",
"OpenDate": "2015-09-23T15:37:00",
"Closed": false,
"ClosedDate": null,
"Messages": [
{
"IBMessageID": 32,
"MessageThreadID": 17,
"MessageText": "Savings",
"FromCustomer": true,
"UserID": null,
"Date": "2015-09-23T15:37:00"
}
]
}
]
My initial call is performed by the following code:
def open_contact_messages(customer_id)
customer_id = #customer.id
response = get_call('/Messages/GetOpenedMessages/' + customer_id.to_s)
response = JSON.parse(response.body)
prettyresponse = JSON.pretty_generate(response)
#openmessages = {}
#openmessages = response.map do |openmessage|
Contact.new(openmessage)
end
return #openmessages
end
The output shown above is from prettyresponse. In my initializer I have the following code. ibmessages is my attempt at referencing the data within messages but i get a cant convert string into integer error.
class Contact
attr_accessor :message_customer_ID, :message_type, :message_subject, :message_text, :message_source, :message_thread_ID
attr_reader :messages
def initialize(options)
#message_customer_ID = options['CustomerID'].to_s
#message_type = options['MessageType'].to_s
#message_subject = options['Subject'].to_s
#message_source = options['Closed'].to_s
#message_thread_ID = options['MessageThreadID'].to_s
#messages = options['Messages']
end
end
In my view I can output the entire hash but referencing the messages hash is proving difficult.
- open_contact_messages(#customer.id).each do |openmessage|
.message-container
%p.message-intro
Filler said....
.message
%h4.message-header.message-contents= "Message Subject : #{openmessage.message_subject}"
%p.message-body.message-text= "Message Text : #{openmessage.message_text}"
%p.message-body.message-source= "Message Source : #{openmessage.message_source}"
%p.message-body.message-thread-ID= "Message Thread ID #{openmessage.message_thread_ID}"
%p.message-body.message-thread-ID= "Message Test"
%p.message-body.message-thread-ID
- openmessage.messages do |message|
= message['MessageText']
I am getting a variety of errors based on the many approaches I have found online but no solution. The most common error is cannot convert integer to string.
What you have here is an array of arrays, i.e. you have a bunch of message threads, each of them contains a bunch of messages.
With this code
#openmessages = response.map do |openmessage|
Contact.new(openmessage)
end
you create a Contact instance, and provide it a message thread, i.e. this part:
{
"MessageThreadID": 1,
"CustomerID": 171,
"MessageType": 1,
"Subject": "Test Message",
"OpenDate": "2015-09-17T00:00:00",
"Closed": false,
"ClosedDate": null,
"Messages": [ ... ]
}
In your constructor, you may directly assign from options hash only keys listed above, i.e.
#message_customer_ID = options['CustomerID'].to_s # ok
#message_type = options['MessageType'].to_s # ok
#message_subject = options['Subject'].to_s # ok
#message_text = options['MessageText'].to_s # not ok
#message_source = options['Closed'].to_s # ok
#message_thread_ID = options['MessageThreadID'].to_s # ok
#messages = options['Messages'].to_s # not ok
#ibmessages = options['Messages']['IBMessageID'].to_s # not ok
Messages key contains an array of messages, so you can't just convert them to string, but instead you need to store them as array, so you may process them further.
If you want to output an array of your messages, you need to change #messages assigning to an array, like this:
#messages = options['Messages']
Add a reader method to your Contact class:
class Contact
attr_reader :messages
end
And after that you can use this reader in your view:
- openmessage.messages do |message|
= message['MessageText']
Please, note to use - in front of openmessage.messages do |message| line, exactly as in my code above, and not =. The difference between them is that = writes the result to the buffer (i.e. outputs something), while - just runs some ruby code. In this context you don't want to write whole openmessage.messages array in your view in a serialized form, but instead to process it in a loop.
Related
I am attempting to scope an answer to a User or Project via a polymorphic relationship via multiple levels but I am having some trouble getting my head around it.
It's tricky to explain, but I will show you what I have at the moment, minus the Answer model/relationship.
I have a Category and a Role model. These are in a belongsTo relationship (a role belongs to a category).
I also have a Question model, this also has a belongsTo relationship with a Role (a question belongs to a role).
I have created a roleables polymorphic relationship, allowing me to assign roles to either a User or a Project.
All of this is working as expected. I am able to return the assigned roles via my endpoint user/1/roles or project/1/roles, and in this endpoint, I am able to return the questions for that role (see example output below).
{
"data": [
{
"id": 20,
"name": "Actor",
"entity_id": 1,
"category_id": 3,
"questions": [
{
"id": 20,
"role_id": 20,
"field_type": "text",
"field_name": "Height",
"field_default_value": null,
"field_validation_rules": "required,string,max:5",
"field_options": false,
"order": 0
},
{
"id": 21,
"role_id": 20,
"field_type": "text",
"field_name": "Weight",
"field_default_value": null,
"field_validation_rules": "required,string,max:5",
"field_options": false,
"order": 1
},
{
"id": 22,
"role_id": 20,
"field_type": "select",
"field_name": "Race",
"field_default_value": null,
"field_validation_rules": "required,string,max:255",
"field_options": [
{
"id": "white",
"name": "White"
},
{
"id": "mixed",
"name": "Mixed"
}
],
"order": 2
},
{
"id": 23,
"role_id": 20,
"field_type": "select",
"field_name": "Gender",
"field_default_value": null,
"field_validation_rules": "required,string,max:255",
"field_options": [
{
"id": "male",
"name": "Male"
},
{
"id": "female",
"name": "Female"
},
{
"id": "other",
"name": "Other"
}
],
"order": 3
}
],
"created_at": {
"date": "2019-07-24 13:35:33.000000",
"timezone_type": 3,
"timezone": "UTC"
},
"updated_at": {
"date": "2019-07-24 13:35:33.000000",
"timezone_type": 3,
"timezone": "UTC"
},
"entity_type": "user"
},
{
"id": 23,
"name": "Singer",
"entity_id": 1,
"category_id": 3,
"questions": [
{
"id": 26,
"role_id": 23,
"field_type": "text",
"field_name": "Description",
"field_default_value": null,
"field_validation_rules": "required,string,max:255",
"field_options": false,
"order": 0
}
],
"created_at": {
"date": "2019-07-24 13:35:33.000000",
"timezone_type": 3,
"timezone": "UTC"
},
"updated_at": {
"date": "2019-07-24 13:35:33.000000",
"timezone_type": 3,
"timezone": "UTC"
},
"entity_type": "user"
},
{
"id": 32,
"name": "Console Operator",
"entity_id": 1,
"category_id": 5,
"questions": [
{
"id": 35,
"role_id": 32,
"field_type": "text",
"field_name": "Description",
"field_default_value": null,
"field_validation_rules": "required,string,max:255",
"field_options": false,
"order": 0
}
],
"created_at": {
"date": "2019-07-24 13:35:33.000000",
"timezone_type": 3,
"timezone": "UTC"
},
"updated_at": {
"date": "2019-07-24 13:35:33.000000",
"timezone_type": 3,
"timezone": "UTC"
},
"entity_type": "user"
},
{
"id": 60,
"name": "Composer",
"entity_id": 1,
"category_id": 7,
"questions": [
{
"id": 63,
"role_id": 60,
"field_type": "text",
"field_name": "Description",
"field_default_value": null,
"field_validation_rules": "required,string,max:255",
"field_options": false,
"order": 0
}
],
"created_at": {
"date": "2019-07-24 13:35:33.000000",
"timezone_type": 3,
"timezone": "UTC"
},
"updated_at": {
"date": "2019-07-24 13:35:33.000000",
"timezone_type": 3,
"timezone": "UTC"
},
"entity_type": "user"
}
]
}
The problem that I am trying to get my head around is Answers to the questions. Just a little background, which may help me explain this in writing:
In the front-end, I have a page in which the user can update their information, on this page they also have the ability to assign themselves some roles. When the click on a role the list of questions is displayed and they have the ability to answer them.
The same is also true for a Project. A user can create projects, and have the ability to assigned roles and answer the relevant questions.
So, I guess I will need to create a new table in the database to store the answers.
The question is how/what relationship should this new answers table have to the question, user/project in order to get all the roles, questions and any answers scoped to the entity (user/project) and it's roles?
I would love the each role in the output from my user/1/roleendpoint to look more like the following (notice the new answers key in questions):
{
"id": 60,
"name": "Composer",
"entity_id": 1,
"category_id": 7,
"questions": [
{
"id": 63,
"role_id": 60,
"field_type": "text",
"field_name": "Description",
"field_default_value": null,
"field_validation_rules": "required,string,max:255",
"field_options": false,
"order": 0,
"answers": { ... }
}
],
"created_at": {
"date": "2019-07-24 13:35:33.000000",
"timezone_type": 3,
"timezone": "UTC"
},
"updated_at": {
"date": "2019-07-24 13:35:33.000000",
"timezone_type": 3,
"timezone": "UTC"
},
"entity_type": "user"
}
i have a collection data
{
"success": true,
"doctor": [
{
"id": 1,
"name": "Dr. Mayank",
"dob": "1975-01-01",
"about": "This is description",
"status": 1,
"rating": 2,
"rating_given_by": 1,
"alternative_number": "7686876876",
"profile_photo": [],
"speciality": [
{
"id": 3,
"name": "Acupuncture",
"image": null,
"dashboard_flag": 1
},
{
"id": 4,
"name": "Acupuncturist",
"image": null,
"dashboard_flag": 1
},
{
"id": 1,
"name": "Accident and emergency medicine",
"image": "http://192.168.16.21/remidify/media/174/detail.png",
"dashboard_flag": 1
}
],
"service": [
{
"id": 78,
"name": "Correction of gummy smile",
"cost": "12.00"
},
{
"id": 77,
"name": "Dental aesthetics",
"cost": "43.00"
}
],
"clinics": [
{
"id": 1,
"name": "akram",
"entity_id": 1,
"entity_type": "App\Doctor",
"contact_number": "2132132132132",
"status": 0,
"consultancy_fee": "12.00",
"available_today": "No",
"owner_name": "Dr. Mayank",
"pivot": {
"doctor_id": 1,
"clinic_id": 1
},
"address": {
"id": 1,
"address_1": "asdasdasdsa",
"address_2": "",
"locality": "downtown",
"city": "noida",
"state": "up",
"postal_code": "41561566"
},
"speciality": [],
"service": [
{
"id": 11,
"name": "Laminates",
"cost": "20.00"
},
{
"id": 12,
"name": "Dental surgery",
"cost": "300.00"
}
],
"clinic_image": [
{
"id": 7,
"model_id": 1,
"model_type": "App\Clinic",
"collection_name": "clinic_image",
"file_name": "1494863957588.566162.jpg",
"disk": "media",
"url": "http://192.168.16.21/remidify/media/7/1494863957588.566162.jpg"
}
],
"id_image": [
{
"id": 8,
"model_id": 1,
"model_type": "App\Clinic",
"collection_name": "id_image",
"file_name": "1494863966218.348877.jpg",
"disk": "media",
"url": "http://192.168.16.21/remidify/media/8/1494863966218.348877.jpg"
}
],
"location": {
"id": 1,
"latitude": 0,
"longitude": 0,
"entity_id": 1,
"entity_type": "App\Clinic",
"created_at": "2017-05-16 03:00:10",
"updated_at": "2017-05-16 03:00:10"
},
"clinic_timings": [
{
"day": "sun",
"opens_at": "09:28:00",
"closes_at": "21:28:00"
}
]
}
],
"education": [
{
"id": 19,
"degree": "MBBS",
"university": "Univercity",
"year": "2017",
"entity_id": 1,
"entity_type": "App\Doctor",
"created_at": "2017-05-16 05:44:11",
"updated_at": "2017-05-16 05:44:11",
"location": "Delhi"
}
],
"experience": [
{
"id": 19,
"hospital": "Hospital name",
"post": "pta ni hai",
"from": "1970-01-01",
"to": "0000-00-00",
"entity_id": 1,
"entity_type": "App\Doctor",
"created_at": "2017-05-16 05:44:12",
"updated_at": "2017-05-16 05:44:12",
"location": "Locations12",
"is_currently_working": 1
}
],
"registration": {
"id": 1,
"registration_number": "Reg # 2324324",
"registration_year": 1975,
"registration_council": "Council",
"experience": null,
"doctor_id": 1,
"created_at": "2017-05-16 02:56:37",
"updated_at": "2017-05-16 02:56:37",
"adhaar_number": "232131231232",
"id_proof": [
{
"id": 2,
"model_id": 1,
"model_type": "App\DoctorRegistration",
"collection_name": "id_proof",
"file_name": "1494863680447.329102.jpg",
"disk": "media",
"url": "http://192.168.16.21/remidify/media/2/1494863680447.329102.jpg"
}
],
"registration_proof": [
{
"id": 3,
"model_id": 1,
"model_type": "App\DoctorRegistration",
"collection_name": "registration_proof",
"file_name": "1494863687436.266846.jpg",
"disk": "media",
"url": "http://192.168.16.21/remidify/media/3/1494863687436.266846.jpg"
}
],
"qualification_proof": [
{
"id": 4,
"model_id": 1,
"model_type": "App\DoctorRegistration",
"collection_name": "qualification_proof",
"file_name": "1494863695576.803955.jpg",
"disk": "media",
"url": "http://192.168.16.21/remidify/media/4/1494863695576.803955.jpg"
}
]
},
"preference": {
"availability": 1,
"appointment_confirmation_method": "manual",
"average_time": 7,
"holiday_from": null,
"holiday_till": null,
"patients_per_hour": null,
"preferred_appointment_type": "timeslot",
"appointment_frequency": null,
"preferred_payment_method": [
{
"payment_method": "cash"
},
{
"payment_method": "online"
}
]
},
"user": null,
"doctor_clinic": [
{
"doctor_id": 1,
"clinic_id": 1,
"consultancy_fee": "12.00",
"deleted_at": null,
"workdays": [
{
"day": "sun",
"available": 1,
"workhours": [
{
"from": "09:28:00",
"to": "21:28:00"
}
]
}
],
"service": []
}
]
}
]
}
Now how can i find the doctors whose "about or specialty name" matches with some given search string.
Thanks in advance.
Try using dd() helper and use like this below
$youcollectionvariables = { "success": true, "doctor": [ { "id": 1, "name": "Dr. Mayank", "dob": "1975-01-01"................. }
dd($youcollectionvariables)
you will see a formatted data and can find what you need clearly.
Hope that helps.
I have few tables, they are room_types, rooms, rates, prices I want to get
room_types with rooms with rates and rates with prices where(price room_type_id = room_types.id) . I tried to do something like that
$roomTypeIds = [];
foreach ($hotel->rooms as $room) {
array_push($roomTypeIds, $room->roomtype->id);
}
$roomsByType = RoomType::with(['rooms' => function ($query) {
$query->with(['rates' => function($q) {
$q->with('policy', 'prices');
}]);
}])->whereIn('id', array_unique($roomTypeIds))->get();
but in this example Im getting all prices but I want to get only that prices which room_type_id IS EQUAL TO current room_type.id. So for that I have tried to do some think like this
$roomsByType = RoomType::with(['rooms' => function ($query) {
$query->with(['rates' => function($q) {
$q->with(['policy', 'prices' => function($q1) {
$q1->where('room_type_id', '');
}]);
}]);
}])->whereIn('id', array_unique($roomTypeIds))->get();
But I don't know what to put in where clouse. So are there any way to do this?
And result that I want to have is
[
{
"id": 2,
"name": "Twin/Double",
"created_at": "2017-12-11 08:56:16",
"updated_at": "2017-12-11 08:56:16",
"rooms": [
{
"id": 4,
"hotel_id": 1,
"room_type_id": 2,
"custom_name": null,
"room_name": "3",
"number_of_type": 2,
"number_of_bedrooms": null,
"number_of_livingrooms": null,
"number_of_bathrooms": null,
"created_at": "2017-12-12 06:37:34",
"updated_at": "2017-12-12 06:37:34",
"rates": [
{
"id": 4,
"user_id": 19,
"custom_name": "Default rate",
"price": 0,
"automatic": 0,
"rate_id": null,
"operand": null,
"amount": null,
"currency": null,
"policy_id": 1,
"meal_types": null,
"created_at": "2017-12-12 09:27:31",
"updated_at": "2017-12-12 09:27:29",
"pivot": {
"room_id": 4,
"rate_id": 4
},
"policy": {
"id": 1,
"name": "Free cancellation before 3 / 60 %",
"hotel_id": 1,
"free": 1,
"before_day": 3,
"before_day_price": 60,
"until_day_price": null,
"created_at": "2017-12-08 14:03:31",
"updated_at": "2017-12-08 14:03:31"
},
"prices": [
{
"id": 1,
"rate_id": 4,
"room_type_id": 2,
"from": "2017-12-01 09:18:46",
"to": "2017-12-18 09:18:57",
"amount": 100,
"created_at": "2017-12-18 09:19:11",
"updated_at": "2017-12-18 09:19:12"
},
{
"id": 3,
"rate_id": 4,
"room_type_id": 3,
"from": "2017-12-22 10:36:30",
"to": "2017-12-30 10:36:35",
"amount": 3000,
"created_at": null,
"updated_at": null
}
]
}
]
},
{
"id": 5,
"hotel_id": 1,
"room_type_id": 2,
"custom_name": null,
"room_name": "3",
"number_of_type": 2,
"number_of_bedrooms": null,
"number_of_livingrooms": null,
"number_of_bathrooms": null,
"created_at": "2017-12-12 06:37:34",
"updated_at": "2017-12-12 06:37:34",
"rates": [
{
"id": 4,
"user_id": 19,
"custom_name": "Default rate",
"price": 0,
"automatic": 0,
"rate_id": null,
"operand": null,
"amount": null,
"currency": null,
"policy_id": 1,
"meal_types": null,
"created_at": "2017-12-12 09:27:31",
"updated_at": "2017-12-12 09:27:29",
"pivot": {
"room_id": 5,
"rate_id": 4
},
"policy": {
"id": 1,
"name": "Free cancellation before 3 / 60 %",
"hotel_id": 1,
"free": 1,
"before_day": 3,
"before_day_price": 60,
"until_day_price": null,
"created_at": "2017-12-08 14:03:31",
"updated_at": "2017-12-08 14:03:31"
},
"prices": [
{
"id": 1,
"rate_id": 4,
"room_type_id": 2,
"from": "2017-12-01 09:18:46",
"to": "2017-12-18 09:18:57",
"amount": 100,
"created_at": "2017-12-18 09:19:11",
"updated_at": "2017-12-18 09:19:12"
},
{
"id": 3,
"rate_id": 4,
"room_type_id": 3,
"from": "2017-12-22 10:36:30",
"to": "2017-12-30 10:36:35",
"amount": 3000,
"created_at": null,
"updated_at": null
}
]
}
]
}
]
}
]
Look at the whereHas function instead of using with
https://laravel.com/docs/5.5/eloquent-relationships#querying-relations
How to create hive table for the below twitter JSON data that is available in HDFS path. I tried some of the query from the web to create the table, but facing some issue.
{
"extended_entities": {
"media": [{
"display_url": "pic.twitter.com/9SoA83sVvP",
"indices": [100, 123],
"sizes": {
"small": {
"w": 340,
"h": 340,
"resize": "fit"
},
"large": {
"w": 480,
"h": 480,
"resize": "fit"
},
"thumb": {
"w": 150,
"h": 150,
"resize": "crop"
},
"medium": {
"w": 480,
"h": 480,
"resize": "fit"
}
},
"id_str": "685710180164579329",
"expanded_url": "http://twitter.com/add7dave/status/685710518456209408/video/1",
"media_url_https": "https://pbs.twimg.com/ext_tw_video_thumb/685710180164579329/pu/img/4wOqavTprNIaMgjK.jpg",
"id": 685710180164579329,
"type": "video",
"media_url": "http://pbs.twimg.com/ext_tw_video_thumb/685710180164579329/pu/img/4wOqavTprNIaMgjK.jpg",
"url": "https://t.co/9SoA83sVvP",
"video_info": {
"aspect_ratio": [1, 1],
"duration_millis": 7567,
"variants": [{
"content_type": "application/x-mpegURL",
"url": "https://video.twimg.com/ext_tw_video/685710180164579329/pu/pl/6JnchC_1FWviydJV.m3u8"
}, {
"content_type": "application/dash+xml",
"url": "https://video.twimg.com/ext_tw_video/685710180164579329/pu/pl/6JnchC_1FWviydJV.mpd"
}, {
"content_type": "video/mp4",
"bitrate": 320000,
"url": "https://video.twimg.com/ext_tw_video/685710180164579329/pu/vid/240x240/W7suov-YC1Iq1-QT.mp4"
}, {
"content_type": "video/webm",
"bitrate": 832000,
"url": "https://video.twimg.com/ext_tw_video/685710180164579329/pu/vid/480x480/bDG_UfEw3jBM7z4e.webm"
}, {
"content_type": "video/mp4",
"bitrate": 832000,
"url": "https://video.twimg.com/ext_tw_video/685710180164579329/pu/vid/480x480/bDG_UfEw3jBM7z4e.mp4"
}]
}
}]
},
"in_reply_to_status_id_str": null,
"in_reply_to_status_id": null,
"created_at": "Sat Jan 09 06:31:42 +0000 2016",
"in_reply_to_user_id_str": null,
"source": "<a href=\"http://twitter.com/download/android\" rel=\"nofollow\">Twitter for Android<\/a>",
"retweet_count": 0,
"retweeted": false,
"geo": null,
"filter_level": "low",
"in_reply_to_screen_name": null,
"is_quote_status": false,
"id_str": "685710518456209408",
"in_reply_to_user_id": null,
"favorite_count": 0,
"id": 685710518456209408,
"text": "New video NO-17\n#BritanniaFilmfareAwards\n#GoodDayCookies\n#BritanniaIndLtd\nAmitabh Bachchan dialogue https://t.co/9SoA83sVvP",
"place": null,
"lang": "en",
"favorited": false,
"possibly_sensitive": false,
"coordinates": null,
"truncated": false,
"timestamp_ms": "1452321102142",
"entities": {
"urls": [],
"hashtags": [{
"indices": [16, 40],
"text": "BritanniaFilmfareAwards"
}],
"media": [{
"display_url": "pic.twitter.com/9SoA83sVvP",
"indices": [100, 123],
"sizes": {
"small": {
"w": 340,
"h": 340,
"resize": "fit"
},
"large": {
"w": 480,
"h": 480,
"resize": "fit"
},
"thumb": {
"w": 150,
"h": 150,
"resize": "crop"
},
"medium": {
"w": 480,
"h": 480,
"resize": "fit"
}
},
"id_str": "685710180164579329",
"expanded_url": "http://twitter.com/add7dave/status/685710518456209408/video/1",
"media_url_https": "https://pbs.twimg.com/ext_tw_video_thumb/685710180164579329/pu/img/4wOqavTprNIaMgjK.jpg",
"id": 685710180164579329,
"type": "photo",
"media_url": "http://pbs.twimg.com/ext_tw_video_thumb/685710180164579329/pu/img/4wOqavTprNIaMgjK.jpg",
"url": "https://t.co/9SoA83sVvP"
}],
"user_mentions": [{
"indices": [41, 56],
"screen_name": "GoodDayCookies",
"id_str": "2197439803",
"name": "Britannia Good Day",
"id": 2197439803
}, {
"indices": [57, 73],
"screen_name": "BritanniaIndLtd",
"id_str": "3281245460",
"name": "Britannia Industries",
"id": 3281245460
}],
"symbols": []
},
"contributors": null,
"user": {
"utc_offset": 19800,
"friends_count": 1517,
"profile_image_url_https": "https://pbs.twimg.com/profile_images/593327096736256001/TT8Ds75__normal.jpg",
"listed_count": 1,
"profile_background_image_url": "http://abs.twimg.com/images/themes/theme19/bg.gif",
"default_profile_image": false,
"favourites_count": 25,
"description": "Sharukhan, Kapil sharma , Narendra modi Fan (Supporter) be happy *↓*",
"created_at": "Thu Sep 15 08:04:58 +0000 2011",
"is_translator": false,
"profile_background_image_url_https": "https://abs.twimg.com/images/themes/theme19/bg.gif",
"protected": false,
"screen_name": "add7dave",
"id_str": "373836462",
"profile_link_color": "9266CC",
"id": 373836462,
"geo_enabled": false,
"profile_background_color": "FFF04D",
"lang": "en",
"profile_sidebar_border_color": "000000",
"profile_text_color": "000000",
"verified": false,
"profile_image_url": "http://pbs.twimg.com/profile_images/593327096736256001/TT8Ds75__normal.jpg",
"time_zone": "Chennai",
"url": null,
"contributors_enabled": false,
"profile_background_tile": false,
"profile_banner_url": "https://pbs.twimg.com/profile_banners/373836462/1428993069",
"statuses_count": 21397,
"follow_request_sent": null,
"followers_count": 438,
"profile_use_background_image": true,
"default_profile": false,
"following": null,
"name": "aditya dave",
"location": "Bhavnagar, Gujarat",
"profile_sidebar_fill_color": "000000",
"notifications": null
}
}
I tried the below table but it gives error.
hive> CREATE EXTERNAL TABLE tweets (
id BIGINT,
created_at STRING,
source STRING,
favorited BOOLEAN,
retweeted_status STRUCT<
text:STRING,
user:STRUCT<screen_name:STRING,name:STRING>,
retweet_count:INT>,
entities STRUCT<
urls:ARRAY<STRUCT<expanded_url:STRING>>,
user_mentions:ARRAY<STRUCT<screen_name:STRING,name:STRING>>,
hashtags:ARRAY<STRUCT<text:STRING>>>,
text STRING,
user STRUCT<
screen_name:STRING,
name:STRING,
friends_count:INT,
followers_count:INT,
statuses_count:INT,
verified:BOOLEAN,
utc_offset:INT,
time_zone:STRING>,
in_reply_to_screen_name STRING
)
PARTITIONED BY (datehour INT)
ROW FORMAT SERDE 'com.cloudera.hive.serde.JSONSerDe'
LOCATION '/user/flume/tweets/01092015';
You can use this utility https://github.com/quux00/hive-json-schema to create a hive schema from JSON. But as Ben Watson in his answer here if there are some columns like user that use a reserved name, you have to either enclose them with backticks or use a serde like https://github.com/rcongiu/Hive-JSON-Serde that can map hive columns to json elements.
I am attempting to reference the message array contained within this JSON but cant seem to get it.
[
{
"MessageThreadID": 1,
"CustomerID": 171,
"MessageType": 1,
"Subject": "Test Message",
"OpenDate": "2015-09-17T00:00:00",
"Closed": false,
"ClosedDate": null,
"Messages": [
{
"IBMessageID": 1,
"MessageThreadID": 1,
"MessageText": "Test Message",
"FromCustomer": true,
"UserID": null,
"Date": "2015-09-17T17:23:00"
},
{
"IBMessageID": 2,
"MessageThreadID": 1,
"MessageText": "Test this Update",
"FromCustomer": false,
"UserID": 1,
"Date": "2015-09-17T17:23:00"
},
{
"IBMessageID": 3,
"MessageThreadID": 1,
"MessageText": "My New Message",
"FromCustomer": false,
"UserID": 1,
"Date": "2015-09-17T17:23:00"
},
{
"IBMessageID": 4,
"MessageThreadID": 1,
"MessageText": "Reply",
"FromCustomer": false,
"UserID": 1,
"Date": "2015-09-17T17:05:00"
},
{
"IBMessageID": 5,
"MessageThreadID": 1,
"MessageText": "Some sensible shit",
"FromCustomer": false,
"UserID": 1,
"Date": "2015-09-17T17:23:00"
},
{
"IBMessageID": 14,
"MessageThreadID": 1,
"MessageText": "Message 2",
"FromCustomer": true,
"UserID": null,
"Date": "2015-09-21T14:10:00"
},
{
"IBMessageID": 16,
"MessageThreadID": 1,
"MessageText": "Message 2",
"FromCustomer": true,
"UserID": null,
"Date": "2015-09-22T16:22:00"
},
{
"IBMessageID": 25,
"MessageThreadID": 1,
"MessageText": "Added via abacus\r\n",
"FromCustomer": false,
"UserID": 1,
"Date": "2015-09-22T16:22:00"
},
{
"IBMessageID": 26,
"MessageThreadID": 1,
"MessageText": "sdsdsdsd",
"FromCustomer": true,
"UserID": null,
"Date": "2015-09-22T16:40:00"
},
{
"IBMessageID": 27,
"MessageThreadID": 1,
"MessageText": "test",
"FromCustomer": true,
"UserID": null,
"Date": "2015-09-22T17:02:00"
},
{
"IBMessageID": 28,
"MessageThreadID": 1,
"MessageText": "test",
"FromCustomer": true,
"UserID": null,
"Date": "2015-09-22T17:06:00"
}
]
}
]
The furthest I have gotten is:
#cleanmessages = JSON.parse([#messages].to_json).first
This returns:
[
{
"IBMessageID" =>1,
"MessageThreadID" =>1,
"MessageText" =>"Test Message",
"FromCustomer" =>true,
"UserID" =>nil,
"Date" =>"2015-09-17T17:23:00 "},
{
"IBMessageID"=>2,
"MessageThreadID" =>1,
"MessageText" =>"Test this Update",
"FromCustomer" =>false,
"UserID" =>1,
"Date" =>"2015-09-17T17:23:00 "},
{
"IBMessageID"=>3,
"MessageThreadID" =>1,
"MessageText" =>"My New Message",
"FromCustomer" =>false,
"UserID" =>1,
"Date" =>"2015-09-17T17:23:00 "},
{
"IBMessageID"=>4,
"MessageThreadID" =>1,
"MessageText" =>"Reply",
"FromCustomer" =>false,
"UserID" =>1,
"Date" =>"2015-09-17T17:05:00 "},
{
"IBMessageID"=>5,
"MessageThreadID" =>1,
"MessageText" =>"Some sensible shit",
"FromCustomer" =>false,
"UserID" =>1,
"Date" =>"2015-09-17T17:23:00 "},
{
"IBMessageID"=>14,
"MessageThreadID" =>1,
"MessageText" =>"Message 2",
"FromCustomer" =>true,
"UserID" =>nil,
"Date" =>"2015-09-21T14:10:00 "},
{
"IBMessageID"=>16,
"MessageThreadID" =>1,
"MessageText" =>"Message 2",
"FromCustomer" =>true,
"UserID" =>nil,
"Date" =>"2015-09-22T16:22:00 "},
{
"IBMessageID"=>25,
"MessageThreadID" =>1,
"MessageText" =>"Added via abacus\r\n",
"FromCustomer" =>false,
"UserID" =>1,
"Date" =>"2015-09-22T16:22:00 "},
{
"IBMessageID"=>26,
"MessageThreadID" =>1,
"MessageText" =>"sdsdsdsd",
"FromCustomer" =>true,
"UserID" =>nil,
"Date" =>"2015-09-22T16:40:00 "},
{
"IBMessageID"=>27,
"MessageThreadID" =>1,
"MessageText" =>"test",
"FromCustomer" =>true,
"UserID" =>nil,
"Date" =>"2015-09-22T17:02:00 "},
{
"IBMessageID"=>28,
"MessageThreadID" =>1,
"MessageText" =>"test",
"FromCustomer" =>true,
"UserID" =>nil,
"Date" =>"2015-09-22T17:06:00
}
]
How do i reference each element within each array. I want to in the end be able to display each element sepearately for each thread.
This will give you a hash with the message grouped by the MessageThreadID
#cleanmessages.group_by{ |msg_hash| msg_hash['MessageThreadID']}
Not clear what output you actually want.
document = JSON.parse([#messages].to_json)
document.each do |thread|
# gives you all text messages within the current thread: "Test Message", "Test this Update", ...
messages = thread['Messages'].map {|m| m['MessageText']}
# iterates over all messages in the current thread
thread['Messages'].each {|m| puts "Message #{m['IBMessageID']} from thread #{thread['MessageThreadID']}"}
end
In general JSON.parse doesn't return something special. It returns either Array for corresponding JSON array or Hash for corresponding JSON object.