Commits

Quinton Anderson committed fd7dabc

got the basic cassandra case working

  • Participants
  • Parent commits 0d9d239

Comments (0)

Files changed (4)

 			<id>twitter4j</id>
 			<url>http://twitter4j.org/maven2</url>
 		</repository>
+		<repository>
+			<id>cloudera</id>
+			<url>https://repository.cloudera.com/artifactory/cloudera-repos/</url>
+		</repository>
 	</repositories>
 
 	<dependencies>
 			<version>4.11</version>
 			<scope>test</scope>
 		</dependency>
+		<dependency>
+			<groupId>org.hectorclient</groupId>
+			<artifactId>hector-core</artifactId>
+			<version>1.1-2</version>
+		</dependency>
+		<dependency>
+			<groupId>org.slf4j</groupId>
+			<artifactId>slf4j-log4j12</artifactId>
+			<version>1.6.1</version>
+		</dependency>
         <dependency>
             <groupId>org.jmock</groupId>
             <artifactId>jmock-junit4</artifactId>
             <groupId>org.jmock</groupId>
             <artifactId>jmock-junit3</artifactId>
             <version>2.5.1</version>
-        </dependency>
-        <dependency>
-            <groupId>org.jmock</groupId>
-            <artifactId>jmock-junit3</artifactId>
-            <version>2.5.1</version>
+            <scope>test</scope>
         </dependency>
     </dependencies>
 

File src/main/resources/cassandra.properties

+# general cluster identity
+cluster.name=DefaultCluster
+#cluster.hosts=127.0.0.1:9161,127.0.0.1:9162,127.0.0.1:9163
+cluster.hosts=127.0.0.1:9160
+logging.keyspace=Logging

File src/main/resources/log4j.properties

+log4j.rootLogger=DEBUG,DRFA
+
+
+log4j.appender.DRFA=org.apache.log4j.DailyRollingFileAppender
+
+log4j.appender.DRFA.File=logs/cassandra-tutorial.log
+
+# Rollver at midnight
+log4j.appender.DRFA.DatePattern=.yyyy-MM-dd
+
+# 30-day backup
+#log4j.appender.DRFA.MaxBackupIndex=30
+log4j.appender.DRFA.layout=org.apache.log4j.PatternLayout
+
+# Pattern format: Date LogLevel LoggerName LogMessage
+#log4j.appender.DRFA.layout.ConversionPattern=%d{ISO8601} %p %c: %m%n
+# Debugging Pattern format
+log4j.appender.DRFA.layout.ConversionPattern=%d{ISO8601} %-5p %c{2} (%F:%M(%L)) - %m%n
+
+# stdout
+log4j.category.com.datastax=DEBUG
+# Uncomment to see Hector debug information 
+log4j.category.me.prettyprint=DEBUG
+log4j.category.org.apache=DEBUG

File src/test/java/storm/cookbook/CassandraSandbox.java

+package storm.cookbook;
+
+import static org.junit.Assert.*;
+
+import java.io.IOException;
+import java.text.SimpleDateFormat;
+import java.util.Arrays;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.Properties;
+
+import me.prettyprint.cassandra.model.ConfigurableConsistencyLevel;
+import me.prettyprint.cassandra.serializers.CompositeSerializer;
+import me.prettyprint.cassandra.serializers.IntegerSerializer;
+import me.prettyprint.cassandra.serializers.LongSerializer;
+import me.prettyprint.cassandra.serializers.StringSerializer;
+import me.prettyprint.cassandra.service.ThriftKsDef;
+import me.prettyprint.hector.api.Cluster;
+import me.prettyprint.hector.api.HConsistencyLevel;
+import me.prettyprint.hector.api.Keyspace;
+import me.prettyprint.hector.api.beans.Composite;
+import me.prettyprint.hector.api.ddl.ColumnFamilyDefinition;
+import me.prettyprint.hector.api.ddl.ColumnType;
+import me.prettyprint.hector.api.ddl.ComparatorType;
+import me.prettyprint.hector.api.ddl.KeyspaceDefinition;
+import me.prettyprint.hector.api.factory.HFactory;
+import me.prettyprint.hector.api.mutation.MutationResult;
+import me.prettyprint.hector.api.mutation.Mutator;
+
+import org.json.simple.JSONObject;
+import org.junit.Test;
+
+public class CassandraSandbox {
+
+	protected static Cluster cluster;
+	protected static Keyspace keyspace;
+	protected static Properties properties;
+
+	static StringSerializer stringSerializer = StringSerializer.get();
+	static LongSerializer longSerializer = LongSerializer.get();
+	static CompositeSerializer compositeSerializer = CompositeSerializer.get();
+
+	private static final String CF = "FileCountsByDay";
+
+	@Test
+	public void GenJSON() {
+		JSONObject content = new JSONObject();
+		JSONObject subElement = new JSONObject();
+		content.put("Test", subElement);
+		subElement.put("Date", "1");
+		System.out.println(content.toJSONString());
+	}
+
+	@Test
+	public void test() {
+		properties = new Properties();
+		try {
+			properties.load(CassandraSandbox.class
+					.getResourceAsStream("/cassandra.properties"));
+		} catch (IOException ioe) {
+			ioe.printStackTrace();
+		}
+
+		cluster = HFactory.getOrCreateCluster(
+				properties.getProperty("cluster.name", "DefaultCluster"),
+				properties.getProperty("cluster.hosts", "127.0.0.1:9160"));
+		ConfigurableConsistencyLevel ccl = new ConfigurableConsistencyLevel();
+		ccl.setDefaultReadConsistencyLevel(HConsistencyLevel.ONE);
+
+		String keyspaceName = properties.getProperty("logging.keyspace",
+				"Logging");
+
+		KeyspaceDefinition keyspaceDef = cluster.describeKeyspace(keyspaceName);
+
+		Calendar today = Calendar.getInstance();
+
+		String key = String.format("%04d:%02d:%02d", today.get(Calendar.YEAR),
+				today.get(Calendar.MONTH), today.get(Calendar.DAY_OF_MONTH));
+
+		if (keyspaceDef == null) {
+			ColumnFamilyDefinition cfDef = HFactory
+					.createColumnFamilyDefinition(properties.getProperty(
+							"logging.keyspace", "Logging"), CF,
+							ComparatorType.UTF8TYPE);
+			cfDef.setDefaultValidationClass(ComparatorType.COUNTERTYPE
+					.getClassName());
+			cfDef.setColumnType(ColumnType.STANDARD);
+
+			KeyspaceDefinition newKeyspace = HFactory.createKeyspaceDefinition(
+					keyspaceName, ThriftKsDef.DEF_STRATEGY_CLASS, 1,
+					Arrays.asList(cfDef));
+			cluster.addKeyspace(newKeyspace, true);
+
+			keyspace = HFactory.createKeyspace(keyspaceName, cluster, ccl);
+		}
+
+		if (keyspace == null)
+			keyspace = HFactory.createKeyspace(keyspaceName, cluster, ccl);
+
+		Mutator<String> mutator = HFactory.createMutator(keyspace,
+				StringSerializer.get());
+		mutator.incrementCounter(key, CF,
+				"localhost:/var/log/apache2/access.log", 1);
+
+		MutationResult mr = mutator.execute();
+		System.out.println(mr.toString());
+
+		cluster.getConnectionManager().shutdown();
+	}
+
+}