This commit is contained in:
尹舟 2025-02-20 11:39:04 +08:00
parent 40ac60bb49
commit be9c66a339
3 changed files with 52 additions and 119 deletions

View File

@ -26,6 +26,7 @@ def convert_sql():
log.info("SQL hologres_connection: %s", hologres_connection)
try:
parsed_result=parse_create_table_sql(sql_input,hologres_connection)
print(parsed_result)
result = {
'parsed_result': parsed_result,
'message': 'SQL processed successfully.'

View File

@ -4,147 +4,75 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>SQL Processor</title>
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700&display=swap" rel="stylesheet">
<style>
body {
font: 1rem 'Roboto', Arial, sans-serif;
margin: 0;
padding: 0;
background: #f5f5f5;
color: #333;
}
.container {
max-width: 1200px;
margin: 0 auto;
padding: 1rem;
}
header {
text-align: center;
padding: 1rem 0;
background: #fff;
border-radius: 10px;
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
margin-bottom: 1rem;
}
h1 {
font-size: 2.5rem;
color: #4a90e2;
margin: 0;
font-weight: 500;
}
form {
background: #fff;
padding: 2rem;
border-radius: 10px;
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
}
textarea, input[type="text"] {
width: 100%;
height: 40px;
margin-bottom: 1rem;
font: inherit;
border: 1px solid #ddd;
border-radius: 6px;
padding: 0.6rem;
background: #f9f9f9;
resize: none;
}
body { font: 1rem 'Roboto', Arial, sans-serif; margin: 0; padding: 0; background: #f5f5f5; color: #333; }
.container { max-width: 1200px; margin: 0 auto; padding: 1rem; }
header { text-align: center; padding: 1rem 0; background: #fff; border-radius: 10px; box-shadow: 0 4px 6px rgba(0,0,0,0.1); margin-bottom: 1rem; }
h1 { font-size: 2.5rem; color: #4a90e2; margin: 0; font-weight: 500; }
form, .output-box { background: #fff; padding: 1rem; border-radius: 10px; box-shadow: 0 4px 6px rgba(0,0,0,0.1); }
textarea, input[type="text"] { width: 100%; padding: 0.6rem; margin-bottom: 1rem; font: inherit; border: 1px solid #ddd; border-radius: 6px; background: #f9f9f9; resize: none; }
textarea { height: 200px; }
button {
display: inline-block;
background: #4a90e2;
color: #fff;
border: none;
border-radius: 6px;
padding: 0.75rem 1.5rem;
font: 600 1rem 'Roboto', Arial, sans-serif;
cursor: pointer;
transition: background 0.3s ease;
}
button { display: inline-block; background: #4a90e2; color: #fff; border: none; border-radius: 6px; padding: 0.75rem 1.5rem; font: 600 1rem 'Roboto'; cursor: pointer; transition: background 0.3s ease; }
button:hover { background: #357ab8; }
#resultArea {
margin-top: 1rem;
padding: 1rem;
background: #fff;
border-radius: 10px;
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
display: none;
}
.output-box {
border: 1px solid #ddd;
padding: 1rem;
margin-bottom: 1rem;
border-radius: 6px;
background: #f9f9f9;
}
h2 {
font-size: 1.5rem;
color: #4a90e2;
margin: 0 0 0.5rem 0;
font-weight: 500;
}
.output-box textarea {
background: #fff;
border: 1px solid #ddd;
}
.error {
color: #dc3545;
background: #ffebee;
padding: 0.6rem;
border-radius: 6px;
}
.success {
color: #28a745;
background: #d4edda;
padding: 0.6rem;
border-radius: 6px;
}
.output-box { margin-bottom: 1rem; border: 1px solid #ddd; border-radius: 6px; background: #f9f9f9; }
.error { color: #dc3545; background: #ffebee; padding: 0.6rem; border-radius: 6px; }
.success { color: #28a745; background: #d4edda; padding: 0.6rem; border-radius: 6px; }
</style>
</head>
<body>
<div class="container">
<header><h1>SQL Processor</h1></header>
<form id="sqlForm">
<textarea id="sqlInput" placeholder="Enter your SQL here..."></textarea>
<label for="hologresConnection">Hologres Connection Info:</label>
<input type="text" id="hologresConnection" name="hologresConnection" value="hgprecn-cn-i7m2ssubq004-cn-hangzhou-internal.hologres.aliyuncs.com:80" placeholder="Enter Hologres connection info...">
<button type="submit">Convert</button>
</form>
<div id="resultArea" class="output-box"></div>
</div>
<div class="container">
<header><h1>SQL Processor</h1></header>
<form id="sqlForm">
<textarea id="sqlInput" placeholder="Enter your SQL here..."></textarea>
<label for="hologresConnection">Hologres Connection Info:</label>
<input type="text" id="hologresConnection" name="hologresConnection" value="hgprecn-cn-i7m2ssubq004-cn-hangzhou-internal.hologres.aliyuncs.com:80" placeholder="Enter Hologres connection info...">
<button type="submit">Convert</button>
</form>
<div id="resultArea" class="output-box" style="display: none;"></div>
</div>
<script>
document.getElementById('sqlForm').addEventListener('submit', async (e) => {
e.preventDefault();
const sql = document.getElementById('sqlInput').value;
const connection = document.getElementById('hologresConnection').value;
const resultArea = document.getElementById('resultArea');
resultArea.style.display = 'block';
resultArea.querySelector('.output-box')?.remove();
const sql = encodeURIComponent(document.getElementById('sqlInput').value);
const connection = encodeURIComponent(document.getElementById('hologresConnection').value);
try {
const response = await fetch('/convert', {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: `sql=${encodeURIComponent(sql)}&hologresConnection=${encodeURIComponent(connection)}`
body: `sql=${sql}&hologresConnection=${connection}`
});
const data = await response.json();
if (data.error) {
resultArea.innerHTML = `<div class="output-box error"><textarea readonly>${data.error}</textarea></div>`;
const div = document.createElement('div');
div.className = 'output-box error';
div.textContent = data.error;
resultArea.appendChild(div);
} else {
const tables = data.parsed_result || '请检查输入建表语句';
resultArea.innerHTML = `
<div class="output-box success">
<h2>Processed SQL:</h2>
<textarea readonly>${tables}</textarea>
<p>${data.message}</p>
</div>
`;
const div = document.createElement('div');
div.className = 'output-box success';
const textarea = document.createElement('textarea');
textarea.readOnly = true;
textarea.value = data.parsed_result || '请检查输入建表语句';
div.appendChild(textarea);
const messageP = document.createElement('p');
messageP.textContent = data.message;
div.appendChild(messageP);
resultArea.appendChild(div);
}
} catch (error) {
console.error('Error:', error);
resultArea.innerHTML = `<div class="output-box error"><textarea readonly>An unexpected error occurred. Please try again.</textarea></div>`;
const errorDiv = document.createElement('div');
errorDiv.className = 'output-box error';
errorDiv.textContent = 'An unexpected error occurred. Please try again.';
resultArea.appendChild(errorDiv);
}
resultArea.style.display = 'block';
});
</script>
</body>

View File

@ -11,10 +11,10 @@ CREATE EXTERNAL TABLE IF NOT EXISTS {table_name}
)
STORED BY 'com.aliyun.odps.jdbc.JdbcStorageHandler'
-- ip设置成经典网络ip 加Schema 加表名
location 'jdbc:postgresql://{hologres_connection}/{schema}?ApplicationName=MaxCompute&currentSchema={schema}&preferQueryMode=simple&useSSL=false&table={table_name}/'
location 'jdbc:postgresql://hgprecn-cn-i7m2ssubq004-cn-hangzhou-internal.hologres.aliyuncs.com:80/sdk_statis?ApplicationName=MaxCompute&currentSchema={schema}&preferQueryMode=simple&useSSL=false&table={table_name}/'
TBLPROPERTIES (
'mcfed.mapreduce.jdbc.driver.class'='org.postgresql.Driver',
'odps.federation.jdbc.target.db.type'='holo',
'mcfed.mapreduce.jdbc.driver.class'='org.postgresql.Driver',
'odps.federation.jdbc.target.db.type'='holo',
-- 格式为MaxCompute字段1 : "Hologres字段1",MaxCompute字段2 : "Hologres字段2"
'odps.federation.jdbc.colmapping'='{colmapping}'
);
@ -70,6 +70,10 @@ def parse_create_table_sql(create_table_sql, hologres_connection):
# 如果是TEXT类型则转换为STRING
if column_type == 'TEXT':
column_type = 'STRING'
if column_type == 'DECIMAL':
column_type = 'DECIMAL(20,8)'
# if column_type == 'INT':
# column_type = 'BIGINT'
columns.append(column_name + " " + column_type)
colmapping.append(column_name + ":" + column_name)
# 将columns,colmapping转换成字符串用,分割