sqoop: truncate table prior export data from hdfs

10 篇文章 0 订阅

We are using Sqoop to export data from the hive to SQL Server. The new data is always appended to the existing data in SQL Server.

Is it possible to truncate the SQL Server table via Sqoop before starting the export?

 

sqoop eval --connect 'jdbc:sqlserver://1.1.1.1;database=SomeDatabase;username=someUser;password=somePassword' --query "TRUNCATE TABLE some_table"

In hue, I create a sqoop action likes

<workflow-app name="inokmobile-delete-before-export" xmlns="uri:oozie:workflow:0.4">
    <start to="inokmobile-delete-before-export"/>
    <action name="inokmobile-delete-before-export">
        <sqoop xmlns="uri:oozie:sqoop-action:0.2">
            <job-tracker>${jobTracker}</job-tracker>
            <name-node>${nameNode}</name-node>
              <arg> eval</arg>
              <arg>--connect</arg>
              <arg>jdbc:mysql://192.168.10.200:3306/new_inoknok</arg>
              <arg>--username</arg>
              <arg>dba</arg>
              <arg>--password</arg>
              <arg>123456</arg>
              <arg>--query</arg>
              <arg>TRUNCATE TABLE  ${tablename}</arg>
        </sqoop>
        <ok to="end"/>
        <error to="kill"/>
    </action>
    <kill name="kill">
        <message>Action failed, error message[${wf:errorMessage(wf:lastErrorNode())}]</message>
    </kill>
    <end name="end"/>
</workflow-app>

 

The table is truncated correctly. But the job is killed due to erro

 IllegalArgumentException: JobId string : is not properly formed 

 

I thought the right way is to write a java procedure to execute this functionality.

 

package com.inoknok.mysql.tool;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class TruncateMysqlTable {
    private static final String MYSQL_DRIVER = "com.mysql.jdbc.Driver";
    private static final String sql_prefix = "TRUNCATE TABLE ";

    public static void main(String[] args) {
	int argsLength = args.length;

	if (argsLength < 4) {
	    System.out
		    .println("Please input parameters: url, user,password, tableTruncated");
	    System.exit(-1);

	}

	String url = args[0];
	String user = args[1];
	String password = args[2];

	List<String> tables = new ArrayList<String>();

	for (int i = 3; i < argsLength; i++) {
	    tables.add(args[i]);
	}

	try {
	    Class.forName(MYSQL_DRIVER);
	    Connection conn = DriverManager.getConnection(url, user, password);
	    if (!conn.isClosed()) {
		Statement statement = conn.createStatement();

		for (String table : tables) {
		    String sql = sql_prefix + table;
		    statement.execute(sql);
		}

		closeResources(conn, statement);
	    }
	} catch (Exception e) {
	    System.out.println("Fail to truncate table !");
	    e.printStackTrace();
	}
    }

    public static void closeResources(Connection conn, Statement pstmt,
	    ResultSet rs) {
	if (null != rs) {
	    try {
		rs.close();
	    } catch (SQLException e) {
		e.printStackTrace();
		throw new RuntimeException(e);
	    } finally {
		if (null != pstmt) {
		    try {
			pstmt.close();
		    } catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		    } finally {
			if (null != conn) {
			    try {
				conn.close();
			    } catch (SQLException e) {
				e.printStackTrace();
				throw new RuntimeException(e);
			    }
			}
		    }
		}
	    }
	}
    }

    public static void closeResources(Connection conn, Statement pstmt) {

	if (null != pstmt) {
	    try {
		pstmt.close();
	    } catch (SQLException e) {
		e.printStackTrace();
		throw new RuntimeException(e);
	    } finally {
		if (null != conn) {
		    try {
			conn.close();
		    } catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		    }
		}
	    }
	}
    }

}

 

 

 

References

http://stackoverflow.com/questions/15808257/sqoop-truncate-sql-server-table-before-exporting-data-from-hadoop

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值