James Dyalim James Dyalim - 1 month ago 8
JSON Question

Query JSON Attributes from JSON-formatted Column in Database

here is the problem I am facing

I have a table called GAMELOG (well, could be SQL Table or NoSQL column family), that looks like this :

ID INT,
REQUESTDATE DATE,
REQUESTMESSAGE VARCHAR,
RESPONSEDATE DATE,
RESPONSEMESSAGE VARCHAR

the REQUESTMESSAGE and RESPONSEMESSAGE column is JSON-formatted.
let's say for example a specific value in REQUESTMESSAGE is :

{
"name" : "John",
"specialty" : "Wizard",
"joinDate" : "17-Feb-1988"
}


and for the RESPONSEMESSAGE is :

{
"name" : "John Doe",
"specialty" : "Wizard",
"joinDate" : "17-Feb-1988",
"level" : 89,
"lastSkillLearned" : "Megindo"
}


now, the data in my table has grown to incredibly large (around billion rows, few terabytes harddisk space)

What I want to do is query the row which contains JSON Property of "name" that has a value of "John" in the REQUESTMESSAGE

What I understand about SQL Database, well Oracle that I've used before, I have to make the REQUESTMESSAGE and RESPONSEMESSAGE as a CLOB, and query using LIKE, i.e.

SELECT * FROM GAMELOG WHERE REQUESTMESSAGE LIKE '%"name" : "John"%';


But, the result is very slow and painfully.

Now, I move to Cassandra, but I don't know how to properly query it, well I haven't used Apache Hadoop yet to get the data, which I intended to use it to get the data sometimes later.

My question is, is there a database product that support the query to select the JSON-formatted JSON Attribute inside the table/column family? As far as I know, MongoDB stores document in JSON, but that means that all of my column family will be stored as JSON, i.e.

{
"ID" : 1,
"REQUESTMESSAGE" : "{
"name" : "John",
"specialty" : "Wizard",
"joinDate" : "17-Feb-1988"
}",
"REQUESTDATE" : "17-Feb-1967"
"RESPONSEMESSAGE" : "{
"name" : "John Doe",
"specialty" : "Wizard",
"joinDate" : "17-Feb-1988",
"level" : 89,
"lastSkillLearned" : "Megindo"
}",
"RESPONSEDATE" : "17-Feb-1967"
}


and I still have trouble to get the JSON Attributes inside the REQUESTMESSAGE column (please correct me if I'm wrong)

Thank you very much

Answer

If you aren't committed to storing your data in Apache Cassandra, MySQL has SQL query functions that can extract data from JSON values, in particular, you would want to look at the JSON_EXTRACT function: https://dev.mysql.com/doc/refman/5.7/en/json-search-functions.html

In your case, the query should look something like the following:

SELECT REQUESTMESSAGE, JSON_EXTRACT(REQUESTMESSAGE, "$.name")  
FROM GAMELOG 
WHERE JSON_EXTRACT(REQUESTMESSAGE, "$.name") = "John";
Comments