Skip to content

Instantly share code, notes, and snippets.

@andrew-dixon
Last active September 6, 2015 11:00
Show Gist options
  • Save andrew-dixon/27788b7c730dff66875f to your computer and use it in GitHub Desktop.
Save andrew-dixon/27788b7c730dff66875f to your computer and use it in GitHub Desktop.
<cfscript>
// Query to retrieve some data from the database
variables.myData = queryExecute(
sql = "
SELECT *
FROM myTable
LIMIT 10
",
options = {datasource="#Application.myDSN#"}
);
// Loop over the array and insert each into ElasticSearch using the HTTP Put API call
for (i in variables.myData) {
// The ElasticSearch server is local and the data is going into myIndex using myType and an ID from the database
http method="put" url="http://localhost:9200/myIndex/myType/#i.myID#" result="putRequest" {
// Pass a JSON serialize string into the ElasticSearch as the document to be added to the index
httpparam type="body" value="#SerializeJSON(i)#";
};
// Dump the returned structure for information
dump(variables.putRequest);
writeOutput("<br><br>")
}
</cfscript>
@andrew-dixon
Copy link
Author

The above example retrieve 10 rows of data from a database table to puts them into an ElasticSearch index.

The above code is for Railo 4.1+

@d1rtym0nk3y
Copy link

You can simplif this by just doing the query and then using

for (row in query) {}

railo will give you a struct for each row.

@webRat
Copy link

webRat commented Jul 27, 2014

@d1rtym0nk3y: queryRowToStruct is meant to be a UDF / global thing, but you're right, if this is all on the same page, he could simplify.

@andrew-dixon
Copy link
Author

@d1rtym0nk3y - I swear I tried that and it didn't work, will try it again... Thanks.

@andrew-dixon
Copy link
Author

@d1rtym0nk3y - Yep, that worked fine, I have updated the gist to show this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment