Friday 29 January 2016

How to find count of multiple fields in MongoDb

Problem Statement:
I have a use case where i need to find the count of activity done on JobId.
I have a mongodb collection jobslog which has following data

jobId,action
1 a (apply)
2       v (view)
3      a (apply)
1 v (view)

I knew the query in mysql
select count(a),count(v) from jobslog group by jobId

But I do not know how to do it in Mongo.So i did lots of search on google for that.

Solution:
Query In MongoDB
db.jobslog.aggregate([
                    { $group: {
                        _id: "$jobId",
                         view_count: { $sum: {
                                        $cond: [ { $eq: [ '$a', 'v' ] }, 1, 0 ]
                                     } },
                         apply_count: { $sum: {
                                        $cond: [ { $eq: [ '$a', 'a' ] }, 1, 0 ]
                                     } },
                     }},
                     { $out : output_collection_name }

])

advantage of using spring hibernate over hibernate alone

Spring provides hibernate template and it has many advantages like
1) It removes boiler plate code like getting connection from data source try/catch block for closing connection. So that developer can focus on writing business logic rather then writing boilier plate code every where.

2) Spring hibernateTemplate also throws RunTime exception compared to checkd exception which allows to remove writing try/catch block in each DAO.

3) It also gives richer template class using which developer can write query code easily. This template class also allows to get session explicitly so if developer wants to get session object and work on it then it's possible.

difference between session.flush() and commit() in hibernate

Commit will make the database commit
- Flushing is the process of synchronizing the underlying persistent
store with persistable state held in memory.
ie. it will update or insert into your tables in the running
transaction, but it _may_ not commit those changes (this depends on
your flush mode).
commit can not be rolback

Configure Url rewrite filter to make url seo friendly in struts 2

UrlRewrite Filter


Configuring Rules

1. type="forward"
This is the same as doing:
RequestDispatcher rq = request.getRequestDispatcher([to value]);
rq.forward(request, response);

- Use if you want to have friendly url's / RESTful urls / transparently forward request,
i.e. from /abc to /xyz without the user noticing it

- Also need to add :
 <dispatcher>FORWARD</dispatcher>
 <dispatcher>REQUEST</dispatcher>
entries for all filters to the <filter-mapping/> section of your apps
web.xml. By default filters are only run for REQUEST (i.e. from an external
http request) not for forwarded (or included for that matter) requests.

2. type="permanent-redirect" (301)
response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
response.setHeader("Location", [to value]);
(note, SC_MOVED_TEMPORARILY is HTTP status code 301)

3. type="temporary-redirect" (302)
response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
response.setHeader("Location", [to value]);
(note, SC_MOVED_TEMPORARILY is HTTP status code 302)

4. Adding Request Parameters to from
- Add it as &amp; E.g.:
<to>/world.jsp?country=$1&amp;city=$2</to>

Dynamically Reload Config
Change in web.xml
 <!-- set the amount of seconds the conf file will be checked for reload
    can be a valid integer (0 denotes check every time,
          empty/not set denotes no reload check) -->
    <init-param>
    <param-name>confReloadCheckInterval</param-name>
    <param-value>60</param-value>
    </init-param>

Debugging
Change in web.xml
 <!-- sets up log level (will be logged to context log)
    can be: TRACE, DEBUG, INFO (default), WARN, ERROR, FATAL, log4j, commons, sysout:{level} (ie, sysout:DEBUG)
          if you are having trouble using normal levels use sysout:DEBUG -->
    <init-param>
    <param-name>logLevel</param-name>
    <param-value>DEBUG</param-value>
    </init-param>

how to make maven project compatiable with eclipse and tomcat

mvn eclipse:eclipse -Dwtpversion=1.0

proxy setting for maven 2 in setting.xml

<proxies>
<proxy>
<id>myproxy</id>
<active>true</active>
<protocol>http</protocol>
<host>proxy.somewhere.com</host>
<port>8080</port>
<username>proxyuser</username>
<password>somepassword</password>
<nonProxyHosts>*.google.com|ibiblio.org</nonProxyHosts>
</proxy>
</proxies> - See more at: http://mail.schoolspedia.com/article/proxy-setting-maven-2-settingxml#sthash.YjPR9d2V.dpuf

struts 2 namespace resolution issue



  <constant name="struts.mapper.alwaysSelectFullNamespace" value="true" />

how to make drupal to work only for case sensitive url

Run below query on your drupal database
ALTER TABLE `url_alias` CHANGE `dst` `dstVARCHAR( 128 ) CHARACTER SET latin1 COLLATE latin1_general_cs NOT NULL

setting up crontab with cpanel drupal

Cron tasks
Many Drupal modules (such as the search functionality) have periodic tasks that must be triggered by a cron job. To activate these tasks, call the cron page by visiting http://www.example.com/cron.php -- this will pass control to the modules and the modules will decide if and what they must do. To call cron.php periodically you need to setup a cron job.
cPanel offers a way to set cronjobs via Server » Cron jobs. Carefully read the instructions on the screen; they may differ from the following example.
To run cron.php every day set the Minute and Hour fields to 0 and the rest of the fields to *. The command to run is:

Thursday 28 January 2016

uppercase to lower case mysql query

uppercase to lower case query

To select all the data from the above table and convert it to lower case you would do this:
SELECT LOWER(name) FROM mytable
This would return this:
+----------------------------+
| LOWER(name)                |
+----------------------------+
| lorem ipsum dolor sit amet |
| ut purus est               |
| leo sed condimentum semper |
| donec velit neque          |
| maecenas ullamcorper       |
+----------------------------+

Hiding jsessionid parameter from Google using apache

If you're running a website on JBoss you may discover that Google has indexed your pages with a jsessionid query parameter in the links.
The Google crawl bot does not support cookies, therefore JBoss uses the jsessionid query parameter in order to maintain a session state without cookies. These query parameters can impact your Google rank and indexing efficiency as the same page can be indexed multiple times with different session ids, and dilute your ranking. Also, it leads to ugly links.
If you want to still be able to support non-cookie using users, but would like Google to see cleaner links, you can use Apache's mod_rewrite to modify the links for the Google bot only, leaving the normal functionality available to the rest of your users.
Assuming you have mod_rewrite enabled in your Apache instance, use this configuration in your apache config:
 # This should strip out jsessionids from google
 RewriteCond %{HTTP_USER_AGENT} (googlebot) [NC]
 ReWriteRule ^(.*);jsessionid=[A-Za-z0-9]+(.*)$ $1$2 [L,R=301]
This rule says for request where the user agent contains "googlebot" (with case insensitive matching), rewrite the URL without the jsessionid. It seems to work nicely.

how to change created date by node import from bulk update

update `users` set access=1266747689-(7828-uid)*20000 where access like '126674%'

Dashes vs. underscores whch is more SEO friendly

Dashes vs. underscores

I often get asked whether I’d recommend dashes or underscores for words in urls. For urls in Google, I would recommend using dashes. Why? To find out, let’s take a trip in the Google Time Machine. Set the dial for 1999, the year Matt first discovered Google. Matt was using, I dunno, maybe HotBot at that point? The curtain rises:
Matt: Hmm, this search for [FTP_BINARY] didn’t turn out the way I wanted. I got a couple scuzzy looking urls, and the other documents just have the words “FTP” and “BINARY” but the term “FTP_BINARY” doesn’t actually appear. (Note: Matt was a bit of a nerd, as you can tell.)
Some Random Person That I Don’t Remember: Have you tried Google?
Matt: What’s that?
SROTIDR: It’s a search engine written by nerds for nerds! They index numbers! Sometimes they even index punctuation, like “C++”. Try your underscore search there.
Matt: Okay, here goes. Whoa! They actually return pages with the literal string “FTP_BINARY”! That’s wicked cool! (Did I mention Matt was a nerd? Big-time nerd.)
SROTIDR: Yeah. The wild thing is that they wrote a paper about how they crawl the web and rank pages.
Matt: Well, now that’s just silly. I wonder why they didn’t keep it a secret? I bet those papers will make great reading for my information retrieval class.
I’ve stylized the conversation quite a bit, but I remember how impressed I was that Google indexed numbers and some punctuation (come to think of it, search engines have come a long way in five years). With underscores, Google’s programmer roots are showing. Lots of computer programming languages have stuff like _MAXINT, which may be different than MAXINT. So if you have a url like word1_word2, Google will only return that page if the user searches for word1_word2 (which almost never happens). If you have a url like word1-word2, that page can be returned for the searches word1word2, and even “word1 word2″.
That’s why I would always choose dashes instead of underscores. To answer a common question, Google doesn’t algorithmically penalize for dashes in the url. Of course I can only speak for Google, not other search engines. And bear in mind that if your domain looks like www.buy-cheap-viagra-online-while-consolidating-your-debt-so-you-can-play-texas-holdem-while-watching-porn.com, that may still attract attention for other reasons. \":)\"

Example Usage

When http://d.com/the_top_apache_htaccess_article.html is requested by a browser or search engine, a Search-Engine Friendly 301 Redirect is sent telling the client that the correct location for the resource is at http://d.com/the-top-apache-htaccess-article.html

The Apache .htaccess underscore to hyphen conversion code


  RewriteRule ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4-$5-$6-$7 [L,R=301]
   RewriteRule ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4-$5-$6 [L,R=301]
   RewriteRule ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4-$5 [L,R=301]
   RewriteRule ^([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4 [L,R=301]
   RewriteRule ^([^_]*)_([^_]*)_(.*)$ $1-$2-$3 [L,R=301]
   RewriteRule ^([^_]*)_(.*)$ $1-$2 [L,R=301]

How to randomize names in column in excel

=INDEX(A:A,INT((RAND()*1000)+1),1)

SEO issue when a webpage is accessiable using HTTP and HTTPS both

1 Problem Statement:If a site is severed on both HTTP and HTTPS . For same content we have http and https URL crawled by Google. 


Solutions: To Avoid duplicate content issue we should remove https URL from google index. To do the we will show different robots.txt content for HTTPS RewriteRule ^/robots.txt$ /robots_ssl.txt [PT,L] robots_ssl.txt content is given below User-agent: * Disallow: /

jk_lb_worker.c All tomcat instances failed, no more workers left for recovery

We are getting a issue on our site getting down with following message in mod_jk.log
[Tue Sep 03 16:22:34 2013][22149:1585183040] [info] service::jk_lb_worker.c (1448): All tomcat instances failed, no more workers left for recovery (attempt=0, retry=0)
[Tue Sep 03 16:22:34 2013][22149:1585183040] [info] service::jk_lb_worker.c (1457): All tomcat instances failed, no more workers left (attempt=1, retry=0)
[Tue Sep 03 16:22:34 2013][22149:1501264192] [info] service::jk_lb_worker.c (1448): All tomcat instances failed, no more workers left for recovery (attempt=0, retry=0)
[Tue Sep 03 16:22:34 2013][22149:1501264192] [info] service::jk_lb_worker.c (1457): All tomcat instances failed, no more workers left (attempt=1, retry=0)
[Tue Sep 03 16:22:34 2013][22149:1616652608] [info] service::jk_lb_worker.c (1457): All tomcat instances failed, no more workers left (attempt=0, retry=1)
[Tue Sep 03 16:22:34 2013][22149:1616652608] [info] service::jk_lb_worker.c (1457): All tomcat instances failed, no more workers left (attempt=1, retry=1)
[Tue Sep 03 16:22:34 2013][22149:1616652608] [info] service::jk_lb_worker.c (1468): All tomcat instances are busy or in error state
[Tue Sep 03 16:22:34 2013][22149:1616652608] [error] service::jk_lb_worker.c (1473): All tomcat instances failed, no more workers left
[Tue Sep 03 16:22:34 2013]loadbalancer - naf.timesjobs.com 0 0.100549 POST /naf/log.html 200
[Tue Sep 03 16:22:34 2013][22149:1616652608] [info] jk_handler::mod_jk.c (2627): Service error=0 for worker=loadbalancer
[Tue Sep 03 16:22:34 2013][22149:1606162752] [info] service::jk_lb_worker.c (1457): All tomcat instances failed, no more workers left (attempt=0, retry=1)
[Tue Sep 03 16:22:34 2013][22149:1606162752] [info] service::jk_lb_worker.c (1457): All tomcat instances failed, no more workers left (attempt=1, retry=1)
[Tue Sep 03 16:22:34 2013][22149:1606162752] [info] service::jk_lb_worker.c (1468): All tomcat instances are busy or in error state
[Tue Sep 03 16:22:34 2013][22149:1606162752] [error] service::jk_lb_worker.c (1473): All tomcat instances failed, no more workers left
[Tue Sep 03 16:22:34 2013]loadbalancer - naf.timesjobs.com 0 0.100469 POST /naf/log.html 200
[Tue Sep 03 16:22:34 2013][22149:1606162752] [info] jk_handler::mod_jk.c (2627): Service error=0 for worker=loadbalancer
[Tue Sep 03 16:22:34 2013][22149:1690081600] [info] service::jk_lb_worker.c (1448): All tomcat instances failed, no more workers left for recovery (attempt=0, retry=0)
[Tue Sep 03 16:22:34 2013][22149:1690081600] [info] service::jk_lb_worker.c (1457): All tomcat instances failed, no more workers left (attempt=1, retry=0) 
mod-jk.conf

<IfModule mpm_worker_module>
    ThreadLimit         60
    ServerLimit         10
    StartServers        3
    MinSpareThreads     5
    MaxSpareThreads     20
    MaxClients          600
    ThreadsPerChild     60
    MaxRequestsPerChild 0
</IfModule>

While in tomcat server.xml   <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" maxThreads="400" acceptCount="100"/>

Due to which the above error is coming at peak load when apache 600 request coming while tomcat is able to serve only 400.


We changed the setting  <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" maxThreads="600" acceptCount="100"/>

and the problem got resolved.