Example usage for java.util NavigableMap entrySet

List of usage examples for java.util NavigableMap entrySet

Introduction

In this page you can find the example usage for java.util NavigableMap 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.lilyproject.tools.recordrowvisualizer.RecordRowVisualizer.java

private void setSystemField(String name, NavigableMap<Long, byte[]> valuesByVersion, ValueDecoder decoder) {
    SystemFields systemFields = recordRow.systemFields;
    SystemFields.SystemField systemField = systemFields.getOrCreateSystemField(name);
    for (Map.Entry<Long, byte[]> entry : valuesByVersion.entrySet()) {
        systemField.values.put(entry.getKey(), decoder.decode(entry.getValue()));
    }/*from  w  w  w. j a v  a2 s  .c  o  m*/
}

From source file:org.apache.hadoop.hbase.mapreduce.CrossSiteTableInputFormat.java

@Override
public List<InputSplit> getSplits(JobContext context) throws IOException {
    if (table == null) {
        throw new IOException("No table was provided.");
    }//from   w  w  w . j  a v  a 2 s .c om
    NavigableMap<HRegionInfo, ServerName> locations = table.getRegionLocations();
    if (!locations.isEmpty()) {
        Scan scan = getScan();
        List<InputSplit> splits = new ArrayList<InputSplit>();
        int i = 0;
        for (Entry<HRegionInfo, ServerName> location : locations.entrySet()) {
            String regionLocation = location.getValue().getHostname();
            byte[] startRow = scan.getStartRow();
            byte[] stopRow = scan.getStopRow();
            if ((startRow.length == 0 || location.getKey().getEndKey().length == 0
                    || Bytes.compareTo(startRow, location.getKey().getEndKey()) < 0)
                    && (stopRow.length == 0 || Bytes.compareTo(stopRow, location.getKey().getStartKey()) > 0)) {
                byte[] splitStart = startRow.length == 0
                        || Bytes.compareTo(location.getKey().getStartKey(), startRow) >= 0
                                ? location.getKey().getStartKey()
                                : startRow;
                byte[] splitStop = (stopRow.length == 0
                        || Bytes.compareTo(location.getKey().getEndKey(), stopRow) <= 0)
                        && location.getKey().getEndKey().length > 0 ? location.getKey().getEndKey() : stopRow;
                TableSplit split = new TableSplit(location.getKey().getTableName(), splitStart, splitStop,
                        regionLocation);
                splits.add(split);
                if (LOG.isDebugEnabled()) {
                    LOG.debug("getSplits: split -> " + i++ + " -> " + split);
                }
            }
        }
        return splits;
    } else {
        return Collections.emptyList();
    }
}

From source file:com.opengamma.analytics.financial.greeks.MixedOrderUnderlying.java

public MixedOrderUnderlying(final NavigableMap<Integer, UnderlyingType> underlyings) {
    Validate.notNull(underlyings, "underlyings");
    if (underlyings.size() < 2) {
        throw new IllegalArgumentException("Must have at least two underlying types to have mixed order");
    }/*www  .j  a v  a  2 s.  co m*/
    _orders = new ArrayList<>();
    _underlyings = new ArrayList<>();
    int totalOrder = 0;
    UnderlyingType underlying;
    for (final Entry<Integer, UnderlyingType> entry : underlyings.entrySet()) {
        final int key = entry.getKey();
        if (key < 1) {
            throw new IllegalArgumentException("Order must be at least one to have mixed order");
        }
        underlying = entry.getValue();
        _orders.add(new NthOrderUnderlying(key, underlying));
        _underlyings.add(underlying);
        totalOrder += key;
    }
    _totalOrder = totalOrder;
}

From source file:net.projectmonkey.spring.acl.hbase.repository.HBaseACLRepository.java

private Map<ObjectIdentity, Acl> mapResults(final List<Sid> sids,
        final Map<Long, ObjectIdentity> identitiesByByteId, final Result[] results) {
    Map<ObjectIdentity, Acl> toReturn = new HashMap<ObjectIdentity, Acl>();
    for (Result result : results) {
        if (!result.isEmpty()) {
            byte[] rowKey = result.getRow();

            Long rowId = createRowId(rowKey);
            ObjectIdentity identity = identitiesByByteId.get(rowId);

            NavigableMap<byte[], byte[]> aclFamilyMap = result.getFamilyMap(ACL_FAMILY);

            AclRecord aclRecord = new AclRecord(rowKey, aclFamilyMap, resolveConverter(identity));

            List<AccessControlEntry> entries = new ArrayList<AccessControlEntry>();
            MutableAcl acl = new SimpleAcl(identity, aclRecord.getOwner(), entries, sids, util);

            NavigableMap<byte[], byte[]> aceMap = result.getFamilyMap(ACE_FAMILY);
            for (Entry<byte[], byte[]> keyValue : aceMap.entrySet()) {
                AccessControlEntryValue value = new AccessControlEntryValue(keyValue.getValue(),
                        permissionFactory);
                AccessControlEntry ace = new AccessControlEntryImpl(value.getId(), acl, value.getSid(),
                        value.getPermission(), value.isGranting(), false, false);
                entries.add(ace);/*from   w  ww.  jav  a2s  .co  m*/
            }
            toReturn.put(identity, acl);
            aclCache.putInCache(acl);
        }
    }
    return toReturn;
}

From source file:edu.indiana.soic.ts.crunch.CrunchDataReader.java

public PTable<String, String> extractText(PTable<ImmutableBytesWritable, Result> tableContent) {
    return tableContent.parallelDo("Read data",
            new DoFn<Pair<ImmutableBytesWritable, Result>, Pair<String, String>>() {
                @Override// w ww . j  a v a 2  s .  co m
                public void process(Pair<ImmutableBytesWritable, Result> row,
                        Emitter<Pair<String, String>> emitter) {
                    SimpleRegression regression;
                    NavigableMap<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> map = row.second()
                            .getMap();
                    System.out.println(map.size());
                    for (Map.Entry<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> columnFamilyMap : map
                            .entrySet()) {
                        regression = new SimpleRegression();
                        int count = 1;
                        for (Map.Entry<byte[], NavigableMap<Long, byte[]>> entryVersion : columnFamilyMap
                                .getValue().entrySet()) {
                            for (Map.Entry<Long, byte[]> entry : entryVersion.getValue().entrySet()) {
                                String rowKey = Bytes.toString(row.second().getRow());
                                String column = Bytes.toString(entryVersion.getKey());
                                byte[] val = entry.getValue();
                                String valOfColumn = new String(val);
                                System.out.println("RowKey : " + rowKey + " Column Key : " + column
                                        + " Column Val : " + valOfColumn);
                                if (!valOfColumn.isEmpty()) {
                                    String[] priceAndCap = valOfColumn.split("_");
                                    if (priceAndCap.length > 1) {
                                        String pr = priceAndCap[0];
                                        if (pr != null && !pr.equals("null")) {
                                            double price = Double.valueOf(pr);
                                            if (price < 0) {
                                                price = price - 2 * price;
                                            }
                                            System.out.println("Price : " + price + " count : " + count);
                                            regression.addData(count, price);
                                        }
                                    }
                                }
                            }
                            count++;
                        }
                        // displays intercept of regression line
                        System.out.println("Intercept : " + regression.getIntercept());

                        // displays slope of regression line
                        System.out.println("Slope : " + regression.getSlope());

                        // displays slope standard error
                        System.out.println("Slope STD Error : " + regression.getSlopeStdErr());
                        emitter.emit(new Pair<String, String>(String.valueOf(regression.getIntercept()),
                                String.valueOf(regression.getSlope())));
                    }
                }
            }, Writables.tableOf(Writables.strings(), Writables.strings()));
}

From source file:org.apache.impala.datagenerator.HBaseTestDataRegionAssigment.java

/**
 * Split the table regions according to splitPoints and pair up adjacent regions to the
 * same server. Each region pair in ([unbound:1,1:3], [3:5,5:7], [7:9,9:unbound])
 * will be on the same server.//from   www.  j a  v a  2  s. co m
 * The table must have data loaded and only a single region.
 */
public void performAssigment(String tableName)
        throws IOException, InterruptedException, TableNotFoundException {
    HTableDescriptor[] desc = hbaseAdmin.listTables(tableName);
    if (desc == null || desc.length == 0) {
        throw new TableNotFoundException("Table " + tableName + " not found.");
    }

    if (hbaseAdmin.getTableRegions(tableName.getBytes()).size() == 1) {
        // Split into regions
        // The table has one region only to begin with. The logic of
        // blockUntilRegionSplit requires that the input regionName has performed a split. 
        // If the table has already been split (i.e. regions count > 1), the same split
        // call will be a no-op and this will cause blockUntilRegionSplit to break.
        for (int i = 0; i < splitPoints.length; ++i) {
            hbaseAdmin.majorCompact(tableName);
            List<HRegionInfo> regions = hbaseAdmin.getTableRegions(tableName.getBytes());
            HRegionInfo splitRegion = regions.get(regions.size() - 1);
            int attempt = 1;
            boolean done = false;
            while (!done && attempt < MAX_SPLIT_ATTEMPTS) {
                // HBase seems to not always properly receive/process this split RPC,
                // so we need to retry the split/block several times.
                hbaseAdmin.split(splitRegion.getRegionNameAsString(), splitPoints[i]);
                done = blockUntilRegionSplit(conf, WAIT_FOR_SPLIT_TIMEOUT, splitRegion.getRegionName(), true);
                Thread.sleep(100);
                ++attempt;
            }
            if (!done) {
                throw new IllegalStateException(String.format("Failed to split region '%s' after %s attempts.",
                        splitRegion.getRegionNameAsString(), WAIT_FOR_SPLIT_TIMEOUT));
            }
            LOG.info(String.format("Split region '%s' after %s attempts.", splitRegion.getRegionNameAsString(),
                    attempt));
        }
    }

    // Sort the region by start key
    List<HRegionInfo> regions = hbaseAdmin.getTableRegions(tableName.getBytes());
    Preconditions.checkArgument(regions.size() == splitPoints.length + 1);
    Collections.sort(regions);

    // Pair up two adjacent regions to the same region server. That is, 
    // region server 1 <- regions (unbound:1), (1:3)
    // region server 2 <- regions (3:5), (5:7)
    // region server 3 <- regions (7:9), (9:unbound)
    NavigableMap<HRegionInfo, ServerName> expectedLocs = Maps.newTreeMap();
    for (int i = 0; i < regions.size(); ++i) {
        HRegionInfo regionInfo = regions.get(i);
        int rsIdx = (i / 2) % sortedRS.size();
        ServerName regionServerName = sortedRS.get(rsIdx);
        hbaseAdmin.move(regionInfo.getEncodedNameAsBytes(), regionServerName.getServerName().getBytes());
        expectedLocs.put(regionInfo, regionServerName);
    }

    // hbaseAdmin.move() is an asynchronous operation. HBase tests use sleep to wait for
    // the move to complete. It should be done in 10sec.
    int sleepCnt = 0;
    HTable hbaseTable = new HTable(conf, tableName);
    try {
        while (!expectedLocs.equals(hbaseTable.getRegionLocations()) && sleepCnt < 100) {
            Thread.sleep(100);
            ++sleepCnt;
        }
        NavigableMap<HRegionInfo, ServerName> actualLocs = hbaseTable.getRegionLocations();
        Preconditions.checkArgument(expectedLocs.equals(actualLocs));

        // Log the actual region location map
        for (Map.Entry<HRegionInfo, ServerName> entry : actualLocs.entrySet()) {
            LOG.info(printKey(entry.getKey().getStartKey()) + " -> " + entry.getValue().getHostAndPort());
        }

        // Force a major compaction such that the HBase table is backed by deterministic
        // physical artifacts (files, WAL, etc.). Our #rows estimate relies on the sizes of
        // these physical artifacts.
        LOG.info("Major compacting HBase table: " + tableName);
        hbaseAdmin.majorCompact(tableName);
    } finally {
        IOUtils.closeQuietly(hbaseTable);
    }
}

From source file:org.opennms.netmgt.rrd.model.RRDv3IT.java

/**
 * Test samples for a single RRA/* w ww.ja v a 2  s  .  co m*/
 *
 * @throws Exception the exception
 */
@Test
public void testSamplesSingleRRA() throws Exception {
    File source = new File("src/test/resources/sample-counter.xml");
    RRDv3 rrd = JaxbUtils.unmarshal(RRDv3.class, source);
    Assert.assertNotNull(rrd);
    NavigableMap<Long, List<Double>> samples = rrd.generateSamples(rrd.getRras().get(0));
    Assert.assertFalse(samples.isEmpty());
    long ts = 1441748400L;
    Double v1 = 600.0;
    Double v2 = 2.0;
    Assert.assertEquals(rrd.getRras().get(0).getRows().size(), samples.size());
    for (Map.Entry<Long, List<Double>> s : samples.entrySet()) {
        System.out.println(s);
        Assert.assertEquals(2, s.getValue().size());
        Assert.assertEquals(ts, (long) s.getKey());
        Assert.assertEquals(v1, s.getValue().get(0));
        Assert.assertEquals(v2, s.getValue().get(1));
        ts += 300L;
        v1 += 300.0 * v2;
        v2 += 1.0;
    }
}

From source file:com.kakao.hbase.TestBase.java

private long getWriteRequestCountActual(String tableName) throws IOException {
    long writeRequestCountActual;
    try (HTable table = (HTable) hConnection.getTable(tableName)) {
        writeRequestCountActual = 0;//from w ww. jav a 2s .c o m
        NavigableMap<HRegionInfo, ServerName> regionLocations = table.getRegionLocations();
        for (Map.Entry<HRegionInfo, ServerName> entry : regionLocations.entrySet()) {
            ServerLoad serverLoad = admin.getClusterStatus().getLoad(entry.getValue());
            for (RegionLoad regionLoad : serverLoad.getRegionsLoad().values()) {
                if (Arrays.equals(entry.getKey().getRegionName(), regionLoad.getName()))
                    writeRequestCountActual += regionLoad.getWriteRequestsCount();
            }
        }
    }
    return writeRequestCountActual;
}

From source file:com.kakao.hbase.TestBase.java

private long getWriteRequestCountActual(String tableName, ServerName serverName) throws IOException {
    long writeRequestCountActual;
    try (HTable table = (HTable) hConnection.getTable(tableName)) {
        writeRequestCountActual = 0;/*from ww w  . ja  va2s .  c o m*/
        NavigableMap<HRegionInfo, ServerName> regionLocations = table.getRegionLocations();
        for (Map.Entry<HRegionInfo, ServerName> entry : regionLocations.entrySet()) {
            if (serverName.equals(entry.getValue())) {
                ServerLoad serverLoad = admin.getClusterStatus().getLoad(entry.getValue());
                for (RegionLoad regionLoad : serverLoad.getRegionsLoad().values()) {
                    if (Arrays.equals(entry.getKey().getRegionName(), regionLoad.getName()))
                        writeRequestCountActual += regionLoad.getWriteRequestsCount();
                }
            }
        }
    }

    Long aLong = getWriteRequestMetric(tableName, serverName);
    return writeRequestCountActual - aLong;
}

From source file:com.kakao.hbase.TestBase.java

protected void waitForSplitting(String tableName, int regionCount) throws IOException, InterruptedException {
    int regionCountActual = 0;
    for (int i = 0; i < MAX_WAIT_ITERATION; i++) {
        try (HTable table = (HTable) hConnection.getTable(tableName)) {
            regionCountActual = 0;/*from w w w . j  a  v  a 2  s.  co  m*/
            NavigableMap<HRegionInfo, ServerName> regionLocations = table.getRegionLocations();
            for (Map.Entry<HRegionInfo, ServerName> entry : regionLocations.entrySet()) {
                ServerLoad serverLoad = admin.getClusterStatus().getLoad(entry.getValue());
                for (RegionLoad regionLoad : serverLoad.getRegionsLoad().values()) {
                    if (Arrays.equals(entry.getKey().getRegionName(), regionLoad.getName()))
                        regionCountActual++;
                }
            }
            if (regionCountActual == regionCount) {
                return;
            }
        } catch (Throwable ignore) {
        }
        Thread.sleep(WAIT_INTERVAL);
    }
    Assert.assertEquals("TestBase.waitForSplitting - failed - ", regionCount, regionCountActual);
}