从脚本运行五个SQL查询

时间:2022-11-19 22:16:01

I have Five SQLquery that I need to run, either Sequentially means one by one or Independently. As currently what I am doing is that, I need to run all those Five SQL query sequentially in one Putty session or I can open five Putty Session and enter one query each in those five Putty Sessions. So its very tedious and I need to run it manually. So I was thinking to automate the process.

我需要运行五个SQLquery,顺序意味着逐个或独立。目前我正在做的是,我需要在一个Putty会话中按顺序运行所有这五个SQL查询,或者我可以打开五个Putty Session并在这五个Putty Sessions中分别输入一个查询。所以它非常繁琐,我需要手动运行它。所以我想要自动化这个过程。

Is there any way using some sort of scripts like Perl Script or Python script from which I can run all my Five queries either Sequentially or Independently?

有没有办法使用某种类型的脚本,如Perl Script或Python脚本,我可以从中顺序或独立地运行我的所有五个查询?

Queries in my case are little bit complex and long but considering the above scenario I am taking the below five queries as an example- And these Five queries are not related to each other. And also these five SQL queries I am running using Hive.

在我的情况下查询有点复杂和冗长,但考虑到上述情况,我将以下五个查询作为示例 - 并且这五个查询彼此无关。还有我使用Hive运行的这五个SQL查询。

SELECT * FROM Table1;
SELECT * FROM Table2;
SELECT * FROM Table3;
SELECT * FROM Table4;
SELECT * FROM Table5;

So My Question is how can I do that in any script language? Also I am not specific to any script, Just wanted to know which is better approach in my case. Any code snippets fulfilling my requirement will be appreciated.

所以我的问题是如何用任何脚本语言来做到这一点?此外,我并不是特定于任何脚本,只是想知道在我的情况下哪种方法更好。任何满足我要求的代码片段都将受到赞赏。

I am working with HiveQL and HiveQL syntax is same as SQL Sytnax.

我正在使用HiveQL和HiveQL语法与SQL Sytnax相同。

P.S. I am new to all the scripting languages.

附:我是所有脚本语言的新手。

1 个解决方案

#1


2  

There are a lot of ways to do this and this is a pretty basic example. The upshot is that in Perl you control the sequence of execution by simply placing the "execute" commands in the order desired. I'm assuming you're familiar with Perl DBI and how to connect to your dbase.

有很多方法可以做到这一点,这是一个非常基本的例子。结果是,在Perl中,您只需按所需顺序放置“执行”命令即可控制执行顺序。我假设您熟悉Perl DBI以及如何连接到您的dbase。

#!/usr/bin/perl 
use DBI; 
use strict; 

my $dbh = DBI->connect($your_connect_string) || die qq{Could not connect to $dbase\n};     
my $sql_1 = qq{ select * from table1}; 
my $sql_2 = qq{ select * from table2};
my $sql_3 = qq{ select * from table3};

my $sth_1 = $dbh->prepare($sql_1); 
my $sth_2 = $dbh->prepare($sql_2);
my $sth_3 = $dbh->prepare($sql_3);

$sth_1->execute();
$sth_2->execute();
$sth_3->execute();

while (my $hashref_1 = $sth_1->selectrow_hashref()) { 
...do something
} 

while (my $hashref_2 = $sth_2->selectrow_hashref()) { 
...do something
} 

while (my $hashref_3 = $sth_3->selectrow_hashref()) { 
...do something
} 

$sth_1->finish(); 
$sth_2->finish(); 
$sth_3->finish(); 

$dbh->disconnect(); 

exit;

#1


2  

There are a lot of ways to do this and this is a pretty basic example. The upshot is that in Perl you control the sequence of execution by simply placing the "execute" commands in the order desired. I'm assuming you're familiar with Perl DBI and how to connect to your dbase.

有很多方法可以做到这一点,这是一个非常基本的例子。结果是,在Perl中,您只需按所需顺序放置“执行”命令即可控制执行顺序。我假设您熟悉Perl DBI以及如何连接到您的dbase。

#!/usr/bin/perl 
use DBI; 
use strict; 

my $dbh = DBI->connect($your_connect_string) || die qq{Could not connect to $dbase\n};     
my $sql_1 = qq{ select * from table1}; 
my $sql_2 = qq{ select * from table2};
my $sql_3 = qq{ select * from table3};

my $sth_1 = $dbh->prepare($sql_1); 
my $sth_2 = $dbh->prepare($sql_2);
my $sth_3 = $dbh->prepare($sql_3);

$sth_1->execute();
$sth_2->execute();
$sth_3->execute();

while (my $hashref_1 = $sth_1->selectrow_hashref()) { 
...do something
} 

while (my $hashref_2 = $sth_2->selectrow_hashref()) { 
...do something
} 

while (my $hashref_3 = $sth_3->selectrow_hashref()) { 
...do something
} 

$sth_1->finish(); 
$sth_2->finish(); 
$sth_3->finish(); 

$dbh->disconnect(); 

exit;