SimpleExampleServlet 클래스는 examples 패키지에 있습니다. 이 클래스는 데이터 소스의 가장 간단한 구현 예를 제공합니다. SimpleExampleServlet는 DataSourceServlet에서 상속되고 generateDataTable()를 구현하며 서블릿 컨테이너 내에서 실행되어야 합니다.
SimpleExampleServlet의 스니펫은 아래에 제공됩니다. generateDataTable 함수는 데이터를 라이브러리에 노출합니다. 이 함수는 데이터 테이블 설명을 만들고, 데이터 테이블 열을 정의하고, 데이터 테이블을 데이터로 채웁니다.
라이브러리는 데이터 테이블을 쿼리 시각화로 반환하는 데 필요한 다른 모든 작업을 처리합니다.
// This example extends DataSourceServlet
public class SimpleExampleServlet extends DataSourceServlet {
@Override
public DataTable generateDataTable(Query query, HttpServletRequest request) {
// Create a data table,
DataTable data = new DataTable();
ArrayList cd = new ArrayList();
cd.add(new ColumnDescription("name", ValueType.TEXT, "Animal name"));
cd.add(new ColumnDescription("link", ValueType.TEXT, "Link to wikipedia"));
cd.add(new ColumnDescription("population", ValueType.NUMBER, "Population size"));
cd.add(new ColumnDescription("vegeterian", ValueType.BOOLEAN, "Vegetarian?"));
data.addColumns(cd);
// Fill the data table.
try {
data.addRowFromValues("Aye-aye", "http://en.wikipedia.org/wiki/Aye-aye", 100, true);
data.addRowFromValues("Sloth", "http://en.wikipedia.org/wiki/Sloth", 300, true);
data.addRowFromValues("Leopard", "http://en.wikipedia.org/wiki/Leopard", 50, false);
data.addRowFromValues("Tiger", "http://en.wikipedia.org/wiki/Tiger", 80, false);
} catch (TypeMismatchException e) {
System.out.println("Invalid type!");
}
return data;
}
}
SimpleExampleServlet 실행 및 테스트
이 섹션에서는 SimpleExampleServlet를 실행하고 테스트하는 방법을 안내합니다.
아직 설치하지 않았다면 설치 섹션에서 기본 요건과 라이브러리 다운로드 및 빌드 방법에 관한 안내를 참조하세요. Apache Tomcat과 같이 서블릿 컨테이너로도 작동하는 웹 서버가 시스템에 아직 없는 경우 이를 설치해야 합니다. 이 섹션의 안내는 Windows 시스템의 Apache Tomcat에 한정됩니다.
SimpleExampleServlet를 실행하고 테스트하려면 SimpleExampleServlet 데이터 소스를 실행하는 웹 애플리케이션을 만든 다음 데이터 소스에서 쿼리된 데이터를 보여주는 시각화가 포함된 웹페이지 예시를 실행합니다. 이 내용은 다음 섹션에서 설명합니다.
아래 안내에 따라 또는 조정하여 Apache Tomcat에서 웹 애플리케이션을 만듭니다. 다음 안내는 Windows 시스템의 Apache Tomcat에만 적용됩니다.
Tomcat을 설치한 디렉터리로 이동합니다. 이 문서에서는 <tomcat_home>로 작성됩니다.
webapps 하위 디렉터리로 이동합니다.
myWebApp라는 하위 디렉터리를 만듭니다.
방금 만든 하위 디렉터리로 변경하고 WEB-INF이라는 다른 하위 디렉터리를 만듭니다.
WEB-INF 하위 디렉터리로 변경하고 lib라는 다른 하위 디렉터리를 만듭니다. 전체 경로는 <tomcat_home>/webapps/myWebApp/WEB-INF/lib이어야 합니다.
web.xml를 <data_source_library_install>/examples/src/html에서 WEB-INF 디렉터리로 복사합니다. 여기서 <data_source_library_install>는 데이터 소스 라이브러리를 설치한 디렉터리입니다.
web.xml의 다음 줄은 SimpleExampleServlet를 정의하고 매핑합니다.
데이터 소스 라이브러리가 설치된 디렉터리로 이동합니다.
이 문서에서는 <data_source_library_install>로 작성됩니다.
모든 종속 항목 패키지를 <tomcat_home>/webapps/myWebApp/WEB-INF/lib에 복사합니다. 패키지는 다른 디렉터리에 두지 않는 한 <data_source_library_install>/lib에 설치됩니다.
라이브러리를 직접 빌드한 경우 visualization-datasource-1.0.2.jar 및 visualization-datasource-examples.jar 를 <data_source_library_install>/build 에서 <tomcat_home>/webapps/myWebApp/WEB-INF/lib으로 복사합니다.
ZIP 파일의 압축을 푼 경우 visualization-datasource-1.0.2.jar 및 visualization-datasource-examples.jar 를 <data_source_library_install> 에서 <tomcat_home>/webapps/myWebApp/WEB-INF/lib로 복사합니다. jar 파일 이름의 버전 번호는 최신 버전 번호에 따라 다를 수 있습니다.
화면 너비에 따라 화면에 6~7줄의 텍스트가 표시됩니다.
텍스트는 google.visualization.Query.setResponse 로 시작하고 /Tiger'},{v:80.0},{v:false}]}]}});로 끝납니다.
이 데이터는 데이터 소스에서 쿼리 시각화에 반환하는 데이터입니다.
시각화를 사용하여 데이터 보기
<data_source_library_install>/examples/src/html 디렉터리의 getting_started.html 파일은 데이터의 시각화를 확인하는 데 사용할 수 있습니다. getting_started.html에서 가져온 다음 줄은 사용할 서블릿을 지정합니다. 서블릿 매핑은 Apache Tomcat에서 웹 애플리케이션 만들기의 8단계에서 설정되었습니다.
var query = new google.visualization.Query('simpleexample');
시각화를 지정하고 쿼리 언어를 사용하는 방법에 대한 자세한 내용은 차트 사용 및 쿼리 언어 참조를 확인하세요.
데이터 소스에서 제공하는 데이터의 시각화를 보려면 아래의 안내를 따르거나 수정하세요.
getting_started.html 파일을 <data_source_library_install>/examples/src/html 디렉터리 에서 <tomcat_home>/webapps/myWebApp/ 디렉터리로 복사합니다.
[[["이해하기 쉬움","easyToUnderstand","thumb-up"],["문제가 해결됨","solvedMyProblem","thumb-up"],["기타","otherUp","thumb-up"]],[["필요한 정보가 없음","missingTheInformationINeed","thumb-down"],["너무 복잡함/단계 수가 너무 많음","tooComplicatedTooManySteps","thumb-down"],["오래됨","outOfDate","thumb-down"],["번역 문제","translationIssue","thumb-down"],["샘플/코드 문제","samplesCodeIssue","thumb-down"],["기타","otherDown","thumb-down"]],["최종 업데이트: 2024-07-10(UTC)"],[[["\u003cp\u003e\u003ccode\u003eSimpleExampleServlet\u003c/code\u003e provides a basic example of a data source implementation within the library, inheriting from \u003ccode\u003eDataSourceServlet\u003c/code\u003e and requiring a servlet container to run.\u003c/p\u003e\n"],["\u003cp\u003eRunning and testing \u003ccode\u003eSimpleExampleServlet\u003c/code\u003e involves setting up a web application, like on Apache Tomcat, to host the servlet and using a visualization (like the provided \u003ccode\u003egetting_started.html\u003c/code\u003e) to display the queried data.\u003c/p\u003e\n"],["\u003cp\u003eThe setup process includes creating a web application directory structure, configuring \u003ccode\u003eweb.xml\u003c/code\u003e to define the servlet, deploying necessary library files, and accessing the servlet and visualization through specific URLs.\u003c/p\u003e\n"]]],[],null,["# Getting Started with Data Sources\n\nThis section introduces `SimpleExampleServlet`, which is\nthe simplest example implementation of a data source that is provided\nwith the library. This section also provides step-by-step instructions\non how to run and test `SimpleExampleServlet`.\n\n- [Introducing `SimpleExampleServlet`](#introducing)\n- [Running and testing `SimpleExampleServlet`](#running)\n\nIntroducing `SimpleExampleServlet`\n----------------------------------\n\nThe `SimpleExampleServlet` class is located in the `examples`\npackage. This class provides an example of the simplest implementation\nof a data source. `SimpleExampleServlet` inherits from `DataSourceServlet`,\nimplements `generateDataTable()`, and must be run\nwithin a servlet container.\n\nA snippet of `SimpleExampleServlet` is provided below. The `generateDataTable` function\nexposes data to the library. This function creates a data table description,\ndefines the data table columns, and populates the data table with data.\nThe library handles all other actions required to return the data table\nto the querying visualization. \n\n```gdscript\n// This example extends DataSourceServlet\npublic class SimpleExampleServlet extends DataSourceServlet {\n\n @Override\n public DataTable generateDataTable(Query query, HttpServletRequest request) {\n // Create a data table,\n DataTable data = new DataTable();\n ArrayList cd = new ArrayList();\n cd.add(new ColumnDescription(\"name\", ValueType.TEXT, \"Animal name\"));\n cd.add(new ColumnDescription(\"link\", ValueType.TEXT, \"Link to wikipedia\"));\n cd.add(new ColumnDescription(\"population\", ValueType.NUMBER, \"Population size\"));\n cd.add(new ColumnDescription(\"vegeterian\", ValueType.BOOLEAN, \"Vegetarian?\"));\n\n data.addColumns(cd);\n\n // Fill the data table.\n try {\n data.addRowFromValues(\"Aye-aye\", \"http://en.wikipedia.org/wiki/Aye-aye\", 100, true);\n data.addRowFromValues(\"Sloth\", \"http://en.wikipedia.org/wiki/Sloth\", 300, true);\n data.addRowFromValues(\"Leopard\", \"http://en.wikipedia.org/wiki/Leopard\", 50, false);\n data.addRowFromValues(\"Tiger\", \"http://en.wikipedia.org/wiki/Tiger\", 80, false);\n } catch (TypeMismatchException e) {\n System.out.println(\"Invalid type!\");\n }\n return data;\n }\n}\n```\n\nRunning and testing `SimpleExampleServlet`\n------------------------------------------\n\nThis section provides instructions on how to run and test `SimpleExampleServlet`.\n\nIf you haven't already done so, see the [Installation](/chart/interactive/docs/dev/dsl_install) section\nfor information about prerequisites, and instructions on how to download\nand build the library. Make sure you install a web server that also functions\nas a servlet container, such as Apache Tomcat, if you do not already have\none on your system. The instructions in this section are specific to Apache\nTomcat on a Windows system.\n\nTo run and test `SimpleExampleServlet`, create a web application\nthat\nruns the `SimpleExampleServlet` data source,\nthen run an example web\npage with a visualization showing data queried from the data\nsource. This is described in the following sections:\n\n- [Creating a Web Application on Apache Tomcat](#webapp)\n- [Using a Visualization to View the Data](#visualization)\n\n### Creating a Web Application on Apache Tomcat\n\nFollow or adapt the instructions below to create a web application\non Apache Tomcat. These instructions are specific to Apache Tomcat on a\nWindows system:\n\n1. Navigate to the directory in which you installed Tomcat. This is written in this document as `\u003ctomcat_home\u003e`. \n2. Navigate to the `webapps` subdirectory. \n3. Create a subdirectory called `myWebApp`. \n4. Change to the subdirectory you have just created and create another subdirectory called `WEB-INF`. \n5. Change to the `WEB-INF` subdirectory and create another subdirectory called `lib`. \n The full path should be `\u003ctomcat_home\u003e/webapps/myWebApp/WEB-INF/lib`. \n6. Copy `web.xml` from `\u003cdata_source_library_install\u003e/examples/src/html` to the `WEB-INF` directory. Where `\u003cdata_source_library_install\u003e` is the directory in which you installed the data source library. The following lines in `web.xml` define and map `SimpleExampleServlet`: \n\n ```carbon\n \u003cservlet\u003e\n \u003cservlet-name\u003eMy Servlet\u003c/servlet-name\u003e\n \u003cdescription\u003eMy servlet description.\u003c/description\u003e\n \u003cservlet-class\u003eSimpleExampleServlet\u003c/servlet-class\u003e\n \u003c/servlet\u003e\n\n \u003cservlet-mapping\u003e\n \u003cservlet-name\u003eMy Servlet\u003c/servlet-name\u003e\n \u003curl-pattern\u003e/simpleexample\u003c/url-pattern\u003e\n \u003c/servlet-mapping\u003e\n ```\n7. Navigate to the directory in which you installed the data source library. This is written in this document as `\u003cdata_source_library_install\u003e`. \n8. Copy all the dependency packages to `\u003ctomcat_home\u003e/webapps/myWebApp/WEB-INF/lib`. The packages are installed in `\u003cdata_source_library_install\u003e/lib`, unless you put them in a different directory. \n9. If you have built the library yourself, copy `visualization-datasource-1.0.2.jar` and `visualization-datasource-examples.jar` \n from `\u003cdata_source_library_install\u003e/build` \n to `\u003ctomcat_home\u003e/webapps/myWebApp/WEB-INF/lib`. \n\n If you have unzipped the zip file, copy `visualization-datasource-1.0.2.jar` and `visualization-datasource-examples.jar` \n from `\u003cdata_source_library_install\u003e` \n to `\u003ctomcat_home\u003e/webapps/myWebApp/WEB-INF/lib`. \n Note that the version number in the jar file name may vary depending on the latest version number. \n10. Start Tomcat, or restart Tomcat if it is already running. \n11. Click the following link: \n\n \u003chttp://localhost:8080/myWebApp/simpleexample\u003e \n\n The screen displays 6-7 lines of text, depending on your screen width. \n The text begins with `google.visualization.Query.setResponse ` \n and ends with `/Tiger'},{v:80.0},{v:false}]}]}});` \n\n This is the data that is returned by your data source to a querying visualization.\n\n### Using a Visualization\nto View the Data\n\nThe `getting_started.html` file in the `\u003cdata_source_library_install\u003e/examples/src/html` directory\ncan be used to view a visualization of the data. The following line, taken\nfrom `getting_started.html`, specifies\nthe servlet to use. The servlet mapping was set up in step 8 of [Creating\na Web Application on Apache Tomcat](#webapp). \n\n```gdscript\nvar query = new google.visualization.Query('simpleexample');\n```\n\nFor more information on how to specify a visualization and use the\nquery language, see [Using\nCharts](/chart/interactive/docs) and the [Query Language\nReference](/chart/interactive/docs/querylanguage).\n\nFollow, or adapt, the instructions below to view a visualization\nof the data provided by the data source:\n\n1. Copy the `getting_started.html` file from the `\u003cdata_source_library_install\u003e/examples/src/html` directory \n to the `\u003ctomcat_home\u003e/webapps/myWebApp/` directory. \n2. Click the following link \u003chttp://localhost:8080/myWebApp/getting_started.html\u003e, you should see the following: \n\n \u003cbr /\u003e\n\n \u003cbr /\u003e\n\n That's it! You have set up your first data source.\n\nNext Steps\n----------\n\nThe next example is described in the [Using an\nExternal Data Store](/chart/interactive/docs/dev/dsl_csv) section. Alternatively\nyou can return to the [Introduction](/chart/interactive/docs/dev/dsl_intro), or\nexplore the following links:\n\n- For an introduction to the library's most commonly used classes, see [Key Classes](/chart/interactive/docs/dev/dsl_packages#keyclasses).\n- For an example of how to implement your own flow of events, and query capabilities, see [Defining Capabilities and the Flow of Events](/chart/interactive/docs/dev/dsl_httpservlet).\n- If you do not want to inherit from `DataSourceServlet`, you can implement a data source as described in [Using\n Your Own Servlet](/chart/interactive/docs/dev/dsl_key_concepts#servlet). For example you might not want to inherit from `DataSourceServlet` if you inherit a servlet from another class.\n- If you do not want to use a servlet, see [Implementing a Non-servlet Data Source](/chart/interactive/docs/dev/dsl_key_concepts#nonservlet).\n- To learn about how to pass parameters from an application to a data source, see [Passing Parameters to `DataTableGenerator.generateDataTable`](/chart/interactive/docs/dev/dsl_key_concepts#params)."]]