Search in sources :

Example 26 with SimpleDateFormat

use of java.text.SimpleDateFormat in project hadoop by apache.

the class StatePool method initialize.

/**
   * Initialized the {@link StatePool}. This API also reloads the previously
   * persisted state. Note that the {@link StatePool} should be initialized only
   * once.
   */
public void initialize(Configuration conf) throws Exception {
    if (isInitialized) {
        throw new RuntimeException("StatePool is already initialized!");
    }
    this.conf = conf;
    String persistDir = conf.get(DIR_CONFIG);
    reload = conf.getBoolean(RELOAD_CONFIG, false);
    persist = conf.getBoolean(PERSIST_CONFIG, false);
    // reload if configured
    if (reload || persist) {
        System.out.println("State Manager initializing. State directory : " + persistDir);
        System.out.println("Reload:" + reload + " Persist:" + persist);
        if (persistDir == null) {
            throw new RuntimeException("No state persist directory configured!" + " Disable persistence.");
        } else {
            this.persistDirPath = new Path(persistDir);
        }
    } else {
        System.out.println("State Manager disabled.");
    }
    // reload
    reload();
    // now set the timestamp
    DateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy-hh'H'-mm'M'-ss'S'");
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    timeStamp = formatter.format(calendar.getTime());
    isInitialized = true;
}
Also used : Path(org.apache.hadoop.fs.Path) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) Calendar(java.util.Calendar) SimpleDateFormat(java.text.SimpleDateFormat)

Example 27 with SimpleDateFormat

use of java.text.SimpleDateFormat in project hadoop by apache.

the class TestJobInfo method testGetFormattedStartTimeStr.

@Test
public void testGetFormattedStartTimeStr() {
    JobReport jobReport = mock(JobReport.class);
    when(jobReport.getStartTime()).thenReturn(-1L);
    Job job = mock(Job.class);
    when(job.getReport()).thenReturn(jobReport);
    when(job.getName()).thenReturn("TestJobInfo");
    when(job.getState()).thenReturn(JobState.SUCCEEDED);
    JobId jobId = MRBuilderUtils.newJobId(1L, 1, 1);
    when(job.getID()).thenReturn(jobId);
    DateFormat dateFormat = new SimpleDateFormat();
    JobInfo jobInfo = new JobInfo(job);
    Assert.assertEquals(JobInfo.NA, jobInfo.getFormattedStartTimeStr(dateFormat));
    Date date = new Date();
    when(jobReport.getStartTime()).thenReturn(date.getTime());
    jobInfo = new JobInfo(job);
    Assert.assertEquals(dateFormat.format(date), jobInfo.getFormattedStartTimeStr(dateFormat));
}
Also used : SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) CompletedJob(org.apache.hadoop.mapreduce.v2.hs.CompletedJob) Job(org.apache.hadoop.mapreduce.v2.app.job.Job) SimpleDateFormat(java.text.SimpleDateFormat) JobId(org.apache.hadoop.mapreduce.v2.api.records.JobId) Date(java.util.Date) JobReport(org.apache.hadoop.mapreduce.v2.api.records.JobReport) Test(org.junit.Test)

Example 28 with SimpleDateFormat

use of java.text.SimpleDateFormat in project hadoop by apache.

the class SwiftNativeFileSystemStore method getObjectMetadata.

/**
   * Get the metadata of an object
   *
   * @param path path
   * @param newest flag to say "set the newest header", otherwise take any entry
   * @return file metadata. -or null if no headers were received back from the server.
   * @throws IOException           on a problem
   * @throws FileNotFoundException if there is nothing at the end
   */
public SwiftFileStatus getObjectMetadata(Path path, boolean newest) throws IOException, FileNotFoundException {
    SwiftObjectPath objectPath = toObjectPath(path);
    final Header[] headers = stat(objectPath, newest);
    //no headers is treated as a missing file
    if (headers.length == 0) {
        throw new FileNotFoundException("Not Found " + path.toUri());
    }
    boolean isDir = false;
    long length = 0;
    long lastModified = 0;
    for (Header header : headers) {
        String headerName = header.getName();
        if (headerName.equals(SwiftProtocolConstants.X_CONTAINER_OBJECT_COUNT) || headerName.equals(SwiftProtocolConstants.X_CONTAINER_BYTES_USED)) {
            length = 0;
            isDir = true;
        }
        if (SwiftProtocolConstants.HEADER_CONTENT_LENGTH.equals(headerName)) {
            length = Long.parseLong(header.getValue());
        }
        if (SwiftProtocolConstants.HEADER_LAST_MODIFIED.equals(headerName)) {
            final SimpleDateFormat simpleDateFormat = new SimpleDateFormat(PATTERN);
            try {
                lastModified = simpleDateFormat.parse(header.getValue()).getTime();
            } catch (ParseException e) {
                throw new SwiftException("Failed to parse " + header.toString(), e);
            }
        }
    }
    if (lastModified == 0) {
        lastModified = System.currentTimeMillis();
    }
    Path correctSwiftPath = getCorrectSwiftPath(path);
    return new SwiftFileStatus(length, isDir, 1, getBlocksize(), lastModified, correctSwiftPath);
}
Also used : Path(org.apache.hadoop.fs.Path) SwiftObjectPath(org.apache.hadoop.fs.swift.util.SwiftObjectPath) Header(org.apache.commons.httpclient.Header) FileNotFoundException(java.io.FileNotFoundException) SwiftException(org.apache.hadoop.fs.swift.exceptions.SwiftException) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) SwiftObjectPath(org.apache.hadoop.fs.swift.util.SwiftObjectPath)

Example 29 with SimpleDateFormat

use of java.text.SimpleDateFormat in project hadoop by apache.

the class TestKafkaMetrics method recordToJson.

StringBuilder recordToJson(MetricsRecord record) {
    // Create a json object from a metrics record.
    StringBuilder jsonLines = new StringBuilder();
    Long timestamp = record.timestamp();
    Date currDate = new Date(timestamp);
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    String date = dateFormat.format(currDate);
    SimpleDateFormat timeFormat = new SimpleDateFormat("hh:mm:ss");
    String time = timeFormat.format(currDate);
    String hostname = new String("null");
    try {
        hostname = InetAddress.getLocalHost().getHostName();
    } catch (Exception e) {
        LOG.warn("Error getting Hostname, going to continue");
    }
    jsonLines.append("{\"hostname\": \"" + hostname);
    jsonLines.append("\", \"timestamp\": " + timestamp);
    jsonLines.append(", \"date\": \"" + date);
    jsonLines.append("\",\"time\": \"" + time);
    jsonLines.append("\",\"name\": \"" + record.name() + "\" ");
    for (MetricsTag tag : record.tags()) {
        jsonLines.append(", \"" + tag.name().toString().replaceAll("[\\p{Cc}]", "") + "\": ");
        jsonLines.append(" \"" + tag.value().toString() + "\"");
    }
    for (AbstractMetric m : record.metrics()) {
        jsonLines.append(", \"" + m.name().toString().replaceAll("[\\p{Cc}]", "") + "\": ");
        jsonLines.append(" \"" + m.value().toString() + "\"");
    }
    jsonLines.append("}");
    return jsonLines;
}
Also used : AbstractMetric(org.apache.hadoop.metrics2.AbstractMetric) MetricsTag(org.apache.hadoop.metrics2.MetricsTag) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 30 with SimpleDateFormat

use of java.text.SimpleDateFormat in project hbase by apache.

the class MemoryBoundedLogMessageBuffer method dumpTo.

/**
   * Dump the contents of the buffer to the given stream.
   */
public synchronized void dumpTo(PrintWriter out) {
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    for (LogMessage msg : messages) {
        out.write(df.format(new Date(msg.timestamp)));
        out.write(" ");
        out.println(new String(msg.message, Charsets.UTF_8));
    }
}
Also used : SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Aggregations

SimpleDateFormat (java.text.SimpleDateFormat)2847 Date (java.util.Date)1590 ParseException (java.text.ParseException)463 DateFormat (java.text.DateFormat)425 Calendar (java.util.Calendar)307 Test (org.junit.Test)305 ArrayList (java.util.ArrayList)232 File (java.io.File)230 IOException (java.io.IOException)185 GregorianCalendar (java.util.GregorianCalendar)139 HashMap (java.util.HashMap)121 Locale (java.util.Locale)70 DateField (edu.uci.ics.textdb.api.field.DateField)64 DoubleField (edu.uci.ics.textdb.api.field.DoubleField)64 IField (edu.uci.ics.textdb.api.field.IField)64 IntegerField (edu.uci.ics.textdb.api.field.IntegerField)64 StringField (edu.uci.ics.textdb.api.field.StringField)63 TextField (edu.uci.ics.textdb.api.field.TextField)63 Map (java.util.Map)63 Tuple (edu.uci.ics.textdb.api.tuple.Tuple)61