1.0.2 • Published 1 year ago

@rasir/xterm-terminal v1.0.2

Weekly downloads
-
License
ISC
Repository
-
Last release
1 year ago

XtermTerminal

  • 采用 xterm 库做的一个 web 版终端工具。

组件演示

显示日志

import React from 'react';
import XtermTerminal from '@rasir/xterm-terminal';

const XtermLogger = () => {
  const testText = `2022-07-29 13:49:20,288 [main] ERROR - execute sql error
    org.apache.hive.service.cli.HiveSQLException: Error while compiling statement: FAILED: SemanticException org.apache.hadoop.hive.ql.parse.SemanticException: Table already exists: sf_hkust.yiqing_755fb_0729_waybill_182
    at org.apache.hive.jdbc.Utils.verifySuccess(Utils.java:266)
    at org.apache.hive.jdbc.Utils.verifySuccessWithInfo(Utils.java:252)
    at org.apache.hive.jdbc.HiveStatement.runAsyncOnServer(HiveStatement.java:318)
    at org.apache.hive.jdbc.HiveStatement.execute(HiveStatement.java:260)
    at com.sf.bdp.schetl.hive.driver.HiveJdbcExecutor.execute(HiveJdbcExecutor.java:93)
    at com.sf.bdp.schetl.hive.driver.HiveJdbcExecutor.execute(HiveJdbcExecutor.java:112)
    at com.sf.bdp.schetl.hive.driver.HiveDriver.main(HiveDriver.java:71)
    Caused by: org.apache.hive.service.cli.HiveSQLException: Error while compiling statement: FAILED: SemanticException org.apache.hadoop.hive.ql.parse.SemanticException: Table already exists: sf_hkust.yiqing_755fb_0729_waybill_182
    at org.apache.hive.service.cli.operation.Operation.toSQLException(Operation.java:388)
    at org.apache.hive.service.cli.operation.SQLOperation.prepare(SQLOperation.java:193)
    at org.apache.hive.service.cli.operation.SQLOperation.runInternal(SQLOperation.java:276)
    at org.apache.hive.service.cli.operation.Operation.run(Operation.java:324)
    at org.apache.hive.service.cli.session.HiveSessionImpl.executeStatementInternal(HiveSessionImpl.java:505)
    at org.apache.hive.service.cli.session.HiveSessionImpl.executeStatementAsync(HiveSessionImpl.java:492)
    at org.apache.hive.service.cli.CLIService.executeStatementAsync(CLIService.java:295)
    at org.apache.hive.service.cli.thrift.ThriftCLIService.ExecuteStatement(ThriftCLIService.java:506)
    at org.apache.hive.service.rpc.thrift.TCLIService$Processor$ExecuteStatement.getResult(TCLIService.java:1437)
    at org.apache.hive.service.rpc.thrift.TCLIService$Processor$ExecuteStatement.getResult(TCLIService.java:1422)
    at org.apache.thrift.ProcessFunction.process(ProcessFunction.java:39)
    at org.apache.thrift.TBaseProcessor.process(TBaseProcessor.java:39)
    at org.apache.hive.service.auth.TSetIpAddressProcessor.process(TSetIpAddressProcessor.java:56)
    at org.apache.thrift.server.TThreadPoolServer$WorkerProcess.run(TThreadPoolServer.java:286)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
    Caused by: java.lang.RuntimeException: org.apache.hadoop.hive.ql.parse.SemanticException:org.apache.hadoop.hive.ql.parse.SemanticException: Table already exists: sf_hkust.yiqing_755fb_0729_waybill_182
    at org.apache.hadoop.hive.ql.parse.SemanticAnalyzer.analyzeCreateTable(SemanticAnalyzer.java:11767)
    at org.apache.hadoop.hive.ql.parse.SemanticAnalyzer.genResolvedParseTree(SemanticAnalyzer.java:10717)
    at org.apache.hadoop.hive.ql.parse.SemanticAnalyzer.analyzeInternal(SemanticAnalyzer.java:10827)
    at org.apache.hadoop.hive.ql.parse.CalcitePlanner.analyzeInternal(CalcitePlanner.java:246)
    at org.apache.hadoop.hive.ql.parse.BaseSemanticAnalyzer.analyze(BaseSemanticAnalyzer.java:251)
    at org.apache.hadoop.hive.ql.Driver.compile(Driver.java:479)
    at org.apache.hadoop.hive.ql.Driver.compileInternal(Driver.java:1253)
    at org.apache.hadoop.hive.ql.Driver.compileAndRespond(Driver.java:1240)
    at org.apache.hive.service.cli.operation.SQLOperation.prepare(SQLOperation.java:191)
    ... 15 more
    Caused by: java.lang.RuntimeException: org.apache.hadoop.hive.ql.parse.SemanticException:Table already exists: sf_hkust.yiqing_755fb_0729_waybill_182
    at org.apache.hadoop.hive.ql.parse.SemanticAnalyzer.analyzeCreateTable(SemanticAnalyzer.java:11764)
    ... 23 more
    Exception in thread "main" com.sf.bdp.schetl.common.exception.ScheduleException: org.apache.hive.service.cli.HiveSQLException: Error while compiling statement: FAILED: SemanticException org.apache.hadoop.hive.ql.parse.SemanticException: Table already exists: sf_hkust.yiqing_755fb_0729_waybill_1822022-07-29 13:49:20,290 [Thread-6] ERROR - execute sql error
    java.sql.SQLException: Method getQueryLog() failed. Because the stmtHandle in HiveStatement is null and the statement execution might fail.
    at org.apache.hive.jdbc.HiveStatement.getQueryLog(HiveStatement.java:894)
    at org.apache.hive.jdbc.HiveStatement.getQueryLog(HiveStatement.java:857)
    at com.sf.bdp.schetl.hive.driver.HiveJdbcExecutor$LogPrinter.run(HiveJdbcExecutor.java:141)
    
    at com.sf.bdp.schetl.hive.driver.HiveJdbcExecutor.execute(HiveJdbcExecutor.java:97)日志收集出错:Method getQueryLog() failed. Because the stmtHandle in HiveStatement is null and the statement execution might fail.
    
    at com.sf.bdp.schetl.hive.driver.HiveJdbcExecutor.execute(HiveJdbcExecutor.java:112)
    at com.sf.bdp.schetl.hive.driver.HiveDriver.main(HiveDriver.java:71)
    Caused by: org.apache.hive.service.cli.HiveSQLException: Error while compiling statement: FAILED: SemanticException org.apache.hadoop.hive.ql.parse.SemanticException: Table already exists: sf_hkust.yiqing_755fb_0729_waybill_182
    at org.apache.hive.jdbc.Utils.verifySuccess(Utils.java:266)
    at org.apache.hive.jdbc.Utils.verifySuccessWithInfo(Utils.java:252)
    at org.apache.hive.jdbc.HiveStatement.runAsyncOnServer(HiveStatement.java:318)
    at org.apache.hive.jdbc.HiveStatement.execute(HiveStatement.java:260)
    at com.sf.bdp.schetl.hive.driver.HiveJdbcExecutor.execute(HiveJdbcExecutor.java:93)
    ... 2 more
    Caused by: org.apache.hive.service.cli.HiveSQLException: Error while compiling statement: FAILED: SemanticException org.apache.hadoop.hive.ql.parse.SemanticException: Table already exists: sf_hkust.yiqing_755fb_0729_waybill_182
    at org.apache.hive.service.cli.operation.Operation.toSQLException(Operation.java:388)
    at org.apache.hive.service.cli.operation.SQLOperation.prepare(SQLOperation.java:193)
    at org.apache.hive.service.cli.operation.SQLOperation.runInternal(SQLOperation.java:276)
    at org.apache.hive.service.cli.operation.Operation.run(Operation.java:324)
    at org.apache.hive.service.cli.session.HiveSessionImpl.executeStatementInternal(HiveSessionImpl.java:505)
    at org.apache.hive.service.cli.session.HiveSessionImpl.executeStatementAsync(HiveSessionImpl.java:492)
    at org.apache.hive.service.cli.CLIService.executeStatementAsync(CLIService.java:295)
    at org.apache.hive.service.cli.thrift.ThriftCLIService.ExecuteStatement(ThriftCLIService.java:506)
    at org.apache.hive.service.rpc.thrift.TCLIService$Processor$ExecuteStatement.getResult(TCLIService.java:1437)
    at org.apache.hive.service.rpc.thrift.TCLIService$Processor$ExecuteStatement.getResult(TCLIService.java:1422)
    at org.apache.thrift.ProcessFunction.process(ProcessFunction.java:39)
    at org.apache.thrift.TBaseProcessor.process(TBaseProcessor.java:39)
    at org.apache.hive.service.auth.TSetIpAddressProcessor.process(TSetIpAddressProcessor.java:56)2022-07-29 13:49:20,292 [Hive-JDBC-Driver-ShutdownHook] INFO  - trigger Hive-JDBC-Driver shutdown.
    
    at org.apache.thrift.server.TThreadPoolServer$WorkerProcess.run(TThreadPoolServer.java:286)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
    Caused by: java.lang.RuntimeException: org.apache.hadoop.hive.ql.parse.SemanticException:org.apache.hadoop.hive.ql.parse.SemanticException: Table already exists: sf_hkust.yiqing_755fb_0729_waybill_182
    at org.apache.hadoop.hive.ql.parse.SemanticAnalyzer.analyzeCreateTable(SemanticAnalyzer.java:11767)
    at org.apache.hadoop.hive.ql.parse.SemanticAnalyzer.genResolvedParseTree(SemanticAnalyzer.java:10717)
    at org.apache.hadoop.hive.ql.parse.SemanticAnalyzer.analyzeInternal(SemanticAnalyzer.java:10827)
    at org.apache.hadoop.hive.ql.parse.CalcitePlanner.analyzeInternal(CalcitePlanner.java:246)
    at org.apache.hadoop.hive.ql.parse.BaseSemanticAnalyzer.analyze(BaseSemanticAnalyzer.java:251)`;
  const loaded = (e, api) => {
    Promise.resolve().then(() => {
      testText.split(/\n/).forEach((text, i) => {
        if (i === 0) {
          api.writeln({ text, color: 'YELLOW' });
        } else {
          api.writeln(text.trim());
        }
      });
    });
  };

  return (
    <div>
      <XtermTerminal
        colorRules={[{ pattern: /error.*$/gi, color: 'RED' }]}
        lineTitle=""
        preLog=""
        disableStdin
        height={300}
        onLoad={loaded}
      />
    </div>
  );
};

export default XtermLogger;

终端操作

import React, { useState } from 'react';
import XtermTerminal from '@rasir/xterm-terminal';

const Terminal = () => {
  const [term, setTerm] = useState();
  const [api, setApi] = useState();

  const loaded = (term, api) => {
    setTerm(term);
    setApi(api);
  };

  const onCommand = (cmd) => {
    api.writeln(`hello Rasir!!!`);
    api.writeln('');
  };

  return (
    <div>
      <XtermTerminal
        colorRules={[{ pattern: /error.*$/gi, color: 'RED' }]}
        lineTitle="$: "
        preLog="### START ###"
        height={300}
        onLoad={loaded}
        onCommand={onCommand}
      />
    </div>
  );
};

export default Terminal;

API

属性说明类型默认值
className容器类名string---
height容器高度,height变化会引起 xterm 自适应number---
style容器高度样式React.CSSProperties---
disableStdin是否禁用输入boolean---
lineTitle终端输出的每一行开头的字符,不计入输入字符string$:
preLog终端预置日志string\r\n*** COMMAND START ***\r\n
command终端执行命令,最好与disableStdin一起使用string---
onCommand在终端输入回车时会调用回调(command?: string) => void---
onClear输入clear会触发回调() => void---
onCopyctrl+c/cmd+c 复制回调(text: string) => Promise<void>---
onPastectrl+v/cmd+v 粘贴回调() => Promise<string>---
onLoad终端初始化完成时的回调(term: XTerminal, api: TerminalApi) => void---
colorRules文字显示颜色的规则ColorRule[]---
optionsxterm原生配置,初始化时使用,初始化之后,如果要改变配置,可以使用xterm原来的setOption方法ITerminalOptions---

TerminalApi

interface TerminalApi {
    clearLog: () => void;
    writeln: (text?: string) => void;
    write: (text?: string) => void;
    getCommand: () => string;
    resize: () => void;
}

ColorRule

type ColorRule = {
    pattern: RegExp;
    color: Color;
}

Color

Color = "BLACK" | "RED" | "GREEN" | "YELLOW" | "BLUE" | "MAGENTA" | "CYAN" | "WHITE" | "RESET"