Search in sources :

Example 6 with Date

use of java.sql.Date in project hive by apache.

the class TestGenericUDFToUnixTimestamp method testDate.

public void testDate() throws HiveException {
    GenericUDFToUnixTimeStamp udf = new GenericUDFToUnixTimeStamp();
    ObjectInspector valueOI = PrimitiveObjectInspectorFactory.writableDateObjectInspector;
    ObjectInspector[] arguments = { valueOI };
    udf.initialize(arguments);
    Date date = Date.valueOf("1970-01-01");
    runAndVerify(udf, new DateWritable(date), new LongWritable(date.getTime() / 1000));
    // test null values
    runAndVerify(udf, null, null);
}
Also used : ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) DateWritable(org.apache.hadoop.hive.serde2.io.DateWritable) LongWritable(org.apache.hadoop.io.LongWritable) Date(java.sql.Date)

Example 7 with Date

use of java.sql.Date in project hive by apache.

the class PrimitiveObjectInspectorUtils method getDate.

public static Date getDate(Object o, PrimitiveObjectInspector oi) {
    if (o == null) {
        return null;
    }
    Date result = null;
    switch(oi.getPrimitiveCategory()) {
        case VOID:
            result = null;
            break;
        case STRING:
            StringObjectInspector soi = (StringObjectInspector) oi;
            String s = soi.getPrimitiveJavaObject(o).trim();
            try {
                result = Date.valueOf(s);
            } catch (IllegalArgumentException e) {
                result = null;
            }
            break;
        case CHAR:
        case VARCHAR:
            {
                try {
                    String val = getString(o, oi).trim();
                    result = Date.valueOf(val);
                } catch (IllegalArgumentException e) {
                    result = null;
                }
                break;
            }
        case DATE:
            result = ((DateObjectInspector) oi).getPrimitiveWritableObject(o).get();
            break;
        case TIMESTAMP:
            result = DateWritable.timeToDate(((TimestampObjectInspector) oi).getPrimitiveWritableObject(o).getSeconds());
            break;
        default:
            throw new RuntimeException("Cannot convert to Date from: " + oi.getTypeName());
    }
    return result;
}
Also used : Date(java.sql.Date)

Example 8 with Date

use of java.sql.Date in project hive by apache.

the class TestPrimitiveObjectInspectorUtils method testgetTimestampWithMillisecondsInt.

@Test
public void testgetTimestampWithMillisecondsInt() {
    DateFormat localDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
    DateFormat gmtDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
    gmtDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
    PrimitiveObjectInspector voidOI = PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(PrimitiveCategory.VOID);
    assertEquals(null, PrimitiveObjectInspectorUtils.getTimestamp(new Object(), voidOI));
    PrimitiveObjectInspector booleanOI = PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(PrimitiveCategory.BOOLEAN);
    assertEquals("1970-01-01 00:00:00.001", gmtDateFormat.format(PrimitiveObjectInspectorUtils.getTimestamp(true, booleanOI)));
    assertEquals("1970-01-01 00:00:00.000", gmtDateFormat.format(PrimitiveObjectInspectorUtils.getTimestamp(false, booleanOI)));
    PrimitiveObjectInspector byteOI = PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(PrimitiveCategory.BYTE);
    assertEquals("1970-01-01 00:00:00.001", gmtDateFormat.format(PrimitiveObjectInspectorUtils.getTimestamp((byte) 1, byteOI)));
    assertEquals("1969-12-31 23:59:59.999", gmtDateFormat.format(PrimitiveObjectInspectorUtils.getTimestamp((byte) -1, byteOI)));
    PrimitiveObjectInspector shortOI = PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(PrimitiveCategory.SHORT);
    assertEquals("1970-01-01 00:00:00.001", gmtDateFormat.format(PrimitiveObjectInspectorUtils.getTimestamp((short) 1, shortOI)));
    assertEquals("1969-12-31 23:59:59.999", gmtDateFormat.format(PrimitiveObjectInspectorUtils.getTimestamp((short) -1, shortOI)));
    PrimitiveObjectInspector intOI = PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(PrimitiveCategory.INT);
    assertEquals("1970-01-17 11:22:01.282", gmtDateFormat.format(PrimitiveObjectInspectorUtils.getTimestamp((int) 1423321282, intOI)));
    assertEquals("1969-12-31 23:59:59.999", gmtDateFormat.format(PrimitiveObjectInspectorUtils.getTimestamp((int) -1, intOI)));
    PrimitiveObjectInspector longOI = PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(PrimitiveCategory.LONG);
    assertEquals("1970-01-17 11:22:01.282", gmtDateFormat.format(PrimitiveObjectInspectorUtils.getTimestamp(1423321282L, longOI)));
    assertEquals("1969-12-31 23:59:59.999", gmtDateFormat.format(PrimitiveObjectInspectorUtils.getTimestamp(-1L, longOI)));
    // Float loses some precisions
    PrimitiveObjectInspector floatOI = PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(PrimitiveCategory.FLOAT);
    assertEquals("2015-02-07 15:02:24.000", gmtDateFormat.format(PrimitiveObjectInspectorUtils.getTimestamp(1423321282.123f, floatOI)));
    assertEquals("1969-12-31 23:59:58.876", gmtDateFormat.format(PrimitiveObjectInspectorUtils.getTimestamp(-1.123f, floatOI)));
    PrimitiveObjectInspector doubleOI = PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(PrimitiveCategory.DOUBLE);
    assertEquals("2015-02-07 15:01:22.123", gmtDateFormat.format(PrimitiveObjectInspectorUtils.getTimestamp((double) 1423321282.123, doubleOI)));
    assertEquals("1969-12-31 23:59:58.877", gmtDateFormat.format(PrimitiveObjectInspectorUtils.getTimestamp((double) -1.123, doubleOI)));
    PrimitiveObjectInspector decimalOI = PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(PrimitiveCategory.DECIMAL);
    assertEquals("2015-02-07 15:01:22.000", gmtDateFormat.format(PrimitiveObjectInspectorUtils.getTimestamp(HiveDecimal.create(1423321282L), decimalOI)));
    assertEquals("1969-12-31 23:59:59.000", gmtDateFormat.format(PrimitiveObjectInspectorUtils.getTimestamp(HiveDecimal.create(-1), decimalOI)));
    PrimitiveObjectInspector stringOI = PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(PrimitiveCategory.STRING);
    assertEquals("2015-02-07 15:01:22.123", localDateFormat.format(PrimitiveObjectInspectorUtils.getTimestamp("2015-02-07 15:01:22.123", stringOI)));
    PrimitiveObjectInspector charOI = PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(PrimitiveCategory.CHAR);
    assertEquals("2015-02-07 15:01:22.123", localDateFormat.format(PrimitiveObjectInspectorUtils.getTimestamp(new HiveChar("2015-02-07 15:01:22.123", 30), charOI)));
    PrimitiveObjectInspector varcharOI = PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(PrimitiveCategory.VARCHAR);
    assertEquals("2015-02-07 15:01:22.123", localDateFormat.format(PrimitiveObjectInspectorUtils.getTimestamp(new HiveVarchar("2015-02-07 15:01:22.123", 30), varcharOI)));
    PrimitiveObjectInspector dateOI = PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(PrimitiveCategory.DATE);
    assertEquals("2015-02-07 00:00:00.000", localDateFormat.format(PrimitiveObjectInspectorUtils.getTimestamp(new Date(1423321282123L), dateOI)));
    PrimitiveObjectInspector timestampOI = PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(PrimitiveCategory.TIMESTAMP);
    assertEquals("2015-02-07 15:01:22.123", gmtDateFormat.format(PrimitiveObjectInspectorUtils.getTimestamp(new Timestamp(1423321282123L), timestampOI)));
}
Also used : SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) HiveChar(org.apache.hadoop.hive.common.type.HiveChar) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) HiveVarchar(org.apache.hadoop.hive.common.type.HiveVarchar) SimpleDateFormat(java.text.SimpleDateFormat) Timestamp(java.sql.Timestamp) Date(java.sql.Date) Test(org.junit.Test)

Example 9 with Date

use of java.sql.Date in project storm by apache.

the class SimpleJdbcMapper method getColumns.

@Override
public List<Column> getColumns(ITuple tuple) {
    List<Column> columns = new ArrayList<Column>();
    for (Column column : schemaColumns) {
        String columnName = column.getColumnName();
        Integer columnSqlType = column.getSqlType();
        if (Util.getJavaType(columnSqlType).equals(String.class)) {
            String value = tuple.getStringByField(columnName);
            columns.add(new Column(columnName, value, columnSqlType));
        } else if (Util.getJavaType(columnSqlType).equals(Short.class)) {
            Short value = tuple.getShortByField(columnName);
            columns.add(new Column(columnName, value, columnSqlType));
        } else if (Util.getJavaType(columnSqlType).equals(Integer.class)) {
            Integer value = tuple.getIntegerByField(columnName);
            columns.add(new Column(columnName, value, columnSqlType));
        } else if (Util.getJavaType(columnSqlType).equals(Long.class)) {
            Long value = tuple.getLongByField(columnName);
            columns.add(new Column(columnName, value, columnSqlType));
        } else if (Util.getJavaType(columnSqlType).equals(Double.class)) {
            Double value = tuple.getDoubleByField(columnName);
            columns.add(new Column(columnName, value, columnSqlType));
        } else if (Util.getJavaType(columnSqlType).equals(Float.class)) {
            Float value = tuple.getFloatByField(columnName);
            columns.add(new Column(columnName, value, columnSqlType));
        } else if (Util.getJavaType(columnSqlType).equals(Boolean.class)) {
            Boolean value = tuple.getBooleanByField(columnName);
            columns.add(new Column(columnName, value, columnSqlType));
        } else if (Util.getJavaType(columnSqlType).equals(byte[].class)) {
            byte[] value = tuple.getBinaryByField(columnName);
            columns.add(new Column(columnName, value, columnSqlType));
        } else if (Util.getJavaType(columnSqlType).equals(Date.class)) {
            Long value = tuple.getLongByField(columnName);
            columns.add(new Column(columnName, new Date(value), columnSqlType));
        } else if (Util.getJavaType(columnSqlType).equals(Time.class)) {
            Long value = tuple.getLongByField(columnName);
            columns.add(new Column(columnName, new Time(value), columnSqlType));
        } else if (Util.getJavaType(columnSqlType).equals(Timestamp.class)) {
            Long value = tuple.getLongByField(columnName);
            columns.add(new Column(columnName, new Timestamp(value), columnSqlType));
        } else {
            throw new RuntimeException("Unsupported java type in tuple " + Util.getJavaType(columnSqlType));
        }
    }
    return columns;
}
Also used : ArrayList(java.util.ArrayList) Time(java.sql.Time) Timestamp(java.sql.Timestamp) Date(java.sql.Date) Column(org.apache.storm.jdbc.common.Column)

Example 10 with Date

use of java.sql.Date in project hive by apache.

the class TestVectorGenericDateExpressions method validateDate.

private void validateDate(VectorizedRowBatch batch, VectorExpression.Type colType, LongColumnVector date) {
    VectorExpression udf;
    if (colType == VectorExpression.Type.STRING) {
        udf = new VectorUDFDateString(0, 1);
    } else if (colType == VectorExpression.Type.TIMESTAMP) {
        udf = new VectorUDFDateTimestamp(0, 1);
    } else {
        udf = new VectorUDFDateLong(0, 1);
    }
    udf.setInputTypes(colType);
    udf.evaluate(batch);
    LongColumnVector output = (LongColumnVector) batch.cols[1];
    for (int i = 0; i < size; i++) {
        String actual;
        if (output.isNull[i]) {
            actual = null;
        } else {
            actual = new String(toString(output.vector[i]));
        }
        if (date.isNull[i]) {
            Assert.assertTrue(output.isNull[i]);
        } else {
            String expected = formatter.format(new Date(DateWritable.daysToMillis((int) date.vector[i])));
            Assert.assertEquals(expected, actual);
        }
    }
}
Also used : LongColumnVector(org.apache.hadoop.hive.ql.exec.vector.LongColumnVector) Date(java.sql.Date)

Aggregations

Date (java.sql.Date)631 Test (org.junit.Test)271 PreparedStatement (java.sql.PreparedStatement)123 ResultSet (java.sql.ResultSet)117 Connection (java.sql.Connection)102 Timestamp (java.sql.Timestamp)89 Money (org.mifos.framework.util.helpers.Money)83 Properties (java.util.Properties)60 ArrayList (java.util.ArrayList)59 SQLException (java.sql.SQLException)55 PDate (org.apache.phoenix.schema.types.PDate)55 ProductDefinitionException (org.mifos.accounts.productdefinition.exceptions.ProductDefinitionException)50 Time (java.sql.Time)42 LocalDate (java.time.LocalDate)39 MeetingBO (org.mifos.application.meeting.business.MeetingBO)39 Test (org.testng.annotations.Test)39 BigDecimal (java.math.BigDecimal)38 Calendar (java.util.Calendar)32 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)30 BaseTest (util.BaseTest)28