Last active
January 5, 2024 15:02
-
-
Save jsteinshouer/6a83764a16f0f18290e4b73d4b64b6c1 to your computer and use it in GitHub Desktop.
Java / CF HTTP Stream
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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