public class DownloadBoxHelper extends WCMUsePojo {
private static final Logger log = LoggerFactory.getLogger(DownloadBoxHelper.class);
private ArrayList<Map<String, String>> downloadList;
private ArrayList<Map<String, String>> downloadListFinal;
DownloadBoxModel downloadBoxModel;
#Override
public void activate() throws Exception {
log.info("Download Box activate Method started");
JcrUtilService jcrUtil = getSlingScriptHelper().getService(JcrUtilService.class);
downloadBoxModel = getResource().adaptTo(DownloadBoxModel.class);
downloadList = downloadBoxModel.getDownloadList();
downloadListFinal =DHLUtil.getSizeTypeOfAsset(downloadList, getResource(), jcrUtil);
downloadBoxModel.setDownloadListFinal(downloadListFinal);
log.info("Download Box activate Method Ended");
}
public DownloadBoxModel getDownloadBoxModel() {
return downloadBoxModel;
}
}
I want to mock this helper class. But this helper class have some static method like downloadListFinal =DHLUtil.getSizeTypeOfAsset(downloadList, getResource(), jcrUtil);
This static method refer to DHLUtil.class file. Here is declaration
**public static ArrayList<Map<String, String>> getSizeTypeOfAsset(ArrayList<Map<String, String>> downloadList,
Resource rs, JcrUtilService jcrUtil) {
log.info("DHLUtil getSizeTypeOfAsset() initiated ");
ArrayList<Map<String, String>> localDownloadList = new ArrayList<Map<String, String>>();
Session session = null;
Node assetMetaNode;
try {
session = jcrUtil.getSession(DHLSubService.readservice);
Iterator<Map<String, String>> it = downloadList.iterator();
while (it.hasNext()) {
Map<String, String> mp = it.next();
if (mp.get(DHLConstants.ASSET_DOWNLOAD_ITEM).toString().contains(".")) {
assetMetaNode = session.getNode((mp.get(DHLConstants.ASSET_DOWNLOAD_ITEM).toString())
+ DHLConstants.SLASH + JcrConstants.JCR_CONTENT +DHLConstants.SLASH + DamConstants.ACTIVITY_TYPE_METADATA);
String assetType = assetMetaNode.getProperty(DamConstants.DC_FORMAT).getString();
if(assetType!=null){
if(assetType.contains("vnd.openxmlformats-officedocument.spreadsheetml.sheet") || assetType.contains("vnd.ms-excel")){
assetType="ms-excel";
}
if(assetType.contains("vnd.openxmlformats-officedocument.wordprocessingml.document") || assetType.contains("msword")){
assetType="ms-word";
}
if(assetType.contains("vnd.openxmlformats-officedocument.presentationml.presentation") || assetType.contains("vnd.ms-powerpoint")){
assetType="ms-powerpoint";
}
}
Property assetSize = assetMetaNode.getProperty(DamConstants.DAM_SIZE);
double assetSizeUpdated = 0d;
DecimalFormat df = new DecimalFormat("0.0");
String assetSizeType = DHLConstants.BYTE;
;
if (assetSize.getLong() < (1024)) {
assetSizeUpdated = (double) assetSize.getLong();
}
if (assetSize.getLong() > 1024 && assetSize.getLong() < (1024 * 1024)) {
assetSizeType = DHLConstants.KILOBYTE;
assetSizeUpdated = (double) assetSize.getLong() / 1024L;
}
if (assetSize.getLong() > (1024 * 1024)) {
assetSizeType = DHLConstants.MEGABYTE;
assetSizeUpdated = ((double) assetSize.getLong() / (1024 * 1024));
}
if (assetType.contains("/")) {
String strSplit[] = assetType.split("/");
assetType = strSplit[1];
}
String strMetaData = assetType.toUpperCase() + DHLConstants.SPACE + DHLConstants.LEFT_BRACKET
+ DHLConstants.SPACE + df.format(assetSizeUpdated) + DHLConstants.SPACE + assetSizeType + DHLConstants.SPACE + DHLConstants.RIGHT_BRACKET;
mp.put(DamConstants.ACTIVITY_TYPE_METADATA, strMetaData);
localDownloadList.add(mp);
}
}
}catch (DHLException dhe) {
log.error("DHLException {}", dhe);
}catch (Exception e) {
log.error("Exception {}", e);
}finally {
if(null!=session && session.isLive()) {
session.logout();
}
}
return localDownloadList;
}
So how I mock this?
My JUnit file is:
**#RunWith(PowerMockRunner.class)
#PrepareForTes({DownloadBoxHelper.class,DHLUtil.class,DownloadBoxModel.class})
public class DownloadBoxHelperTest extends PowerMockTestCase {
private DownloadBoxHelper aFinalClass_mock = null;
#Test
public void mockFinalClassTest() {
ArrayList<Map<String, String>> downloadList = new ArrayList<Map<String, String>>();;
ArrayList<Map<String, String>> downloadListFinal;
Map<String, String> n = new HashMap<String, String>();
n.put("a", "a");
n.put("b", "b");
downloadList.add(n);
DownloadBoxModel downloadBoxModel;
aFinalClass_mock = PowerMockito.mock(DownloadBoxHelper.class);
Mockito.when(aFinalClass_mock.getSlingScriptHelper()).thenReturn(null);
// Assert the mocked result is returned from method call
//Assert.assertEquals(aFinalClass_mock.getSlingScriptHelper()).thenReturn(null);
}
#Test
public void mockFinalClassTest_1() {
JcrUtilService jcrUtil;s
ArrayList<Map<String, String>> downloadListFinal;
Map<String, String> n1 = new HashMap<String, String>();
n1.put("a", "a");
n1.put("b", "b");
downloadListFinal.add(n1);
Mockito.when(aFinalClass_mock.getDownloadListFinal()).thenReturn(downloadListFinal);
// Assert the mocked result is returned from method call
//Assert.assertEquals(aFinalClass_mock.getSizeTypeOfAsset(downloadListFinal, getResource(), jcrUtil);, mockedResult);
}
Please Provide me solution or one reference JUnit file where we are using ["
public static ArrayList<Map<String, String>> getSizeTypeOfAsset(ArrayList<Map<String, String>> downloadList,
Resource rs, JcrUtilService jcrUtil) {
log.info("DHLUtil getSizeTypeOfAsset() initiated "); "
] this type of class.
Thanks
you should add:
PowerMockito.mockStatic(DHLUtil.class);
and the you can use this method like any other mock:
when(DHLUtil.getSizeTypeOfAsset()).thenReturn(whatever);
Related
I found article below to do in python.
https://docs.aws.amazon.com/textract/latest/dg/examples-export-table-csv.html
also I used article below to extract text.
https://docs.aws.amazon.com/textract/latest/dg/detecting-document-text.html
but above article helped to get only text, I also used function "block.getBlockType()"
of Block but none of block returned its type as "CELL" even tables are there in image/pdf.
Help me found java library similar to "boto3" to extract all tables.
What I did, I created models of each dataset in the json response and can use this models to build a table view in jsf.
public static List<TableModel> getTablesFromTextract(TextractModel textractModel) {
List<TableModel> tables = null;
try {
if (textractModel != null) {
tables = new ArrayList<>();
List<BlockModel> tableBlocks = new ArrayList<>();
Map<String, BlockModel> blockMap = new HashMap<>();
for (BlockModel block : textractModel.getBlocks()) {
if (block.getBlockType().equals("TABLE")) {
tableBlocks.add(block);
}
blockMap.put(block.getId(), block);
}
for (BlockModel blockModel : tableBlocks) {
Map<Long, Map<Long, String>> rowMap = new HashMap<>();
for (RelationshipModel relationship : blockModel.getRelationships()) {
if (relationship.getType().equals("CHILD")) {
for (String id : relationship.getIds()) {
BlockModel cell = blockMap.get(id);
if (cell.getBlockType().equals("CELL")) {
long rowIndex = cell.getRowIndex();
long columnIndex = cell.getColumnIndex();
if (!rowMap.containsKey(rowIndex)) {
rowMap.put(rowIndex, new HashMap<>());
}
Map<Long, String> columnMap = rowMap.get(rowIndex);
columnMap.put(columnIndex, getCellText(cell, blockMap));
}
}
}
}
tables.add(new TableModel(blockModel, rowMap));
}
System.out.println("row Map " + tables.toString());
}
} catch (Exception e) {
LOG.error("Could not get table from textract model", e);
}
return tables;
}
private static String getCellText(BlockModel cell, Map<String, BlockModel> blockMap) {
String text = "";
try {
if (cell != null
&& CollectionUtils.isNotEmpty(cell.getRelationships())) {
for (RelationshipModel relationship : cell.getRelationships()) {
if (relationship.getType().equals("CHILD")) {
for (String id : relationship.getIds()) {
BlockModel word = blockMap.get(id);
if (word.getBlockType().equals("WORD")) {
text += word.getText() + " ";
} else if (word.getBlockType().equals("SELECTION_ELEMENT")) {
if (word.getSelectionStatus().equals("SELECTED")) {
text += "X ";
}
}
}
}
}
}
} catch (Exception e) {
LOG.error("Could not get cell text of table", e);
}
return text;
}
TableModel to create the view from:
public class TableModel {
private BlockModel table;
private Map<Long, Map<Long, String>> rowMap;
public TableModel(BlockModel table, Map<Long, Map<Long, String>> rowMap) {
this.table = table;
this.rowMap = rowMap;
}
public BlockModel getTable() {
return table;
}
public void setTable(BlockModel table) {
this.table = table;
}
public Map<Long, Map<Long, String>> getRowMap() {
return rowMap;
}
public void setRowMap(Map<Long, Map<Long, String>> rowMap) {
this.rowMap = rowMap;
}
#Override
public String toString() {
return table.getId() + " - " + rowMap.toString();
}
I have something similar:
public class AnalyzeDocument {
public DocumentModel startProcess(byte[] content) {
Region region = Region.EU_WEST_2;
TextractClient textractClient = TextractClient.builder().region(region)
.credentialsProvider(EnvironmentVariableCredentialsProvider.create()).build();
return analyzeDoc(textractClient, content);
}
public DocumentModel analyzeDoc(TextractClient textractClient, byte[] content) {
try {
SdkBytes sourceBytes = SdkBytes.fromByteArray(content);
Util util = new Util();
Document myDoc = Document.builder().bytes(sourceBytes).build();
List<FeatureType> featureTypes = new ArrayList<FeatureType>();
featureTypes.add(FeatureType.FORMS);
featureTypes.add(FeatureType.TABLES);
AnalyzeDocumentRequest analyzeDocumentRequest = AnalyzeDocumentRequest.builder().featureTypes(featureTypes)
.document(myDoc).build();
AnalyzeDocumentResponse analyzeDocument = textractClient.analyzeDocument(analyzeDocumentRequest);
List<Block> docInfo = analyzeDocument.blocks();
// util.displayBlockInfo(docInfo);
PageModel pageModel = util.getTableResults(docInfo);
DocumentModel documentModel = new DocumentModel();
documentModel.getPages().add(pageModel);
Iterator<Block> blockIterator = docInfo.iterator();
while (blockIterator.hasNext()) {
Block block = blockIterator.next();
log.debug("The block type is " + block.blockType().toString());
}
return documentModel;
} catch (TextractException e) {
System.err.println(e.getMessage());
}
return null;
}
and this is the util file:
public PageModel getTableResults(List<Block> blocks) {
List<Block> tableBlocks = new ArrayList<>();
Map<String, Block> blockMap = new HashMap<>();
for (Block block : blocks) {
blockMap.put(block.id(), block);
if (block.blockType().equals(BlockType.TABLE)) {
tableBlocks.add(block);
log.debug("added table: " + block.text());
}
}
PageModel page = new PageModel();
if (tableBlocks.size() == 0) {
return null;
}
int i = 0;
for (Block table : tableBlocks) {
page.getTables().add(generateTable(table, blockMap, i++));
}
return page;
}
private TableModel generateTable(Block table, Map<String, Block> blockMap, int index) {
TableModel model = new TableModel();
Map<Integer, Map<Integer, String>> rows = getRowsColumnsMap(table, blockMap);
model.setTableId("Table_" + index);
for (Map.Entry<Integer, Map<Integer, String>> entry : rows.entrySet()) {
RowModel rowModel = new RowModel();
Map<Integer, String> value = entry.getValue();
for (int i = 0; i < value.size(); i++) {
rowModel.getCells().add(value.get(i));
}
model.getRows().add(rowModel);
}
return model;
}
private Map<Integer, Map<Integer, String>> getRowsColumnsMap(Block block, Map<String, Block> blockMap) {
Map<Integer, Map<Integer, String>> rows = new HashMap<>();
for (Relationship relationship : block.relationships()) {
if (relationship.type().equals(RelationshipType.CHILD)) {
for (String childId : relationship.ids()) {
Block cell = blockMap.get(childId);
if (cell != null) {
int rowIndex = cell.rowIndex();
int colIndex = cell.columnIndex();
if (rows.get(rowIndex) == null) {
Map<Integer, String> row = new HashMap<>();
rows.put(rowIndex, row);
}
rows.get(rowIndex).put(colIndex, getText(cell, blockMap));
}
}
}
}
return rows;
}
public String getText(Block block, Map<String, Block> blockMap) {
String text = "";
if (block.relationships() != null && block.relationships().size() > 0) {
for (Relationship relationship : block.relationships()) {
if (relationship.type().equals(RelationshipType.CHILD)) {
for (String childId : relationship.ids()) {
Block wordBlock = blockMap.get(childId);
if (wordBlock != null && wordBlock.blockType() != null) {
if (wordBlock.blockType().equals(BlockType.WORD))) {
text += wordBlock.text() + " ";
}
}
}
}
}
}
return text;
}
I am a beginner to regex.
I have below String:
fail:2,success:1,fetch:1
Output Map: Get Map which contains all key-values as below:
fail - 2 (key=fail, value=2)
success - 1
fetch - 1
I have tried using below solution:
public static void main(String arg[]) {
String msg = "fail:1,success:1,policyfetch:1";
System.out.println(getKeyValuesFromMsg(msg));
}
public static Map getKeyValuesFromMsg(String msg) {
if (msg != null) {
Map keyvalues = new HashMap();
Pattern p = Pattern.compile("(\\w+):(,+)");
Matcher m = p.matcher(msg);
while (m.find()) {
keyvalues.put(m.group(1), m.group(2));
}
return keyvalues;
} else
return Collections.emptyMap();
}
You can use the split function, The following snippet should work fine
Map<String,String> map = new HashMap();
String str = "fail:2,success:1,fetch:1";
String[] keyValueParts = str.split(",");
for(String s : keyValueParts){
String parts[] = s.split(":");
map.put(parts[0],parts[1]);
}
System.out.println(map);
i would have used below method for the same.
public static void main(String arg[]) {
String msg = "fail:1,success:1,policyfetch:1";
System.out.println(getKeyValuesFromMsg(msg));
}
private static Map<Object, Object> getKeyValuesFromMsg(String msg) {
Map<Object,Object> mapObj = new HashMap<Object,Object>();
for (int i=0;i<msg.split(",").length;i++)
mapObj.put(msg.split(",")[i].split(":")[0],msg.split(",")[i].split(":")[1]);
return mapObj;
}
my solution:
public static Map<String, Integer> trans2Map(String source) {
if (null == source) {
return Collections.emptyMap();
}
Map<String, Integer> result = new HashMap<>();
Arrays.stream(source.split(","))
.filter(pair -> pair.split(":").length == 2)
.forEach(pair -> {
String key = pair.split(":")[0];
Integer value;
try {
value = Integer.parseInt(pair.split(":")[1]);
} catch (Exception e) {
return;
}
result.put(key, value);
});
return result;
}
I do not know why elasticsearch does not set the appropriate values for the index, and type. He needs to retrieve data from the index = auctions and type = auctions like this is in Model:
AuctionIndex.java:
#Document(indexName = "auctions", type = "auctions")
public class AuctionIndex {
#Id
private String id;
private Long cat;
private Long tcat;
private String curr;
private Long price;
private Long start_date;
private Long end_date;
private String title;
private String pow;
private String woj;
private String loc;
private String cat_name;
private Long catdec;
private Long uid;
private Long qty;
...getters and setters...
}
This code works when downloading data as follows:
public Map searchByIndexParams(AuctionIndexSearchParams searchParams, Pageable pageable) {
Map response = new HashMap();
NativeSearchQuery searchQuery = this.getSearchQuery(searchParams, pageable).build();
final FacetedPage<AuctionIndex> search = auctionIndexRepository.search(searchQuery);
List<AuctionIndex> content = search.getContent();
response.put("content", content.stream().map(row -> {
return Auction.builder()
.cat(row.getCat())
.item(Long.parseLong(row.getId()))
.endts(row.getEnd_date())
.startts(row.getStart_date())
.loc(row.getLoc())
.pow(row.getPow())
.woj(row.getWoj())
.price(row.getPrice())
.qty(row.getQty())
.title(row.getTitle())
.user(row.getUid())
.catName(row.getCat_name())
.build();
}).collect(Collectors.toList()));
response.put("first", search.isFirst());
response.put("last", search.isLast());
response.put("number", search.getNumber());
response.put("numberOfElements", search.getNumberOfElements());
response.put("size", search.getSize());
response.put("sort", search.getSort());
response.put("totalElements", search.getTotalElements());
response.put("totalPages", search.getTotalPages());
return response;
}
By downloading all the records in this way:
public Map findAllByIndexParams(AuctionIndexSearchParams searchParams, Pageable pageable) {
List rows = new ArrayList();
Map response = new HashMap();
final List<FilterBuilder> filters = Lists.newArrayList();
final NativeSearchQueryBuilder searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery());
Optional.ofNullable(searchParams.getCategoryId()).ifPresent(v -> filters.add(boolFilter().must(termFilter("cat", v))));
Optional.ofNullable(searchParams.getCurrency()).ifPresent(v -> filters.add(boolFilter().must(termFilter("curr", v))));
Optional.ofNullable(searchParams.getTreeCategoryId()).ifPresent(v -> filters.add(boolFilter().must(termFilter("tcat", v))));
Optional.ofNullable(searchParams.getUid()).ifPresent(v -> filters.add(boolFilter().must(termFilter("uid", v))));
final BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
//access for many uids
if (searchParams.getUids() != null) {
if (searchParams.getItemId() != null || searchParams.getTitle() != null) {
Optional.ofNullable(searchParams.getUids().split(",")).ifPresent(v -> {
filters.add(boolFilter().must(termsFilter("uid", v)));
});
} else {
for (String user : searchParams.getUids().split(",")) {
boolQueryBuilder.should(queryStringQuery(user).field("uid"));
}
}
}
//access for many categories
if (searchParams.getCategories() != null) {
Optional.ofNullable(searchParams.getCategories().split(",")).ifPresent(v -> {
filters.add(boolFilter().must(termsFilter("cat", v)));
});
}
if (searchParams.getItemId() != null) {
boolQueryBuilder.must(queryStringQuery(searchParams.getItemId()).field("_id"));
}
if (Optional.ofNullable(searchParams.getTitle()).isPresent()) {
boolQueryBuilder.must(queryStringQuery(searchParams.getTitle()).analyzeWildcard(true).field("title"));
}
if (searchParams.getLoc() != null) {
filters.add(boolFilter().must(termsFilter("loc", searchParams.getLoc())));
// boolQueryBuilder.must(queryStringQuery(searchParams.getLoc()).field("loc"));
}
if (searchParams.getPow() != null) {
filters.add(boolFilter().must(termsFilter("pow", searchParams.getPow())));
// boolQueryBuilder.must(queryStringQuery(searchParams.getPow()).field("pow"));
}
if (searchParams.getWoj() != null) {
filters.add(boolFilter().must(termsFilter("woj", searchParams.getWoj())));
// boolQueryBuilder.must(queryStringQuery(searchParams.getWoj()).field("woj"));
}
if (searchParams.getCatdec() != null) {
boolQueryBuilder.must(queryStringQuery(String.valueOf(searchParams.getCatdec())).field("catdec"));
}
if (Optional.ofNullable(searchParams.getStartDateFrom()).isPresent()
|| Optional.ofNullable(searchParams.getStartDateTo()).isPresent()) {
filters.add(rangeFilter("start_date").from(searchParams.getStartDateFrom()).to(searchParams.getStartDateTo()));
}
if (Optional.ofNullable(searchParams.getEndDateFrom()).isPresent()
|| Optional.ofNullable(searchParams.getEndDateTo()).isPresent()) {
filters.add(rangeFilter("end_date").from(searchParams.getEndDateFrom()).to(searchParams.getEndDateTo()));
}
if (Optional.ofNullable(searchParams.getPriceFrom()).isPresent()
|| Optional.ofNullable(searchParams.getPriceTo()).isPresent()) {
filters.add(rangeFilter("price").from(searchParams.getPriceFrom()).to(searchParams.getPriceTo()));
}
searchQuery.withQuery(boolQueryBuilder);
FilterBuilder[] filterArr = new FilterBuilder[filters.size()];
filterArr = filters.toArray(filterArr);
searchQuery.withFilter(andFilter(filterArr));
if (searchParams.getOrderBy() != null && searchParams.getOrderDir() != null) {
if (searchParams.getOrderDir().toLowerCase().equals("asc")) {
searchQuery.withSort(SortBuilders.fieldSort(searchParams.getOrderBy()).order(SortOrder.ASC));
} else {
searchQuery.withSort(SortBuilders.fieldSort(searchParams.getOrderBy()).order(SortOrder.DESC));
}
}
String scrollId = searchTemplate.scan(searchQuery.build(), 100000, false);
System.out.println(scrollId);
Page<AuctionIndex> page = searchTemplate.scroll(scrollId, 500000, AuctionIndex.class);
System.out.println(page.getTotalElements());
if (page.hasContent()) {
while (true) {
for (AuctionIndex hit : page.getContent()) {
Auction row = Auction.builder()
.cat(hit.getCat())
.item(Long.parseLong(hit.getId()))
.endts(hit.getEnd_date())
.startts(hit.getStart_date())
.loc(hit.getLoc())
.pow(hit.getPow())
.woj(hit.getWoj())
.price(hit.getPrice())
.qty(hit.getQty())
.title(hit.getTitle())
.user(hit.getUid())
.catName(hit.getCat_name())
.build();
rows.add(row);
}
page = searchTemplate.scroll(scrollId, 500000, AuctionIndex.class);
if (page.hasContent() == false) {
break;
}
}
}
response.put("content", rows);
return response;
}
AuctionService.java:
private AuctionRepository auctionRepository;
private AuctionIndexRepository auctionIndexRepository;
#Autowired
public AuctionService(AuctionRepository auctionRepository, AuctionIndexRepository auctionIndexRepository) {
this.auctionRepository = auctionRepository;
this.auctionIndexRepository = auctionIndexRepository;
}
#Autowired
private ElasticsearchTemplate searchTemplate;
AuctionIndexRepository.java:
public interface AuctionIndexRepository extends ElasticsearchRepository<AuctionIndex, Integer> {
}
AuctionRepository.java:
#Repository
public class AuctionRepository {
private final AerospikeClient aerospikeClient;
#Autowired
public AuctionRepository(AerospikeClient aerospikeClient) {
this.aerospikeClient = aerospikeClient;
}
/**
*
* #param auctionId
* #param transactionIndexId
* #return
*/
public Map findTransactionAuctionById(Long auctionId, String transactionIndexId) {
final Statement stmt = new Statement();
stmt.setNamespace(NAMESPACE_ALLEK);
stmt.setSetName(SET_U);
final Map<String, Object> aMap = findAuctionUserInSetA(auctionId);
final Key uKey = new Key(NAMESPACE_ALLEK, SET_U, aMap.get("u") + "_" + aMap.get("p"));
final Object uRecord = aerospikeClient.execute(null, uKey, NAMESPACE_ALLEK, FUN_FIND_U_ITEM, Value.get(auctionId));
return parseTransactionAuction((HashMap) uRecord, auctionId, transactionIndexId);
}
/**
*
* #param r
* #return
*/
private Map parseTransactionAuction(HashMap r, Long auctionId, String transactionIndexId) {
return parseTransactionAuction(new Record(r, 0, 0), auctionId, transactionIndexId);
}
/**
*
* #param r rekord z aerospike
* #return
* #return
*/
private Map parseTransactionAuction(Record r, Long auctionId, String transactionIndexId) {
Map response = new HashMap();
final Object recordTrans = r.getValue("t");
final ArrayList<HashMap> trans = Optional.ofNullable(recordTrans).isPresent() ? (ArrayList<HashMap>) recordTrans : new ArrayList<>();
Object qty = 0;
Object price = 0;
for (HashMap hit : trans) {
if (transactionIndexId.equals(auctionId + "_" + hit.get("buyer") + "_" + hit.get("ts"))) {
qty = hit.get("qty");
price = hit.get("price");
break;
}
}
response.put("qty", qty);
response.put("price", price);
response.put("startts", r.getLong("startts"));
response.put("endts", r.getLong("endts"));
response.put("qty_auction", r.getLong("qty"));
return response;
}
public AuctionRaw findAuctionRawById(Long auctionId) {
final Statement stmt = new Statement();
stmt.setNamespace(NAMESPACE_ALLEK);
stmt.setSetName(SET_U);
final Map<String, Object> aMap = findAuctionUserInSetA(auctionId);
final Key uKey = new Key(NAMESPACE_ALLEK, SET_U, aMap.get("u") + "_" + aMap.get("p"));
final Object uRecord = aerospikeClient.execute(null, uKey, NAMESPACE_ALLEK, FUN_FIND_U_ITEM, Value.get(auctionId));
return parseAuctionRaw((HashMap) uRecord);
}
private AuctionRaw parseAuctionRaw(HashMap r) {
return parseAuctionRaw(new Record(r, 0, 0));
}
private AuctionRaw parseAuctionRaw(Record r) {
return AuctionRaw.builder()
.cat(r.getLong("cat"))
.len(r.getInt("len"))
.start(r.getLong("start"))
.build();
}
public Auction findAuctionById(Long auctionId) {
final Statement stmt = new Statement();
stmt.setNamespace(NAMESPACE_ALLEK);
stmt.setSetName(SET_U);
final Map<String, Object> aMap = findAuctionUserInSetA(auctionId);
final Key uKey = new Key(NAMESPACE_ALLEK, SET_U, aMap.get("u") + "_" + aMap.get("p"));
final Object uRecord = aerospikeClient.execute(null, uKey, NAMESPACE_ALLEK, FUN_FIND_U_ITEM, Value.get(auctionId));
return parseAuction((HashMap) uRecord);
}
public Map<String, Object> findAuctionUserInSetA(Long auctionId) {
final Statement stmt = new Statement();
stmt.setNamespace(NAMESPACE_ALLEK);
stmt.setSetName(SET_U);
final Key aKey = new Key(NAMESPACE_ALLEK, SET_A, Value.get(auctionId / 1024));
final Map<String, Object> aMap = (Map<String, Object>) aerospikeClient.execute(null, aKey, NAMESPACE_ALLEK, FUN_FIND_A_ITEM, Value.get(auctionId));
return aMap;
}
public List<Auction> findAuctionByUserId(Long userId) {
final Statement stmt = new Statement();
stmt.setNamespace(NAMESPACE_ALLEK);
stmt.setSetName(SET_U);
stmt.setFilters(Filter.equal("u", userId));
final RecordSet records = aerospikeClient.query(null, stmt);
return StreamSupport.stream(records.spliterator(), true)
.flatMap(l -> {
final ArrayList<HashMap> auctionsFromRecord = (ArrayList<HashMap>) l.record.getValue("v");
return Optional.ofNullable(auctionsFromRecord).isPresent() ? auctionsFromRecord.stream() : Stream.<HashMap>empty();
})
.map(r -> parseAuction(r))
.collect(Collectors.toList());
}
private Auction parseAuction(HashMap r) {
return parseAuction(new Record(r, 0, 0));
}
private Auction parseAuction(Record r) {
// final Object recordTrans = r.getValue("t");
// final ArrayList<HashMap> trans = Optional.ofNullable(recordTrans).isPresent() ? (ArrayList<HashMap>) recordTrans : new ArrayList<>();
// final List<Transaction> transactions = trans.stream()
// .map(m -> {
// HashMap recordComment = (HashMap) m.get("c");
// Comment comment = null;
// if (recordComment != null && recordComment.size() > 0) {
// comment = Comment.builder()
// .id((Long) recordComment.get("id"))
// .ts((Long) recordComment.get("ts"))
// .text((String) recordComment.get("text"))
// .type((Long) recordComment.get("type"))
// .build();
// }
// return Transaction.builder()
// .ts((Long) m.get("ts"))
// .qty((Long) m.get("qty"))
// .price((Long) m.get("price"))
// .c(comment)
// .buyer((Long) m.get("buyer"))
// .build();
// })
// .collect(Collectors.toList());
return Auction.builder()
.item(r.getLong("item"))
.startts(r.getLong("startts"))
.endts(r.getLong("endts"))
.user(r.getLong("user"))
.qty(r.getLong("qty"))
.price(r.getLong("price"))
.title(r.getString("title"))
.cat(r.getLong("cat"))
// .tcat(r.getLong("tcat"))
// .curr(r.getString("curr"))
.loc(r.getString("loc"))
.woj(r.getString("woj"))
.pow(r.getString("pow"))
.catName(r.getString("cat_name"))
// .t(transactions)
// .len(r.getInt("len"))
// .detSt(r.getLong("det_st"))
// .detLen(r.getLong("det_len"))
// .start(r.getLong("start"))
.build();
}
}
I do not know why but scroll retrieves the data from the old the index = allek and type = auctions.
How do I know that the old index? And so the result in the old Index is equal to 16k (there is just more data and there are other fields than in the new Index) while in the new Index is the records of about 400.
My question is why is this happening? What I should change to be able to use the scrollbar configuration index = auctions and type = auctions?
I ask you for help I have no idea why this is happening.
I have MaterailInfo and StyleInfo, I want to set styleDescription based on StyleNumber matching with materialNumber. I am using 2 for loops, is there any alternative solution?
MaterailInfo:
class MaterailInfo {
private String materialNumber;
private String materialDescription;
public MaterailInfo(String materialNumber, String materialDescription) {
this.materialNumber = materialNumber;
this.materialDescription = materialDescription;
}
// getter setter methods
}
StyleInfo:
class StyleInfo {
private String StyleNumber;
private String styleDescription;
public StyleInfo(String styleNumber, String styleDescription) {
StyleNumber = styleNumber;
this.styleDescription = styleDescription;
}
// getter setter toString methods
}
TEst12:
public class TEst12 {
public static void main(String[] args) {
List<MaterailInfo> mList = new ArrayList<MaterailInfo>();
mList.add(new MaterailInfo("a", "a-desc"));
mList.add(new MaterailInfo("b", "b-desc"));
mList.add(new MaterailInfo("c", "c-desc"));
List<StyleInfo> sList = new ArrayList<StyleInfo>();
sList.add(new StyleInfo("a", ""));
sList.add(new StyleInfo("b", ""));
sList.add(new StyleInfo("c", ""));
for (MaterailInfo m : mList) {
for (StyleInfo s : sList) {
if (s.getStyleNumber().equals(m.getMaterialNumber())) {
s.setStyleDescription(m.getMaterialDescription());
}
}
}
System.out.println(sList);
}
}
If you use a Map instead of a List to store your data, you can get away with doing only a single loop:
Map<String, String> mMap = new HashMap<String, String>();
mMap.put("a", "a-desc");
mMap.put("b", "b-desc");
mMap.put("c", "c-desc");
Map<String, String> sMap = new HashMap<String, String>();
sMap.put("a", "");
sMap.put("b", "");
sMap.put("c", "");
for (Map.Entry<String, String> entry : mMap.entrySet()) {
sMap.put(entry.getKey(), mMap.get(entry.getKey());
}
This code will leave the style description empty if the style number does not match any known material number.
If your numbers can't have duplicates, using a HashMap instead of classes can be a bit faster.
import java.io.*;
import java.util.*;
import java.util.Map.Entry;
public class Demo {
public static void main(String[] args) {
HashMap<String, String> mList = new HashMap();
HashMap<String, String> sList = new HashMap();
mList.put("a", "a-desc");
mList.put("b", "b-desc");
mList.put("c", "c-desc");
sList.put("a", "");
sList.put("b", "");
sList.put("c", "");
Iterator entries = sList.entrySet().iterator();
while (entries.hasNext()) {
Entry entry = (Entry) entries.next();
if (mList.containsKey(entry.getKey())) {
sList.put((String) entry.getKey(), mList.get(entry.getKey()));
}
}
for (Map.Entry<String, String> entry : sList.entrySet()) {
System.out.println(entry.getKey() + " " + entry.getValue());
}
}
}
You can do this using one for loop like this
for (int i = 0; i < mList.size(); i++) {
sList.get(i).setStyleDescription(mList.get(i).getMaterialDescription());
}
Note: i am assuming you have balanced lists in term of size.
I found this older article how-to-monitor-the-computers-cpu-memory-and-disk-usage-in-java
and wated to ask, if there is something new in java 7. I want to get the current CPU-, RAM- and netzwork-Usage of my App periodically. It has to work for linux (mac) and windows. The data must not be extremely detailed, 3 values would be enough (cpu: 10%, Ram 4%, Network 40%). Would be cool if the data is just for the app and not the whole os-system, however this would work, too.
Thank's for help
answering my own question ;P some code i have written...
NetworkData:
public class NetworkData {
static Map<String, Long> rxCurrentMap = new HashMap<String, Long>();
static Map<String, List<Long>> rxChangeMap = new HashMap<String, List<Long>>();
static Map<String, Long> txCurrentMap = new HashMap<String, Long>();
static Map<String, List<Long>> txChangeMap = new HashMap<String, List<Long>>();
private static Sigar sigar;
/**
* #throws InterruptedException
* #throws SigarException
*
*/
public NetworkData(Sigar s) throws SigarException, InterruptedException {
sigar = s;
getMetric();
System.out.println(networkInfo());
Thread.sleep(1000);
}
public static void main(String[] args) throws SigarException,
InterruptedException {
new NetworkData(new Sigar());
NetworkData.startMetricTest();
}
public static String networkInfo() throws SigarException {
String info = sigar.getNetInfo().toString();
info += "\n"+ sigar.getNetInterfaceConfig().toString();
return info;
}
public static String getDefaultGateway() throws SigarException {
return sigar.getNetInfo().getDefaultGateway();
}
public static void startMetricTest() throws SigarException, InterruptedException {
while (true) {
Long[] m = getMetric();
long totalrx = m[0];
long totaltx = m[1];
System.out.print("totalrx(download): ");
System.out.println("\t" + Sigar.formatSize(totalrx));
System.out.print("totaltx(upload): ");
System.out.println("\t" + Sigar.formatSize(totaltx));
System.out.println("-----------------------------------");
Thread.sleep(1000);
}
}
public static Long[] getMetric() throws SigarException {
for (String ni : sigar.getNetInterfaceList()) {
// System.out.println(ni);
NetInterfaceStat netStat = sigar.getNetInterfaceStat(ni);
NetInterfaceConfig ifConfig = sigar.getNetInterfaceConfig(ni);
String hwaddr = null;
if (!NetFlags.NULL_HWADDR.equals(ifConfig.getHwaddr())) {
hwaddr = ifConfig.getHwaddr();
}
if (hwaddr != null) {
long rxCurrenttmp = netStat.getRxBytes();
saveChange(rxCurrentMap, rxChangeMap, hwaddr, rxCurrenttmp, ni);
long txCurrenttmp = netStat.getTxBytes();
saveChange(txCurrentMap, txChangeMap, hwaddr, txCurrenttmp, ni);
}
}
long totalrxDown = getMetricData(rxChangeMap);
long totaltxUp = getMetricData(txChangeMap);
for (List<Long> l : rxChangeMap.values())
l.clear();
for (List<Long> l : txChangeMap.values())
l.clear();
return new Long[] { totalrxDown, totaltxUp };
}
private static long getMetricData(Map<String, List<Long>> rxChangeMap) {
long total = 0;
for (Entry<String, List<Long>> entry : rxChangeMap.entrySet()) {
int average = 0;
for (Long l : entry.getValue()) {
average += l;
}
total += average / entry.getValue().size();
}
return total;
}
private static void saveChange(Map<String, Long> currentMap,
Map<String, List<Long>> changeMap, String hwaddr, long current,
String ni) {
Long oldCurrent = currentMap.get(ni);
if (oldCurrent != null) {
List<Long> list = changeMap.get(hwaddr);
if (list == null) {
list = new LinkedList<Long>();
changeMap.put(hwaddr, list);
}
list.add((current - oldCurrent));
}
currentMap.put(ni, current);
}
}
CPU-Data:
public class CpuData {
private static Sigar sigar;
public CpuData(Sigar s) throws SigarException {
sigar = s;
System.out.println(cpuInfo());
}
public static void main(String[] args) throws InterruptedException, SigarException {
new CpuData(new Sigar());
CpuData.startMetricTest();
}
private static void startMetricTest() throws InterruptedException, SigarException {
new Thread() {
public void run() {
while(true)
BigInteger.probablePrime(MAX_PRIORITY, new Random());
};
}.start();
while(true) {
String pid = ""+sigar.getPid();
System.out.print(getMetric(pid));
for(Double d:getMetric()){
System.out.print("\t"+d);
}
System.out.println();
Thread.sleep(1000);
}
}
public String cpuInfo() throws SigarException {
CpuInfo[] infos = sigar.getCpuInfoList();
CpuInfo info = infos[0];
String infoString = info.toString();
if ((info.getTotalCores() != info.getTotalSockets())
|| (info.getCoresPerSocket() > info.getTotalCores())) {
infoString+=" Physical CPUs: " + info.getTotalSockets();
infoString+=" Cores per CPU: " + info.getCoresPerSocket();
}
long cacheSize = info.getCacheSize();
if (cacheSize != Sigar.FIELD_NOTIMPL) {
infoString+="Cache size...." + cacheSize;
}
return infoString;
}
public static Double[] getMetric() throws SigarException {
CpuPerc cpu = sigar.getCpuPerc();
double system = cpu.getSys();
double user = cpu.getUser();
double idle = cpu.getIdle();
// System.out.println("idle: " +CpuPerc.format(idle) +", system: "+CpuPerc.format(system)+ ", user: "+CpuPerc.format(user));
return new Double[] {system, user, idle};
}
public static double getMetric(String pid) throws SigarException {
ProcCpu cpu = sigar.getProcCpu(pid);
// System.out.println(sigar.getProcFd(pid));
// System.err.println(cpu.toString());
return cpu.getPercent();
}
}
RAM-Data:
public class RamData {
private static Sigar sigar;
private static Map<String, Long> pageFoults;
public RamData(Sigar s) throws SigarException {
sigar = s;
System.out.println(getMetric().toString());
}
public static void main(String[] args) throws SigarException,
InterruptedException {
new RamData(new Sigar());
RamData.startMetricTest();
}
public static void startMetricTest() throws SigarException,
InterruptedException {
while (true) {
Map<String, String> map = RamData.getMetric("" + sigar.getPid());
System.out.println("Resident: \t\t"
+ Sigar.formatSize(Long.valueOf(map.get("Resident"))));
System.out.println("PageFaults: \t\t" + map.get("PageFaults"));
System.out.println("PageFaultsTotal:\t" + map.get("PageFaultsTotal"));
System.out.println("Size: \t\t"
+ Sigar.formatSize(Long.valueOf(map.get("Size"))));
Map<String, String> map2 = getMetric();
for (Entry<String, String> e : map2.entrySet()) {
String s;
try {
s = Sigar.formatSize(Long.valueOf(e.getValue()));
} catch (NumberFormatException ex) {
s = ((int) (double) Double.valueOf(e.getValue())) + "%";
}
System.out.print(" " + e.getKey() + ": " + s);
}
System.out.println("\n------------------");
Thread.sleep(1000);
}
}
public static Map<String, String> getMetric() throws SigarException {
Mem mem = sigar.getMem();
return (Map<String, String>) mem.toMap();
}
public static Map<String, String> getMetric(String pid)
throws SigarException {
if (pageFoults == null)
pageFoults = new HashMap<String, Long>();
ProcMem state = sigar.getProcMem(pid);
Map<String, String> map = new TreeMap<String, String>(state.toMap());
if (!pageFoults.containsKey(pid))
pageFoults.put(pid, state.getPageFaults());
map.put("PageFaults", ""
+ (state.getPageFaults() - pageFoults.get(pid)));
map.put("PageFaultsTotal", ""+state.getPageFaults());
return map;
}
}
PROCES-Data:
public class ProcessData {
private static Sigar sigar;
public ProcessData(Sigar s) throws SigarException {
this.sigar = s;
System.out.println(getMetric().toString());
System.out.println(getMetric(getPidString()).toString());
}
public static void main(String[] args) throws SigarException {
new ProcessData(new Sigar());
System.out.println(ProcessData.getMetric());
System.out.println(ProcessData.getMetric(getPidString()));
}
public static Map<String, String> getMetric() throws SigarException {
ProcStat state = sigar.getProcStat();
return (Map<String, String>) state.toMap();
}
public static Map<String, String> getMetric(String pid) throws SigarException {
ProcState state = sigar.getProcState(pid);
return (Map<String, String>) state.toMap();
}
public static long getPid() {
return sigar.getPid();
}
public static String getPidString() {
return ""+sigar.getPid();
}
}
why cant you just use like bellow,
try {
for (String ni : sigar.getNetInterfaceList()) {
NetInterfaceStat netStat = sigar.getNetInterfaceStat(ni);
total+=netStat.getRxBytes();
}
} catch (SigarException e) {
e.printStackTrace();
}
what is the difference???
I would use the Metrics java library: http://metrics.codahale.com/
It comes with Sigar integration: https://github.com/cb372/metrics-sigar
Not much has changed since then unless it was done by another group.
below are some of the significant changes that were included in Java 7 SE. Sadly none of them are what you are looking for.
http://www.oracle.com/technetwork/java/javase/jdk7-relnotes-418459.html
javamelody i have used before it is pretty simple and I was able to get it running in a short amount of time
https://code.google.com/p/javamelody/
http://demo.javamelody.cloudbees.net/monitoring
below is another alternative
https://github.com/oshi/oshi
OSHI WILL NOT GIVE YOU NETWORK USAGE OR LATENCY PER PROCESS
Check This :
Java (Windows) - By process id, get memory usage, disk usage, network usage