Solved

How can I make this more efficient?

Posted on 2014-09-23
6
119 Views
Last Modified: 2014-09-26
I'm parsing out a JSON file using the following code:

 //This input should be from somewhere else, hard-coded in this example
$file_name = '00_8ptcd6jgjn201311060000_day.json.gz';
// Raising this value may increase performance
$buffer_size = 4096; // read 4kb at a time
$out_file_name = str_replace('.gz', '', $file_name); 
// Open our files (in binary mode)
$file = gzopen($file_name, 'rb');
$out_file = fopen($out_file_name, 'wb'); 
// Keep repeating until the end of the input file
while(!gzeof($file)) {
// Read buffer-size bytes
// Both fwrite and gzread and binary-safe
  fwrite($out_file, gzread($file, $buffer_size));
}  
// Files are done, close files
fclose($out_file);
gzclose($file);

$jsondata = file_get_contents("00_8ptcd6jgjn201311060000_day.json");
$json = json_decode($jsondata, true);
//echo $json
$output = "<ul>";
	foreach($json['id'] as $id) {
	$output .= "<h4>".$id."</h4>";
	$output .="<li>".$id['actor/id']."</li>";
	$output .="<li>".$id['actor/displayName']."</li>";
	$output .="<li>".$id['actor/postedTime']."</li>";
	$output .="<li>".$id['generator/displayName']."</li>";
	$output .="<li>".$id['geo/type']."</li>";
	$output .="<li>".$id['geo/coordinates/0']."</li>";
	$output .="<li>".$id['geo/coordinates/1']."</li>";
	}
$output .="</ul>";
echo $output;

Open in new window


The first part, as far as decompressing the file, works fine. The problem comes when I'm printing the output. I get this:

( ! ) Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 1086054108 bytes) in C:\wamp\www\json\uncompress.php on line 30

How can I process things incrementally so I don't time out?
0
Comment
Question by:brucegust
  • 3
  • 3
6 Comments
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
What is line 30?  Compare these numbers.  I think we will have to find a way to process this data incrementally.

   134,217,728 - memory limit
1,086,054,108 - requirement
0
 

Author Comment

by:brucegust
Comment Utility
Morning, Ray!

I agree. The size of the file is 1,060,592 kb so doing things in stages is going to be essential.

The file, by the way, is a decompressed JSON file that I need to parse and then insert into a database. I've got 365 such files to process that way. After it's all done, I plan on writing a script that exports the parsed results from the database to a csv file.

That's the goal for today.

What do you think?
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
PHP may not be the right tool for this, or you may need to get a very, very large server and increase the memory limit to the stratosphere.  What instruction is on line 30?
0
Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:brucegust
Comment Utility
Hey, Ray!

The "instruction" at line 30 is $jsondata=file_get_contents("00_8ptcd6jgjn201311060000_day.json");

Since getting into work this am, I've been trying to figure out how to break the elephant down into bite sized pieces and I've yet to figure it out.

Here's what I've got thus far:

$jsondata = file_get_contents("00_8ptcd6jgjn201311060000_day.json");
//breaking the elephant down into byte sized pieces
$json_size = 4096;
$buffer = fgets($jsondata, $json_size);
$json = json_decode(($buffer), true);
//echo $json
 while (!feof($json))
 {

Open in new window


Problem is, I can't get to "$buffer" because I'm getting hung up $jsondata in light of the file being over 1 GB.

Is there a way to do something like $jsondata=file_get_contents($file_name, $json_size)?

I see how it works with fgets, but how about on the "get_contents" side?
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Where did that file come from?  Is there a URL that I can read?

PHP functions are all documented in the online man pages.  Example:
http://php.net/manual/en/function.file-get-contents.php
0
 

Author Comment

by:brucegust
Comment Utility
Ray, here's what I came up with:

$chunk_size=4096;
$url = '00_8ptcd6jgjn201311060000_day.json';
$handle=@fopen($url,'r');
      if(!$handle)
      {
            echo "failed to open JSON file";
      }
while (!feof($handle))
{
$buffer = fgets($handle, $chunk_size);
      if(trim($buffer)!=='')
      {
$obj=json_decode(($buffer), true);
//the rest of my code

It works!

Thanks for your help!
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now