Load a big json file in Mysql or Oracle database - java
At work, we supply files for other services. Size of them are between 5mo and 500mo.
We want to use Json instead of XML, but i m wondering how our customers could
upload those files in an easy way in their database, Oracle or Mysql.
I mean, i can t find on the web APi or functions or tools, in Mysql or oracle, to do that.
I know that it s easy to work data by data to load a small Json file, decode each
object or array and put them at the right place in database.
But is there an other way to do this, like sqlloader in Oracle ?
And if so, size of our file aren t they too big to produce JSON file, in JAVA for example ?
I guess it might be difficult to do this load job automatically, especially because of arrays like this :
{"employees":[
{"firstName":"John", "lastName":"Doe", "salaryHistory":[1000,2000,3000]},
{"firstName":"Anna", "lastName":"Smith", "salaryHistory":[500,800]},
{"firstName":"Peter", "lastName":"Jones", "salaryHistory":[400]}
]}
where salaryHistory must produce problems because their sizes are different, and data are not madatoryly
in the same table.
Any ideas or help would be welcomed !
Edit
i m looking for a solution to put each data in the good column of a table, i don t need to store a Json structure in a single column of simple table.
like this :
table employees : column are id, FirstName, lastName and
table salaryHistory : column are id, order, salary
and each data must go in the good column like "John" in firstname, "Doe" in lastname, then "1000" in a new row of table salaryHistory , "2000" in another new row of salaryHistory and so on.
Starting with MySQL 5.7 there is a new data type: JSON.
Take a look here for more details.
Example for Oracle 12c:
create table transactions (
id number not null primary key,
trans_msg clob,
constraint
check_json check (trans_msg is json)
);
regular insert:
insert into transactions
values
(
sys_guid(),
systimestamp,
'{
"TransId" : 3,
"TransDate" : "01-JAN-2015",
"TransTime" : "10:05:00",
"TransType" : "Deposit",
"AccountNumber" : 125,
"AccountName" : "Smith, Jane",
"TransAmount" : 300.00,
"Location" : "website",
"CashierId" : null,
"ATMDetails" : null,
"WebDetails" : {
"URL" : "www.proligence.com/acme/dep.htm"
},
"Source" : "Transfer",
"TransferDetails" :
{
"FromBankRouting" : "012345678",
"FromAccountNo" : "1234567890",
"FromAccountType" : "Checking"
}
}'
)
/
SQL*Loader control file and data file:
load data into table transactions
fields terminated by ','
(
trans_id sequence(max,1),
fname filler char(80),
trans_body lobfile(fname) terminated by EOF
)
Related
How do I convert String to an Object in MongoDB?
In my Mongo database, I have a "Books" collection with a "category" field. In the past, "category" was an enum mapped as String in Mongo but now I need "category" to be an object within my "Books" collection. What can I do? Is there a query that can be executed to convert all documents at once? Example, today category is like that: "category" : "Sci-Fi and Fantasy" But I need to convert to this: "category" : { "_id" : ObjectId("3f07bc56po324021df23a8f1"), "code" : NumberLong(1), "name" : "Sci-Fi and Fantasy" }
MongoDB is no SQL you can have both kind of records present in your mongo collection, although while reading the records you might have to frame some logic for creating identical entities. This might cause issues if you search based on that particular property. You can create a new property with different name keeping the category as well and then add the new property to all the records.
Unable to store complete json field in mysql table field
I am getting a large json data in a api response. I am trying to store this json response in local mysql table. But unable to store complete json response. Please find the below json info. Sample API json response : { "responseCode": 200, "date": "2020-06-03", "message": "Success", "couponDetails": { "total": 14949, "codes": "35033769,35441136,35803675,34407176,34717909,34950692,35059148,35452352,35688911,35904465,35904658,35904753,35904824,35904942,35905306,35905318,35905434,35905673,35906615,35907029,35907154,35907222,35907345,35907592,35907683,35907951,35908161,35908194,35908206,34664348,34664436,34665057,34665072,34665768,34665950,34666051,34666110,34666879,34667228,34668101,34670133,34670162,34670259,34670661,34670687,34670994,34671179,34671296,34672207,34672276,34672631,34672747,34673619,34673709,34675355,34676588,34677690,34678019,34679260,34679468,34680550,34680694,34680838,34683321,34684752,34684796,34685198,34685826,34686220,34686276,34351922,34352193,34352369,34352553,34353629,34353971,34355064,34355541,34355625,34356802,34357668,34357869,34357922,34360451,34360500,34360764,34361049,34361174,34361315,34362337,34362412,34363370,34364187,34365025,34365188,34365415,34365904,34366777,34366877,34367361,34368025,34368078,35542974,35543013,35543084,35268238,35268397,35268774,35269689,35269933,35270038,35250597,35063719,35064231,35064237,35270577,35270705,35270969,35064514,35064963,35065129,35251645,35251660,35251798,35253022,35253300,35272389,35272446,35272519,35272640,35272641,35273596,35273716,35423127,35423184,35423372,35424244,35425607,35485524,35486647,35486711,35486970,35487111,35470199,35470485,35488099,35488145,35488270,35490204,35534378,35535484,35535520,35535559,35535601,35535818,21979363,21508096,26237385,24734847,22263784,26889428,29292212,20415646,21836743,20300178,21831783,21198543,23739734,29773862,20715551,25488915,28894112,26536357,26695866,27133857,29133336,28763373,21850298,21990790,27757421,2421785723" } } In my local DB table I am able to insert the below information only, which is not complete : { "responseCode": 200, "date": "2020-06-03", "message": "Success", "couponDetails": { "total": 14949, "codes": "35033769,35441136,35803675,34407176,34717909,34950692,35059148,35452352,35688911,35904465,35904658,35904753,35904824,35904942,35905306, Mysql Table Structure : CREATE TABLE `bookdata_codeinfo_history` ( `generated_date` date DEFAULT NULL, `book_code` longtext, `service` varchar(45) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8; I want to store json in 'book_code' field but only few information is storing. I am using mysql-5.7.13 version. Please tell me how to resolve this issue
I think rather than attempting to store the book codes as a string, a better way to model the table would be to break the book_code string into individual rows. This would help in searching the data and future extensibility of the data model.
That's really strange a LONGTEXT can support up to 4,294,967,295 bytes ~4GB. (kindly recheck your table strucure) . Or Put these jsons in files and save the path in the database Or Split the codes fields into multiple row, something like idrequest , code and every code contains one value
Get column name ( Meta Data ) Talend
I'm trying to export data and meta data from Mysql Database to a JSON . My JSON output need to have this structure : { "classifier":[ { "name":"Frequency", "value":"75 kHz" }, { "name":"depth", "value":"100 m" } ]} Frequency for me represent a column Name and 75 Khz is the value of the column for a specific row. I'm using Talend data integration to do this, and i can get the data, but i can't figure out how to get the meta data, do i have to enter it myself ? or there is a more easy way to do this ?
You cannot export metadata of json file from Mysql because Mysql provide a structured data, hence we have to create our json structure independently using an existing file or manually, the easiest way is to create a sample file like the one used in your question. See Talend Help.
Getting "err" : "E11000 duplicate key error when inserting into mongo using the Java driver
Exception in thread "main" com.mongodb.MongoException$DuplicateKey: { "serverUsed" : "localhost/127.0.0.1:27017" , "err" : "E11000 duplicate key error index: twitterdb03.LevelAFollowers.$id dup key: { : ObjectId('52d5636de408652b4853a8fe') }" , "code" : 11000 , "n" : 0 , "connectionId" : 12 , "ok" : 1.0} I'm using mongo 2.11.1 Never had problems with simple write operations in java myMap.put(inid, followersList); myObj.putAll(myMap); myIdMapCollection.insert(myObj);
I found an answer on this page. I’m guessing your code looks something like this (greatly simplified)?: doc = {} for i in xrange(2): doc['i'] = i collection.insert(doc) The problem is that PyMongo injects an _id field into the document, if the _id field does not exist, before inserting it (_id is always generated client side with 10gen drivers). That means that the first time through the loop _id is added by the insert method. Since doc is defined outside the loop, each subsequent pass through the loop uses the same value for _id. Solution: Delete the key _id for i in xrange(2): doc['i'] = i if '_id' in doc: del doc['_id'] collection.insert(doc) Or create manually a new one: from bson.objectid import ObjectId for i in xrange(2): doc['i'] = i doc['_id'] = ObjectId() collection.insert(doc)
Try calling myIdMapCollection.save(myObj); instead of myIdMapCollection.insert(myObj); The save method, unlike insert does upsert, meaning if a document contains _id, it replaces that document. My guess is that you had fetched the DBObject using a cursor | query, had manipulated it, and you want to persist the changes. In that case, save is the right way to do it. So, when calling insert the DBObject is already associated with _id, calling insert thus fails, because you already have a document with that _id in the collection, which should be unique (duplicate index error).
java mongodb search on a given date
We have a user_audit entity(table) in mongodb which stores all the login/logout information. For example, "_id" : ObjectId("5228cf0156961de6693b74c0"), "active" : true, "fname" : "Tom", "lastlogin" : ISODate("2013-09-05T18:35:45.608Z"), "lastloginip" : "0:0:0:0:0:0:0:1", "lname" : "Bailey", "lastlogout" : ISODate("2013-09-05T18:36:45.568Z"), There are thousands of records in this table in production. Now, the admin wants to look for all the logins on a particular date. i am not able to look for exact match of date because of the "time" information attached to ISODate in the "lastlogin" field. In Java, new Date() had been used to insert this field value. The issue is the time information keeps changing for logins on a particular day. Tried, query.put("lastlogin", new BasicDBObject("$lte", givenDate).append("$gte", givenDate)); Obviously it does not return results because the time part of the date does not match. The query passed from Java to mongo is: query={ "lastlogin" : { "$lte" : { "$date" : "2013-09-05T04:00:00.000Z"} , "$gte" : { "$date" : "2013-09-05T04:00:00.000Z"}}} [Note: It defaults to 04:00:00.000Z always if we format with MM_dd_yyyy in java, not sure why..] The issue is we have a lot of records in production database. For fresh records, i can blank out the time portion before inserting from Java. But for existing records, not sure how to handle it. How can i get the records on a given date?
According to mongodb cookbook, you are in the right direction. You may just query for something like query.put("lastlogin", new BasicDBObject("$lt", givenDatePlusOneDay).append("$gte", givenDateAt00h00min)); which is an interval and not the same date.