package com.ssl.datasource;
import com.aliyun.odps.*;
import com.aliyun.odps.data.Record;
import com.aliyun.odps.task.SQLTask;
import org.apache.commons.io.output.WriterOutputStream;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class ModelSpace {
static ArrayList<String> tableList = new ArrayList();
static Odps odpsClient = OdpsUtils.getOdpsClient();
public static void deal() {
System.out.println("start get table name and save to arraylsit");
odpsClient.setDefaultProject("xx");
Tables tables = odpsClient.tables();
for (Table table : tables) {
tableList.add(table.getName());
}
System.out.println("table name save complete");
System.out.println("start deal table");
ArrayList<ArrayList<String>> arrayLists = new ArrayList<>();
if (tableList.size()<20){
arrayLists.add(tableList);
}else {
for (int i = 0; i < tableList.size(); i++) {
int num = i /20;
if (i%20 == 0){
arrayLists.add(new ArrayList<String>(20));
}
if (arrayLists.size() == num + 1){
arrayLists.get(num).add(tableList.get(i));
}
}
}
//创建线程池
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10,10,0, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
for (int i = 0; i < arrayLists.size(); i++) {
}
}
public static class UnitThread extends Thread{
private int pageIndex;
private ArrayList<ArrayList<String>> arrayLists;
public UnitThread(int pageIndex, ArrayList<ArrayList<String>> arrayLists) {
this.pageIndex = pageIndex;
this.arrayLists = arrayLists;
}
@Override
public void run() {
System.out.println("thread"+pageIndex+"start");
String sql = "xxx";
ArrayList<String> tableList = arrayLists.get(pageIndex);
for (String table : tableList) {
try{
Instance run = SQLTask.run(odpsClient, sql + table + ";");
run.waitForSuccess();
List<Record> result = SQLTask.getResult(run);
System.out.println(result.get(0).get(0));
}catch (OdpsException e){
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
ModelSpace modelSpace = new ModelSpace();
modelSpace.deal();
}
}