|
@@ -3,13 +3,18 @@ package com.tzld.piaoquan.recommend.server.service.odps;
|
|
import com.aliyun.odps.Instance;
|
|
import com.aliyun.odps.Instance;
|
|
import com.aliyun.odps.Odps;
|
|
import com.aliyun.odps.Odps;
|
|
import com.aliyun.odps.OdpsException;
|
|
import com.aliyun.odps.OdpsException;
|
|
|
|
+import com.aliyun.odps.PartitionSpec;
|
|
import com.aliyun.odps.account.Account;
|
|
import com.aliyun.odps.account.Account;
|
|
import com.aliyun.odps.account.AliyunAccount;
|
|
import com.aliyun.odps.account.AliyunAccount;
|
|
import com.aliyun.odps.data.Record;
|
|
import com.aliyun.odps.data.Record;
|
|
|
|
+import com.aliyun.odps.data.RecordReader;
|
|
import com.aliyun.odps.task.SQLTask;
|
|
import com.aliyun.odps.task.SQLTask;
|
|
|
|
+import com.aliyun.odps.tunnel.TableTunnel;
|
|
|
|
+import com.aliyun.odps.tunnel.TunnelException;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
+import java.io.IOException;
|
|
import java.text.SimpleDateFormat;
|
|
import java.text.SimpleDateFormat;
|
|
import java.time.LocalDateTime;
|
|
import java.time.LocalDateTime;
|
|
import java.time.format.DateTimeFormatter;
|
|
import java.time.format.DateTimeFormatter;
|
|
@@ -24,6 +29,7 @@ public class ODPSManager {
|
|
private final static String ACCESSID = "LTAIWYUujJAm7CbH";
|
|
private final static String ACCESSID = "LTAIWYUujJAm7CbH";
|
|
private final static String ACCESSKEY = "RfSjdiWwED1sGFlsjXv0DlfTnZTG1P";
|
|
private final static String ACCESSKEY = "RfSjdiWwED1sGFlsjXv0DlfTnZTG1P";
|
|
private final static String ENDPOINT = "http://service.cn.maxcompute.aliyun.com/api";
|
|
private final static String ENDPOINT = "http://service.cn.maxcompute.aliyun.com/api";
|
|
|
|
+ private final static String VPC_ENDPOINT = "http://service.cn-hangzhou-vpc.maxcompute.aliyun-inc.com/api";
|
|
|
|
|
|
public List<Record> query(String sql) {
|
|
public List<Record> query(String sql) {
|
|
Account account = new AliyunAccount(ACCESSID, ACCESSKEY);
|
|
Account account = new AliyunAccount(ACCESSID, ACCESSKEY);
|
|
@@ -48,6 +54,36 @@ public class ODPSManager {
|
|
return LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHH"));
|
|
return LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHH"));
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ public List<Record> tableTunnelQuery(String project, String table, String partition){
|
|
|
|
+ Account account = new AliyunAccount(ACCESSID, ACCESSKEY);
|
|
|
|
+ Odps odps = new Odps(account);
|
|
|
|
+ odps.setEndpoint(VPC_ENDPOINT);
|
|
|
|
+ odps.setDefaultProject(project);
|
|
|
|
+ TableTunnel tunnel = new TableTunnel(odps);
|
|
|
|
+ List<Record> records = new ArrayList<>();
|
|
|
|
+ RecordReader recordReader = null;
|
|
|
|
+ try {
|
|
|
|
+ TableTunnel.DownloadSession downloadSession = tunnel.createDownloadSession(project, table, new PartitionSpec(partition));
|
|
|
|
+ long count = downloadSession.getRecordCount();
|
|
|
|
+ recordReader = downloadSession.openRecordReader(0, count);
|
|
|
|
+ Record record;
|
|
|
|
+ while ((record = recordReader.read()) != null) {
|
|
|
|
+ records.add(record);
|
|
|
|
+ }
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error("tableTunnelQuery error", e);
|
|
|
|
+ } finally {
|
|
|
|
+ if (!Objects.isNull(recordReader)) {
|
|
|
|
+ try {
|
|
|
|
+ recordReader.close();
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error("error: ", e);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return records;
|
|
|
|
+ }
|
|
|
|
+
|
|
public static void main(String[] args) {
|
|
public static void main(String[] args) {
|
|
Calendar cal = Calendar.getInstance();
|
|
Calendar cal = Calendar.getInstance();
|
|
cal.setTime(new Date());
|
|
cal.setTime(new Date());
|