需求 表结构如下
#!/bin/bash
# ** 存储过程名称: ysp.ddl
# ** 功能描述:
# ** 创建者: wx
# ** 创建日期:
# ** 修改者:
# ** 创建日期:
# ** 修改内容:
drop table if exists ods_payment_flow_sf7;
create table ods_payment_flow_sf7(
paymentid string ,
currency string ,
paytype string ,
recordtype string ,
appname string ,
channelid string ,
userid string ,
merchantaccount string ,
result string ,
amount float ,
createtime string ,
finishtime string ,
lastup string ,
productorderid string ,
refundpaymentid string ,
serviceid string ,
migupaypaytype string ,
migupayothertype string ,
migupayorderid string ,
strdata string ,
resultsource string ,
portaltype string ,
ip string ,
bizdate string ,
importtime date
)partitioned by
(in_month string)
row format
delimited fields terminated by '|';
drop table if exists ods_payment_flow_sf71;
create table ods_payment_flow_sf71(
paymentid string ,
currency string ,
paytype string ,
recordtype string ,
appname string ,
result string ,
amount float ,
createtime string ,
finishtime string ,
lastup string ,
productorderid string ,
)partitioned by
(in_month string)
row format
delimited fields terminated by '|';
--货币信息表
drop table if exists dim_scope_currency;
create table dim_scope_currency
(
currency string comment ' 货币类型 ' ,
scope string comment ' scope ' ,
status string comment ' 状态(0:未启用/1:启用) ' ,
remark string comment ' 备注 ' ,
)
partitioned by (in_month string)
row format
delimited fields terminated by '|';
将上百张表导出单张分表。
代码如下
package other;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class SplitWord {
public static void main(String[] args) throws Exception {
splitFileDemo("C:\\Users\\rongx\\Desktop\\ddl-bat\\yp_ods2.ddl",1200);
}
public static void splitFileDemo(String path,int maxline) throws Exception {
FileInputStream fis = new FileInputStream(path);
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
BufferedReader br = new BufferedReader(isr);
// 获取文件名
/* String fileName = path.substring(0, path.indexOf("."));
// 获取文件后缀
String endName = path.substring(path.lastIndexOf("."));*/
try {
int i = 0;
boolean end = false;//判断文件是否读取完毕
while (true) {
if (end)
break;
StringBuffer sb = new StringBuffer();
String[] arr=null;
/* sb.append(fileName);
sb.append("_data");
sb.append(i);
sb.append(endName);*/
int flag=-1;
if(flag==-1) {
sb.append("E:\\test\\");
sb.append("first");
sb.append(".txt");
}
int count=0;
System.out.println(sb.toString());// 新生成的文件名
// 写入文件
FileOutputStream fos=null;
OutputStreamWriter osw=null;
BufferedWriter bw=null;
if(count==0) {
fos = new FileOutputStream(new File(
sb.toString()),true);
osw = new OutputStreamWriter(fos, "UTF-8");
bw = new BufferedWriter(osw);
bw.flush();
}
String line = "";// 一行行读取文件
int m = 1;
while((line = br.readLine())!=null ){
boolean b = line.contains("drop table");
if(b){
String[] split=null;
if(line.contains("exists")){
split = line.split("exists");
}else{
split = line.split("table");
}
System.out.println(split[1]);
String replace = split[1].replace(";", "").trim();
arr=new String[1];
arr[0]=replace;
}
if( line.startsWith("drop table")){
flag++;
count++;
}
if(count==1){
bw.write(line);
bw.write("\r\n");
}
if(count>1){
sb.setLength(0);
sb.append("E:\\test\\");
sb.append(arr[0]);
sb.append(".txt");
fos = new FileOutputStream(new File(
sb.toString()),true);
osw = new OutputStreamWriter(fos, "UTF-8");
bw = new BufferedWriter(osw);
bw.write(line);
bw.write("\r\n");
bw.flush();
}
if(m>=maxline){
break;
}
m++;
}
if(m<maxline)
end = true;
//关闭写入流
bw.close();
osw.close();
fos.close();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//关闭输入流
br.close();
isr.close();
fis.close();
}
System.out.println("--- 文件分割完成 ---");
}
}
结果如下