linux导出单表数据,Linux下mysql多表数据拆分单表

需求 表结构如下

#!/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

end = true;

//关闭写入流

bw.close();

osw.close();

fos.close();

}

} catch (Exception e) {

e.printStackTrace();

} finally {

//关闭输入流

br.close();

isr.close();

fis.close();

}

System.out.println("--- 文件分割完成---");

}

}

结果如下

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值