public HiveStreamingConnection connect() throws StreamingException {
      if (database == null) {
        throw new StreamingException("Database cannot be null for streaming connection");
      }
      if (table == null) {
        if (tableObject == null) {
          throw new StreamingException("Table and table object cannot be "
              + "null for streaming connection");
        } else {
          table = tableObject.getTableName();
        }
      }

      if (tableObject != null && !tableObject.getTableName().equals(table)) {
        throw new StreamingException("Table must match tableObject table name");
      }

      if (recordWriter == null) {
        throw new StreamingException("Record writer cannot be null for streaming connection");
      }
      if ((writeId != -1 && tableObject == null) ||
          (writeId == -1 && tableObject != null)){
        throw new StreamingException("If writeId is set, tableObject "
            + "must be set as well and vice versa");
      }

      HiveStreamingConnection streamingConnection = new HiveStreamingConnection(this);
      streamingConnection.onShutdownRunner = streamingConnection::close;
      // assigning higher priority than FileSystem shutdown hook so that streaming connection gets closed first before
      // filesystem close (to avoid ClosedChannelException)
      ShutdownHookManager.addShutdownHook(streamingConnection.onShutdownRunner,  FileSystem.SHUTDOWN_HOOK_PRIORITY + 1);
      Thread.setDefaultUncaughtExceptionHandler((t, e) -> streamingConnection.close());
      return streamingConnection;
    }
Returning a streaming connection to hive.

@return - hive streaming connection
streaming/src/java/org/apache/hive/streaming/HiveStreamingConnection.java