Run python or javascript file from postgres function? - javascript

How i can Run python or javascript file from postgres function ?
I have two postgres data base with totally different structure .
but i need to sync that two databases .
I will write the script that will read data from local data base and write into server data base in any language (python , java , javascript, node).
but the main problem is :
I do not know how to call that script from postgres procedure or function .
so the ques is :
How we can call or run any script from postgres procedure ?

Use plperl, plpython or pljava and use the system utils of the language. I.e in plpython use subprocess, in java use Runtime.getRuntime().exec and so on:
For example in plpython:
CREATE LANGUAGE plpythonu; -- create language if does not exist
CREATE OR REPLACE FUNCTION venue.test_execute()
RETURNS void AS
$BODY$
import subprocess
subprocess.call(['/path/to/script/test.py'])
$BODY$
LANGUAGE plpythonu;
SELECT venue.test_execute();
CONSIDERATIONS
Actually you can launch a script from a Postgres function, but this is not the preferred way to synchronize data between databases. You probably will have issues related to absolute and relative paths to scripts and data, and file system permissions. Instead use a database function to export data in a known format and call your synchronize script from outside the database. Another options is use the dblink module.

You can create a Perl Function and in that call your script to sync the databases.

Related

How to call JavaScript function from PHP file

I have a JavaScript file on my server that contains a function. I would like to develop a REST Api to connect to this server, run the JavaScript function and send back the output.
Is it possible to call a JavaScript function from a php file?
I read this but it doesn't answer my question, because my js file is hosted on the same server as the php file.
Is the V8Js extensions what I am looking for?
Edit
The js function looks like this:
function (line, userWeight, weightunit){
//logic is here
var computed = {
userLengthFtin: userLengthFtin,
userLevel: userLevel,
proId: line['id'],
proLengthFeetin: proLengthFeetin,
proThick: proThickFtin,
weightunit: weightunit
};
return computed;
}
Is it possible to call a javascript function from a php file ?
You would need to hand things over to some other software which can execute JS. This might be through shelling out or it might be though a library such as Selenium or the V8js library you found.
Whatever you choose, it would need to be able to handle the particular needs of the JS (e.g. if the JS expects to be embedded in a webpage with access to a DOM and all the APIs provided by a web browser, then you couldn't simply run it with Node.js).
It would probably be simpler to rewrite the function in PHP.

Bash/PHP/Javascript: How to run a php file that outputs javascript, and execute that javascript? [duplicate]

I have php file that is to be executed as cronjob, this php file contains some javascript.
I will explain the flow :
the Php is used to retrive some data(A LIST OF URLS) from DB.
For each URL Obtained, a Java script API is used.
THe result Obj returned from API contains data for each url.
The data is then sent back to as an AJAX Call for each url to a php file .
Can this be implemented Via CRON JOBS ?
OR
Is there any method to schedule javascript to run periodically, like cron for php?
UPDATE: i could manage the javascript call to API with PHP curl ,And the cron Job is getting executed perfectly. But i dont think it is the correct solution to this question may be Node.Js is the solution(i didnt test it yet).
You can't run Javascript in Cronjobs because Javascript is ran by browsers. I think you should take a look at curl in php to call an api instead.
http://www.php.net/manual/en/book.curl.php
You have to split the work: Cron the JS, Cron the PHP. In the middle, deliver one's results to another. Agree with phantomjs usage for JS execution (or casperJS-I prefer). Execute the JS, output to JSON as a file, read from the file using file_get_contents from PHP. And define these actions in two different cron jobs.
You can run Javascript via cron in a Javascript runtime like node.js: http://nodejs.org/
phantomjs is one possibility, see this thread wget + JavaScript?
Otherwise you could run Node.js on your server to execute JavaScript in a CLI type environment but mixing node.js and PHP could become complicated.
you can schedule javascript with cron by using Node.js

Create d3.js graph from data on mongodb server

How can I create d3.js graph from data on mongodb server using node.js?
D3.js includes ways to request non-local data either as json or text (csv) via urls and such.
In a setup that is not security sensitive (like local development or a demo environment) you could fairly directly use the mongo rest api if you enable it, which will give you json output for objects.
Or you could write build a simple http server (like in python, perl or go) that execs (python (also subprocess), perl (also backticks and qx{}), go) the mongoexport tool with the right parameters to provide csv output from mongo.
If you already have data in Mongo, and you've got Node already setup, then maybe that's what you want to use:
⇒ ⇒
If so, there's someone out there that's used Node.js® with some npm modules for MongoDB® to specifically drive a D3.js® visualization.

Using OpenCPU to access custom R function

I have an R code which loads the RandomForest model, I am looking to create a function which
load(model)
randomforest_func = function(data)
{
data$pred = predict(model,data,type="prob")
output = data.frame(data$customerid,data$pred[,2])
return(output)
}
I need to make this function enabled in webserver, where an external application feeds data and retrieves the output.
The problem is, the model needs to be preloaded and cannot load into R env for each request.
The function needs to support parallel connections.
I tried installing opencpu in R.
The above code should be running in R and available at
http://localhost:1234/ocpu/
I now made changes to the opencpu.js to point to this URL and used the function in jquery to below. ocpu.r_fun_call("randomforest_func",parameters)
However this is seems to be not working..
ocpu.r_fun_call does not seem to be accessing the R script.
My question is how to correctly configure the opencpu to be able access the randomforest_func
The above code should be running in R and available at
http://localhost:1234/ocpu/
No. You need to create a package in which you put your custom functions. If the package is called foo, then the app will be available at
http://localhost:xxxx/ocpu/library/foo/www
(where xxxx is a random value for the port, given when you run opencpu$browse()).
Also, you have to use ocpu.call, not ocpu.r_fun_call.
This should help with deploying it as an app, making it easier for any external application to consume the services.
This should help with including the model.

Javascript and MongoDB - write to file

I'm using mongoDB on an ubuntu server. However I'm using a javascript to store data and do some map/reduce. I would like to measure these operations and write the results into a file. Somehow I fail to open a file and write into it.. I tried the following:
f = new File("myfile.txt");
if(f.open("w") == true)
{
f.write("test");
f.Close();
}
The file object within spidermonkey (the JS engine MongoDB uses, and coincidently the console) has been made obsolete: https://developer.mozilla.org/en-US/docs/SpiderMonkey/File_object . Not only that but it wasn't standard to spidermonkey anyway so that was a long shot.
As #gkamal states, the best way is to pipe the output of the MongoDB console like so:
mongo someprocess.js > outpout
Simple answer: you can not write files using Javascript.
It is probably better to write to the console using println and redirect the output to file, pass the javascript file as a parameter to mongo.
mongo someprocess.js > outpout

Categories