Class ClickHouseUtils

java.lang.Object
com.clickhouse.data.ClickHouseUtils

public final class ClickHouseUtils extends Object
  • Field Details

  • Method Details

    • applyVariables

      public static String applyVariables(String template, UnaryOperator<String> applyFunc)
    • applyVariables

      public static String applyVariables(String template, Map<String,String> variables)
    • createTempFile

      public static File createTempFile() throws IOException
      Creates a temporary file. Same as createTempFile(null, null, true).
      Returns:
      non-null temporary file
      Throws:
      IOException - when failed to create the temporary file
    • createTempFile

      public static File createTempFile(String prefix, String suffix) throws IOException
      Creates a temporary file with given prefix and suffix. Same as createTempFile(prefix, suffix, true).
      Parameters:
      prefix - prefix, could be null
      suffix - suffix, could be null
      Returns:
      non-null temporary file
      Throws:
      IOException - when failed to create the temporary file
    • createTempFile

      public static File createTempFile(String prefix, String suffix, boolean deleteOnExit) throws IOException
      Creates a temporary file with the given prefix and suffix. The file has only read and write access granted to the owner.
      Parameters:
      prefix - prefix, null or empty string is taken as "ch"
      suffix - suffix, null or empty string is taken as ".data"
      deleteOnExit - whether the file be deleted on exit
      Returns:
      non-null temporary file
      Throws:
      IOException - when failed to create the temporary file
    • decode

      public static String decode(String encodedString)
      Decode given string using URLDecoder and StandardCharsets.UTF_8.
      Parameters:
      encodedString - encoded string
      Returns:
      non-null decoded string
    • encode

      public static String encode(String str)
      Encode given string using URLEncoder and StandardCharsets.UTF_8.
      Parameters:
      str - string to encode
      Returns:
      non-null encoded string
    • extractParameters

      public static Map<String,String> extractParameters(String query, Map<String,String> params)
      Extracts parameters, usually key-value pairs, from the given query string.
      Parameters:
      query - non-empty query string
      params - mutable map for extracted parameters, a new HashMap will be created when it's null
      Returns:
      map with extracted parameters, usually same as params
    • getFile

      public static Path getFile(String file)
      Gets absolute and normalized path to the given file.
      Parameters:
      file - non-empty file
      Returns:
      non-null absolute and normalized path to the file
    • findFiles

      public static List<Path> findFiles(String pattern, String... paths) throws IOException
      Finds files according to the given pattern and path.
      Parameters:
      pattern - non-empty pattern may or may have syntax prefix as in FileSystem.getPathMatcher(String), defaults to glob syntax
      paths - path to search, defaults to current work directory
      Returns:
      non-null list of normalized absolute paths matching the pattern
      Throws:
      IOException - when failed to find files
    • toJavaByteArrayExpression

      public static String toJavaByteArrayExpression(byte[] bytes)
    • indexOf

      public static int indexOf(byte[] bytes, byte[] search)
    • newThreadPool

      public static ExecutorService newThreadPool(Object owner, int maxThreads, int maxRequests)
    • newThreadPool

      public static ExecutorService newThreadPool(Object owner, int coreThreads, int maxThreads, int maxRequests, long keepAliveTimeoutMs, boolean allowCoreThreadTimeout)
    • isCloseBracket

      public static boolean isCloseBracket(char ch)
    • isOpenBracket

      public static boolean isOpenBracket(char ch)
    • isQuote

      public static boolean isQuote(char ch)
    • isSeparator

      public static boolean isSeparator(char ch)
    • escape

      public static String escape(String str, char quote)
      Escape quotes in given string.
      Parameters:
      str - string
      quote - quote to escape
      Returns:
      escaped string
    • unescape

      public static String unescape(String str)
      Unescape quoted string.
      Parameters:
      str - quoted string
      Returns:
      unescaped string
    • format

      public static String format(String template, Object... args)
      Wrapper of String.format(Locale.ROOT, ...).
      Parameters:
      template - string to format
      args - arguments used in substitution
      Returns:
      formatted string
    • normalizeDirectory

      public static String normalizeDirectory(String dir)
      Normalizes given directory by appending back slash if it does exist.
      Parameters:
      dir - original directory
      Returns:
      normalized directory
    • parseJson

      public static Object parseJson(String json)
      Simple and un-protected JSON parser.
      Parameters:
      json - non-empty JSON string
      Returns:
      object array, Boolean, Number, null, String, or Map
      Throws:
      IllegalArgumentException - when JSON string is null or empty
    • getCloseBracket

      public static char getCloseBracket(char openBracket)
    • getService

      public static <T> T getService(Class<? extends T> serviceInterface)
    • getService

      public static <T> T getService(Class<? extends T> serviceInterface, T defaultService)
      Load service according to given interface using ServiceLoader, fallback to given default service or supplier function if not found.
      Type Parameters:
      T - type of service
      Parameters:
      serviceInterface - non-null service interface
      defaultService - optionally default service
      Returns:
      non-null service
    • getService

      public static <T> T getService(Class<? extends T> serviceInterface, Supplier<T> supplier)
    • getFileInputStream

      public static InputStream getFileInputStream(String file) throws FileNotFoundException
      Search file in current directory, home directory, and then classpath, Get input stream to read the given file.
      Parameters:
      file - path to the file
      Returns:
      input stream
      Throws:
      FileNotFoundException - when the file does not exists
    • getFileOutputStream

      public static OutputStream getFileOutputStream(String file) throws IOException
      Get output stream for writing a file. Directories and file will be created if they do not exist.
      Parameters:
      file - path to the file
      Returns:
      output stream
      Throws:
      IOException - when failed to create directories and/or file
    • getLeadingComment

      public static String getLeadingComment(String sql)
    • getProperty

      public static String getProperty(String key, Properties... props)
    • getProperty

      public static String getProperty(String key, String defaultValue, Properties... props)
    • skipBrackets

      public static int skipBrackets(String args, int startIndex, int len, char bracket)
      Skip brackets and content inside with consideration of nested brackets, quoted string and comments.
      Parameters:
      args - non-null string to scan
      startIndex - start index, optionally index of the opening bracket
      len - end index, usually length of the given string
      bracket - opening bracket supported by isOpenBracket(char)
      Returns:
      index next to matched closing bracket
      Throws:
      IllegalArgumentException - when missing closing bracket(s)
    • skipQuotedString

      public static int skipQuotedString(String args, int startIndex, int len, char quote)
      Skip quoted string.
      Parameters:
      args - non-null string to scan
      startIndex - start index, optionally start of the quoted string
      len - end index, usually length of the given string
      quote - quote supported by isQuote(char)
      Returns:
      index next to the other matched quote
      Throws:
      IllegalArgumentException - when missing quote
    • skipSingleLineComment

      public static int skipSingleLineComment(String args, int startIndex, int len)
      Skip single line comment.
      Parameters:
      args - non-null string to scan
      startIndex - start index, optionally start of the single line comment
      len - end index, usually length of the given string
      Returns:
      index of start of next line, right after \n
    • skipMultiLineComment

      public static int skipMultiLineComment(String args, int startIndex, int len)
      Skip nested multi-line comment.
      Parameters:
      args - non-null string to scan
      startIndex - start index, optionally start of the multi-line comment
      len - end index, usually length of the given string
      Returns:
      index next to end of the outter most multi-line comment
      Throws:
      IllegalArgumentException - when multi-line comment is unclosed
    • skipContentsUntil

      public static int skipContentsUntil(String args, int startIndex, int len, char... endChars)
      Skip quoted string, comments, and brackets until seeing one of endChars or reaching end of the given string.
      Parameters:
      args - non-null string to scan
      startIndex - start index
      len - end index, usually length of the given string
      endChars - skip characters until seeing one of the specified characters or reaching end of the string; '\0' is used when it's null or empty
      Returns:
      index of endChar or len
    • skipContentsUntil

      public static int skipContentsUntil(String args, int startIndex, int len, String keyword, boolean caseSensitive)
      Skip quoted string, comments, and brackets until seeing the keyword or reaching end of the given string.
      Parameters:
      args - non-null string to scan
      startIndex - start index
      len - end index, usually length of the given string
      keyword - keyword, null or empty string means any character
      caseSensitive - whether keyword is case sensitive or not
      Returns:
      index of endChar or len
    • skipContentsUntil

      public static int skipContentsUntil(String args, int startIndex, int len, String[] keywords, boolean caseSensitive)
      Skip quoted string, comments, and brackets until seeing all the given keywords(with only whitespaces or comments in between) or reaching end of the given string.
      Parameters:
      args - non-null string to scan
      startIndex - start index
      len - end index, usually length of the given string
      keywords - keywords, null or empty one means any character
      caseSensitive - whether keyword is case sensitive or not
      Returns:
      index of endChar or len
    • readNameOrQuotedString

      public static int readNameOrQuotedString(String args, int startIndex, int len, StringBuilder builder)
    • readEnumValues

      public static int readEnumValues(String args, int startIndex, int len, Map<String,Integer> values)
    • readValueArray

      public static List<String> readValueArray(String args, int startIndex, int len)
    • readValueArray

      public static int readValueArray(String args, int startIndex, int len, Consumer<String> func)
    • readParameters

      public static int readParameters(String args, int startIndex, int len, List<String> params)
    • waitFor

      public static boolean waitFor(AtomicBoolean flag, long timeout, TimeUnit unit) throws InterruptedException
      Waits until the flag turns to true or timed out.
      Parameters:
      flag - non-null boolean flag to check
      timeout - timeout, negative or zero means forever
      unit - non-null time unit
      Returns:
      true if the flag turns to true within given timeout; false otherwise
      Throws:
      InterruptedException - when thread was interrupted