parse sparksql syntax, format SQL
Install cuopyue using npm:
npm install cuopyue -S
Import coupyue in your js file
import { parser } from 'cuopyue';
Then, call parserSparksql
method
const sql = 'select age, nickname from userInfo; select id, price from payInfo;'
const tree = parser.parserSparkSql(sql);
...
You will get an array of syntax tree.
import { parser } from 'cuopyue';
const sql = 'select age, nickname fr1om userInfo;'
parser.parserSparkSql(sql, (recognizer, offendingSymbol, line, column, msg) => {
const errorText = offendingSymbol.text;
console.error(msg);
});
import { parser, visitor } from 'cuopyue';
const sql = 'select age, nickname from userInfo as u;'
class Visitor extends visitor.SparkTreeVisitor {
visitTableName (ctx) {
const tableName = ctx.tableIdentifier().getText();
const tableAlias = ctx.tableAlias().strictIdentifier().getText();
super.visitTableName(ctx);
}
}
parser.parserSparkSql(sql, null, new Visitor());
import { parser, visitor } from 'cuopyue';
const sql = 'select age, nickname from userInfo as u;'
let formatVisitor = new visitor.SparkFormatVisitor();
parser.parserSparkSql(sql, null, formatVisitor);
console.log(formatVisitor.getFormatText());