Connect to hbase from remote machine - hadoop

I have VM where habse is installed.
IP:192.168.20.10
I want to try to connect to hbase from my desktop:
Here is what I am trying>
public static void main(String[] args) throws IOException {
AbstractApplicationContext context = new ClassPathXmlApplicationContext("/META-INF/spring/hbase-beans.xml", HBaseConnection.class);
context.registerShutdownHook();
UserRepository userRepository = context.getBean(UserRepository.class);
List<User> users = userRepository.findAll();
System.out.println("Number of users = " + users.size());
System.out.println(users);
}
public List<User> findAll() {
return hbaseTemplate.find(tableName, "cfInfo", new RowMapper<User>() {
public User mapRow(Result result, int rowNum) throws Exception {
return new User(Bytes.toString(result.getValue(CF_INFO, qUser)),
Bytes.toString(result.getValue(CF_INFO, qEmail)),
Bytes.toString(result.getValue(CF_INFO, qPassword))
);
}
});
}
And this is my bean.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:hadoop="http://www.springframework.org/schema/hadoop" xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/hadoop http://www.springframework.org/schema/hadoop/spring-hadoop.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<context:component-scan base-package="com.hbase.dao"/>
<context:component-scan base-package="com.hbase.object"/>
<hadoop:configuration id="hadoopConfiguration">fs.default.name=hdfs://192.168.20.10:9000</hadoop:configuration>
<hadoop:hbase-configuration configuration-ref="hadoopConfiguration" zk-port="2181" zk-quorum="192.168.20.10"></hadoop:hbase-configuration>
<bean id="hbaseTemplate" class="org.springframework.data.hadoop.hbase.HbaseTemplate">
<property name="configuration" ref="hbaseConfiguration" />
</bean>
</beans>
This code works fine when I run directly in remote machine.When I am running same code from my windows machine by providing ip of hbase .,it doesnot return any value.

Configure your windows machine C:\Windows\System32\drivers\etc\hosts
file to resolve your HBase cluster node details. Recommended Make sure
DNS configuration is right!. i.e. addresses of all
nodes(Regionservers) of HBase Cluster is resolved from windows
machine.

Related

Spring - Service registration with Eureka ignored by Ribbon

I have a Spring Boot app trying to fetch an instance of a service through Eureka/Ribbon:
#LoadBalanced
#Autowired
RestTemplate restTemplate;
#RequestMapping("/hi")
public String hi(#RequestParam(value="name", defaultValue="superuser") String name) {
AccountRest account = this.restTemplate.getForObject("http://AUTHENTICATION-SERVICE/authservice/v1/accounts/userId/"+name,
AccountRest.class);
return "hi, " + account.getId();
}
I am manually registering the "AUTHENTICATION-SERVICE", note that this service only needs to register itself, it does not need to query eureka:
public class ServiceDiscoveryManager {
private ApplicationInfoManager applicationInfoManager;
private EurekaClient eurekaClient;
public void start() {
MyDataCenterInstanceConfig instanceConfig = new MyDataCenterInstanceConfig();
DefaultEurekaClientConfig clientConfig = new DefaultEurekaClientConfig();
InstanceInfo instanceInfo = new EurekaConfigBasedInstanceInfoProvider(instanceConfig).get();
applicationInfoManager = new ApplicationInfoManager(instanceConfig, instanceInfo);
eurekaClient = new DiscoveryClient(applicationInfoManager, clientConfig);
applicationInfoManager.getInfo();
eurekaClient.getApplications();
instanceInfo.setStatus(InstanceStatus.UP);
}
public void stop() {
eurekaClient.shutdown();
}
}
Spring wiring for the ServiceDiscoveryManager:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<bean id="serviceDiscoveryManager" class="com.embotics.authervice.serviceDiscovery.ServiceDiscoveryManager" init-method="start"/>
</beans>
the authentication-service eureka-client.properties file:
eureka.name=AUTHENTICATION-SERVICE
eureka.appGroup=AUTHENTICATION-SERVICE
eureka.vipAddress=http://localhost:9080
eureka.port.enabled=true
eureka.port=9080
eureka.traffic.enabled=true
eureka.preferSameZone=true
eureka.serviceUrl.default=http://localhost:8000/eureka/
eureka.decoderName=JacksonJson
eureka.healthCheckUrl=http://localhost:9080/authservice/health
eureka.healthCheckPath=/authservice/health
The Eureka Dashboard shows my authentication service as registered:
When I try to get an instance of the authentication-service with the load balancer I receive the following exception:
java.lang.IllegalStateException: No instances available for AUTHENTICATION-SERVICE
at org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.execute(RibbonLoadBalancerClient.java:90) ~[spring-cloud-netflix-core-1.2.5.RELEASE.jar:1.2.5.RELEASE]
I've confirmed that the load balancer is aware of the authentication-service, but the DynamicServerListLoadBalancer has the allServerList property as empty.
I've been playing with this for a few hours, wondering if some configuration is invalid. Thanks for any help.

spring callable not working

I am trying to implement long polling using spring,here is the sample code
Here is the code for my async method:
#RequestMapping(value= "failed.html" ,method = RequestMethod.POST)
protected Callable<String> callable(#ModelAttribute("user") Message user1, BindingResult bindingResult){
return new Callable<String>() {
#Override
public String call() throws Exception {
System.out.println("call--------->");
Thread.sleep(2000);
return "success";
}
};
}
Here is the callablecontroller-servlet.xml:
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<bean id= "viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix"><value></value></property>
<property name="suffix" value=".jsp"></property>
</bean>
<bean id="callable" class="spring.controller.CallableController">
</bean>
However "call------------" is never printed in the console and it displays failed.jsp instead of success.jsp. Any help would be appreciated.
My first suspicion is about the arguments to the callable method.
Try adding a ExceptionHandler to your controller class as follows, to know whats going wrong:
#ExceptionHandler
#ResponseBody
public String handleException(IllegalStateException ex) {
System.out.println("Exception --------->");
return "Handled exception: " + ex.getMessage();
}

Spring 3 JNDI look up in glassfish3

I want to look up some properties from JNDI configured in glassfish v3 server. I want to do it using spring. Here is my spring configuration:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.0.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml"/>
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
<!--
JNDI look ups.
!-->
<jee:jndi-lookup id="properties"
jndi-name="java:comp/env/jndi/ws_properties"
expected-type="java.util.Properties"/>
</beans>
I have mapped jndi/ws_properties in sun-web.xml and web.xml files. Problem is that this lookup always gives me null properties. But if I do it in java code:
try {
InitialContext context = new InitialContext();
properties = (Properties) context.lookup("jndi/ws_properties");
} catch (NamingException e) {
LOGGER.error("", e);
}
It is ok. I see my properties keys and values.
Could somebody tell me where is the problem here?
This is probably because of your "jndi-name" property.
You don't have to put "java:comp/env/" in the name.
The "resource-ref" property defaults to true and unless you set it to false, it will automatically add the java:comp/env to the name.

Adding Xml configurations to a Spring ApplicationContext that is already setup

I was wondering if there is a way to do the following:
have my wep app startup with its servle-context.xml
When, at a certain point, one particular bean in this xml config is instantiated, it will add it's own xml configuration to the application context (or to a child perhaps?).
I'm asking this because I want to pack some functionality in a stand alone library and then reuse it in different projects, so that initializing a bean of this library will load its xml config.
What I wrote is:
public class IrisLibHelper {
ApplicationContext context;
ApplicationContext irisContext;
#Required
#Autowired
public void setContext(ApplicationContext ctx){
this.context = ctx;
ClassPathXmlApplicationContext xap = new ClassPathXmlApplicationContext(ctx);
xap.setConfigLocation("classpath:com/dariodario/irislib/xmldefs/irisconfig.xml");
this.irisContext = xap;
}
public ApplicationContext getIrisContext() {
return irisContext;
}
public void setIrisContext(ApplicationContext irisContext) {
this.irisContext = irisContext;
}
}
and the irisconfig.xml is:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- <context:component-scan base-package="com.dariodario"></context:component-scan> -->
<mvc:annotation-driven />
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"
p:synchronizeOnSession="true" />
<context:component-scan base-package="com.dariodario.iris.controllers"></context:component-scan>
</beans>
The problem is that it doesn't seem to scan the com.dariodario.iris.controllers package, in fact the controllers don't get mapped! (I've logging debugging on and I don't seen anything).
Why not use the tag <import resource="classpath:applicationConfig.xml" /> ? You can load a spring configuration file which is in jar. In a jar, the Spring XML configuration is always at the root. But if not, you can use this notation: <import resource="${configurablePath}/applicationConfig.xml" /> where configurablePath can be reach by a property place holder or other.
I think this way is cleaner than merging two Spring context.

loading multiple properties files from projects in workspace

I have three projects - proj-a, proj-b, and main such that main depends on proj-a and proj-b.
proj-a and proj-b each contains a module-context.xml and properties file.
proj-a module-context.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:property-placeholder location="classpath:/META-INF/proj-a.properties"/>
<bean ... someProperty="${property-a}" />
</bean>
proj-a.properties
property-a=hello-a
proj-b's configuration is identical except a is replaced by b.
proj-b module-context.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:property-placeholder location="classpath:/META-INF/proj-b.properties"/>
<bean ... someProperty="${property-b}" />
</bean>
proj-b.properties
property-b=hello-b
A Class in main wants to create an ApplicationContext that consists of both proj-a and proj-b's module-context.xml. The problem is that only one of the properties file is processed by spring. If proj-a's module-context.xml's loaded first, proj-b's properties file is never read.
The following snippet throws an Exception.
public static void main( String[] args ) throws IOException {
ApplicationContext context = new FileSystemXmlApplicationContext( new String[] {
"../proj-a/src/main/resources/META-INF/spring/module-context.xml",
"../proj-b/src/main/resources/META-INF/spring/module-context.xml"
} );
}
throws
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name '...' defined in file [...\proj-b\src\main\resources\META-INF\spring\module-context.xml]: Could not resolve placeholder 'property-b'
at org.springframework.beans.factory.config.PropertyPlaceholderConfigurer.processProperties(PropertyPlaceholderConfigurer.java:272)
at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:75)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:624)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:599)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:398)
How can I properly load the properties files? Merging them is not a solution because properties are project specific.
I found a solution - set property ignoreUnresolvablePlaceholders to true.

Resources