Skip to content

Instantly share code, notes, and snippets.

@jsteinshouer
Last active January 5, 2024 15:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsteinshouer/6a83764a16f0f18290e4b73d4b64b6c1 to your computer and use it in GitHub Desktop.
Save jsteinshouer/6a83764a16f0f18290e4b73d4b64b6c1 to your computer and use it in GitHub Desktop.
Java / CF HTTP Stream
<cfscript>
fileURL = "https://raw.githubusercontent.com/jsteinshouer/data-science-foundations-python-scientific-stack-3084641/main/Ch04/challenge/track.csv";
uri = createObject("java", "java.net.URI").init( fileURL );
inputStream = uri.toURL().openStream();
sc = createObject("java", "java.util.Scanner").init(inputStream);
while ( sc.hasNextLine() ) {
line = sc.nextLine();
//Process data 1 line at a time
writeDump( listToArray(line) );
cfflush();
}
inputStream.close();
</cfscript>
<cfscript>
setting requestTimeout=240;
//Large data file 1.5+ GB
fileURL = "https://s3.amazonaws.com/nyc-tlc/csv_backup/yellow_tripdata_2016-01.csv";
destination = getTempFile( getTempDirectory(), "myTempData");
// 8KB
bufferSizeKB = 1024*8;
uri = createObject("java", "java.net.URI").init( fileURL );
inputStream = uri.toURL().openStream();
fileOutputStream = createObject("java", "java.io.FileOutputStream").init(destination);
//read 8kb at a time + Seems to be several ways to create a byte array in CF . Found this one on Ben Nadel's blog
buffer = repeatString( chr( 32 ), bufferSizeKB ).getBytes() ;
length = inputStream.read( buffer );
//writeOutput( charsetEncode( buffer, "UTF-8") )
while ( length > 0 ) {
//Do data processing here
//myData = charsetEncode( buffer, "utf-8" );
//Or just Write to file ao we do not have to load entire file in memory at once
fileOutputStream.write( buffer, 0, length );
length = inputStream.read( buffer );
}
writeOutput( "File written to #destination#")
fileOutputStream.close();
inputStream.close();
</cfscript>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment