下面示例包括使用 BufferedReader
读,FileWriter
、FileOutputStream
写。
package gaei.cn.x5l.x5lhive2cos.utils;
import java.io.*;
//历史数据自动生成sql修复分区
public class HdfsTboxAddPartitionSql {
public static void main(String[] args) {
String dbName = "database";
String tableName = "table_name";
String filePathInput = "C:/Users/MECHREVO/Desktop/新建文件夹/history_partitions/分区详情/" + tableName + ".txt";
String filePathOutput = "C:/Users/MECHREVO/Desktop/新建文件夹/history_partitions/分区修改至s3-sql/" + tableName + ".txt";
File fileIn = new File(filePathInput);
File fileOut = new File(filePathOutput);
BufferedReader reader = null;
FileWriter fw = null;
// FileOutputStream fos = null;
String partitionDir = null;
int line = 1;
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileIn), "GBK"));
fw = new FileWriter(fileOut);
// fos = new FileOutputStream(fileOut);
while ((partitionDir = reader.readLine()) != null) {
StringBuilder alterSql = new StringBuilder();
alterSql.append("alter table ");
String sampleDateDir = partitionDir.split("/")[0];//sample_date=20230510
String partitionNameDir = partitionDir.split("/")[1];//partition_name=101
String sample_date = sampleDateDir.split("=")[0];//sample_date
String sample_date_yyyyMMdd = sampleDateDir.split("=")[1];//20230510
String partition_name = partitionNameDir.split("=")[0];//partition_name
String partition_name_num = partitionNameDir.split("=")[1];//100
alterSql.append(dbName)
.append(".")
.append(tableName)
.append(" add if not exists partition(")
.append(sample_date)
.append("='")
.append(sample_date_yyyyMMdd)
.append("', ")
.append(partition_name)
.append("='")
.append(partition_name_num)
.append("') ")
.append("location '")
// .append("hdfs://prdns/warehouse/tablespace/external/hive/ods_x5l.db/")
.append("s3a://buceketname/prd/data/")
.append(dbName)
.append("/")
.append(tableName)
.append("/")
.append(partitionDir)
.append("';");
fw.write(alterSql + "\n");
// fos.write((alterSql + "\n").getBytes());
System.out.println(alterSql);
line++;
}
fw.flush();
fw.close();
// fos.flush();
// fos.close();
reader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
fw.close();
// fos.flush();
// fos.close();
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}