การใช้ที่เก็บข้อมูลภายนอก

ส่วนนี้จะ แนะนํา CsvDataSourceServlet CsvDataSourceServlet เป็นตัวอย่างการใช้ไฟล์ CSV เป็นที่เก็บข้อมูลภายนอก ส่วนนี้ยังให้คําแนะนําแบบทีละขั้นตอนเกี่ยวกับวิธีเรียกใช้และทดสอบ CsvDataSourceServlet

หมายเหตุ: คุณควรกรอกข้อมูลในส่วนการเริ่มต้นใช้งานแหล่งข้อมูลให้เสร็จสมบูรณ์ก่อนเริ่มส่วนนี้

ขอแนะนํา CsvDataSourceServlet

คลาส CsvDataSourceServlet อยู่ในแพ็กเกจ examples คลาสนี้มีตัวอย่างการใช้งานที่ใช้ไฟล์ CSV เป็นที่เก็บข้อมูลภายนอก CsvDataSourceServlet ได้รับค่าจาก DataSourceServlet, นํา generateDataTable() มาใช้ และต้องเรียกใช้ภายในคอนเทนเนอร์ของเซิร์ฟเล็ต

ตัวอย่างข้อมูลของ CsvDataSourceServlet มีดังนี้ ฟังก์ชัน generateDataTable จะส่งข้อมูลไปยังไลบรารี ฟังก์ชันนี้จะสร้างคําอธิบายตารางข้อมูล กําหนดคอลัมน์ของตารางข้อมูล และป้อนข้อมูลในตารางที่ได้รับจาก ไฟล์ CSV ไฟล์ CSV จะอ่านจาก URL ที่ระบุในคําขอการแสดงภาพ##39 ไลบรารีจะจัดการการดําเนินการอื่นๆ ทั้งหมดที่จําเป็นในการแสดงผลตารางข้อมูลไปยังการแสดงภาพการค้นหา

/**
 * A demo servlet for serving a simple, constant data table.
 * This servlet extends DataSourceServlet, but does not override the default
 * empty implementation of method getCapabilities(). This servlet therefore ignores the
 * user query (as passed in the 'tq' url parameter), leaving the
 * query engine to apply it to the data table created here.
 *
 * @author Nimrod T.
 */
public class CsvDataSourceServlet extends DataSourceServlet {

  /**
   * Log.
   */
  private static final Log log = LogFactory.getLog(CsvDataSourceServlet.class.getName());

  /**
   * The name of the parameter that contains the url of the CSV to load.
   */
  private static final String URL_PARAM_NAME = "url";

  /**
   * Generates the data table.
   * This servlet assumes a special parameter that contains the CSV URL from which to load
   * the data.
   */
  @Override
  public DataTable generateDataTable(Query query, HttpServletRequest request)
      throws DataSourceException {
    String url = request.getParameter(URL_PARAM_NAME);
    if (StringUtils.isEmpty(url)) {
      log.error("url parameter not provided.");
      throw new DataSourceException(ReasonType.INVALID_REQUEST, "url parameter not provided");
    }

    Reader reader;
    try {
      reader = new BufferedReader(new InputStreamReader(new URL(url).openStream()));
    } catch (MalformedURLException e) {
      log.error("url is malformed: " + url);
      throw new DataSourceException(ReasonType.INVALID_REQUEST, "url is malformed: " + url);
    } catch (IOException e) {
      log.error("Couldn't read from url: " + url, e);
      throw new DataSourceException(ReasonType.INVALID_REQUEST, "Couldn't read from url: " + url);
    }
    DataTable dataTable = null;
    ULocale requestLocale = DataSourceHelper.getLocaleFromRequest(request);
    try {
      // Note: We assume that all the columns in the CSV file are text columns. In cases where the
      // column types are known in advance, this behavior can be overridden by passing a list of
      // ColumnDescription objects specifying the column types. See CsvDataSourceHelper.read() for
      // more details.
      dataTable = CsvDataSourceHelper.read(reader, null, true, requestLocale);
    } catch (IOException e) {
      log.error("Couldn't read from url: " + url, e);
      throw new DataSourceException(ReasonType.INVALID_REQUEST, "Couldn't read from url: " + url);
    }
    return dataTable;
  }
}

กําลังทํางานและทดสอบ CsvDataSourceServlet

ส่วนนี้จะแสดงวิธีเรียกใช้และทดสอบ CsvDataSourceServlet

หากต้องการเรียกใช้และทดสอบ CsvDataSourceServlet ให้สร้างไฟล์ CSV อัปเดตเว็บแอปพลิเคชัน และตั้งค่าการแสดงภาพที่สืบค้นแหล่งข้อมูลตามที่อธิบายไว้ในส่วนต่อไปนี้

การสร้างไฟล์ CSV

ไฟล์ csv_example.csv ได้อยู่ในไดเรกทอรี <data_source_library_install>/examples/src/html ซึ่งประกอบด้วยค่าต่อไปนี้

Employee,Manager
Roger,John
Robert,John
Jane,Roger
Jack,Jane
Bob,Jane

คัดลอกไฟล์นี้ไปที่ไดเรกทอรี <tomcat_home>/webapps/myWebApp ที่คุณสร้างในส่วนเริ่มต้นใช้งาน

การอัปเดตเว็บแอปพลิเคชันของคุณบน Apache Tomcat

ทําตามหรือปรับวิธีการด้านล่างนี้เพื่ออัปเดตเว็บแอปพลิเคชันบน Apache Tomcat วิธีการเหล่านี้ใช้สําหรับ Apache Tomcat ในระบบ Windows โดยเฉพาะ

  1. ไฟล์ web.xml ที่คัดลอกไว้ก่อนหน้านี้ไปยังไดเรกทอรี WEB-INF มีคําจํากัดความและการแมปที่จําเป็นสําหรับตัวอย่างนี้แล้ว บรรทัดต่อไปนี้คือ
    <servlet>
      <servlet-name>CSV Example</servlet-name>
      <description>
      CsvDataSourceServlet
      </description>
      <servlet-class>CsvDataSourceServlet</servlet-class>
    </servlet>
    
    <servlet-mapping>
      <servlet-name>CSV Example</servlet-name>
      <url-pattern>/csv</url-pattern>
    </servlet-mapping>
    
  2. เริ่ม Tomcat หรือรีสตาร์ท Tomcat ถ้าทํางานอยู่แล้ว
     
  3. คลิกลิงก์ต่อไปนี้ http://localhost:8080/myWebApp/csv?url=http://localhost:8080/myWebApp/csv_example.csv

    หน้าจอจะแสดงข้อความ 6-7 บรรทัด ขึ้นอยู่กับความกว้างของหน้าจอ
    ข้อความขึ้นต้นด้วย google.visualization.Query.setResponse
    และลงท้ายด้วย {c:[{v:'Bob'},{v:'Jane'}]}]}});

    นี่คือการตอบกลับที่แหล่งข้อมูล CSV ตัวอย่างส่งไปยังการแสดงภาพ

การใช้การแสดงภาพ เพื่อดูข้อมูล

คุณสามารถใช้ไฟล์ all_examples.html ในไดเรกทอรี <data_source_library_install>/examples/src/html เพื่อดูภาพข้อมูล

หากดูแหล่งที่มาของไฟล์ all_examples.html คุณจะเห็นว่ามีการแสดงภาพ 3 ภาพอยู่ในไฟล์ ข้อมูลโค้ดต่อไปนี้จะจําลองข้อกําหนดของการแสดงภาพเหล่านี้

  • บรรทัดต่อไปนี้ระบุตัวอย่าง csv ที่ครอบคลุมในส่วนนี้
    query = new google.visualization.Query('csv?url=http://localhost:8080/myWebApp/csv_example.csv');
    บรรทัดต่อไปนี้จะระบุการแสดงภาพแผนภูมิขององค์กร
    var chart = new google.visualization.OrgChart(document.getElementById('csv_div'));
  • บรรทัดต่อไปนี้จะระบุ simpleexample ที่ครอบคลุมในส่วนเริ่มต้นใช้งาน
    var query = new google.visualization.Query('simpleexample?tq=select name,population');
    บรรทัดต่อไปนี้ระบุการแสดงภาพแผนภูมิวงกลม
    var chart = new google.visualization.PieChart(document.getElementById('simple_div'));
  • บรรทัดต่อไปนี้ระบุตัวอย่าง advanced ที่ครอบคลุมในส่วนการกําหนดความสามารถและโฟลว์ของเหตุการณ์
    query = new google.visualization.Query('advanced?tableId=planets&tq=select planet,mass');
    บรรทัดต่อไปนี้จะระบุการแสดงภาพแผนภูมิแท่ง
    var chart = new google.visualization.BarChart(document.getElementById('advanced_div'));

ดูข้อมูลเพิ่มเติมเกี่ยวกับวิธีระบุแผนภูมิและใช้ภาษาในการค้นหาได้ที่ข้อมูลเบื้องต้นเกี่ยวกับการใช้เครื่องมือแผนภูมิและข้อมูลอ้างอิงภาษาการค้นหา

ทําตามหรือปรับวิธีการด้านล่างนี้เพื่อดูการแสดงภาพข้อมูลที่มาจาก CsvDataSourceServlet

  1. คัดลอกไฟล์ all_examples.html จากไดเรกทอรี <data_source_library_install>/examples/src/html ไปยังไดเรกทอรี <tomcat_home>/webapps/myWebApp/
  2. คลิกลิงก์ต่อไปนี้ http://localhost:8080/myWebApp/all_examples.html คุณควรเห็นการแสดงผลต่อไปนี้


ดูตัวอย่างแหล่งที่มาของข้อมูลขั้นสูงได้ในการกําหนดความสามารถและโฟลว์เหตุการณ์

ขั้นตอนถัดไป

ตัวอย่างถัดไปจะอธิบายไว้ในส่วนการกําหนดความสามารถและโฟลว์ของกิจกรรม หรือสํารวจลิงก์ต่อไปนี้