NamedParameterJdbcTemplate how return only one row? - spring-jdbc

I have select:
SELECT hz FROM my_tablw WHERE id=1
It return me 1 row and 1 column:
hz
some data
I have
#Autowired
private NamedParameterJdbcTemplate jdbcTemplate;
And I need make query and return 1 row - String some data.
I create
jdbcTemplate.query("SELECT hz FROM my_tablw WHERE id=:id", insertManagerParameters, (rs, rowNum) -> {
if (rs.next()) {
return rs.getString(1);
}
return "";
});
but this method return List<String>

I would use the queryForObject method, like:
jdbcTemplate.queryForObject("SELECT hz FROM my_tablw WHERE id=:id", insertManagerParameters, String.class);
Which should return a String with the query results.

MapSqlParameterSource paramSource = new MapSqlParameterSource();
paramSource.addValue("id", id);
return this.customNamedParameterJdbcTemplate.getNamedParameterJdbcTemplate().queryForObject(sqlConsultarPorId, paramSource,new MapeoReserva());

Related

UWP SQLite - Return null but data in table

I have datain my database (shown in the screen)
But the returned values are null? How is that possible, in the output I got this:
This is the code I'm using:
public static SQLiteConnection dbConnection = new SQLiteConnection("RPR_REKENSOFTWARE_DB.db");
public static List<String> GetParts()
{
var items = new List<String>();
try
{
string sSQL = #"SELECT * FROM parts;";
ISQLiteStatement dbState = dbConnection.Prepare(sSQL);
// Get the records
while (dbState.Step() == SQLiteResult.ROW)
{
// Say what it is.
string partNr = dbState[1] as string;
items.Add(partNr);
}
return items;
}
catch (Exception ex)
{
Debug.WriteLine(ex);
throw ex;
}
}
This is the database I'm using:
The reason is that you are converting the value from database to string, although it is actually an int. You should do:
string partNr = ( ( int )dbState[ 1 ] ).ToString();
Or more simply:
string partNr = dbState[ 1 ].ToString();
The reason is that the row returned from database is contains .NET equivalents of the DB column types and hence dbState[1] is an int. When you use as string on an int however, it cannot be cast and you get null.

Aggregation of records from Kinesis Stream every 1 min

I am trying to write a Flink program to process a Kinesis Stream. The Kinesis stream comes from AWS DynamoDB stream and represents inserts made in DynamoDB table.
Each record in the Stream can contain multiple insert records. The number of insert records can be variable ( can vary from 1 to 10)
I want to group all the insert records from all the streams within a interval of 1 min and sum the impression count (impressionCount) field
[
{
"country":"NL",
"userOS":"mac",
"createdOn":"2017-08-02 16:22:17.135600",
"trafficType":"D",
"affiliateId":"87",
"placement":"4",
"offerId":"999",
"advertiserId":"139",
"impressionCount":"1",
"uniqueOfferCount":"0"
},
{
"country":"NL",
"userOS":"mac",
"createdOn":"2017-08-02 16:22:17.135600",
"trafficType":"D",
"affiliateId":"85",
"placement":"4",
"offerId":"688",
"advertiserId":"139",
"impressionCount":"1",
"uniqueOfferCount":"0"
}
]
My code:
DataStream<List> kinesisStream = env.addSource(new FlinkKinesisConsumer<>(
"Impressions-Stream", new RawImpressionLogSchema(), consumerConfig));
/** CLASS: RawImpressionLogSchema **/
public class RawImpressionLogSchema implements DeserializationSchema<List> {
#Override
public List<RawImpressionLogRecord> deserialize(byte[] bytes) {
return RawImpressionLogRecord.parseImpressionLog(bytes);
}
#Override
public boolean isEndOfStream(List event) {
return false;
}
#Override
public TypeInformation<List> getProducedType() {
return TypeExtractor.getForClass(List.class);
}
}
/** parse Method **/
public static List<RawImpressionLogRecord> parseImpressionLog(
byte[] impressionLogBytes) {
JsonReader jsonReader = new JsonReader(new InputStreamReader(
new ByteArrayInputStream(impressionLogBytes)));
JsonElement jsonElement = Streams.parse(jsonReader);
if (jsonElement == null) {
throw new IllegalArgumentException(
"Event does not define a eventName field: "
+ new String(impressionLogBytes));
} else {
Type listType = new TypeToken<ArrayList<RawImpressionLogRecord>>(){}.getType();
return gson.fromJson(jsonElement, listType);
}
}
I was able to parse the input and create the kinesisStream. Wanted to know is it the correct way ? and how do I achieve the aggregation.
Also once I have the DataStream, how can I apply the map/filter/group by function on List Stream.
I am new to Flink and any help would be appreciated.
Update
Tried to come with the below code to solve the above use case. But somehow the reduce function is not getting called. Any idea what is wrong in the below code ?
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime);
DataStream<List<ImpressionLogRecord>> rawRecords = env.addSource(new ImpressionLogDataSourceFunction("C:\\LogFiles\\input.txt"));
DataStream<ImpressionLogRecord> impressionLogDataStream = rawRecords
.flatMap(new Splitter())
.assignTimestampsAndWatermarks(
new BoundedOutOfOrdernessTimestampExtractor<ImpressionLogRecord>(Time.seconds(5)) {
#Override
public long extractTimestamp(
ImpressionLogRecord element) {
return element.getCreatedOn().atZone(ZoneOffset.systemDefault()).toInstant().toEpochMilli();
}
}
);
//impressionLogDataStream.print();
KeyedStream<ImpressionLogRecord, String> keyedImpressionLogDataStream = impressionLogDataStream
.keyBy(impressionLogRecordForKey -> {
StringBuffer groupByKey = new StringBuffer();
groupByKey.append(impressionLogRecordForKey.getCreatedOn().toString().substring(0, 16));
groupByKey.append("_");
groupByKey.append(impressionLogRecordForKey.getOfferId());
groupByKey.append("_");
groupByKey.append(impressionLogRecordForKey.getAdvertiserId());
groupByKey.append("_");
groupByKey.append(impressionLogRecordForKey.getAffiliateId());
groupByKey.append("_");
groupByKey.append(impressionLogRecordForKey.getCountry());
groupByKey.append("_");
groupByKey.append(impressionLogRecordForKey.getPlacement());
groupByKey.append("_");
groupByKey.append(impressionLogRecordForKey.getTrafficType());
groupByKey.append("_");
groupByKey.append(impressionLogRecordForKey.getUserOS());
System.out.println("Call to Group By Function===================" + groupByKey);
return groupByKey.toString();
});
//keyedImpressionLogDataStream.print();
DataStream<ImpressionLogRecord> aggImpressionRecord = keyedImpressionLogDataStream
.timeWindow(Time.minutes(5))
.reduce((prevLogRecord, currentLogRecord) -> {
System.out.println("Calling Reduce Function-------------------------");
ImpressionLogRecord aggregatedImpressionLog = new ImpressionLogRecord();
aggregatedImpressionLog.setOfferId(prevLogRecord.getOfferId());
aggregatedImpressionLog.setCreatedOn(prevLogRecord.getCreatedOn().truncatedTo(ChronoUnit.MINUTES));
aggregatedImpressionLog.setAdvertiserId(prevLogRecord.getAdvertiserId());
aggregatedImpressionLog.setAffiliateId(prevLogRecord.getAffiliateId());
aggregatedImpressionLog.setCountry(prevLogRecord.getCountry());
aggregatedImpressionLog.setPlacement(prevLogRecord.getPlacement());
aggregatedImpressionLog.setTrafficType(prevLogRecord.getTrafficType());
aggregatedImpressionLog.setUserOS(prevLogRecord.getUserOS());
aggregatedImpressionLog.setImpressionCount(prevLogRecord.getImpressionCount() + currentLogRecord.getImpressionCount());
aggregatedImpressionLog.setUniqueOfferCount(prevLogRecord.getUniqueOfferCount() + currentLogRecord.getUniqueOfferCount());
return aggregatedImpressionLog;
});
aggImpressionRecord.print();
Working Code
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime);
DataStream<List<ImpressionLogRecord>> rawRecords = env.addSource(new ImpressionLogDataSourceFunction("C:\\LogFiles\\input.txt"));
//This method converts the DataStream of List<ImpressionLogRecords> into a single stream of ImpressionLogRecords.
//Also assigns timestamp to each record in the stream
DataStream<ImpressionLogRecord> impressionLogDataStream = rawRecords
.flatMap(new RecordSplitter())
.assignTimestampsAndWatermarks(
new BoundedOutOfOrdernessTimestampExtractor<ImpressionLogRecord>(Time.seconds(5)) {
#Override
public long extractTimestamp(
ImpressionLogRecord element) {
return element.getCreatedOn().atZone(ZoneOffset.systemDefault()).toInstant().toEpochMilli();
}
}
);
//This method groups the records in the stream by a user defined key.
KeyedStream<ImpressionLogRecord, String> keyedImpressionLogDataStream = impressionLogDataStream
.keyBy(impressionLogRecordForKey -> {
StringBuffer groupByKey = new StringBuffer();
groupByKey.append(impressionLogRecordForKey.getCreatedOn().toString().substring(0, 16));
groupByKey.append("_");
groupByKey.append(impressionLogRecordForKey.getOfferId());
groupByKey.append("_");
groupByKey.append(impressionLogRecordForKey.getAdvertiserId());
groupByKey.append("_");
groupByKey.append(impressionLogRecordForKey.getAffiliateId());
groupByKey.append("_");
groupByKey.append(impressionLogRecordForKey.getCountry());
groupByKey.append("_");
groupByKey.append(impressionLogRecordForKey.getPlacement());
groupByKey.append("_");
groupByKey.append(impressionLogRecordForKey.getTrafficType());
groupByKey.append("_");
groupByKey.append(impressionLogRecordForKey.getUserOS());
return groupByKey.toString();
});
//This method aggregates the grouped records every 1 min and calculates the sum of impression count and unique offer count.
DataStream<ImpressionLogRecord> aggImpressionRecord = keyedImpressionLogDataStream
.timeWindow(Time.minutes(1))
.reduce((prevLogRecord, currentLogRecord) -> {
ImpressionLogRecord aggregatedImpressionLog = new ImpressionLogRecord();
aggregatedImpressionLog.setOfferId(prevLogRecord.getOfferId());
aggregatedImpressionLog.setCreatedOn(prevLogRecord.getCreatedOn().truncatedTo(ChronoUnit.MINUTES));
aggregatedImpressionLog.setAdvertiserId(prevLogRecord.getAdvertiserId());
aggregatedImpressionLog.setAffiliateId(prevLogRecord.getAffiliateId());
aggregatedImpressionLog.setCountry(prevLogRecord.getCountry());
aggregatedImpressionLog.setPlacement(prevLogRecord.getPlacement());
aggregatedImpressionLog.setTrafficType(prevLogRecord.getTrafficType());
aggregatedImpressionLog.setUserOS(prevLogRecord.getUserOS());
aggregatedImpressionLog.setImpressionCount(prevLogRecord.getImpressionCount() + currentLogRecord.getImpressionCount());
aggregatedImpressionLog.setUniqueOfferCount(prevLogRecord.getUniqueOfferCount() + currentLogRecord.getUniqueOfferCount());
return aggregatedImpressionLog;
});
aggImpressionRecord.print();
aggImpressionRecord.addSink(new ImpressionLogDataSink());
env.execute();
}
public static class RecordSplitter
implements
FlatMapFunction<List<ImpressionLogRecord>, ImpressionLogRecord> {
#Override
public void flatMap(List<ImpressionLogRecord> rawImpressionRecords,
Collector<ImpressionLogRecord> impressionLogRecordCollector)
throws Exception {
for (int i = 0; i < rawImpressionRecords.size(); i++) {
impressionLogRecordCollector.collect(rawImpressionRecords.get(i));
}
}
}`enter code here`

Loop in string and get word between 2 character and save to list

we have string example :
www.example.com/default.aspx?code-1/price-2/code-4/
i want to get integers from code and price and save to list of integers.
for example , 1 and 4 are codes , 2 is price for filter in site.
InBetween = GetStringInBetween("Brand-", "/", Example, false, false);
please help me.
Below is a simple program that completes your requirement.
class Program
{
public void GetCodesAndPrice(string url,out List<int> listOfCodes, out List<int> listOfPrice )
{
listOfCodes=new List<int>();
listOfPrice = new List<int>();
url = url.Substring(url.IndexOf('?')+1);
var strArray = url.Split('/');
foreach (string s in strArray)
{
if(s.ToLower().Contains("code"))
listOfCodes.Add(GetIntValue(s));
else if(s.ToLower().Contains("price"))
listOfPrice.Add(GetIntValue(s));
}
// Now you have list of price in "listOfPrice" and codes in "listOfCodes",
// If you want to return these two list then declare as out
}
public int GetIntValue(string str)
{
try
{
return Convert.ToInt32(str.Substring(str.IndexOf('-') + 1));
}
catch (Exception ex)
{
// Handle your exception over here
}
return 0; // It depends on you what do you want to return if exception occurs in this function
}
public static void Main()
{
var prog = new Program();
List<int> listOfCodes;
List<int> listOfPrice;
prog.GetCodesAndPrice("www.example.com/default.aspx?code-1/price-2/code-4/", out listOfCodes,out listOfPrice);
Console.ReadKey();
}
}
It is complete console program. Test it and Embed in your program. Hope this will help you

Asp.Net MVC3, Update query in Linq

I'd like to know how to run this query in Linq way.
UPDATE orders SET shipDate = '6/15/2012' WHERE orderId IN ('123123','4986948','23947439')
My Codes,
[HttpGet]
public void test()
{
EFOrdersRepository ordersRepository = new EFOrdersRepository();
var query = ordersRepository.Orders;
// How to run this query in LINQ
// Query : UPDATE orders SET shipDate = '6/15/2012' WHERE orderId IN ('123123','4986948','23947439')
}
EFOrdersRepository.cs
public class EFOrdersRepository
{
private EFMysqlContext context = new EFMysqlContext();
public IQueryable<Order> Orders
{
get { return context.orders; }
}
}
EFMysqlContext.cs
class EFMysqlContext : DbContext
{
public DbSet<Order> orders { get; set; }
}
Actually it's pretty easy check the following code
EFOrdersRepository db = new EFOrdersRepository();
int[] ids= new string[] { "123123", "4986948", "23947439"};
//this linq give's the orders with the numbers
List<Order> orders = db.Order().ToList()
.Where( x => ids.Contains(x.orderId.Contains));
foreach(var order in orders)
{
order.ShipDate = '06/15/2012';
db.Entry(usuario).State = EntityState.Modified;
}
db.SaveChanges();
Something like this should work (warning Pseudo code ahead!!)
EDIT I like using the Jorge's method of retrieving the orders better (using contains), but leaving this here as another alternative. The statements below the code sample still hold true however.
[HttpGet]
public void test()
{
EFOrdersRepository ordersRepository = new EFOrdersRepository();
var query = ordersRepository.Orders.Where(x=>x.orderId == '123123' ||
x.orderId == '4986948' || x.orderId = '23947439').ToList();
foreach(var order in query){
var localOrder = order;
order.ShipDate = '06/15/2012';
}
ordersRepository.SaveChanges();
}
Basically, LINQ does not do 'bulk updates' well. You either have to fetch and loop through your orders or write a stored procedure that can take an array of ids and bulk update them that way. If you are only doing a few at a time, the above will work ok. If you have tons of orders that need to be updated, the ORM probably will not be the best choice. I look forward to see if anyone else has a better approach.
Disclaimer: the var localOrder = order line is to ensure that there are no modified closure issues. Also, ReSharper and other tools may have a less verbose way of writing the above.
Note: You need to call SaveChanges from your DBContext at the end
Short answer:
var f = new[] { 123123, 4986948, 23947439 };
var matchingOrders = orders.Where(x => f.Contains(x.ID)).ToList();
matchingOrders.ForEach(x => x.ShipDate = newDate);
Complete test:
// new date value
var newDate = new DateTime(2012, 6, 15);
// id's
var f = new[] { 123123, 4986948, 23947439 };
// simpulating the orders from the db
var orders = Builder<Order2>.CreateListOfSize(10).Build().ToList();
orders.Add(new Order2 { ID = 123123 });
orders.Add(new Order2 { ID = 4986948 });
orders.Add(new Order2 { ID = 23947439 });
// selecting only the matching orders
var matchingOrders = orders.Where(x => f.Contains(x.ID)).ToList();
matchingOrders.ForEach(x => Console.WriteLine("ID: " + x.ID + " Date: " + x.ShipDate.ToShortDateString()));
// setting the new value to all the results
matchingOrders.ForEach(x => x.ShipDate = newDate);
matchingOrders.ForEach(x => Console.WriteLine("ID: " + x.ID + " Date: " + x.ShipDate.ToShortDateString()));
Output:
ID: 123123 Date: 1/1/0001
ID: 4986948 Date: 1/1/0001
ID: 23947439 Date: 1/1/0001
ID: 123123 Date: 6/15/2012
ID: 4986948 Date: 6/15/2012
ID: 23947439 Date: 6/15/2012
In ORMs, You have to fetch the record first make the change to the record then save it back. To do that, I will add an UpdateOrder method to my Repositary like this
public bool UpdateOrder(Order order)
{
int result=false;
int n=0;
context.Orders.Attach(order);
context.Entry(order).State=EntityState.Modified;
try
{
n=context.SaveChanges();
result=true;
}
catch (DbUpdateConcurrencyException ex)
{
ex.Entries.Single().Reload();
n= context.SaveChanges();
result= true;
}
catch (Exception ex2)
{
//log error or propogate to parent
}
return result;
}
And i will call it from my Action method like this
int orderId=123232;
var orders=ordersRepository.Orders.Where(x=> x.orderId.Contains(orderId)).ToList();
if(orders!=null)
{
foreach(var order in orders)
{
order.ShipDate=DateTime.Parse('12/12/2012);
var result= ordersRepository.UpdateOrder();
}
}
In this Approach, if you have to update many number of records, you are executing thatn many number of update statement to the database. In this purticular case, i would like to execute the Raw SQL statement with only one query using the Database.SqlQuery method
string yourQry="UPDATE orders SET shipDate = '6/15/2012'
WHERE orderId IN ('123123','4986948','23947439')";
var reslt=context.Database.SqlQuery<int>(yourQry);

How to create auto generate id for ListOf rows returned

I am accessing list of data as shown below.
var result = (from Pages in PagesList.Items.OfType<SPListItem>()
select new ListImages
{
desc = Convert.ToString(Pages["Description"])
}).ToList();
What I want is to auto generate customized increamental id for the no of rows generated.
ex, slide-img-1, slide-img-2 etc.
public class ListImages
{
string _desc;
string _id;
public string id
{
get
{
if (_id != null)
return _id;
else
return string.Empty;
}
set { _id = value; }
}
public string desc
{
get
{
if (_desc != null)
return _desc;
else
return string.Empty;
}
set { _desc = value; }
}
}
Thanks,
Ashish
I don't know if it can be achieved with the Linq specific syntax, but writing your query like this, you could use the .Select() method that provides an index:
var result = PagesList.Items.OfType<SPListItem>()
.Select((page, index) => new ListImages
{
desc = Convert.ToString(page["Description"]),
id = String.Concat("slide-img-", index + 1)
})
.ToList();

Resources