@Override public SSRJSON convert(TweetWithRaw tweetAndRaw) throws TException { Tweet tweet = tweetAndRaw.getTweet(); SSRJSON ssrJson = new SSRJSON(); SSR ssr = new SSR(); ssr.setUri(uriPrefix + tweet.getId()); ssr.setTitle(String.valueOf(tweet.getId())); ssr.setVisibility(new Visibility().setFormalVisibility("U")); ssr.setSnippet(tweet.getText()); if (tweet.getGeoLocation() != null) { Coordinate coordinate = new Coordinate(); coordinate.setLatitude(tweet.getGeoLocation().getLatitude()); coordinate.setLongitude(tweet.getGeoLocation().getLongitude()); } ssr.setResultDate(TimeUtil.convertToThriftDateTime(tweet.getTimestamp())); ssrJson.setSsr(ssr); ssrJson.setJsonString(new TSerializer(new TSimpleJSONProtocol.Factory()).toString(tweet)); return ssrJson; }
public static <T extends TBase> String ThriftToJSON(T thrift) { TSerializer serializer = new TSerializer(new TSimpleJSONProtocol.Factory()); try { return serializer.toString(thrift); } catch (TException e) { } throw new IllegalStateException("Convert to json failed : " + thrift); }
private String generateMockProxyToken(String userSubject) throws EzSecurityTokenException { ProxyUserToken token = new ProxyUserToken(new X509Info(userSubject), "EzSecurity", "", System.currentTimeMillis()+expiry); try { return new String(new TSerializer(new TSimpleJSONProtocol.Factory()).serialize(token), StandardCharsets.UTF_8); } catch (TException e) { throw new EzSecurityTokenException("Unable to generate a mock user principal: "+e.getMessage()); } }
public static String serializeProxyUserTokenToJSON(ProxyUserToken token) throws TException { try { return new String(new TSerializer(new TSimpleJSONProtocol.Factory()).serialize(token), EzSecurityConstant.CHARSET); } catch (UnsupportedEncodingException e) { throw new TException("Unable to encode " + EzSecurityConstant.CHARSET + " string"); } }
private void assertEquals( final TBase<?,?> thriftObject, final DBObject dbObject ) throws Exception { //serialize the thrift object in JSON TSerializer tjsonSerializer = new TSerializer(new TSimpleJSONProtocol.Factory()); byte[] jsonObject = tjsonSerializer.serialize(thriftObject); // Parse the JSON into DBObject DBObject expectedDBObject = (DBObject) JSON.parse(new String(jsonObject)); System.out.println("Thrift source=" + expectedDBObject.toString()); System.out.println("DB source=" + dbObject.toString()); // Are the DBObject equals ? Assert.assertEquals(expectedDBObject.toString(), dbObject.toString()); }
public static String thriftToDebugJson(TBase<?, ?> thriftObject) { TSerializer serializer = new TSerializer(new TSimpleJSONProtocol.Factory()); try { return new String(serializer.serialize(thriftObject)); } catch (TException e) { LOGGER.error( e, String.format( "Failed trying to serialize type [%s] to debug JSON.", thriftObject.getClass().getName())); return "FAILED_TO_DESERIALIZE"; } }
protected TProtocol createThriftProtocol(TTransport transport) { switch(this.protocol) { case BINARY: return new TBinaryProtocol(transport); case JSON: return new TJSONProtocol(transport); case SIMPLE_JSON: return new TSimpleJSONProtocol(transport); default: throw new IllegalArgumentException("Unknown Thrift Protocol."); } }
public TSimpleJsonProtocolSerializer() { super(DEFAULT_STRICT, new TSimpleJSONProtocol.Factory(), false, MEDIA_TYPE); }
private static String generateProxyToken() throws TException { ProxyUserToken token = new ProxyUserToken(new X509Info(DN), App, "", System.currentTimeMillis() + 1000); return new String(new TSerializer(new TSimpleJSONProtocol.Factory()).serialize(token), StandardCharsets.UTF_8); }
private static String generateProxyToken(long expiration) throws TException { ProxyUserToken token = new ProxyUserToken(new X509Info(DN), App, "", expiration); return new String(new TSerializer(new TSimpleJSONProtocol.Factory()).serialize(token), StandardCharsets.UTF_8); }
private String insertTweet(HttpServletRequest request, HttpServletResponse response) { String userName = request.getParameter("userName"); String CAPCO = request.getParameter("CAPCO"); String tweetContent = request.getParameter("tweetContent"); String result = null; ContentPublisher.Client client = null; String feedName = "tweet-ingest"; try { logger.info("Initiating request to Content Publisher Service"); client = pool.getClient(ContentPublisherServiceConstants.SERVICE_NAME, ContentPublisher.Client.class); PublishData data = new PublishData(); Tweet tweet = new Tweet(); tweet.setTimestamp(System.currentTimeMillis()); tweet.setId(idCount++); tweet.setText(tweetContent); tweet.setUserId(1); tweet.setUserName(userName); tweet.setIsFavorite(new Random().nextBoolean()); tweet.setIsRetweet(new Random().nextBoolean()); UpdateEntry entry = new UpdateEntry(getUriPrefix(feedName, getToken()) + tweet.getId()); entry.setRawData(new TSerializer(new TSimpleJSONProtocol.Factory()).serialize(tweet)); entry.setParsedData(ThriftUtils.serialize(tweet)); data.setEntry(entry); data.setFeedname(feedName); SSRJSON ssrJson = new SSRJSON(); ssrJson.setJsonString(new TSerializer(new TSimpleJSONProtocol.Factory()).toString(tweet)); SSR ssr = new SSR(); ssr.setUri(entry.getUri()); ssr.setTitle(String.valueOf(tweet.getId())); ssr.setVisibility(new Visibility().setFormalVisibility("U")); ssr.setSnippet(tweet.getText()); if (tweet.getGeoLocation() != null) { Coordinate coordinate = new Coordinate(); coordinate.setLatitude(tweet.getGeoLocation().getLatitude()); coordinate.setLongitude(tweet.getGeoLocation().getLongitude()); } ssr.setResultDate(TimeUtil.convertToThriftDateTime(tweet.getTimestamp())); ssrJson.setSsr(ssr); data.setSsrjson(ssrJson); client.publish(data, new Visibility().setFormalVisibility(CAPCO), getToken()); logger.info("Sent Tweet to the Content Publisher Service"); result = "Successfully added the tweet(id=" + tweet.getId() + ")"; } catch (IOException | TException e) { result = "Failed to insert data: " + e.getMessage(); logger.error("Failed to insert data", e); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); } finally { pool.returnToPool(client); } return result; }
public void run() { Properties properties = new Properties(); if (mMetadataBrokerList == null || mMetadataBrokerList.isEmpty()) { properties.put("metadata.broker.list", "localhost:9092"); } else { properties.put("metadata.broker.list", mMetadataBrokerList); } properties.put("partitioner.class", "com.pinterest.secor.tools.RandomPartitioner"); properties.put("serializer.class", "kafka.serializer.DefaultEncoder"); properties.put("key.serializer.class", "kafka.serializer.StringEncoder"); properties.put("request.required.acks", "1"); ProducerConfig config = new ProducerConfig(properties); Producer<String, byte[]> producer = new Producer<String, byte[]>(config); TProtocolFactory protocol = null; if(mType.equals("json")) { protocol = new TSimpleJSONProtocol.Factory(); } else if (mType.equals("binary")) { protocol = new TBinaryProtocol.Factory(); } else { throw new RuntimeException("Undefined message encoding type: " + mType); } TSerializer serializer = new TSerializer(protocol); for (int i = 0; i < mNumMessages; ++i) { long time = (System.currentTimeMillis() - mTimeshift * 1000L) * 1000000L + i; TestMessage testMessage = new TestMessage(time, "some_value_" + i); if (i % 2 == 0) { testMessage.setEnumField(TestEnum.SOME_VALUE); } else { testMessage.setEnumField(TestEnum.SOME_OTHER_VALUE); } byte[] bytes; try { bytes = serializer.serialize(testMessage); } catch(TException e) { throw new RuntimeException("Failed to serialize message " + testMessage, e); } KeyedMessage<String, byte[]> data = new KeyedMessage<String, byte[]>( mTopic, Integer.toString(i), bytes); producer.send(data); } producer.close(); }