用java代码调用shell脚本执行sqoop将hive表中数据导出到mysql
1:创建shell脚本
touch sqoop_options.sh
chmod 777 sqoop_options.sh
编辑文件 特地将执行map的个数设置为变量 测试 可以java代码传参数 同时也验证sqoop的 options 属性支持这种写法
#!/bin/bash
/opt/cdh-5.3.6/sqoop-1.4.5-cdh5.3.6/bin/sqoop --options-file /opt/cdh-5.3.6/sqoop-1.4.5-cdh5.3.6/sqoop-import-mysql.txt --num-mappers $1
if [ $? -eq 0 ];then
echo "success"
else
echo "error"
fi
2:创建 sqoop-import-mysql.txt 文件并编辑
touch sqoop-import-mysql.txt
export
--connect
jdbc:mysql://172.16.71.27:3306/babasport
--username
root
--password
root
--table
test_hive
--export-dir
/user/hive/warehouse/hive_bbs_product_snappy
--input-fields-terminated-by
'\t'
hive数据存在hdfs位置
3:开始写java后台代码 目前只支持 window写法 后期加上linux调用shell脚本的写法
package com.liveyc.common.utils; import java.util.Properties; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; public class FileToHbase {
/**
* shell脚本执行成功标识
*/
public static int SHELL_EXIT_OK = 0;
public static Log log = LogFactory.getLog(FileToHbase.class);
public static String connIp = "172.16.71.120";
public static String connUser = "root";
public static String connPwd = "123456"; public static void main(String[] args) throws Exception {
boolean result = export();
System.out.println(result);
} public static boolean export() throws Exception {
boolean result = false;
// 如果当前系统是window系统需要远程ssh连接系统
if (isWinSystem()) {
ConnectShell connectShell = new ConnectShell(connIp, connUser, connPwd, "utf-8");
String url = "/opt/cdh-5.3.6/sqoop-1.4.5-cdh5.3.6/sqoop_options.sh" + " " +1;
result = connectShell.excuteShellCommand(url);
}
return result;
} /**
* 当前操作系统类型
*
* @return true 为windos系统,false为linux系统
*/
public static boolean isWinSystem() {
// 获取当前操作系统类型
Properties prop = System.getProperties();
String os = prop.getProperty("os.name");
if (os.startsWith("win") || os.startsWith("Win")) {
return true;
} else {
return false;
}
}
}
package com.liveyc.common.utils; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import ch.ethz.ssh2.ChannelCondition;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler; /**
*
* ConnectShell
*
* @Description:连接Shell脚本所在服务器
* @author:aitf
* @date: 2016年3月31日
*
*/
public class ConnectShell {
private Connection conn;
private String ipAddr;
private String userName;
private String password;
private String charset = Charset.defaultCharset().toString();
private static final int TIME_OUT = 1000 * 5 * 60;
public static Log log = LogFactory.getLog(ConnectShell.class); public ConnectShell(String ipAddr, String userName, String password, String charset) {
this.ipAddr = ipAddr;
this.userName = userName;
this.password = password;
if (charset != null) {
this.charset = charset;
}
} public boolean login() throws IOException {
conn = new Connection(ipAddr);
conn.connect();
return conn.authenticateWithPassword(userName, password); // 认证
} /**
*
* @Title: excuteShellCommand
* @Description: 执行shell脚本命令
* @param shellpath
* @return
*/
public boolean excuteShellCommand(String shellpath) {
InputStream in = null;
boolean result = false;
String str = "";
try {
if (this.login()) {
Session session = conn.openSession();
//session.execCommand("cd /root");
session.execCommand(shellpath);
in = new StreamGobbler(session.getStdout());
// in = session.getStdout();
str = this.processStdout(in, charset);
session.waitForCondition(ChannelCondition.EXIT_STATUS, TIME_OUT);
session.close();
conn.close();
if (str.contains("success")) {
result = true;
}else{
result = false;
}
}
} catch (IOException e1) {
e1.printStackTrace();
}
return result;
} public String excuteShellCommand2(String shellpath) throws Exception {
InputStream in = null;
String result = "";
try {
if (this.login()) {
Process exec = Runtime.getRuntime().exec(shellpath);// ipconfig
in = exec.getInputStream();
result = this.processStdout(in, this.charset);
}
} catch (IOException e1) {
e1.printStackTrace();
}
return result;
} /**
* 转化结果
*
* @param in
* @param charset
* @return
* @throws UnsupportedEncodingException
*/
public String processStdout(InputStream in, String charset) throws UnsupportedEncodingException {
String line = null;
BufferedReader brs = new BufferedReader(new InputStreamReader(in, charset));
StringBuffer sb = new StringBuffer();
try {
while ((line = brs.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
log.error("---转化出现异常---");
}
return sb.toString();
} }
4:开始测试
在mysql创建一个表 hive中数据格式 是 int int String
CREATE TABLE test_hive(
id INT,
brand_id INT,
NAME VARCHAR(200)
)
执行java main方法 开始测试
观看8088端口 查看MapReduce的运行状况 发现正在运行(开心)
执行完毕
可以看到 只有1个 MapReduce任务 (默认的个数是4个 这样看来第一步写的shell脚本 参数是传递过来了 sqoop的 options 也支持这种直接指定参数的写法)
现在转过来看java代码
返回值 :
Warning: /opt/cdh-5.3.6/sqoop-1.4.5-cdh5.3.6/bin/../../hbase does not exist! HBase imports will fail.
Please set $HBASE_HOME to the root of your HBase installation.
Warning: /opt/cdh-5.3.6/sqoop-1.4.5-cdh5.3.6/bin/../../hcatalog does not exist! HCatalog jobs will fail.
Please set $HCAT_HOME to the root of your HCatalog installation.
Warning: /opt/cdh-5.3.6/sqoop-1.4.5-cdh5.3.6/bin/../../accumulo does not exist! Accumulo imports will fail.
Please set $ACCUMULO_HOME to the root of your Accumulo installation.
Warning: /opt/cdh-5.3.6/sqoop-1.4.5-cdh5.3.6/bin/../../zookeeper does not exist! Accumulo imports will fail.
Please set $ZOOKEEPER_HOME to the root of your Zookeeper installation.
success
发现返回 success 说明shell脚本执行成功了
一切执行正常 看下mysql 数据库表中有没有数据
OK 一切正常 , 后期把linux执行shell脚本的语句也补充上 。
用java代码调用shell脚本执行sqoop将hive表中数据导出到mysql的更多相关文章
- Java代码调用Shell脚本并传入参数实现DB2数据库表导出到文件
本文通过Java代码调用Shell脚本并传入参数实现DB2数据库表导出到文件,代码如下: import java.io.File; import java.io.IOException; import ...
- (MySQL里的数据)通过Sqoop Import Hive 里 和 通过Sqoop Export Hive 里的数据到(MySQL)
Sqoop 可以与Hive系统结合,实现数据的导入和导出,用户需要在 sqoop-env.sh 中添加HIVE_HOME的环境变量. 具体,见我的如下博客: hadoop2.6.0(单节点)下Sqoo ...
- Java如何调用shell脚本的
有些时候会碰到这样的场景:java的功能里面要嵌入一个功能点,这个功能是通过是shell脚本实现的.这种时候就需要Java对脚本调用的支持了. 测试环境 Ubuntu16.04 i3-6100,12G ...
- 通过ant调用shell脚本执行adb命令
在Hudson或者Jenkins中利用ant的exec 来调用shell命令,通过shell脚本来执行adb shell命令,可以正常执行,不会出现在ant中直接调用adb shell出现的假死情况. ...
- 利用jmeter发起java请求调用shell脚本
1.创建maven项目 在pom文件中加入依赖: 2.在路径src/main/java下创建类,如类名shellclass 3. 创建jmet ...
- java调用shell脚本执行操作
//定时清空 日志 String shellString = "sh /home/jyapp/delete_log.sh"; Process process = Runtime.g ...
- Java 调用 shell 脚本详解
这一年的项目中,有大量的场景需要Java 进程调用 Linux的bash shell 脚本实现相关功能. 从之前的项目中拷贝的相关模块和网上的例子来看,有个别的“陷阱”造成调用shell 脚本在某些特 ...
- python调用shell脚本时需要切换目录
最近遇到了一个问题,就是python代码调用shell脚本时,发现输入输出的文件,总是和自己预想的有偏差,但是单独在linux下执行命令的时候,却没有错误.后来发现是相对路径的问题,因为执行pytho ...
- Java代码调用服务器上的Shell脚本
Java代码调用服务器上的Shell脚本 这里主要是因为我们报表平台有用到用户手工录入的数据作为结果数据且需要纳入saiku去展示 如我们所知,saiku不会自动刷新,所以需要在数据更新接口中调用服务 ...
随机推荐
- Java实现的词频统计
要求: 1.读取文件: 2.记录出现的词汇及出现频率: 3.按照频率降序排列: 4.输出结果. 概要: 1.读取的文件路径是默认的,为了方便调试,将要统计的文章.段落复制到文本中即可:2.只支持英文: ...
- PAT 甲级 1041 Be Unique
https://pintia.cn/problem-sets/994805342720868352/problems/994805444361437184 Being unique is so imp ...
- DataTable Excel 导出:
public static class CSVFileHelper { public static string ToHtmlTable(this DataTable target) { return ...
- Visual Studio 中设置npm
VS2017自带的npm会去国外的镜像下载文件, 奇慢无比, 还是马云家淘宝的镜像适合国内用户. 淘宝npm镜像地址: https://registry.npm.taobao.org VS中使用淘宝 ...
- floyd最短路
floyd可以在O(n^3)的时间复杂度,O(n^2)的空间复杂度下求解正权图中任意两点间的最短路长度. 本质是动态规划. 定义f[k][i][j]表示从i出发,途中只允许经过编号小于等于k的点时的最 ...
- 转:解决Python中文编码问题
Python 文本挖掘:解决Python中文编码问题 转于:http://rzcoding.blog.163.com/blog/static/2222810172013101785738166/ ...
- [TJOI2008]彩灯 线性基
题面 题面 题解 题意:给定n个01串,求互相异或能凑出多少不同的01串. 线性基的基础应用. 对于线性基中的01串,如果我们取其中一些凑成一个新的01串,有一个重要的性质:任意2个不同方案凑出的01 ...
- Java 工作2年后需要达到怎么样的技术水平
有人回答说这只能是大企业或者互联网企业的工程师才能拿到.也许是的,小公司或者非互联网企业拿两万的不太可能是码农了,应该是已经转管理后才有可能.还有区域问题,这个不在我的考虑范围内,因为除了北上广深杭, ...
- bzoj1912【Apio2010】patrol 巡逻
题解: 显然需要分类讨论了,首先理解k==0即原图时按照dfs序来说 , 每条边至少走两次: k==1,相当于可以省去dfs回溯时第二次走过某条路径的浪费,所以答案是k==0的答案-直径 : k==2 ...
- 框架----Django内置Admin
Django内置的Admin是对于model中对应的数据表进行增删改查提供的组件,使用方式有: 依赖APP: django.contrib.auth django.contrib.contenttyp ...