【问题标题】:Executing java program on redshift在redshift上执行java程序
【发布时间】:2014-02-09 10:35:41
【问题描述】:

例如,我编写了以下 php 代码并在 redshift 上运行,如下所示。

<?php

$connect = pg_connect("host=xx.xxxx.us-east-1.redshift.amazonaws.com port=xxxx dbname=testdb user=xxxx password=xxxxxx");

$query = "select name, email from mytable LIMIT 7";
$result = pg_query($connect,$query);
while(($row = pg_fetch_array($result)) != null) {
        $name = $row[0];
        $email = $row[1];

        echo $name." <<>> ".$email." <<>> ";
}
?>

我保存了上面的php文件,并按照下面的方式执行。

myuser@ip-20-143-43-144:/home/myuser$ php runquery.php     <Enter>

然后它给我输出。

我想用 java 测试同样的东西。因此,我从 aws 中获取了示例并保存在同一位置。

public class ConnectToCluster {

    static final String dbURL = "jdbc:postgresql://xxxx.xxxxx.us-east-1.redshift.amazonaws.com:xxxx/testdb";
    static final String MasterUsername = "XXXX";
    static final String MasterUserPassword = "XXXXXX";

    public static void main(String[] args) {
        Connection conn = null;
        Statement stmt = null;
        try{
           //Dynamically load postgresql driver at runtime.
           Class.forName("org.postgresql.Driver");

           //Open a connection and define properties.
           System.out.println("Connecting to database...");
           Properties props = new Properties();

           //Uncomment the following line if using a keystore.
           //props.setProperty("ssl", "true");  
           props.setProperty("user", MasterUsername);
           props.setProperty("password", MasterUserPassword);
           conn = DriverManager.getConnection(dbURL, props);

           //Try a simple query.
           System.out.println("Listing system tables...");
           stmt = conn.createStatement();
           String sql;
           sql = "select * from information_schema.tables;";
           ResultSet rs = stmt.executeQuery(sql);

           //Get the data from the result set.
           while(rs.next()){
              //Retrieve two columns.
              String catalog = rs.getString("table_catalog");
              String name = rs.getString("table_name");

              //Display values.
              System.out.print("Catalog: " + catalog);
              System.out.println(", Name: " + name);
           }
           rs.close();
           stmt.close();
           conn.close();
        }catch(Exception ex){
           //For convenience, handle all errors here.
           ex.printStackTrace();
        }finally{
           //Finally block to close resources.
           try{
              if(stmt!=null)
                 stmt.close();
           }catch(Exception ex){
           }// nothing we can do
           try{
              if(conn!=null)
                 conn.close();
           }catch(Exception ex){
              ex.printStackTrace();
           }
        }
        System.out.println("Finished connectivity test.");
     }
 }

我也将这个.java 文件保存在同一位置。那么,有人可以告诉我如何在命令上运行这个 java 文件,就像我对 php 所做的那样。

【问题讨论】:

标签: java amazon-web-services amazon-redshift


【解决方案1】:

(1) 从以下链接下载一个postgresql JDBC驱动,放到ConnectToCluster.java同目录下。

(2)在java文件头部添加缺少的行(import java library)。

 import java.sql.*;
 import java.util.*;

 public class ConnectToCluster {
 ...

(3) 运行以下命令。

 javac ConnectToCluster.java && java -cp .:postgresql-8.4-703.jdbc4.jar ConnectToCluster 

【讨论】:

  • 非常感谢您的回答。每次都需要编译吗?我希望这个程序每天在特定时间运行,我听说 cronjob 会这样做。如果您能就此提出建议,我将不胜感激。
  • 不,一旦你编译了java文件,你不需要每次都编译它。说到 cronjob,您需要更改类路径(cp 选项)以使用绝对路径,例如“-cp /path/to/your-java-file-dir/:/path/to/your-java-file-dir/ postgresql-8.4-703.jdbc4.jar" 因为cronjob的当前目录不同。
  • 好的,谢谢。我还有一个 jar 文件要包含在该项目中,我需要使用逗号作为分隔符并继续吗?比如-cp.:postgresql-8.4-703.jdbc4.jar, anotherjarfile.jar
  • 我通过在两个单独的命令中编译和执行它来实现它。像 javac -cp.:one.jar:two.jar 和 java -cp.:one.jar:two.jar
猜你喜欢
  • 2012-08-13
  • 2017-04-07
  • 2020-03-28
  • 2019-04-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多