What can I use instead of doInTable(HTableInterface table) which is deprecated.
Below is the code.
hbaseTemplate.execute(tableName, new TableCallback<User>() {
public User doInTable(HTableInterface table) throws Throwable {
Put p = new Put(Bytes.toBytes(KEY),timestamp);
p.addColumn(FAMILY, XXX, Bytes.toBytes(user.getUserId()));
table.put(p);
}
});
I am using Spring Boot with Hbase.
Thank you in advance.
The source code mentioned using org.apache.hadoop.hbase.client.Table instead of HTableInterface.
/**
* Used to communicate with a single HBase table.
* Obtain an instance from an {#link HConnection}.
*
* #since 0.21.0
* #deprecated use {#link org.apache.hadoop.hbase.client.Table} instead
*/
#Deprecated
#InterfaceAudience.Private
#InterfaceStability.Stable
public interface HTableInterface extends Table ...
Related
I'm having a difficulty understanding how to configure the following.
Since spring-kafka:2.8.4 thee KafkaListener interface can be configured with a filter which will be applied to all incoming messages, the Javadoc for the filter method:
/**
* Set an {#link org.springframework.kafka.listener.adapter.RecordFilterStrategy} bean
* name to override the strategy configured on the container factory. If a SpEL
* expression is provided ({#code #{...}}), the expression can either evaluate to a
* {#link org.springframework.kafka.listener.adapter.RecordFilterStrategy} instance or
* a bean name.
* #return the error handler.
* #since 2.8.4
*/
String filter() default "";
RecordFilterStrategy has a single method:
/**
* Return true if the record should be discarded.
* #param consumerRecord the record.
* #return true to discard.
*/
boolean filter(ConsumerRecord<K, V> consumerRecord);
Basically I need to create a kind of a lambda, but I don't understand how to reference the consumerRecord variable, this is what I have already tried:
#{#consumerRecord.key().equals(T(com.example.kafkaconsumer.EventType).CREATE.toString())}
It fails with the exception:
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1011E:
Method call: Attempted to call method key() on null context object
This is what I'm trying to implement using SPEL:
#Bean
public RecordFilterStrategy<String, Foo> recordFilterStrategy() {
return rec -> !Objects.equals(rec.key(), EventType.CREATE.toString());
}
See that JavaDocs one more time:
Set an {#link org.springframework.kafka.listener.adapter.RecordFilterStrategy} bean
* name
Since you already have a recordFilterStrategy bean, so that's enough for your to use in that filter() attribute:
filter = "recordFilterStrategy"
No need to fight with a complex SpEL.
Versions:
Spring: 5.2.16.RELEASE
Spring Integrations: 5.3.9.RELEASE
PostgreSQL: 13.x
I have implemented a pure Spring 5.x webapp; there is no Spring Boot.
I am using the JdbcMetadataStore and require that the PostgreSQL database be initialized with schema definitions located on the classpath in:
classpath:org/springframework/integration/jdbc/schema-postgresql.sql
Following a very useful article on the topic, here are properties I put in spring.properties:
spring.integration.jdbc.initialize-schema=always
spring.integration.jdbc.schema=classpath:org/springframework/integration/jdbc/schema-postgresql.sql
This DOES NOT work. After researching this issue, I have learned that the start-up initialization is supported, but in Spring Boot.
QUESTION: Short of explicitly handling the execution of this SQL script elsewhere during webapp initialization, is there any standard way to load the script listed above at start-up? NOTE: I am not using schema.sql scripts or similar to initialize my backend.
See DataSourceInitializer in spring-jdbc:
/**
* Used to {#linkplain #setDatabasePopulator set up} a database during
* initialization and {#link #setDatabaseCleaner clean up} a database during
* destruction.
* #see DatabasePopulator
*/
public class DataSourceInitializer implements InitializingBean, DisposableBean {
You need to inject over there a ResourceDatabasePopulator based on that script location:
/**
* Populates, initializes, or cleans up a database using SQL scripts defined in
* external resources.
*
* <ul>
* <li>Call {#link #addScript} to add a single SQL script location.
* <li>Call {#link #addScripts} to add multiple SQL script locations.
* <li>Consult the setter methods in this class for further configuration options.
* <li>Call {#link #populate} or {#link #execute} to initialize or clean up the
* database using the configured scripts.
* </ul>
*
* #see DatabasePopulatorUtils
* #see ScriptUtils
*/
public class ResourceDatabasePopulator implements DatabasePopulator {
Some docs are here: https://docs.spring.io/spring-framework/docs/current/reference/html/data-access.html#jdbc-initializing-datasource
with org.springframework.boot update from 2.0.4.RELEASE to 2.0.5.RELEASE start to get exception:
RepositoryConfigurationExtensionSupport
=> useRepositoryConfiguration
=> InvalidDataAccessApiUsageException: "Reactive Repositories are not supported by %s. Offending repository is %s!"
what is the best/simplest Spring Data workaround for this? Could someone provide example? if it is possible without redefinition of #EnableJpaRepositories and JpaRepositoriesRegistrar...
This is solution, but I don't like it.. +3 new classes..
EnableXxxJpaRepositories is practically exact copy of EnableJpaRepositories..
EnableXxxJpaRepositories:
#Target(ElementType.TYPE)
#Retention(RetentionPolicy.RUNTIME)
#Documented
#Inherited
#Import(XxxJpaRepositoriesRegistrar.class)
public #interface EnableXxxJpaRepositories {
/**
* Alias for the {#link #basePackages()} attribute. Allows for more concise annotation
* declarations e.g.: {#code #EnableXxxJpaRepositories("org.my.pkg")} instead of {#code
*
* #EnableXxxJpaRepositories(basePackages="org.my.pkg")}.
*/
String[] value() default {};
/**
* Base packages to scan for annotated components. {#link #value()} is an alias for (and mutually
* exclusive with) this attribute. Use {#link #basePackageClasses()} for a type-safe alternative
* to String-based package names.
*/
String[] basePackages() default {};
/**
* Type-safe alternative to {#link #basePackages()} for specifying the packages to scan for
* annotated components. The package of each class specified will be scanned. Consider creating a
* special no-op marker class or interface in each package that serves no purpose other than being
* referenced by this attribute.
*/
Class<?>[] basePackageClasses() default {};
/**
* Specifies which types are eligible for component scanning. Further narrows the set of candidate
* components from everything in {#link #basePackages()} to everything in the base packages that
* matches the given filter or filters.
*/
Filter[] includeFilters() default {};
/**
* Specifies which types are not eligible for component scanning.
*/
Filter[] excludeFilters() default {};
/**
* Returns the postfix to be used when looking up custom repository implementations. Defaults to
* {#literal Impl}. So for a repository named {#code PersonRepository} the corresponding
* implementation class will be looked up scanning for {#code PersonRepositoryImpl}.
*/
String repositoryImplementationPostfix() default "Impl";
/**
* Configures the location of where to find the Spring Data named queries properties file. Will
* default to {#code META-INF/jpa-named-queries.properties}.
*/
String namedQueriesLocation() default "";
/**
* Returns the key of the {#link QueryLookupStrategy} to be used for lookup queries for query
* methods. Defaults to {#link Key#CREATE_IF_NOT_FOUND}.
*/
Key queryLookupStrategy() default Key.CREATE_IF_NOT_FOUND;
/**
* Returns the {#link FactoryBean} class to be used for each repository instance. Defaults to
* {#link JpaRepositoryFactoryBean}.
*/
Class<?> repositoryFactoryBeanClass() default JpaRepositoryFactoryBean.class;
/**
* Configure the repository base class to be used to create repository proxies for this particular
* configuration.
*
* #since 1.9
*/
Class<?> repositoryBaseClass() default DefaultRepositoryBaseClass.class;
// JPA specific configuration
/**
* Configures the name of the {#link EntityManagerFactory} bean definition to be used to create
* repositories discovered through this annotation. Defaults to {#code entityManagerFactory}.
*/
String entityManagerFactoryRef() default "entityManagerFactory";
/**
* Configures the name of the {#link PlatformTransactionManager} bean definition to be used to
* create repositories discovered through this annotation. Defaults to {#code
* transactionManager}.
*/
String transactionManagerRef() default "transactionManager";
/**
* Configures whether nested repository-interfaces (e.g. defined as inner classes) should be
* discovered by the repositories infrastructure.
*/
boolean considerNestedRepositories() default false;
/**
* Configures whether to enable default transactions for Spring Data JPA repositories. Defaults to
* {#literal true}. If disabled, repositories must be used behind a facade that's configuring
* transactions (e.g. using Spring's annotation driven transaction facilities) or repository
* methods have to be used to demarcate transactions.
*
* #return whether to enable default transactions, defaults to {#literal true}.
*/
boolean enableDefaultTransactions() default true;
}
XxxJpaRepositoriesRegistrar:
#Configuration
public class XxxJpaRepositoriesRegistrar extends RepositoryBeanDefinitionRegistrarSupport {
#Override
protected Class<? extends Annotation> getAnnotation() {
return EnableXxxJpaRepositories.class;
}
#Override
protected RepositoryConfigurationExtension getExtension() {
return new XxxJpaRepositoryConfigExtension();
}
}
XxxJpaRepositoryConfigExtension:
#Component
public class XxxJpaRepositoryConfigExtension extends JpaRepositoryConfigExtension {
#Override
protected boolean useRepositoryConfiguration(RepositoryMetadata metadata) {
//return super.useRepositoryConfiguration(metadata);
//return metadata.isReactiveRepository();
return true;
}
}
I am beginner and as I understand #Transactional simply make sure that all the internal work of a class or method annotated with #Transactional will be wrapped in one transaction and all of the calls from external sources will create a new transaction but why do we actually need these annotations in Repository below and what are advantages of using it with readOnly = true in common cases? This is Spring pet-clinic example application using Spring & Hibernate (https://github.com/spring-projects/spring-petclinic).
/**
* Repository class for <code>Pet</code> domain objects All method names are compliant with Spring Data naming
* conventions so this interface can easily be extended for Spring Data See here: http://static.springsource.org/spring-data/jpa/docs/current/reference/html/jpa.repositories.html#jpa.query-methods.query-creation
*
* #author Ken Krebs
* #author Juergen Hoeller
* #author Sam Brannen
* #author Michael Isvy
*/
public interface PetRepository extends Repository<Pet, Integer> {
/**
* Retrieve all {#link PetType}s from the data store.
* #return a Collection of {#link PetType}s.
*/
#Query("SELECT ptype FROM PetType ptype ORDER BY ptype.name")
#Transactional(readOnly = true)
List<PetType> findPetTypes();
/**
* Retrieve a {#link Pet} from the data store by id.
* #param id the id to search for
* #return the {#link Pet} if found
*/
#Transactional(readOnly = true)
Pet findById(Integer id);
/**
* Save a {#link Pet} to the data store, either inserting or updating it.
* #param pet the {#link Pet} to save
*/
void save(Pet pet);
}
From the explanation of Oliver Gierke - the Spring Data author:
Reading methods like findAll() and findOne(…) are using
#Transactional(readOnly = true) which is not strictly necessary but
triggers a few optimizations in the transaction infrastructure
(setting the FlushMode to MANUAL to let persistence providers
potentially skip dirty checks when closing the EntityManager). Beyond
that the flag is set on the JDBC Connection as well which causes
further optimizations on that level.
Depending on what database you use it can omit table locks or even
reject write operations you might trigger accidentally. Thus we
recommend using #Transactional(readOnly = true) for query methods as
well which you can easily achieve adding that annotation to you
repository interface. Make sure you add a plain #Transactional to the
manipulating methods you might have declared or re-decorated in that
interface.
Further reading:
Spring read-only transaction Hibernate optimization
Read-write and read-only transaction routing with Spring
Documentation on laravel.com is not sufficient. Can any one guide me through how to How To Create contracts in Laravel from scratch.
I need implementation of Contracts in Laravel. Right now, I'm using Laravel 5.4
Contract is just a fancy name for php interfaces. We have being using them all along and its not a new thing.
Contracts/Interfaces help us to maintain a loosely coupled code base. See the example from doc below.
<?php
namespace App\Orders;
class Repository
{
/**
* The cache instance.
*/
protected $cache;
/**
* Create a new repository instance.
*
* #param \SomePackage\Cache\Memcached $cache
* #return void
*/
public function __construct(\SomePackage\Cache\Memcached $cache)
{
$this->cache = $cache;
}
/**
* Retrieve an Order by ID.
*
* #param int $id
* #return Order
*/
public function find($id)
{
if ($this->cache->has($id)) {
//
}
}
}
Here when ever the Repository instantiate we should give a \SomePackage\Cache\Memcached instance in order for code to work. Hence our code is tightly coupled with \SomePackage\Cache\Memcached. Now look at below code.
<?php
namespace App\Orders;
use Illuminate\Contracts\Cache\Repository as Cache;
class Repository
{
/**
* The cache instance.
*/
protected $cache;
/**
* Create a new repository instance.
*
* #param Cache $cache
* #return void
*/
public function __construct(Cache $cache)
{
$this->cache = $cache;
}
}
Same thing but now we just need to provide some cache interface. And behind the scene you could have done something like this.
<?php
namespace App\Orders;
use Illuminate\Contracts\Cache\Repository as Cache;
class RedisCache implements Cache {
//
}
When above Repository instantiate, php will look at the Illuminate\Contracts\Cache\Repository and It has been implemented by RedisCache class.
I'm afraid Gayan's answer needs further elaboration to hit Rajan's question.
Yes Gayan is correct that creating a Contract class basically means creating a php interface.
Continuing the Cache example above, if we look into its source code (you can find it at this Github repo file), we can see something like this
<?php
namespace Illuminate\Contracts\Cache;
use Closure;
interface Repository
{
/**
* Determine if an item exists in the cache.
*
* #param string $key
* #return bool
*/
public function has($key);
/**
* Retrieve an item from the cache by key.
*
* #param string $key
* #param mixed $default
* #return mixed
*/
public function get($key, $default = null);
// the rest...
}
If we are using this interface in our laravel app, it is said to be a "Contract". It is declaring what methods/properties a class should have if it implements this interface. For example in our app...
<?php
namespace App\Whatever;
use Illuminate\Contracts\Cache\Repository;
class Foo implements Repository {
//
}
Then class Foo will need to have methods has and get in order to fulfil what has been stated in the Repository contract.