Insert DateTime into BigQuery in Stream - java

I am trying to insert data into a datetime field in BigQuery via WriteStream in Java and keep getting the error
java.util.concurrent.ExecutionException: io.grpc.StatusRuntimeException: INVALID_ARGUMENT: Cannot return an invalid datetime value of 1639484762470 microseconds relative to the Unix epoch. The range of valid datetime values is [0001-01-01 00:00:00, 9999-12-31 23:59:59.999999] on field date_time.
I tried different options: seconds, milliseconds, microseconds, cast from Unix format to Microsoft, insert formatted string (then I get an error that it should be INT64).
Where am I going wrong?
Code is pretty simple
try (JsonStreamWriter writer = JsonStreamWriter.newBuilder(writeStream.getName(), writeStream.getTableSchema(), client).build()) {
for (int i = 0; i < 2; i++) {
JSONArray jsonArr = new JSONArray();
for (int j = 0; j < 10; j++) {
JSONObject record = new JSONObject();
Date date = new Date();
record.put("date_hash", date.hashCode());
record.put("date_string", date.toString());
record.put("date_time", date.getTime());
jsonArr.put(record);
}
ApiFuture<AppendRowsResponse> future = writer.append(jsonArr, i * 10);
AppendRowsResponse response = future.get();
}
}

Related

Problem with Jackson ArrayNode.add Java function

I have a function that should change some values on Object that have some objects of array.
This Object is:
{"avg":{"00:00":[],"01:00":[],"02:00":[],"03:00":[],"04:00":[],"05:00":[],"06:00":[],"07:00":[],"08:00":[],"09:00":[],"10:00":[],"11:00":[],"12:00":[],"13:00":[],"14:00":[],"15:00":[],"16:00":[],"17:00":[],"18:00":[],"19:00":[],"20:00":[],"21:00":[],"22:00":[],"23:00":[]},"avg_hum":{"00:00":[],"01:00":[],"02:00":[],"03:00":[],"04:00":[],"05:00":[],"06:00":[],"07:00":[],"08:00":[],"09:00":[],"10:00":[],"11:00":[],"12:00":[],"13:00":[],"14:00":[],"15:00":[],"16:00":[],"17:00":[],"18:00":[],"19:00":[],"20:00":[],"21:00":[],"22:00":[],"23:00":[]},"max":{"00:00":[],"01:00":[],"02:00":[],"03:00":[],"04:00":[],"05:00":[],"06:00":[],"07:00":[],"08:00":[],"09:00":[],"10:00":[],"11:00":[],"12:00":[],"13:00":[],"14:00":[],"15:00":[],"16:00":[],"17:00":[],"18:00":[],"19:00":[],"20:00":[],"21:00":[],"22:00":[],"23:00":[]},"min":{"00:00":[],"01:00":[],"02:00":[],"03:00":[],"04:00":[],"05:00":[],"06:00":[],"07:00":[],"08:00":[],"09:00":[],"10:00":[],"11:00":[],"12:00":[],"13:00":[],"14:00":[],"15:00":[],"16:00":[],"17:00":[],"18:00":[],"19:00":[],"20:00":[],"21:00":[],"22:00":[],"23:00":[]},"max_hum":{"00:00":[],"01:00":[],"02:00":[],"03:00":[],"04:00":[],"05:00":[],"06:00":[],"07:00":[],"08:00":[],"09:00":[],"10:00":[],"11:00":[],"12:00":[],"13:00":[],"14:00":[],"15:00":[],"16:00":[],"17:00":[],"18:00":[],"19:00":[],"20:00":[],"21:00":[],"22:00":[],"23:00":[]},"min_hum":{"00:00":[],"01:00":[],"02:00":[],"03:00":[],"04:00":[],"05:00":[],"06:00":[],"07:00":[],"08:00":[],"09:00":[],"10:00":[],"11:00":[],"12:00":[],"13:00":[],"14:00":[],"15:00":[],"16:00":[],"17:00":[],"18:00":[],"19:00":[],"20:00":[],"21:00":[],"22:00":[],"23:00":[]},"beginDate":"","endDate":""}
And I need to add values to arrays (like: {"00:00": [25],...})
So, I tried something like ("data_raw" is the received object):
ArrayNode array = mapper.createArrayNode();
array = (ArrayNode) data_raw.get("avg").get("00:00");
array.add((double) 25.11666711171468);
System.out.println("data_raw: " + data_raw.toString());
And the result ("data_raw" object) is:
data_raw: {"avg":{"00:00":[25.11666711171468],"01:00":[],"02:00":[],"03:00":[],"04:00":[],"05:00":[],"06:00":[],"07:00":[],"08:00":[],"09:00":[],"10:00":[],"11:00":[],"12:00":[],"13:00":[],"14:00":[],"15:00":[],"16:00":[],"17:00":[],"18:00":[],"19:00":[],"20:00":[],"21:00":[],"22:00":[],"23:00":[]},"avg_hum":{"00:00":[25.11666711171468],"01:00":[],"02:00":[],"03:00":[],"04:00":[],"05:00":[],"06:00":[],"07:00":[],"08:00":[],"09:00":[],"10:00":[],"11:00":[],"12:00":[],"13:00":[],"14:00":[],"15:00":[],"16:00":[],"17:00":[],"18:00":[],"19:00":[],"20:00":[],"21:00":[],"22:00":[],"23:00":[]},"max":{"00:00":[25.11666711171468],"01:00":[],"02:00":[],"03:00":[],"04:00":[],"05:00":[],"06:00":[],"07:00":[],"08:00":[],"09:00":[],"10:00":[],"11:00":[],"12:00":[],"13:00":[],"14:00":[],"15:00":[],"16:00":[],"17:00":[],"18:00":[],"19:00":[],"20:00":[],"21:00":[],"22:00":[],"23:00":[]},"min":{"00:00":[25.11666711171468],"01:00":[],"02:00":[],"03:00":[],"04:00":[],"05:00":[],"06:00":[],"07:00":[],"08:00":[],"09:00":[],"10:00":[],"11:00":[],"12:00":[],"13:00":[],"14:00":[],"15:00":[],"16:00":[],"17:00":[],"18:00":[],"19:00":[],"20:00":[],"21:00":[],"22:00":[],"23:00":[]},"max_hum":{"00:00":[25.11666711171468],"01:00":[],"02:00":[],"03:00":[],"04:00":[],"05:00":[],"06:00":[],"07:00":[],"08:00":[],"09:00":[],"10:00":[],"11:00":[],"12:00":[],"13:00":[],"14:00":[],"15:00":[],"16:00":[],"17:00":[],"18:00":[],"19:00":[],"20:00":[],"21:00":[],"22:00":[],"23:00":[]},"min_hum":{"00:00":[25.11666711171468],"01:00":[],"02:00":[],"03:00":[],"04:00":[],"05:00":[],"06:00":[],"07:00":[],"08:00":[],"09:00":[],"10:00":[],"11:00":[],"12:00":[],"13:00":[],"14:00":[],"15:00":[],"16:00":[],"17:00":[],"18:00":[],"19:00":[],"20:00":[],"21:00":[],"22:00":[],"23:00":[]},"beginDate":"","endDate":""}
I expect that just "avg":{"00:00":[X],...} be changed, but all objects with "00:00" name are being replaced regardless of the selected "primary field": "avg".
Can someone help me?
EDIT:
Complete related code:
String[] name_db_data = {"avg", "avg_hum", "max", "min", "max_hum", "min_hum"};
SimpleDateFormat hourFormatted = new SimpleDateFormat("HH:mm");
reType = reportType.detailedDailyReport;
ResultSet rs = stm.executeQuery(query); // The String "query" is a dynamic MySQL query.
while(rs.next())
{
if(reType == reportType.detailedDailyReport)
{
int count4 = 0;
Calendar c = Calendar.getInstance();
c.setTime(rs.getTime("hour"));
for(count4 = 0; count4 < name_db_data.length; count4 ++)
{
ArrayNode array_aux2 = mapper.createArrayNode();
array_aux2 = (ArrayNode) data_raw.get(name_db_data[count4]).get(hourFormatted.format(c.getTime()));
array_aux2.add(rs.getDouble(name_db_data[count4]));
}
}

How to set current time in swift?

I am trying to convert pure java code into swift.But,I am having a little problem with setting the current time.I can get the current time in milliseconds.But I cant set the current time.Any help with that?
Here is my code
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
let now = NSDate()
var auctionDate = [String:String]()
let dateFormatter = NSDateFormatter()
dateFormatter.locale = NSLocale(localeIdentifier: "en_US")
var sdfDDMMYYYYEEE : NSDateFormatter!
sdfDDMMYYYYEEE.dateFormat = "dd/MM/yyyy (EEE)"
var sdfDDMMYYYY : NSDateFormatter!
sdfDDMMYYYY.dateFormat = "dd/MM/yyyy"
var sdfEEE : NSDateFormatter!
sdfEEE.dateFormat = "EEE"
// This is how i get the current time
println((now.timeIntervalSince1970)*1000 + 86400000.00)
for var i = 0; i < 5; i++ {
if sdfEEE.stringFromDate(now) == "Sun" {
// This is where i have to set my time with that ((now.timeIntervalSince1970)*1000 + 86400000.00)
i--;
continue;
}
auctionDate[(sdfDDMMYYYY.stringFromDate(now) as String)] = (sdfDDMMYYYYEEE.stringFromDate(now) as String)
// Here too I have to set my time
}
println(dateFormatter.stringFromDate(now))
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Here is the java code that I want to convert into swift code
import java.text.*;
import java.util.*;
public class DateTest{
Date now = new Date();
Map<String, String> myDate = new LinkedHashMap<String, String>();
myDate.put("", "All");
SimpleDateFormat sdfDDMMYYYYEEE = new SimpleDateFormat("dd/MM/yyyy (EEE)");
SimpleDateFormat sdfDDMMYYYY = new SimpleDateFormat("dd/MM/yyyy");
SimpleDateFormat sdfEEE = new SimpleDateFormat("EEE");
for (int i = 0; i < 5; i++) {
if (sdfEEE.format(now).equals("Sun")) {
now.setTime(now.getTime() + 86400000);
i--;
continue;
}
myDate.put(sdfDDMMYYYY.format(now), sdfDDMMYYYYEEE.format(now));
now.setTime(now.getTime() + 86400000);
}
}
Any help with setting the current time in swift.I am almost closing to my answer.
In your code you want to add seconds to time eince 1970. You're in luck, there is an initializer for that: NSDate(timeIntervalSince1970: NSTimeInterval)
let now = NSDate(timeIntervalSince1970: 86400000)
This will create a new NSDate and add 86400000 seconds to the time in seconds right now since 1970.

Getting exception while using NotesCalendar.getNewInvitations

I am getting exception while invoking NotesCalendar.getNewInvitations method.
NotesException: NotesCalendar error: The database handle is NULL
at lotus.domino.local.NotesCalendar.NgetNewInvitations(Native Method)
at lotus.domino.local.NotesCalendar.getNewInvitations(Unknown Source)
at JavaAgent.main(Unknown Source)
I am trying to run this code locally to Domino Server.
import lotus.domino.*;
public class JavaAgent extends NotesThread {
public static void main(String []args){
try
{
NotesThread.sinitThread(); // start thread
Session session = NotesFactory.createSession();
System.out.println("session="+session);
// (Your code goes here)
DbDirectory dbdir = session.getDbDirectory("");
System.out.println("dbdir="+dbdir);
Database db1= session.getDatabase("server", "Conf");
NotesCalendar cal = session.getCalendar(db1);
java.util.Calendar jdt = java.util.Calendar.getInstance();
jdt.set(2015, 1, 1, 1, 1, 1);
DateTime dt1 = session.createDateTime(jdt);
DateTime dt2 = session.createDateTime("Yesterday 02");
// java.util.Vector invites = cal.getNewInvitations(dt1, dt2);
// System.out.println("invites "+invites.size());
java.util.Vector invites= cal.getEntries(dt1, dt2);
for (int j = 0; j < 3; j++) {
// Create document to post results
Document doc = db1.createDocument();
doc.appendItemValue("Form", "main");
doc.appendItemValue("subject", "New invitations");
RichTextItem body = doc.createRichTextItem("body");
if (invites.size() == 0) body.appendText("No invitations");
else {
for (int i = 0; i < invites.size(); i++) {
NotesCalendarNotice cale = (NotesCalendarNotice)invites.elementAt(i);
body.appendText(cale.read());
cale.recycle();
body.addNewLine();
}
}
doc.save(true, true);
java.util.concurrent.TimeUnit.MINUTES.sleep(30);
invites = cal.getNewInvitations(dt1, cal.getUntilTime());
} }
catch(Exception e)
{
e.printStackTrace();
}
finally
{
NotesThread.stermThread(); // must terminate every thread
}
}
}
The simple answer is getNewInvitations() doesn't make sense in the context of a resource reservation database. The method was designed to read unprocessed invitations from a mail file.
However, in the comments you say your code doesn't work for mail files either. It might be helpful to get something simpler to work and then build on that. I just tried the following code:
database = session.getDatabase(null, "mail/user.nsf");
NotesCalendar calendar = session.getCalendar(database);
// Start date is 01-Jan-2015
java.util.Calendar start = java.util.Calendar.getInstance();
start.set(2015, 0, 1);
dtStart = session.createDateTime(start);
// End date is now
java.util.Calendar end = java.util.Calendar.getInstance();
dtEnd = session.createDateTime(end);
// Get entries on the calendar between start and end
Vector<?> entries = calendar.getEntries(dtStart, dtEnd);
System.out.println("Number of entries is " + entries.size());
// Get invitations from start date (from the Inbox)
Vector<?> invites = calendar.getNewInvitations(dtStart, calendar.getUntilTime());
System.out.println("Number of invites is " + invites.size());
Everything worked as expected. Perhaps you could try the above code and see if getNewInvitations() still throws an exception.

setting a format for a date in ms access

I'm trying to format a date formatted result from a database into date only. ("dd"). The query results are formatted like this, 2014-05-17 00:00:00 I want to extract only "17", How can I achieved this? This is my code.
String query = "SELECT DISTINCT date FROM Attendance;";
Object[][] queryResult = connectToDB(headerQuery);
for(int x = 0; x < queryResult.length; x++){
for(int y=0; y < queryResult[x].length; y++){
Object temp = queryResult[x][y];
SimpleDateFormat format = new SimpleDateFormat("dd");
System.out.print(format.format(temp));
System.out.println(temp+ "<-----");
}
System.out.println("---");
}
this is my error
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Date
UPDATE: I've changed the for-loop into this:
for(int x = 0; x < queryResult.length; x++){
for(int y=0; y < queryResult[x].length; y++){
String temp = (queryResult[x][y]).toString();
SimpleDateFormat format = new SimpleDateFormat("dd");
Date date = (Date) format.parse(temp);
System.out.println(date+ "<-----");
}
System.out.println("---");
}
but it still does not format the date;
OK so you're trying to format a String. That is, temp is a String and you can't pass that into format. You need to turn that String into a Date then pass it into format. Here's how.
But, even better, you should be returning a Date from the query instead of a String. That'll make your life much easier. Then you don't need to change any other part of the code.
Show us your connectToDB. Assuming it's using JDBC, you should get the result by calling ResultSet.getDate
Why not use SQL to extract the day part?
SELECT DatePart('d', [date]) as theDay FROM Attendance

Array and date format issue

I am a beginer in jquery and java.I have two string variables and a string array.I passed these 3 to a java servlet using a post request from javascript.Below is the code:
var stDate= $('#drpstart').jqxDateTimeInput('getText');
var edDate= $('#drpend').jqxDateTimeInput('getText');
var items = $("#target").jqxListBox('getItems');
var itemsid=[];
$("#jqxWidget").html('');
for (var i = 0; i < items.length; i++) {
itemsid[i]=items[i].value;
}
$.post('ActionItem',
{startdt:stDate,enddt:edDate,item:itemsid},function(responseJson) {
});
Then I access these variables and array at servlet side:
String starts=request.getParameter("startdt");
String ends=request.getParameter("enddt");
String[] myArray =request.getParameterValues("item");
if (myArray != null ) {
for (int i = 0; i < myArray.length; i++){
System.out.println(myArray[i]);
}
}
else {
System.out.println("null value");
}
but the myarray is geting a null value.How to get the values correctly to myarray?
Also my database table StockRegisterHeader contain field 'Date' with Date as datatype,so i converted the string variables starts and ends to Date format as below
SimpleDateFormat from = new SimpleDateFormat("dd/MM/yyyy");
SimpleDateFormat to = new SimpleDateFormat("yyyy-MM-dd");
Date startg = from.parse(starts); // 01/02/2014
Date endg = from.parse(ends);
Now I need to select data from 4 tables within this date range. I wrote the query as below:
String ss="SELECT Items.ParentID,Items.Name ,StockRegisterHeader.MovementType,
StockRegisterHeader.Date, UOM.Name,StockRegisterDetails.Quantity,
StockRegisterDetails.Rate from Items,StockRegisterHeader,UOM,StockRegisterDetails
where Items.ID = StockRegisterDetails.ItemID and
StockRegisterDetails.StockRegisterHeaderID = StockRegisterHeader.Id and
StockRegisterDetails.UOMID = UOM.ID and StockRegisterHeader.Date between '"+startg+"'
and '"+endg+"'";
rs=stmt.executeQuery(ss);
But the resultset object rs is showing null even if the database contain the requested data.I think this may be due to the date format. How to solve these two issues? I am using mysql database. Please help me.
try changing
String[] myArray =request.getParameterValues("item");
to
String[] myArray =request.getParameterValues("item[]"); //brackets must be specified to retrive array value

Categories

Resources