0

これは必要以上に複雑かもしれませんが、私は以下を使用しています:

  • モンゴDB
  • スパーク
  • フリーマーカー
  • ジャワ

mongodb の埋め込みデータにアクセスして Web サイトに印刷する方法がわかりません。レコードを調べて、値が「MGMT」の「ラベル」を持つインターフェイスを見つけ、そのインターフェイスの情報を出力できるようにしたいと考えています。問題は、それらを「ネットワーキング」コレクションに埋め込むことです。その情報にアクセスするにはどうすればよいですか?

コードの重要な行:

ジャバ:

try {
                        Template helloTemplate = configuration.getTemplate("hello.ftl");

                        DBObject document = collection.findOne();



                        helloTemplate.process(document, writer);

                        System.out.println(writer);
                    } catch (Exception e) {
                        halt(500);
                        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                    }

完全なコード:

.java:

import com.mongodb.*;
import freemarker.template.Configuration;
import freemarker.template.Template;
import spark.Request;
import spark.Response;
import spark.Route;
import spark.Spark;

import java.io.StringWriter;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: sysrjt1
 * Date: 6/13/13
 * Time: 1:09 PM
 * To change this template use File | Settings | File Templates.
 */
public class HelloWorldMongoDBSparkFreemarkerSyle {
    public static void main(String[] args) {
        final Configuration configuration = new Configuration();
        configuration.setClassForTemplateLoading(HelloWorldMongoDBSparkFreemarkerSyle.class, "/");

        MongoClient client = null;
        try {
            client = new MongoClient(new ServerAddress("localhost", 27017 ));
        } catch (UnknownHostException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

        DB database = client.getDB("systems");
        final DBCollection collection;
        collection = database.getCollection("systems");

        Spark.get(new Route("/") {
            @Override
            public Object handle(Request request, Response response) {
                StringWriter writer = new StringWriter();
                try {
                    Template helloTemplate = configuration.getTemplate("hello.ftl");

                    DBObject document = collection.findOne();



                    helloTemplate.process(document, writer);

                    System.out.println(writer);
                } catch (Exception e) {
                    halt(500);
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }
                return writer;
            }
        });
    }
}

私の HTML テンプレート (hello.ftl):

<!DOCTYPE html>
<html>
<head>
    <title>Welcome!</title>
</head>
<body>
    <table>
        <thead>
            <tr>
                <th>Name</th>
                <th>Management IP</th>
                <th>RAM</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>${name}</td>
                <td>${ipAddr}</td> <! -- this line fails -->
                <td>${ram}</td>
            </tr>
        </tbody>
    </table>

     <h1>Server Name: ${name}</h1>
</body>
</html>

モンゴッドから:

> db.systems.findOne()
{
        "_id" : ObjectId("51b9dde5d05f675ad37ac990"),
        "name" : "abcdev01",
        "networking" : {
                "interface1" : {
                        "name" : "eth0",
                        "ipAddr" : "12.34.45.56",
                        "sNet" : "255.255.255.0",
                        "label" : "MGMT"
                },
                "interface2" : {
                        "name" : "eth1",
                        "ipAddr" : "1.2.3.4"
                }
        },
        "ram" : 102390
}
4

2 に答える 2

1

私は(MongoDBを知らなくても)失敗した行は次のようになるはずだと思い${networking.interface1.ipAddr}ます。しかし、おそらくすべてのインターフェースをリストしたいと思うでしょう。次に、次のことができます。

<#list networing.keySet() as ifname>
  ...
  ${networking[ifname].ipAddr}
  ...
</#list>

ただし、特定の名前のインターフェイスに関する情報のみを出力する場合は、MongoDB API を使用して適切なクエリを発行し、その単一のインターフェイス オブジェクトのみを FreeMarker に渡す必要があります。

于 2013-06-13T23:04:14.657 に答える