
net.wicp.tams.common.http.flink.SqlGateResultPo Maven / Gradle / Ivy
The newest version!
package net.wicp.tams.common.http.flink;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
import lombok.AccessLevel;
import lombok.Data;
import lombok.Setter;
import net.wicp.tams.common.apiext.CollectionUtil;
import net.wicp.tams.common.apiext.StringUtil;
import net.wicp.tams.common.exception.ExceptAll;
import net.wicp.tams.common.exception.ProjectExceptionRuntime;
//{
// "results": {
// "columns": [
// {
// "name": "current catalog name",
// "logicalType": {
// "type": "VARCHAR",
// "nullable": true,
// "length": 2147483647
// },
// "comment": null
// }
// ],
// "data": [
// {
// "kind": "INSERT",
// "fields": [
// "mycatalog"
// ]
// }
// ]
// },
// "resultType": "PAYLOAD",
// "nextResultUri": "/v1/sessions/4aeeeee9-44a8-4e1b-8eeb-078fa09a1ab6/operations/3e47c18c-e4ac-4b8c-a86f-3557d9e504ba/result/1"
//}
@Data
public class SqlGateResultPo {
private ResultType resultType;
private String nextResultUri;// eg:/v1/sessions/4aeeeee9-44a8-4e1b-8eeb-078fa09a1ab6/operations/3e47c18c-e4ac-4b8c-a86f-3557d9e504ba/result/1
// private String dataKind;// 来自data->kind
// private List preData = new ArrayList();// 前一页
private final List cols = new ArrayList();// 列相关信息
@Setter(value = AccessLevel.PRIVATE)
private String[] colNames;// cols的名字
@Setter(value = AccessLevel.PRIVATE)
private List curData = new ArrayList();// 当前页
// private List nextDate = new ArrayList();// 后一页
@Setter(value = AccessLevel.PRIVATE)
private int curPage;
@Setter(value = AccessLevel.PRIVATE)
private String operationHandle;
@Setter(value = AccessLevel.PRIVATE)
private int cursor = -1;// 当前所在位置
private String errors;// 如果是错误则有信息,否则是成功
private String jobID;
public void beforeFirst() {
this.cursor=-1;
}
public void afterLast() {
this.cursor=curData.size()-1;
}
public boolean isSuc() {
return StringUtil.isNull(this.errors);
}
public String[] getColNames() {
if (!isSuc()) {// 非成功的结果
return null;
}
if (colNames != null && colNames.length == cols.size()) {
return colNames;
} else {
List> colFromObj = CollectionUtil.getColFromObj(cols, "name");
this.colNames = colFromObj.toArray(new String[colFromObj.size()]);
return this.colNames;
}
}
public void setNextResultUri(String nextResultUri) {
this.nextResultUri = nextResultUri;
this.cursor = -1;// 要先设置为-1
if (StringUtil.isNull(this.nextResultUri)) {
this.curPage = 0;
this.operationHandle = "";
} else {
this.curPage = Integer.parseInt(this.nextResultUri.substring(this.nextResultUri.indexOf("/result/") + 8))
- 1;// 当前页面是下一页-1
int begin = this.nextResultUri.indexOf("/operations/");
int end = this.nextResultUri.indexOf("/result/");
this.operationHandle = this.nextResultUri.substring(begin + 12, end);
}
}
// 得到当前行数据
public String[] getDataCurRow() {
if (!isSuc()) {// 非成功的结果
return null;
}
if (cursor == -1) {
throw new ProjectExceptionRuntime(ExceptAll.jdbc_sql_notOpenRs);
}
return curData.get(cursor);
}
// 只得到第一列数据,适合于简单查贸易
public String[] getSimpleData() {
if (!isSuc()) {// 非成功的结果
return null;
}
String[] ret = new String[this.curData.size()];
for (int i = 0; i < this.curData.size(); i++) {
ret[i] = this.curData.get(i)[0];
}
return ret;
}
// 得到result列,一般来说,DDL语句,一般是下面这种反回结果
// {
// "nextResultUri": "/v1/sessions/d127d966-1b7b-4161-8b2e-ddec9a58d9bc/operations/252fd91b-6a3b-473e-9101-1ebc9367d52e/result/1",
// "results": {
// "data": [{
// "kind": "INSERT",
// "fields": ["CREATE TABLE `mycatalog`.`duckula`.`t_666` (\n `aa` VARCHAR(2147483647) NOT NULL,\n `bb` VARCHAR(2147483647),\n CONSTRAINT `PK_aa` PRIMARY KEY (`aa`) NOT ENFORCED\n) COMMENT '通过sql创建'\n"]
// }],
// "columns": [{
// "logicalType": {
// "nullable": true,
// "length": 2147483647,
// "type": "VARCHAR"
// },
// "name": "result"
// }]
// },
// "resultType": "PAYLOAD"
// }
public String getResultColData() {
this.cursor = 0;// 有且只有一行
String dataColByCurRow = getDataColByCurRow("result");
return dataColByCurRow;
}
public String getDataColByCurRow(String col) {
if (!isSuc()) {// 非成功的结果
return null;
}
int indexOf = ArrayUtils.indexOf(getColNames(), col);
if (indexOf == -1) {
throw new ProjectExceptionRuntime(ExceptAll.Project_default, "没有此列");
}
return getDataCurRow()[indexOf];
}
@Setter(value = AccessLevel.PRIVATE)
private long maxCursor=0;
public boolean next(SqlGateWay sqlGateWay,long maxRows) {
if (!isSuc()) {// 非成功的结果
return false;
}
if(++maxCursor>maxRows) {//超过了最大行数
return false;
}
if (cursor < curData.size() - 1) {// cursor从0开始,因为游标要下移一位,所以要-1
this.cursor++;
return true;
} else {// 翻到下一页。
//正常结束且没拿到一条结果也算结束
if (this.resultType == ResultType.EOS|| (this.resultType == ResultType.PAYLOAD && CollectionUtils.isEmpty(this.curData)) || sqlGateWay == null || StringUtil.isNull(operationHandle)) {// 已结束
return false;
} else {
SqlGateResultPo next = sqlGateWay.next(this);
this.setResultType(next.getResultType());
this.setNextResultUri(next.getNextResultUri());
this.setCurData(next.getCurData());
return CollectionUtils.isNotEmpty(this.curData);
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy