The example is C# code. I am using RxJava to use lambda in Android.
var dex = device.OrderBy(x => x.Connection1).ThenBy(x => x.EventName1).GroupBy(x => new { x.Connection1, x.EventName1 }, y => new { EventNames = y.EventName1 }).
Select(x => new { Connector = x.Key.Connection1, EventName = x.Key.EventName1, Counter = x.Select(y => y.EventNames).Count() });
So i need this code written in Java with the help of RxJava does anyone know how to use a multible group by in RxJava ???
Related
Im trying to translate this part of a code to Java, i understand its waiting for a callback from a BLE action, if returns any data will be added to statusReport, only issue is that it seems that is doing a loop somewhere because in the real application gets this callback multiple times but on my side i only get one response.
private val statusReportCallback = DataReceivedCallback { _, data -> convertByteArrayToASCII(data.value)?.let { Endpoint.addToStatusReport(it)
if (nextWillBeICCID) iccid = it.also { notifyDeviceEvent(EventType.ICCID) }.also { nextWillBeICCID = false }
when (it) {
"power off" -> Endpoint.commsTestInProgress.set(false)
"ICCID:" -> nextWillBeICCID = true
"POST: Success" -> Endpoint.onServerConnected.value = Event(true)
}
} }
I am currently trying to do a batch update to a google sheet via a scala project however i am having a lot of trouble converting a Array[ValueRange] to util.List[ValueRange]
here is the code produceing the information for the body value for the batch update:
val values = util.Arrays.asList(util.Arrays.asList[AnyRef](
"Y"
))
val data =
allTasksToBeCompleted.map {
id =>
val range = s"$sheetName!G${id.toInt}:G${id.toInt}"
new ValueRange().setRange(range).setValues(values)
}
I have tried converting via util.Arrays.asList(data) and asInstanceOf however neither seems to work at runtime.
Have you tried JavaConverters decorators?
import scala.collection.JavaConverters._
val data = Array(1, 2, 3)
val list = data.toList.asJava
data: Array[Int] = Array(1, 2, 3)
list: java.util.List[Int] = [1, 2, 3]
This should convert any Array[T] to java.util.List[T]
I have tried multiple approaches but nothing seems to be working
Here's what I have done,
Created a Cloud9 Instance, initiated a maven application, added aws sdk java, x-ray core, x-ray instrumentor, x-ray sdk dependencies, Created DynamoDB Client ran the application, data inserted but error subsegment not found. Manually added segment, Error gone but no traces.
Create Spring Boot App, added same dependencies, added Xray servlet filter, added begin segment, begin subsegment, no error but no traces.
I have more approaches as well but these seems to be very close. Also I have not installed any agent or Daemon. Can anyone tell where I am going wrong?
I am trying to create a simple java application, even a single page to insert data in DynamoDB and get the traces that's it.
i don't have experience working on java sharing here Node JS example hope that will be helpful. tested this : https://github.com/aws-samples/aws-xray-sdk-node-sample
const AWSXRay = require('aws-xray-sdk');
const XRayExpress = AWSXRay.express;
const express = require('express');
// Capture all AWS clients we create
const AWS = AWSXRay.captureAWS(require('aws-sdk'));
AWS.config.update({region: process.env.DEFAULT_AWS_REGION || 'us-west-2'});
// Capture all outgoing https requests
AWSXRay.captureHTTPsGlobal(require('https'));
const https = require('https');
// Capture MySQL queries
const mysql = AWSXRay.captureMySQL(require('mysql'));
const app = express();
const port = 3000;
app.use(XRayExpress.openSegment('SampleSite'));
app.get('/', (req, res) => {
const seg = AWSXRay.getSegment();
const sub = seg.addNewSubsegment('customSubsegment');
setTimeout(() => {
sub.close();
res.sendFile(`${process.cwd()}/index.html`);
}, 500);
});
app.get('/aws-sdk/', (req, res) => {
const ddb = new AWS.DynamoDB();
const ddbPromise = ddb.listTables().promise();
ddbPromise.then(function(data) {
res.send(`ListTables result:\n ${JSON.stringify(data)}`);
}).catch(function(err) {
res.send(`Encountered error while calling ListTables: ${err}`);
});
});
app.get('/http-request/', (req, res) => {
const endpoint = 'https://amazon.com/';
https.get(endpoint, (response) => {
response.on('data', () => {});
response.on('error', (err) => {
res.send(`Encountered error while making HTTPS request: ${err}`);
});
response.on('end', () => {
res.send(`Successfully reached ${endpoint}.`);
});
});
});
app.get('/mysql/', (req, res) => {
const mysqlConfig = require('./mysql-config.json');
const config = mysqlConfig.config;
const table = mysqlConfig.table;
if (!config.user || !config.database || !config.password || !config.host || !table) {
res.send('Please correctly populate mysql-config.json');
return;
}
const connection = mysql.createConnection(config);
connection.query(`SELECT * FROM ${table}`, (err, results, fields) => {
if (err) {
res.send(`Encountered error while querying ${table}: ${err}`);
return;
}
res.send(`Retrieved the following results from ${table}:\n${results}`);
});
connection.end();
});
app.use(XRayExpress.closeSegment());
app.listen(port, () => console.log(`Example app listening on port ${port}!`));
spark.sqlContext.udf.register('dataSource', (data: scala.collection.mutable.WrappedArray$ofRef, ofType : String) => {
var flag = ''
try{
val array: Seq[String] = data.flatten(x => Seq(x.toString()))
array.foreach(s => if(s.contains(ofType)) { flag = ofType })
}
catch {
println("Exception :" + x.getMessage)
flag = ''
}
flag;
})
It is not required to convert this Scala code into Java code. In Spark you can register a UDF in any of the languages and also make use of it in either Java or Scala as long as it is used in the same SparkSession or Context.
I am new to ZeroMQ and seem to be losing messages in a loop in my begin() method.
I'm wondering if I am missing a piece where I am not queuing messages or something?
When I cause an event on my publisher, that sends two messages to my subscriber with a small gap in between, I seem not to be getting the second message that is relayed. What am I missing?
class ZMQSubscriber[T <: Transaction, B <: Block](
socket: InetSocketAddress,
hashTxListener: Option[HashDigest => Future[Unit]],
hashBlockListener: Option[HashDigest => Future[Unit]],
rawTxListener: Option[Transaction => Future[Unit]],
rawBlockListener: Option[Block => Future[Unit]]) {
private val logger = BitcoinSLogger.logger
def begin()(implicit ec: ExecutionContext) = {
val context = ZMQ.context(1)
// First, connect our subscriber socket
val subscriber = context.socket(ZMQ.SUB)
val uri = socket.getHostString + ":" + socket.getPort
//subscribe to the appropriate feed
hashTxListener.map { _ =>
subscriber.subscribe(HashTx.topic.getBytes(ZMQ.CHARSET))
logger.debug("subscribed to the transaction hashes from zmq")
}
rawTxListener.map { _ =>
subscriber.subscribe(RawTx.topic.getBytes(ZMQ.CHARSET))
logger.debug("subscribed to raw transactions from zmq")
}
hashBlockListener.map { _ =>
subscriber.subscribe(HashBlock.topic.getBytes(ZMQ.CHARSET))
logger.debug("subscribed to the hashblock stream from zmq")
}
rawBlockListener.map { _ =>
subscriber.subscribe(RawBlock.topic.getBytes(ZMQ.CHARSET))
logger.debug("subscribed to raw block")
}
subscriber.connect(uri)
subscriber.setRcvHWM(0)
logger.info("Connection to zmq client successful")
while (true) {
val notificationTypeStr = subscriber.recvStr(ZMQ.DONTWAIT)
val body = subscriber.recv(ZMQ.DONTWAIT)
Future(processMsg(notificationTypeStr, body))
}
}
private def processMsg(topic: String, body: Seq[Byte])(implicit ec: ExecutionContext): Future[Unit] = Future {
val notification = ZMQNotification.fromString(topic)
val res: Option[Future[Unit]] = notification.flatMap {
case HashTx =>
hashTxListener.map { f =>
val hash = Future(DoubleSha256Digest.fromBytes(body))
hash.flatMap(f(_))
}
case RawTx =>
rawTxListener.map { f =>
val tx = Future(Transaction.fromBytes(body))
tx.flatMap(f(_))
}
case HashBlock =>
hashBlockListener.map { f =>
val hash = Future(DoubleSha256Digest.fromBytes(body))
hash.flatMap(f(_))
}
case RawBlock =>
rawBlockListener.map { f =>
val block = Future(Block.fromBytes(body))
block.flatMap(f(_))
}
}
}
}
So this seems to have been solved by using a ZMsg.recvMsg() in the while-loop instead of
val notificationTypeStr = subscriber.recvStr(ZMQ.DONTWAIT)
val body = subscriber.recv(ZMQ.DONTWAIT)
I'm not sure why this works, but it does. So here is what my begin method looks like now
while (run) {
val zmsg = ZMsg.recvMsg(subscriber)
val notificationTypeStr = zmsg.pop().getString(ZMQ.CHARSET)
val body = zmsg.pop().getData
Future(processMsg(notificationTypeStr, body))
}
Future.successful(Unit)
}
What am I missing?
How the blocking v/s non-blocking modus operandi work :
The trick is in the (non-)blocking mode of the respective call to the .recv() method.
A second call to the subscriber.recv( ZMQ.DONTWAIT )-method thus returns immediately, so your second part, ( the body ) may and will legally contain nothing, even though your promise stated a pair of messages was indeed dispached from the publisher-side ( a pair of .send() method calls - one may also object, there are chances the sender was actually sending just one message, in a multi-part fashion - MCVE-code is not specific on this part ).
So, once you have moved your code from non-blocking mode ( in the O/P ) into a principally blocking-mode ( which locked / sync-ed the further flow of the code with the external event of an arrival of any plausibly formatted message, not returning earlier ), in:
val zmsg = ZMsg.recvMsg(subscriber) // which BLOCKS-till-a-1st-zmsg-arrived
both the further processed .pop()-ed parts just unload the components ( ref. the remark on actual ZMsg multi-part structure actually sent by the published-side, presented above )
Safety next :unlimited alloc-s v/s a mandatory blocking / dropping messages ?
the code surprised me on several points. Besides a rather very "late" call to the .connect()-method, compared to all the previous socket-archetype detailed settings ( that normally get arranged "after" a request to setup a connection ). While this may work fine, as intended, yet it exposes even tighter ( smaller ) time-window for the .Context()-instance to setup and (re-)negotiate all the relevant connection-details so as to become RTO.
One particular line attracted my attention: subscriber.setRcvHWM( 0 ) this is a version-archetype dependent trick. Yet, the value of zero causes an application to become vulnerable and I would not advise doing so in any production-grade application.