/** * @param _clusterName * @param _server * @param _keySpaceName * @return KeySpace - the Cassandra KeySpace we are going to work in, and that we give back to the * caller ( if everything goes well ). * @throws java.lang.Exception */ public static final Keyspace doInit( final String _clusterName, final String _server, final String _keySpaceName) throws Exception { AstyanaxContext<Keyspace> context = new AstyanaxContext.Builder() .forCluster(_clusterName) .forKeyspace(_keySpaceName) .withAstyanaxConfiguration( new AstyanaxConfigurationImpl() .setDiscoveryType(NodeDiscoveryType.RING_DESCRIBE) .setCqlVersion("3.0.0") .setTargetCassandraVersion("2.0.4")) .withConnectionPoolConfiguration( new ConnectionPoolConfigurationImpl("ubicity file loading pool") .setPort(9160) .setMaxConnsPerHost(16) .setSeeds(_server + ":9160") .setMaxOperationsPerConnection(10000000) .setConnectTimeout(10000)) .withConnectionPoolMonitor(new CountingConnectionPoolMonitor()) .buildKeyspace(ThriftFamilyFactory.getInstance()); context.start(); Keyspace keySpace = context.getClient(); try { keySpace.describeKeyspace(); logger.log(Level.INFO, "keyspace " + keySpace.getKeyspaceName() + " does exist"); } catch (BadRequestException bre) { logger.log( Level.INFO, "keyspace " + keySpace.getKeyspaceName() + " does NOT exist, creating it"); keySpace.createKeyspace( ImmutableMap.<String, Object>builder() .put( "strategy_options", ImmutableMap.<String, Object>builder().put("replication_factor", "1").build()) .put("strategy_class", "SimpleStrategy") .build()); } log = CassandraCrawlLogSchema.checkOrBuildMonitrixSchema(keySpace); if (log == null) { logger.log(Level.SEVERE, "could not create Monitrix ColumnFamily ( table ) "); } return keySpace; }
public static Keyspace getKeySpace(String keySpaceName) { AstyanaxContext<Keyspace> context = new AstyanaxContext.Builder() .forCluster(CLUSTER_NAME) .forKeyspace(keySpaceName) .withAstyanaxConfiguration( new AstyanaxConfigurationImpl().setDiscoveryType(NodeDiscoveryType.RING_DESCRIBE) // .setCqlVersion("3.0.0") // .setTargetCassandraVersion("1.2") ) .withConnectionPoolConfiguration( new ConnectionPoolConfigurationImpl("MinderConnectionPool") .setPort(9160) .setMaxConnsPerHost(1) .setSeeds(SEEDS)) .withConnectionPoolMonitor(new CountingConnectionPoolMonitor()) .buildKeyspace(ThriftFamilyFactory.getInstance()); context.start(); return context.getClient(); }
public static void doDataLoad() { Properties properties = new Properties(); try { properties.load(new FileInputStream("cassandraClient.properties")); } catch (FileNotFoundException e1) { e1.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } String cassandraHost = properties.getProperty("cassandraHost"); String clusterName = properties.getProperty("clusterName"); String keyspaceName = properties.getProperty("keyspace"); String targetTableName = "Radio_Plays_1"; AstyanaxContext<Keyspace> context = new AstyanaxContext.Builder() .forCluster(clusterName) .forKeyspace(keyspaceName) .withAstyanaxConfiguration( new AstyanaxConfigurationImpl().setDiscoveryType(NodeDiscoveryType.RING_DESCRIBE)) .withConnectionPoolConfiguration( new ConnectionPoolConfigurationImpl("MyConnectionPool") .setPort(9160) .setMaxConnsPerHost(1) .setSeeds(cassandraHost + ":9160")) .withConnectionPoolMonitor(new CountingConnectionPoolMonitor()) .buildKeyspace(ThriftFamilyFactory.getInstance()); context.start(); Keyspace keyspace = context.getClient(); System.out.println("keyspace :" + keyspace.getKeyspaceName()); // targetTableName ColumnFamily<String, String> CF_INFO = new ColumnFamily<String, String>( // "MUSICMETRIC_CONFIG", // Column Family Name targetTableName, StringSerializer.get(), // Key Serializer StringSerializer.get()); // Column Serializer // load each line data into cassandra try { // Open the file that is the first command line parameter FileInputStream fstream = new FileInputStream("musicmetric-facebook-data.tsv.gz"); // Get the object of DataInputStream DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine; // Read File Line By Line while ((strLine = br.readLine()) != null) { String[] splitArray = strLine.split("\\s+"); String key = splitArray[0]; String value = splitArray[1]; logger.info("key --> " + key); logger.info("value --> " + value); // keyspace.prepareColumnMutation(CF_INFO, key, "value").putValue(value, null).execute(); } // Close the input stream in.close(); } catch (Exception e) { // Catch exception if any System.err.println("Error: " + e.getMessage()); } finally { context.shutdown(); } }