Advanced I/O Functionの設定
Advanced I/O Functionディレクトリ(functions/NewsApp_AIO)には以下のファイルが含まれています:
- DataStoreFetch.javaメイン関数ファイル
- catalyst-config.json設定ファイル
- libフォルダ内のJavaライブラリファイル
- .classpathおよび.project依存関係ファイル
DatastoreFetch.javaファイルにコードを追加します。
Advanced I/O Functionは、Data Storeのソーステーブルからニュースアイテムを取得し、JSONレスポンスとしてクライアントコンポーネントに転送します。
以下のコードをコピーして、functions/NewsApp_AIOディレクトリのDatastoreFetch.javaに貼り付けます。
DatastoreFetch.java
copy
import java.util.logging.Logger;
import java.util.ArrayList;
import java.util.logging.Level;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.catalyst.advanced.CatalystAdvancedIOHandler;
import com.zc.component.object.ZCRowObject;
import com.zc.component.zcql.ZCQL;
import org.json.JSONArray;
import org.json.JSONObject;
public class DatastoreFetch implements CatalystAdvancedIOHandler {
private static final Logger LOGGER = Logger.getLogger(DatastoreFetch.class.getName());
@Override
public void runner(HttpServletRequest request, HttpServletResponse response) throws Exception {
try {
// エンドポイントとHTTPメソッドを取得
String url = request.getRequestURI();
String method = request.getMethod();
if (url.equals("/fetchData") && method.equals("GET")) {
String tableName = request.getParameter("tablename");
// ZCQLを使用してData Storeをクエリ
String query = "select title, url from " + tableName;
ArrayList<ZCRowObject> rowList = ZCQL.getInstance().executeQuery(query);
JSONObject data = new JSONObject();
JSONArray content = new JSONArray();
// JSONレスポンスを構築
for (int i = 0; i < rowList.size(); i++) {
JSONObject rowData = new JSONObject();
JSONObject tableData = new JSONObject();
ZCRowObject row = rowList.get(i);
String title = (String) row.get("title");
String urlVal = (String) row.get("url");
rowData.put("title", title);
rowData.put("url", urlVal);
tableData.put(tableName, rowData);
content.put(tableData);
}
data.put("content", content);
// クライアントにレスポンスを送信
response.setContentType("application/json");
response.getWriter().write(data.toString());
response.setStatus(200);
} else {
// 無効なリクエスト
LOGGER.log(Level.SEVERE, "Error. Invalid Request");
response.setStatus(404);
}
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "Exception in DatastoreFetch", e);
response.setStatus(500);
}
}
}
Advanced I/O Functionの設定が完了しました。クライアントの設定後にアプリケーションのアーキテクチャについて説明します。
最終更新日 2026-03-05 11:43:24 +0530 IST