本文整理汇总了Java中com.couchbase.client.java.document.JsonDocument.create方法的典型用法代码示例。如果您正苦于以下问题:Java JsonDocument.create方法的具体用法?Java JsonDocument.create怎么用?Java JsonDocument.create使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.couchbase.client.java.document.JsonDocument
的用法示例。
在下文中一共展示了JsonDocument.create方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: loadJson
import com.couchbase.client.java.document.JsonDocument; //导入方法依赖的package包/类
public void loadJson(Bucket bucket, String artistId)
{
SpotifyLoaderService loaderService = new SpotifyLoaderServiceImpl();
String jsonU2Artist = loaderService.getArtist(artistId);
JsonObject json = JsonObject.fromJson(jsonU2Artist);
JsonDocument docs = JsonDocument.create("artist", json);
bucket.upsert(docs);
String jsonU2Albums = loaderService.getAlbums(artistId);
json = JsonObject.fromJson(jsonU2Albums);
docs = JsonDocument.create("albums", json);
bucket.upsert(docs);
String jsonU2RelatedArtists = loaderService.getRelatedArtists(artistId);
json = JsonObject.fromJson(jsonU2RelatedArtists);
docs = JsonDocument.create("relArtists", json);
bucket.upsert(docs);
}
示例2: fromJSON
import com.couchbase.client.java.document.JsonDocument; //导入方法依赖的package包/类
/**
* Returns an instance of the specified target class with the property values from the specified JSON String.
*
* @param targetClass <code>JsonArray.class</code>, <code>JsonObject.class</code> or <code>JsonDocument.class</code>
* @param json
* @return
*/
public static <T> T fromJSON(final Class<T> targetClass, final String json) {
if (targetClass.equals(JsonObject.class)) {
return (T) JsonObject.from(N.fromJSON(Map.class, json));
} else if (targetClass.equals(JsonArray.class)) {
return (T) JsonArray.from(N.fromJSON(List.class, json));
} else if (targetClass.equals(JsonDocument.class)) {
final JsonObject jsonObject = JsonObject.from(N.fromJSON(Map.class, json));
final String id = N.stringOf(jsonObject.get(_ID));
jsonObject.removeKey(_ID);
return (T) JsonDocument.create(id, jsonObject);
} else {
throw new IllegalArgumentException("Unsupported type: " + ClassUtil.getCanonicalClassName(targetClass));
}
}
示例3: toJsonDocument
import com.couchbase.client.java.document.JsonDocument; //导入方法依赖的package包/类
static JsonDocument toJsonDocument(final Object obj, final JsonObject jsonObject) {
final Class<?> cls = obj.getClass();
final Method idSetMethod = getObjectIdSetMethod(obj.getClass());
final String idPropertyName = N.isEntity(cls) ? (idSetMethod == null ? null : ClassUtil.getPropNameByMethod(idSetMethod)) : _ID;
String id = null;
if (idPropertyName != null && jsonObject.containsKey(idPropertyName)) {
id = N.stringOf(jsonObject.get(idPropertyName));
jsonObject.removeKey(idPropertyName);
}
if (N.isNullOrEmpty(id)) {
throw new IllegalArgumentException("No id property included the specified object: " + N.toString(jsonObject));
}
return JsonDocument.create(id, jsonObject);
}
示例4: basicJSONtest
import com.couchbase.client.java.document.JsonDocument; //导入方法依赖的package包/类
@Test
public void basicJSONtest() {
JsonObject user = JsonObject.empty()
.put("firstname", "Walter")
.put("lastname", "White")
.put("job", "chemistry teacher")
.put("age", 50);
assertEquals( "{\"firstname\":\"Walter\",\"job\":\"chemistry teacher\",\"age\":50,\"lastname\":\"White\"}", user.toString());
JsonDocument doc = JsonDocument.create("walter", user);
assertEquals("JsonDocument{id='walter', cas=0, expiry=0, content={\"firstname\":\"Walter\",\"job\":\"chemistry teacher\",\"age\":50,\"lastname\":\"White\"}, mutationToken=null}\n" +
"", doc.toString());
}
示例5: handle
import com.couchbase.client.java.document.JsonDocument; //导入方法依赖的package包/类
@Override
public void handle(RoutingContext rc) {
Long albumId = PathUtil.parseLongParam(rc.pathParam("albumId"));
if (albumId == null) {
rc.next();
return;
}
User user = rc.user();
if (user == null) {
rc.response().setStatusCode(HTTP_UNAUTHORIZED).end();
return;
}
String comment = rc.getBodyAsString();
if (comment == null || comment.isEmpty()) {
rc.response().setStatusCode(HTTP_BAD_REQUEST).end();
return;
}
long timestamp = System.currentTimeMillis();
JsonObject content = new JsonObject()
.put("albumId", albumId)
.put("username", user.principal().getValue("username"))
.put("timestamp", timestamp)
.put("comment", comment);
JsonDocument document = JsonDocument.create("comment::" + timestamp, com.couchbase.client.java.document.json.JsonObject.from(content.getMap()));
Vertx vertx = rc.vertx();
Observable<JsonDocument> documentObservable = albumCommentsBucket.upsert(document);
RxJavaInterop.toV2Single(documentObservable.toSingle())
.observeOn(RxHelper.scheduler(vertx.getOrCreateContext()))
.doOnSuccess(doc -> vertx.setTimer(1000, v -> {
String address = "album." + albumId + ".comments.new";
vertx.eventBus().<JsonObject>publish(address, content);
}))
.subscribe(doc -> rc.response().end(), rc::fail);
}
示例6: upsert
import com.couchbase.client.java.document.JsonDocument; //导入方法依赖的package包/类
@Override
public Tt upsert(Tt tt) {
JsonObject content = JsonObject.fromJson(gson.toJson(tt));
content.put(DOCTYPE_KEY, DOCTYPE_TT);
Document<?> document = JsonDocument.create(tt.getId().toString(), content);
bucket.upsert(document);
return tt;
}
示例7: toJson
import com.couchbase.client.java.document.JsonDocument; //导入方法依赖的package包/类
JsonDocument toJson() {
ObjectMapper mapper = new ObjectMapper();
String json;
try {
json = mapper.writeValueAsString(this);
} catch (JsonProcessingException ex) {
throw new RuntimeException(ex);
}
return JsonDocument.create(getId(), JsonObject.fromJson(json));
}
示例8: eval
import com.couchbase.client.java.document.JsonDocument; //导入方法依赖的package包/类
@Override
public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException {
final CouchbaseClusterManager cmm = CouchbaseClusterManager.getInstance();
// Get connection details
final String clusterId = args[0].itemAt(0).getStringValue();
// Get reference to cluster
final CouchbaseCluster cluster = cmm.validate(clusterId);
// Retrieve other parameters
final String bucketName = (args[1].isEmpty()) ? Constants.DEFAULT_BUCKET : args[1].itemAt(0).getStringValue();
final String bucketPassword = cmm.getBucketPassword(clusterId);
final String docName = args[2].itemAt(0).getStringValue();
try {
// Prepare input
final JsonObject jsonObject = (JsonObject) MapToJson.convert(args[3]);
final JsonDocument jsonDocument = JsonDocument.create(docName, jsonObject);
// Perform action
final JsonDocument result = isCalledAs(UPSERT)
? cluster.openBucket(bucketName, bucketPassword).upsert(jsonDocument)
: cluster.openBucket(bucketName, bucketPassword).insert(jsonDocument);
// Return results
return JsonToMap.convert(result.content(), context);
} catch (final Throwable ex) {
return GenericExceptionHandler.handleException(this, ex);
}
}
示例9: mapRow
import com.couchbase.client.java.document.JsonDocument; //导入方法依赖的package包/类
public JsonDocument mapRow(ResultSet rs, int rowNum) throws SQLException {
String id = table.getName() + "::" + rs.getString(table.getPrimaryKey());
JsonObject obj = JsonObject.create();
for (Column col : table.getColumns()) {
Object value = getJsonTypedValue(col.type, rs.getObject(col.name), col.name);
obj.put(col.name, value);
}
return JsonDocument.create(id, obj);
}
示例10: populateBucket
import com.couchbase.client.java.document.JsonDocument; //导入方法依赖的package包/类
private void populateBucket() {
CouchbaseEnvironment env = DefaultCouchbaseEnvironment.builder()
.socketConnectTimeout(60000)
.connectTimeout(60000)
.keepAliveInterval(60000)
.build();
CouchbaseCluster cluster = CouchbaseCluster.create(env, bootstrapNodes);
Bucket bucket = cluster.openBucket(bucketName, password);
assertTrue(bucket.bucketManager().flush());
JsonDocument document = JsonDocument.create("foo", JsonObject.create().put("bar", 42));
bucket.upsert(document, PersistTo.MASTER);
bucket.close();
cluster.disconnect();
}
示例11: testServer
import com.couchbase.client.java.document.JsonDocument; //导入方法依赖的package包/类
@Test
public static void testServer()
throws InterruptedException, IOException {
CouchbaseTestServer couchbaseTestServer = new CouchbaseTestServer(TestUtils.findFreePort());
couchbaseTestServer.start();
int port = couchbaseTestServer.getPort();
int serverPort = couchbaseTestServer.getServerPort();
try {
CouchbaseEnvironment cbEnv = DefaultCouchbaseEnvironment.builder().bootstrapHttpEnabled(true)
.bootstrapHttpDirectPort(port)
.bootstrapCarrierDirectPort(serverPort)
.connectTimeout(TimeUnit.SECONDS.toMillis(15))
.bootstrapCarrierEnabled(true).build();
CouchbaseCluster cbCluster = CouchbaseCluster.create(cbEnv, "localhost");
Bucket bucket = cbCluster.openBucket("default","");
try {
JsonObject content = JsonObject.empty().put("name", "Michael");
JsonDocument doc = JsonDocument.create("docId", content);
JsonDocument inserted = bucket.insert(doc);
}
catch (Exception e)
{
Assert.fail("Should not throw exception on insert", e);
}
}
finally
{
couchbaseTestServer.stop();
}
}
示例12: setup
import com.couchbase.client.java.document.JsonDocument; //导入方法依赖的package包/类
@Before
public void setup() {
List<View> views = new ArrayList<>();
String mapBuilder = "function (doc, meta) {\n" +
" if ((/^TESTER/).test(meta.id)) {\n" +
" emit(meta.id, null);\n" +
" }\n" +
"}";
View view = DefaultView.create("findAll", mapBuilder);
views.add(view);
factory = mock(CouchbaseClientFactory.class);
BucketManager mgr = mock(BucketManager.class);
DesignDocument design = mock(DesignDocument.class);
Bucket bucket = mock(Bucket.class);
ViewResult result = mock(ViewResult.class);
List<ViewRow> rows = new ArrayList<>();
ViewRow row = mock(ViewRow.class);
rows.add(row);
ViewQuery query = ViewQuery.from("TESTER", "findAll");
query.stale(Stale.FALSE);
JsonDocument doc = JsonDocument.create("TESTER:123", JsonObject.fromJson("{\"name\": \"name\", \"other\": \"other\"}"));
when(factory.bucket()).thenReturn(bucket);
when(bucket.bucketManager()).thenReturn(mgr);
when(mgr.getDesignDocument("TESTER")).thenReturn(design);
when(design.views()).thenReturn(views);
when(bucket.query(query)).thenReturn(result);
when(result.allRows()).thenReturn(rows);
when(row.document()).thenReturn(doc);
}
示例13: main
import com.couchbase.client.java.document.JsonDocument; //导入方法依赖的package包/类
public static void main(String[] args) {
Cluster cluster = CouchbaseCluster.create(DefaultCouchbaseEnvironment
.builder()
.queryEnabled(true)
.build());
Bucket defaultBucket = cluster.openBucket("default");
defaultBucket.remove("user:walter");
JsonArray friends = JsonArray.empty()
.add(JsonObject.empty().put("name", "Mike Ehrmantraut"))
.add(JsonObject.empty().put("name", "Jesse Pinkman"));
JsonObject content = JsonObject.empty()
.put("firstname", "Walter")
.put("lastname", "White")
.put("age", 52)
.put("aliases", JsonArray.from("Walt Jackson", "Mr. Mayhew", "David Lynn"))
.put("friends", friends);
JsonDocument walter = JsonDocument.create("user:walter", content);
JsonDocument inserted = defaultBucket.insert(walter);
JsonDocument foundGuy = defaultBucket.get("user:walter");
System.out.println(foundGuy.content().toMap());
Bucket beerBucket = cluster.openBucket("beer-sample");
N1qlQueryResult result = beerBucket.query(N1qlQuery.simple(select("*").from(i("beer-sample")).limit(10)));
System.out.println("Errors found: " + result.errors());
for (N1qlQueryRow row : result.allRows()) {
JsonObject jsonObject = row.value();
System.out.println(jsonObject.toMap());
}
cluster.disconnect();
}
示例14: save
import com.couchbase.client.java.document.JsonDocument; //导入方法依赖的package包/类
@Override
public void save(final Session session) {
JsonObject json = JsonObject.from(session.attributes());
// session metadata
json.put("_accessedAt", session.accessedAt());
json.put("_createdAt", session.createdAt());
json.put("_savedAt", session.savedAt());
JsonDocument doc = JsonDocument.create(N1Q.qualifyId(SESSION, session.id()), expiry, json);
bucket.upsert(doc);
}
示例15: fromEntity
import com.couchbase.client.java.document.JsonDocument; //导入方法依赖的package包/类
@Override
public JsonDocument fromEntity(final EntityDocument<Object> source) {
Object bean = source.content();
Map json = mapper.convertValue(bean, HashMap.class);
json.put(N1Q.CLASS, bean.getClass().getName());
return JsonDocument.create(source.id(), JsonObject.from(json));
}