Example usage for java.util SortedMap entrySet

List of usage examples for java.util SortedMap entrySet

Introduction

In this page you can find the example usage for java.util SortedMap entrySet.

Prototype

Set<Map.Entry<K, V>> entrySet();

Source Link

Document

Returns a Set view of the mappings contained in this map.

Usage

From source file:org.apache.accumulo.tserver.tablet.Tablet.java

private Map<FileRef, Pair<Key, Key>> getFirstAndLastKeys(SortedMap<FileRef, DataFileValue> allFiles)
        throws IOException {
    Map<FileRef, Pair<Key, Key>> result = new HashMap<FileRef, Pair<Key, Key>>();
    FileOperations fileFactory = FileOperations.getInstance();
    VolumeManager fs = getTabletServer().getFileSystem();
    for (Entry<FileRef, DataFileValue> entry : allFiles.entrySet()) {
        FileRef file = entry.getKey();/*from   w w  w .  j a  v a 2  s  .  co  m*/
        FileSystem ns = fs.getVolumeByPath(file.path()).getFileSystem();
        FileSKVIterator openReader = fileFactory.newReaderBuilder()
                .forFile(file.path().toString(), ns, ns.getConf())
                .withTableConfiguration(this.getTableConfiguration()).seekToBeginning().build();
        try {
            Key first = openReader.getFirstKey();
            Key last = openReader.getLastKey();
            result.put(file, new Pair<Key, Key>(first, last));
        } finally {
            openReader.close();
        }
    }
    return result;
}

From source file:com.github.dryangkun.hbase.tidx.hive.HiveHFileOutputFormat.java

@Override
public RecordWriter getHiveRecordWriter(final JobConf jc, final Path finalOutPath,
        Class<? extends Writable> valueClass, boolean isCompressed, Properties tableProperties,
        final Progressable progressable) throws IOException {

    // Read configuration for the target path, first from jobconf, then from table properties
    String hfilePath = getFamilyPath(jc, tableProperties);
    if (hfilePath == null) {
        throw new RuntimeException("Please set " + HFILE_FAMILY_PATH + " to target location for HFiles");
    }//  ww  w  .j a v  a2  s  . c  o m

    // Target path's last component is also the column family name.
    final Path columnFamilyPath = new Path(hfilePath);
    final String columnFamilyName = columnFamilyPath.getName();
    final byte[] columnFamilyNameBytes = Bytes.toBytes(columnFamilyName);
    final Job job = new Job(jc);
    setCompressOutput(job, isCompressed);
    setOutputPath(job, finalOutPath);

    // Create the HFile writer
    final org.apache.hadoop.mapreduce.TaskAttemptContext tac = ShimLoader.getHadoopShims()
            .newTaskAttemptContext(job.getConfiguration(), progressable);

    final Path outputdir = FileOutputFormat.getOutputPath(tac);
    final org.apache.hadoop.mapreduce.RecordWriter<ImmutableBytesWritable, KeyValue> fileWriter = getFileWriter(
            tac);

    // Individual columns are going to be pivoted to HBase cells,
    // and for each row, they need to be written out in order
    // of column name, so sort the column names now, creating a
    // mapping to their column position.  However, the first
    // column is interpreted as the row key.
    String columnList = tableProperties.getProperty("columns");
    String[] columnArray = columnList.split(",");
    final SortedMap<byte[], Integer> columnMap = new TreeMap<byte[], Integer>(Bytes.BYTES_COMPARATOR);
    int i = 0;
    for (String columnName : columnArray) {
        if (i != 0) {
            columnMap.put(Bytes.toBytes(columnName), i);
        }
        ++i;
    }

    return new RecordWriter() {

        @Override
        public void close(boolean abort) throws IOException {
            try {
                fileWriter.close(null);
                if (abort) {
                    return;
                }
                // Move the hfiles file(s) from the task output directory to the
                // location specified by the user.
                FileSystem fs = outputdir.getFileSystem(jc);
                fs.mkdirs(columnFamilyPath);
                Path srcDir = outputdir;
                for (;;) {
                    FileStatus[] files = fs.listStatus(srcDir, FileUtils.STAGING_DIR_PATH_FILTER);
                    if ((files == null) || (files.length == 0)) {
                        throw new IOException("No family directories found in " + srcDir);
                    }
                    if (files.length != 1) {
                        throw new IOException("Multiple family directories found in " + srcDir);
                    }
                    srcDir = files[0].getPath();
                    if (srcDir.getName().equals(columnFamilyName)) {
                        break;
                    }
                }
                for (FileStatus regionFile : fs.listStatus(srcDir, FileUtils.STAGING_DIR_PATH_FILTER)) {
                    fs.rename(regionFile.getPath(), new Path(columnFamilyPath, regionFile.getPath().getName()));
                }
                // Hive actually wants a file as task output (not a directory), so
                // replace the empty directory with an empty file to keep it happy.
                fs.delete(outputdir, true);
                fs.createNewFile(outputdir);
            } catch (InterruptedException ex) {
                throw new IOException(ex);
            }
        }

        private void writeText(Text text) throws IOException {
            // Decompose the incoming text row into fields.
            String s = text.toString();
            String[] fields = s.split("\u0001");
            assert (fields.length <= (columnMap.size() + 1));
            // First field is the row key.
            byte[] rowKeyBytes = Bytes.toBytes(fields[0]);
            // Remaining fields are cells addressed by column name within row.
            for (Map.Entry<byte[], Integer> entry : columnMap.entrySet()) {
                byte[] columnNameBytes = entry.getKey();
                int iColumn = entry.getValue();
                String val;
                if (iColumn >= fields.length) {
                    // trailing blank field
                    val = "";
                } else {
                    val = fields[iColumn];
                    if ("\\N".equals(val)) {
                        // omit nulls
                        continue;
                    }
                }
                byte[] valBytes = Bytes.toBytes(val);
                KeyValue kv = new KeyValue(rowKeyBytes, columnFamilyNameBytes, columnNameBytes, valBytes);
                try {
                    fileWriter.write(null, kv);
                } catch (IOException e) {
                    LOG.error("Failed while writing row: " + s);
                    throw e;
                } catch (InterruptedException ex) {
                    throw new IOException(ex);
                }
            }
        }

        private void writePut(PutWritable put) throws IOException {
            ImmutableBytesWritable row = new ImmutableBytesWritable(put.getPut().getRow());
            SortedMap<byte[], List<Cell>> cells = put.getPut().getFamilyCellMap();
            for (Map.Entry<byte[], List<Cell>> entry : cells.entrySet()) {
                Collections.sort(entry.getValue(), new CellComparator());
                for (Cell c : entry.getValue()) {
                    try {
                        fileWriter.write(row, KeyValueUtil.copyToNewKeyValue(c));
                    } catch (InterruptedException e) {
                        throw (InterruptedIOException) new InterruptedIOException().initCause(e);
                    }
                }
            }
        }

        @Override
        public void write(Writable w) throws IOException {
            if (w instanceof Text) {
                writeText((Text) w);
            } else if (w instanceof PutWritable) {
                writePut((PutWritable) w);
            } else {
                throw new IOException("Unexpected writable " + w);
            }
        }
    };
}

From source file:org.apache.accumulo.server.tabletserver.Tablet.java

private static Set<FileRef> lookupScanFiles(KeyExtent extent, SortedMap<Key, Value> tabletsKeyValues,
        VolumeManager fs) throws IOException {
    HashSet<FileRef> scanFiles = new HashSet<FileRef>();

    Text row = extent.getMetadataEntry();
    for (Entry<Key, Value> entry : tabletsKeyValues.entrySet()) {
        Key key = entry.getKey();
        if (key.getRow().equals(row) && key.getColumnFamily().equals(ScanFileColumnFamily.NAME)) {
            String meta = key.getColumnQualifier().toString();
            Path path = fs.getFullPath(extent.getTableId().toString(), meta);
            scanFiles.add(new FileRef(meta, path));
        }//from ww  w.java2s.c  o m
    }

    return scanFiles;
}

From source file:org.apache.accumulo.tserver.Tablet.java

private static Set<FileRef> lookupScanFiles(KeyExtent extent, SortedMap<Key, Value> tabletsKeyValues,
        VolumeManager fs) throws IOException {
    HashSet<FileRef> scanFiles = new HashSet<FileRef>();

    Text row = extent.getMetadataEntry();
    for (Entry<Key, Value> entry : tabletsKeyValues.entrySet()) {
        Key key = entry.getKey();
        if (key.getRow().equals(row) && key.getColumnFamily().equals(ScanFileColumnFamily.NAME)) {
            scanFiles.add(new FileRef(fs, key));
        }// ww  w .  j  ava 2 s.com
    }

    return scanFiles;
}

From source file:org.apache.accumulo.server.tabletserver.ScanRunState.java

static Value checkTabletMetadata(KeyExtent extent, TServerInstance instance,
        SortedMap<Key, Value> tabletsKeyValues, Text metadataEntry) throws AccumuloException {

    TServerInstance future = null;/* w w w .j  a v a2  s. c  o m*/
    Value prevEndRow = null;
    Value dir = null;
    Value time = null;
    for (Entry<Key, Value> entry : tabletsKeyValues.entrySet()) {
        Key key = entry.getKey();
        if (!metadataEntry.equals(key.getRow())) {
            log.info("Unexpected row in tablet metadata " + metadataEntry + " " + key.getRow());
            return null;
        }
        Text cf = key.getColumnFamily();
        if (cf.equals(TabletsSection.FutureLocationColumnFamily.NAME)) {
            if (future != null) {
                throw new AccumuloException("Tablet has multiple future locations " + extent);
            }
            future = new TServerInstance(entry.getValue(), key.getColumnQualifier());
        } else if (cf.equals(TabletsSection.CurrentLocationColumnFamily.NAME)) {
            log.info("Tablet seems to be already assigned to "
                    + new TServerInstance(entry.getValue(), key.getColumnQualifier()));
            return null;
        } else if (TabletsSection.TabletColumnFamily.PREV_ROW_COLUMN.hasColumns(key)) {
            prevEndRow = entry.getValue();
        } else if (TabletsSection.ServerColumnFamily.DIRECTORY_COLUMN.hasColumns(key)) {
            dir = entry.getValue();
        } else if (TabletsSection.ServerColumnFamily.TIME_COLUMN.hasColumns(key)) {
            time = entry.getValue();
        }
    }

    if (prevEndRow == null) {
        throw new AccumuloException("Metadata entry does not have prev row (" + metadataEntry + ")");
    } else {
        KeyExtent ke2 = new KeyExtent(metadataEntry, prevEndRow);
        if (!extent.equals(ke2)) {
            log.info("Tablet prev end row mismatch " + extent + " " + ke2.getPrevEndRow());
            return null;
        }
    }

    if (dir == null) {
        throw new AccumuloException("Metadata entry does not have directory (" + metadataEntry + ")");
    }

    if (time == null) {
        throw new AccumuloException("Metadata entry does not have time (" + metadataEntry + ")");
    }

    if (future == null) {
        log.info("The master has not assigned " + extent + " to " + instance);
        return null;
    }

    if (!instance.equals(future)) {
        log.info("Table " + extent + " has been assigned to " + future + " which is not " + instance);
        return null;
    }

    return dir;
}

From source file:org.apache.accumulo.server.tabletserver.Tablet.java

private static String lookupTime(AccumuloConfiguration conf, KeyExtent extent,
        SortedMap<Key, Value> tabletsKeyValues) {
    SortedMap<Key, Value> entries;

    if (extent.isRootTablet()) {
        return null;
    } else {/*from   www.j a va  2 s.c  o m*/
        entries = new TreeMap<Key, Value>();
        Text rowName = extent.getMetadataEntry();
        for (Entry<Key, Value> entry : tabletsKeyValues.entrySet()) {
            if (entry.getKey().compareRow(rowName) == 0
                    && TabletsSection.ServerColumnFamily.TIME_COLUMN.hasColumns(entry.getKey())) {
                entries.put(new Key(entry.getKey()), new Value(entry.getValue()));
            }
        }
    }

    // log.debug("extent : "+extent+"   entries : "+entries);

    if (entries.size() == 1)
        return entries.values().iterator().next().toString();
    return null;
}

From source file:fll.scheduler.TournamentSchedule.java

private void outputPerformanceSchedule(final Document detailedSchedules) throws DocumentException {
    final SortedMap<PerformanceTime, TeamScheduleInfo> performanceTimes = new TreeMap<PerformanceTime, TeamScheduleInfo>();
    for (int round = 0; round < getNumberOfRounds(); ++round) {
        for (final TeamScheduleInfo si : _schedule) {
            performanceTimes.put(si.getPerf(round), si);
        }/*  w ww.  j  a  v  a  2  s .  c  o  m*/
    }

    // list of teams staying around to even up the teams
    final List<TeamScheduleInfo> teamsStaying = new LinkedList<TeamScheduleInfo>();

    final PdfPTable table = PdfUtils.createTable(7);
    table.setWidths(new float[] { 2, 1, 3, 3, 2, 2, 2 });

    final PdfPCell tournamentCell = PdfUtils.createHeaderCell("Tournament: " + getName() + " Performance");
    tournamentCell.setColspan(7);
    table.addCell(tournamentCell);

    table.addCell(PdfUtils.createHeaderCell(TEAM_NUMBER_HEADER));
    table.addCell(PdfUtils.createHeaderCell(DIVISION_HEADER));
    table.addCell(PdfUtils.createHeaderCell(ORGANIZATION_HEADER));
    table.addCell(PdfUtils.createHeaderCell(TEAM_NAME_HEADER));
    table.addCell(PdfUtils.createHeaderCell("Time"));
    table.addCell(PdfUtils.createHeaderCell("Table"));
    table.addCell(PdfUtils.createHeaderCell("Round"));
    table.setHeaderRows(1);

    for (final Map.Entry<PerformanceTime, TeamScheduleInfo> entry : performanceTimes.entrySet()) {
        final PerformanceTime performance = entry.getKey();
        final TeamScheduleInfo si = entry.getValue();
        final int round = si.computeRound(performance);

        // check if team needs to stay and color the cell magenta if they do
        final BaseColor backgroundColor;
        if (null != checkIfTeamNeedsToStay(si, round)) {
            teamsStaying.add(si);
            backgroundColor = BaseColor.MAGENTA;
        } else {
            backgroundColor = null;
        }

        table.addCell(PdfUtils.createCell(String.valueOf(si.getTeamNumber())));
        table.addCell(PdfUtils.createCell(si.getAwardGroup()));
        table.addCell(PdfUtils.createCell(si.getOrganization()));
        table.addCell(PdfUtils.createCell(si.getTeamName()));
        table.addCell(PdfUtils.createCell(formatTime(si.getPerfTime(round)), backgroundColor));
        table.addCell(PdfUtils.createCell(si.getPerfTableColor(round) + " " + si.getPerfTableSide(round),
                backgroundColor));
        table.addCell(PdfUtils.createCell(String.valueOf(round + 1)));
    }

    detailedSchedules.add(table);

    // output teams staying
    if (!teamsStaying.isEmpty()) {
        final String formatString = "Team %d will please stay at the table and compete again - score will not count.";
        final PdfPTable stayingTable = PdfUtils.createTable(1);
        for (final TeamScheduleInfo si : teamsStaying) {
            stayingTable.addCell(PdfUtils.createCell(
                    new Formatter().format(formatString, si.getTeamNumber()).toString(), BaseColor.MAGENTA));
        }
        detailedSchedules.add(stayingTable);

    }

    detailedSchedules.add(Chunk.NEXTPAGE);
}

From source file:org.apache.accumulo.server.tabletserver.Tablet.java

private static List<LogEntry> lookupLogEntries(KeyExtent ke, SortedMap<Key, Value> tabletsKeyValues) {
    List<LogEntry> logEntries = new ArrayList<LogEntry>();

    if (ke.isMeta()) {
        try {//from w w  w  .  ja  v a 2s .  c o  m
            logEntries = MetadataTableUtil.getLogEntries(SystemCredentials.get(), ke);
        } catch (Exception ex) {
            throw new RuntimeException("Unable to read tablet log entries", ex);
        }
    } else {
        log.debug("Looking at metadata " + tabletsKeyValues);
        Text row = ke.getMetadataEntry();
        for (Entry<Key, Value> entry : tabletsKeyValues.entrySet()) {
            Key key = entry.getKey();
            if (key.getRow().equals(row)) {
                if (key.getColumnFamily().equals(LogColumnFamily.NAME)) {
                    logEntries.add(MetadataTableUtil.entryFromKeyValue(key, entry.getValue()));
                }
            }
        }
    }

    log.debug("got " + logEntries + " for logs for " + ke);
    return logEntries;
}

From source file:org.apache.accumulo.tserver.Tablet.java

private static List<LogEntry> lookupLogEntries(KeyExtent ke, SortedMap<Key, Value> tabletsKeyValues) {
    List<LogEntry> logEntries = new ArrayList<LogEntry>();

    if (ke.isMeta()) {
        try {//w  w w.j av  a  2s .  c  om
            logEntries = MetadataTableUtil.getLogEntries(SystemCredentials.get(), ke);
        } catch (Exception ex) {
            throw new RuntimeException("Unable to read tablet log entries", ex);
        }
    } else {
        log.debug("Looking at metadata " + tabletsKeyValues);
        Text row = ke.getMetadataEntry();
        for (Entry<Key, Value> entry : tabletsKeyValues.entrySet()) {
            Key key = entry.getKey();
            if (key.getRow().equals(row)) {
                if (key.getColumnFamily().equals(LogColumnFamily.NAME)) {
                    logEntries.add(LogEntry.fromKeyValue(key, entry.getValue()));
                }
            }
        }
    }

    log.debug("got " + logEntries + " for logs for " + ke);
    return logEntries;
}