com.datastax.driver.mapping.MappingManager Java Examples

The following examples show how to use com.datastax.driver.mapping.MappingManager. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CassandraPojoOutputFormat.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * Opens a Session to Cassandra and initializes the prepared statement.
 *
 * @param taskNumber The number of the parallel instance.
 */
@Override
public void open(int taskNumber, int numTasks) {
	this.session = cluster.connect();
	MappingManager mappingManager = new MappingManager(session);
	this.mapper = mappingManager.mapper(outputClass);
	if (mapperOptions != null) {
		Mapper.Option[] optionsArray = mapperOptions.getMapperOptions();
		if (optionsArray != null) {
			mapper.setDefaultSaveOptions(optionsArray);
		}
	}
	this.callback = new FutureCallback<Void>() {
		@Override
		public void onSuccess(Void ignored) {
			onWriteSuccess();
		}

		@Override
		public void onFailure(Throwable t) {
			onWriteFailure(t);
		}
	};
}
 
Example #2
Source File: CassandraPojoSink.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Override
public void open(Configuration configuration) {
	super.open(configuration);
	try {
		this.mappingManager = new MappingManager(session);
		this.mapper = mappingManager.mapper(clazz);
		if (options != null) {
			Mapper.Option[] optionsArray = options.getMapperOptions();
			if (optionsArray != null) {
				this.mapper.setDefaultSaveOptions(optionsArray);
			}
		}
	} catch (Exception e) {
		throw new RuntimeException("Cannot create CassandraPojoSink with input: " + clazz.getSimpleName(), e);
	}
}
 
Example #3
Source File: CassandraPojoOutputFormat.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Opens a Session to Cassandra and initializes the prepared statement.
 *
 * @param taskNumber The number of the parallel instance.
 */
@Override
public void open(int taskNumber, int numTasks) {
	this.session = cluster.connect();
	MappingManager mappingManager = new MappingManager(session);
	this.mapper = mappingManager.mapper(outputClass);
	if (mapperOptions != null) {
		Mapper.Option[] optionsArray = mapperOptions.getMapperOptions();
		if (optionsArray != null) {
			mapper.setDefaultSaveOptions(optionsArray);
		}
	}
	this.callback = new FutureCallback<Void>() {
		@Override
		public void onSuccess(Void ignored) {
			onWriteSuccess();
		}

		@Override
		public void onFailure(Throwable t) {
			onWriteFailure(t);
		}
	};
}
 
Example #4
Source File: CassandraPojoSink.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void open(Configuration configuration) {
	super.open(configuration);
	try {
		this.mappingManager = new MappingManager(session);
		this.mapper = mappingManager.mapper(clazz);
		if (options != null) {
			Mapper.Option[] optionsArray = options.getMapperOptions();
			if (optionsArray != null) {
				this.mapper.setDefaultSaveOptions(optionsArray);
			}
		}
	} catch (Exception e) {
		throw new RuntimeException("Cannot create CassandraPojoSink with input: " + clazz.getSimpleName(), e);
	}
}
 
Example #5
Source File: CassandraPojoSink.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void open(Configuration configuration) {
	super.open(configuration);
	try {
		this.mappingManager = new MappingManager(session);
		this.mapper = mappingManager.mapper(clazz);
		if (options != null) {
			Mapper.Option[] optionsArray = options.getMapperOptions();
			if (optionsArray != null) {
				this.mapper.setDefaultSaveOptions(optionsArray);
			}
		}
	} catch (Exception e) {
		throw new RuntimeException("Cannot create CassandraPojoSink with input: " + clazz.getSimpleName(), e);
	}
}
 
Example #6
Source File: AbstractCassandraCluster.java    From iotplatform with Apache License 2.0 6 votes vote down vote up
private void initSession() {
    long endTime = System.currentTimeMillis() + initTimeout;
    while (System.currentTimeMillis() < endTime) {
        try {

            if (this.keyspaceName != null) {
                session = cluster.connect(keyspaceName);
            } else {
                session = cluster.connect();
            }
            mappingManager = new MappingManager(session);
            break;
        } catch (Exception e) {
            log.warn("Failed to initialize cassandra cluster due to {}. Will retry in {} ms", e.getMessage(), initRetryInterval);
            try {
                Thread.sleep(initRetryInterval);
            } catch (InterruptedException ie) {
                log.warn("Failed to wait until retry", ie);
            }
        }
    }
}
 
Example #7
Source File: _PersistentTokenRepository.java    From jhipster-ribbon-hystrix with GNU General Public License v3.0 6 votes vote down vote up
@PostConstruct
public void init() {
    mapper = new MappingManager(session).mapper(PersistentToken.class);

    findPersistentTokenSeriesByUserIdStmt = session.prepare(
        "SELECT persistent_token_series " +
        "FROM persistent_token_by_user " +
        "WHERE user_id = :user_id");

    insertPersistentTokenSeriesByUserIdStmt = session.prepare(
        "INSERT INTO persistent_token_by_user (user_id, persistent_token_series) " +
            "VALUES (:user_id, :persistent_token_series) " +
            "USING TTL 2592000"); // 30 days

    insertPersistentTokenStmt = session.prepare(
        "INSERT INTO persistent_token (series, token_date, user_agent, token_value, login, user_id, ip_address) " +
            "VALUES (:series, :token_date, :user_agent, :token_value, :login, :user_id, :ip_address) " +
            "USING TTL 2592000"); // 30 days
}
 
Example #8
Source File: CassandraPojoOutputFormat.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Opens a Session to Cassandra and initializes the prepared statement.
 *
 * @param taskNumber The number of the parallel instance.
 */
@Override
public void open(int taskNumber, int numTasks) {
	this.session = cluster.connect();
	MappingManager mappingManager = new MappingManager(session);
	this.mapper = mappingManager.mapper(outputClass);
	if (mapperOptions != null) {
		Mapper.Option[] optionsArray = mapperOptions.getMapperOptions();
		if (optionsArray != null) {
			mapper.setDefaultSaveOptions(optionsArray);
		}
	}
	this.callback = new FutureCallback<Void>() {
		@Override
		public void onSuccess(Void ignored) {
			onWriteSuccess();
		}

		@Override
		public void onFailure(Throwable t) {
			onWriteFailure(t);
		}
	};
}
 
Example #9
Source File: CassandraPojoInputFormat.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public void open(InputSplit split) {
	this.session = cluster.connect();
	MappingManager manager = new MappingManager(session);

	Mapper<OUT> mapper = manager.mapper(inputClass);

	if (mapperOptions != null) {
		Mapper.Option[] optionsArray = mapperOptions.getMapperOptions();
		if (optionsArray != null) {
			mapper.setDefaultGetOptions(optionsArray);
		}
	}
	this.resultSet = mapper.map(session.execute(query));
}
 
Example #10
Source File: CassandraPojoInputFormat.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void open(InputSplit split) {
	this.session = cluster.connect();
	MappingManager manager = new MappingManager(session);

	Mapper<OUT> mapper = manager.mapper(inputClass);

	if (mapperOptions != null) {
		Mapper.Option[] optionsArray = mapperOptions.getMapperOptions();
		if (optionsArray != null) {
			mapper.setDefaultGetOptions(optionsArray);
		}
	}
	this.resultSet = mapper.map(session.execute(query));
}
 
Example #11
Source File: DefaultObjectMapperFactory.java    From beam with Apache License 2.0 5 votes vote down vote up
@Override
public Mapper apply(Session session) {
  if (mappingManager == null) {
    this.mappingManager = new MappingManager(session);
  }

  return new DefaultObjectMapper<T>(mappingManager.mapper(entity));
}
 
Example #12
Source File: CassandraPojoInputFormat.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void open(InputSplit split) {
	this.session = cluster.connect();
	MappingManager manager = new MappingManager(session);

	Mapper<OUT> mapper = manager.mapper(inputClass);

	if (mapperOptions != null) {
		Mapper.Option[] optionsArray = mapperOptions.getMapperOptions();
		if (optionsArray != null) {
			mapper.setDefaultGetOptions(optionsArray);
		}
	}
	this.resultSet = mapper.map(session.execute(query));
}
 
Example #13
Source File: MetricRepository.java    From gpmr with Apache License 2.0 4 votes vote down vote up
@PostConstruct
public void init() {
    mapper = new MappingManager(session).mapper(Metric.class);
    createPaging("gpmr","metric");
}
 
Example #14
Source File: RaceNormalRepository.java    From gpmr with Apache License 2.0 4 votes vote down vote up
@PostConstruct
public void init() {
    mapper = new MappingManager(session).mapper(RaceNormal.class);
    createPaging("gpmr","race_normal");
}
 
Example #15
Source File: PetRepository.java    From gpmr with Apache License 2.0 4 votes vote down vote up
@PostConstruct
public void init() {
    mapper = new MappingManager(session).mapper(Pet.class);
    createPaging("gpmr","pet");
}
 
Example #16
Source File: DataCounterRepository.java    From gpmr with Apache License 2.0 4 votes vote down vote up
@PostConstruct
public void init() {
    mapper = new MappingManager(session).mapper(DataCounter.class);
    createPaging("gpmr","data_counter");
}
 
Example #17
Source File: RaceRepository.java    From gpmr with Apache License 2.0 4 votes vote down vote up
@PostConstruct
public void init() {
    mapper = new MappingManager(session).mapper(Race.class);
    createPaging("gpmr","race");
}
 
Example #18
Source File: RaceDataRepository.java    From gpmr with Apache License 2.0 4 votes vote down vote up
@PostConstruct
public void init() {
    mapper = new MappingManager(session).mapper(RaceData.class);
    createPaging(keyspace,table);
}
 
Example #19
Source File: RaceResultRepository.java    From gpmr with Apache License 2.0 4 votes vote down vote up
@PostConstruct
public void init() {
    mapper = new MappingManager(session).mapper(RaceResult.class);
    createPaging("gpmr","race_result");
}
 
Example #20
Source File: UserRepository.java    From gpmr with Apache License 2.0 4 votes vote down vote up
@PostConstruct
public void init() {
    mapper = new MappingManager(session).mapper(User.class);

    findAllStmt = session.prepare("SELECT * FROM user");

    findOneByActivationKeyStmt = session.prepare(
        "SELECT id " +
            "FROM user_by_activation_key " +
            "WHERE activation_key = :activation_key");

    findOneByResetKeyStmt = session.prepare(
        "SELECT id " +
            "FROM user_by_reset_key " +
            "WHERE reset_key = :reset_key");

    insertByActivationKeyStmt = session.prepare(
        "INSERT INTO user_by_activation_key (activation_key, id) " +
            "VALUES (:activation_key, :id)");

    insertByResetKeyStmt = session.prepare(
        "INSERT INTO user_by_reset_key (reset_key, id) " +
            "VALUES (:reset_key, :id)");

    deleteByActivationKeyStmt = session.prepare(
        "DELETE FROM user_by_activation_key " +
            "WHERE activation_key = :activation_key");

    deleteByResetKeyStmt = session.prepare(
        "DELETE FROM user_by_reset_key " +
            "WHERE reset_key = :reset_key");

    findOneByLoginStmt = session.prepare(
        "SELECT id " +
            "FROM user_by_login " +
            "WHERE login = :login");

    insertByLoginStmt = session.prepare(
        "INSERT INTO user_by_login (login, id) " +
            "VALUES (:login, :id)");

    deleteByLoginStmt = session.prepare(
        "DELETE FROM user_by_login " +
            "WHERE login = :login");

    findOneByEmailStmt = session.prepare(
        "SELECT id " +
            "FROM user_by_email " +
            "WHERE email     = :email");

    insertByEmailStmt = session.prepare(
        "INSERT INTO user_by_email (email, id) " +
            "VALUES (:email, :id)");

    deleteByEmailStmt = session.prepare(
        "DELETE FROM user_by_email " +
            "WHERE email = :email");
}
 
Example #21
Source File: RaceParticipantRepository.java    From gpmr with Apache License 2.0 4 votes vote down vote up
@PostConstruct
public void init() {
    mapper = new MappingManager(session).mapper(RaceParticipant.class);
    createPaging("gpmr","race_participant");
}
 
Example #22
Source File: PetCategoryRepository.java    From gpmr with Apache License 2.0 4 votes vote down vote up
@PostConstruct
public void init() {
    mapper = new MappingManager(session).mapper(PetCategory.class);
    createPaging("gpmr","pet_category");
}
 
Example #23
Source File: CustomerDao.java    From micro-service with MIT License 4 votes vote down vote up
@PostConstruct
public void init() {
	session = cluster.connect("mykeyspace");
	customerMapper = new MappingManager(session).mapper(Customer.class);
	customerMapper.setDefaultSaveOptions(Mapper.Option.saveNullFields(false));
}
 
Example #24
Source File: _UserRepository.java    From jhipster-ribbon-hystrix with GNU General Public License v3.0 4 votes vote down vote up
@PostConstruct
public void init() {
    mapper = new MappingManager(session).mapper(User.class);

    findAllStmt = session.prepare("SELECT * FROM user");

    findOneByActivationKeyStmt = session.prepare(
        "SELECT id " +
        "FROM user_by_activation_key " +
        "WHERE activation_key = :activation_key");

    findOneByResetKeyStmt = session.prepare(
        "SELECT id " +
        "FROM user_by_reset_key " +
        "WHERE reset_key = :reset_key");

    insertByActivationKeyStmt = session.prepare(
        "INSERT INTO user_by_activation_key (activation_key, id) " +
        "VALUES (:activation_key, :id)");

    insertByResetKeyStmt = session.prepare(
        "INSERT INTO user_by_reset_key (reset_key, id) " +
        "VALUES (:reset_key, :id)");

    deleteByActivationKeyStmt = session.prepare(
        "DELETE FROM user_by_activation_key " +
        "WHERE activation_key = :activation_key");

    deleteByResetKeyStmt = session.prepare(
        "DELETE FROM user_by_reset_key " +
        "WHERE reset_key = :reset_key");

    findOneByLoginStmt = session.prepare(
        "SELECT id " +
        "FROM user_by_login " +
        "WHERE login = :login");

    insertByLoginStmt = session.prepare(
        "INSERT INTO user_by_login (login, id) " +
            "VALUES (:login, :id)");

    deleteByLoginStmt = session.prepare(
        "DELETE FROM user_by_login " +
            "WHERE login = :login");

    findOneByEmailStmt = session.prepare(
        "SELECT id " +
        "FROM user_by_email " +
        "WHERE email     = :email");

    insertByEmailStmt = session.prepare(
        "INSERT INTO user_by_email (email, id) " +
            "VALUES (:email, :id)");

    deleteByEmailStmt = session.prepare(
        "DELETE FROM user_by_email " +
            "WHERE email = :email");
}
 
Example #25
Source File: CassandraFactory.java    From database-transform-tool with Apache License 2.0 4 votes vote down vote up
/**
	 * 描述: 初始化配置
	 * 时间: 2017年11月15日 上午11:25:07
	 * @author yi.zhang
	 * @param servers	服务地址
	 * @param keyspace	命名空间
	 * @param username	账号
	 * @param password	密码
	 */
	public void init(String servers,String keyspace,String username,String password) {
		try {
			// socket 链接配置
			SocketOptions socket = new SocketOptions();
			socket.setKeepAlive(true);
			socket.setReceiveBufferSize(1024* 1024);
			socket.setSendBufferSize(1024* 1024);
			socket.setConnectTimeoutMillis(5 * 1000);
			socket.setReadTimeoutMillis(1000);
			//设置连接池
			PoolingOptions pool = new PoolingOptions();
			// pool.setMaxRequestsPerConnection(HostDistance.LOCAL, 32);
			// pool.setMaxRequestsPerConnection(HostDistance.REMOTE, 32);
			// pool.setCoreConnectionsPerHost(HostDistance.LOCAL, 2);
			// pool.setCoreConnectionsPerHost(HostDistance.REMOTE, 2);
			// pool.setMaxConnectionsPerHost(HostDistance.LOCAL, 4);
			// pool.setMaxConnectionsPerHost(HostDistance.REMOTE, 4);
			pool.setHeartbeatIntervalSeconds(60);
			pool.setIdleTimeoutSeconds(120);
			pool.setPoolTimeoutMillis(5 * 1000);
			List<InetSocketAddress> saddress = new ArrayList<InetSocketAddress>();
			if (servers != null && !"".equals(servers)) {
				for (String server : servers.split(",")) {
					String[] address = server.split(":");
					String ip = address[0];
					int port = 9042;
					if (address != null && address.length > 1) {
						port = Integer.valueOf(address[1]);
					}
					saddress.add(new InetSocketAddress(ip, port));
				}
			}
			InetSocketAddress[] addresses = new InetSocketAddress[saddress.size()];
			saddress.toArray(addresses);
			
			Builder builder = Cluster.builder();
	        builder.withSocketOptions(socket);
	        // 设置压缩方式
	        builder.withCompression(ProtocolOptions.Compression.LZ4);
	        // 负载策略
//	        DCAwareRoundRobinPolicy loadBalance = DCAwareRoundRobinPolicy.builder().withLocalDc("localDc").withUsedHostsPerRemoteDc(2).allowRemoteDCsForLocalConsistencyLevel().build();
//	        builder.withLoadBalancingPolicy(loadBalance);
	        // 重试策略
	        builder.withRetryPolicy(DefaultRetryPolicy.INSTANCE);
			builder.withPoolingOptions(pool);
			builder.addContactPointsWithPorts(addresses);
			builder.withCredentials(username, password);
			Cluster cluster = builder.build();
			if (keyspace != null && !"".equals(keyspace)) {
				session = cluster.connect(keyspace);
			} else {
				session = cluster.connect();
			}
			mapping = new MappingManager(session);
		} catch (Exception e) {
			logger.error("-----Cassandra Config init Error-----", e);
		}
	}