Skip to content

데이터베이스 연결 및 데이터 검색

Kotlin Notebook은 MariaDB, PostgreSQL, MySQL, SQLite와 같은 다양한 유형의 SQL 데이터베이스에 연결하고 데이터를 검색하는 기능을 제공합니다. Kotlin DataFrame 라이브러리를 활용하여 Kotlin Notebook은 데이터베이스에 연결하고, SQL 쿼리를 실행하며, 추가 작업을 위해 결과를 가져올 수 있습니다.

자세한 예시는 KotlinDataFrame SQL Examples GitHub 저장소의 Notebook을 참조하세요.

시작하기 전에

Kotlin Notebook은 Kotlin Notebook 플러그인에 의존하며, 이 플러그인은 IntelliJ IDEA에 기본적으로 번들되어 활성화되어 있습니다.

Kotlin Notebook 기능이 사용 가능하지 않다면, 플러그인이 활성화되어 있는지 확인하세요. 자세한 내용은 환경 설정을 참조하세요.

새 Kotlin Notebook 생성:

  1. File | New | Kotlin Notebook를 선택합니다.
  2. MariaDB 또는 MySQL과 같은 SQL 데이터베이스에 접근할 수 있는지 확인하세요.

데이터베이스 연결

SQL 데이터베이스에 연결하고 상호 작용하려면 Kotlin DataFrame 라이브러리의 특정 함수를 사용할 수 있습니다. DatabaseConfiguration을 사용하여 데이터베이스에 연결하고 getSchemaForAllSqlTables()를 사용하여 데이터베이스 내 모든 테이블의 스키마를 검색할 수 있습니다.

예시를 살펴보겠습니다.

  1. Kotlin Notebook 파일(.ipynb)을 엽니다.

  2. JDBC (Java Database Connectivity) 드라이버에 대한 종속성을 추가하고 JDBC 드라이버 버전을 지정합니다. 이 예시에서는 MariaDB를 사용합니다:

    kotlin
    USE {
       dependencies("org.mariadb.jdbc:mariadb-java-client:$version")
    }
  3. 데이터 조작 작업에 필수적인 Kotlin DataFrame 라이브러리와 SQL 연결 및 유틸리티 함수에 필요한 Java 라이브러리를 임포트합니다:

    kotlin
    %use dataframe
    import java.sql.DriverManager
    import java.util.*
  4. DatabaseConfiguration 클래스를 사용하여 URL, 사용자 이름, 비밀번호를 포함한 데이터베이스 연결 매개변수를 정의합니다:

    kotlin
    val URL = "YOUR_URL"
    val USER_NAME = "YOUR_USERNAME"
    val PASSWORD = "YOUR_PASSWORD"
    
    val dbConfig = DatabaseConfiguration(URL, USER_NAME, PASSWORD)
  5. 연결되면 getSchemaForAllSqlTables() 함수를 사용하여 데이터베이스 내 각 테이블의 스키마 정보를 가져와 표시합니다:

    kotlin
    val dataschemas = DataFrame.getSchemaForAllSqlTables(dbConfig)
    
    dataschemas.forEach { 
        println("---Yet another table schema---")
        println(it)
        println()
    }

    SQL 데이터베이스 연결에 대한 자세한 내용은 Kotlin DataFrame 문서의 SQL 데이터베이스에서 읽기를 참조하세요.

데이터 검색 및 조작

SQL 데이터베이스 연결을 설정한 후, Kotlin DataFrame 라이브러리를 활용하여 Kotlin Notebook에서 데이터를 검색하고 조작할 수 있습니다. readSqlTable() 함수를 사용하여 데이터를 검색할 수 있습니다. 데이터를 조작하려면 filter, groupBy, convert와 같은 메서드를 사용할 수 있습니다.

IMDB 데이터베이스에 연결하고 Quentin Tarantino 감독이 연출한 영화에 대한 데이터를 검색하는 예시를 살펴보겠습니다:

  1. readSqlTable() 함수를 사용하여 "movies" 테이블에서 데이터를 검색하고, 효율성을 위해 쿼리를 처음 100개 레코드로 제한하도록 limit을 설정합니다:

    kotlin
    val dfs = DataFrame.readSqlTable(dbConfig, tableName = "movies", limit = 100)
  2. SQL 쿼리를 사용하여 Quentin Tarantino 감독이 연출한 영화와 관련된 특정 데이터셋을 검색합니다. 이 쿼리는 영화 세부 정보를 선택하고 각 영화의 장르를 결합합니다:

    kotlin
    val props = Properties()
    props.setProperty("user", USER_NAME)
    props.setProperty("password", PASSWORD)
    
    val TARANTINO_FILMS_SQL_QUERY = """
        SELECT name, year, rank, GROUP_CONCAT(genre) as "genres"
        FROM movies JOIN movies_directors ON movie_id = movies.id
        JOIN directors ON directors.id=director_id LEFT JOIN movies_genres ON movies.id = movies_genres.movie_id
        WHERE directors.first_name = "Quentin" AND directors.last_name = "Tarantino"
        GROUP BY name, year, rank
        ORDER BY year
        """
    
    // Retrieves a list of Quentin Tarantino's movies, including their name, year, rank, and a concatenated string of all genres. 
    // The results are grouped by name, year, rank, and sorted by year.
    
    var dfTarantinoMovies: DataFrame<*>
    
    DriverManager.getConnection(URL, props).use { connection ->
       connection.createStatement().use { st ->
          st.executeQuery(TARANTINO_FILMS_SQL_QUERY).use { rs ->
             val dfTarantinoFilmsSchema = DataFrame.getSchemaForResultSet(rs, connection)
             dfTarantinoFilmsSchema.print()
    
             dfTarantinoMovies = DataFrame.readResultSet(rs, connection)
             dfTarantinoMovies
          }
       }
    }
  3. Tarantino 영화 데이터셋을 가져온 후, 데이터를 추가로 조작하고 필터링할 수 있습니다.

    kotlin
    val df = dfTarantinoMovies
        // Replaces any missing values in the 'year' column with 0.
        .fillNA { year }.with { 0 }
        
        // Converts the 'year' column to integers.
        .convert { year }.toInt()
    
        // Filters the data to include only movies released after the year 2000.
        .filter { year > 2000 }
    df

결과 출력은 fillNA 메서드를 사용하여 year 열의 누락된 값을 0으로 대체한 DataFrame입니다. year 열은 convert 메서드로 정수 값으로 변환되고, filter 메서드를 사용하여 2000년 이후의 행만 포함하도록 데이터가 필터링됩니다.

Kotlin Notebook에서 데이터 분석

SQL 데이터베이스 연결을 설정한 후, Kotlin DataFrame 라이브러리를 활용하여 Kotlin Notebook에서 심층적인 데이터 분석을 수행할 수 있습니다. 여기에는 데이터 그룹화, 정렬 및 집계 기능이 포함되어 데이터 내의 패턴을 발견하고 이해하는 데 도움이 됩니다.

영화 데이터베이스에서 배우 데이터를 분석하는 예시를 살펴보겠습니다. 이 예시는 배우의 가장 자주 나타나는 이름에 초점을 맞춥니다:

  1. readSqlTable() 함수를 사용하여 "actors" 테이블에서 데이터를 추출합니다:

    kotlin
    val actorDf = DataFrame.readSqlTable(dbConfig, "actors", 10000)
  2. 검색된 데이터를 처리하여 가장 일반적인 배우의 이름 상위 20개를 식별합니다. 이 분석에는 여러 DataFrame 메서드가 포함됩니다:

    kotlin
    val top20ActorNames = actorDf
        // Groups the data by the first_name column to organize it based on actor first names.
       .groupBy { first_name }
    
        // Counts the occurrences of each unique first name, providing a frequency distribution.
       .count()
    
        // Sorts the results in descending order of count to identify the most common names.
       .sortByDesc("count")
    
        // Selects the top 20 most frequent names for analysis.
       .take(20)
    top20ActorNames

다음 단계