在scala shell中执行linux命令

ui7jx7zq  于 2021-05-29  发布在  Spark
关注(0)|答案(1)|浏览(734)

我正在做一个项目,需要在scala应用程序中执行一些linux命令(sqoop命令)。请参阅我在vm上尝试使用mysql执行的示例命令。

import sys.process._ 

"sqoop eval --connect jdbc:mysql://localhost:3306/retail_db --username root --password cloudera --query 'select * from categories'".!

我得到以下错误:

Warning: /usr/lib/sqoop/../accumulo does not exist! Accumulo imports will fail.
Please set $ACCUMULO_HOME to the root of your Accumulo installation.
20/06/24 15:25:27 INFO sqoop.Sqoop: Running Sqoop version: 1.4.6-cdh5.13.0
20/06/24 15:25:27 WARN tool.BaseSqoopTool: Setting your password on the command-line is insecure. 
Consider using -P instead.
20/06/24 15:25:27 ERROR tool.BaseSqoopTool: Error parsing arguments for eval:
20/06/24 15:25:27 ERROR tool.BaseSqoopTool: Unrecognized argument: *
20/06/24 15:25:27 ERROR tool.BaseSqoopTool: Unrecognized argument: from
20/06/24 15:25:27 ERROR tool.BaseSqoopTool: Unrecognized argument: categories

我也使用了这个命令,得到了同样的错误信息:

"sqoop eval --connect jdbc:mysql://localhost:3306/retail_db --username root --password cloudera --query 'select * from categories'".!<

有人能帮我找出错误的原因吗。我试过用单引号和双引号,都没有用。我到处找,但找不到任何解决办法。这就是我在这里发帖的原因。注意:在pyspark中成功执行相同的命令,如下所示:

>>> import os
>>> import sys

>>> query = "sqoop eval --connect jdbc:mysql://localhost:3306/retail_db --username root --password 
cloudera --query 'select * from categories'" 
>>> os.system(query)
Warning: /usr/lib/sqoop/../accumulo does not exist! Accumulo imports will fail.
Please set $ACCUMULO_HOME to the root of your Accumulo installation.
20/06/24 15:28:56 INFO sqoop.Sqoop: Running Sqoop version: 1.4.6-cdh5.13.0
20/06/24 15:28:56 WARN tool.BaseSqoopTool: Setting your password on the command-line is insecure. 
Consider using -P instead.
20/06/24 15:28:58 INFO manager.MySQLManager: Preparing to use a MySQL streaming resultset.
----------------------------------------------------
| category_id | category_department_id | category_name        | 
----------------------------------------------------
| 1           | 2           | Football             | 
| 2           | 2           | Soccer               | 
| 3           | 2           | Baseball & Softball  | 
| 4           | 2           | Basketball           | 
| 5           | 2           | Lacrosse             | 
| 6           | 2           | Tennis & Racquet     |
jdg4fx2g

jdg4fx2g1#

看起来像 sqoop 不认识 * , from ,和 categories 作为个人论点。当从命令行调用时,它工作的原因是shell解释引号并将它们表示为单个引号 select * from categories 争论。换句话说,shell在将所有内容交给 sqoop 程序。
这个 .! 方法(即scala ProcessBuilder )直接启动进程,这意味着命令元素不会传递给shell进行预处理。有两种方法可以解决这个问题。
您可以直接调用shell并将命令行作为单个参数传递给它,或者
你可以自己做大部分明显的预处理。
下面是第二个选项的示例。

Seq("sqoop"
   ,"eval"
   ,"--connect"
   ,"jdbc:mysql://localhost:3306/retail_db"
   ,"--username"
   ,"root"
   ,"--password"
   ,"cloudera"
   ,"--query"
   ,"select * from categories").!

如您所见,所有单独的参数都作为单独的参数显示,包括最后一个参数。

相关问题